~netlandish/django-wiki

933ac1967676a2588fcc5ed1dd8ca2567cee7375 — benjaoming 11 years ago 919d45e + d6f3724
Merge pull request #105 from SacNaturalFoods/master

added core macros plugin with initial article_list macro
A wiki/plugins/macros/__init__.py => wiki/plugins/macros/__init__.py +0 -0
A wiki/plugins/macros/markdown_extensions.py => wiki/plugins/macros/markdown_extensions.py +47 -0
@@ 0,0 1,47 @@
import markdown
import re

from django.template.loader import render_to_string
from django.template import Context

MACRO_RE = re.compile(r'.*(\[(?P<macro>\w+)(\:(?P<arg>\w+))?\]).*', re.IGNORECASE)


class MacroExtension(markdown.Extension):
    """ Macro plugin markdown extension for django-wiki. """

    def extendMarkdown(self, md, md_globals):
        """ Insert MacroPreprocessor before ReferencePreprocessor. """
        md.preprocessors.add('dw-macros', MacroPreprocessor(md), '>html_block')


class MacroPreprocessor(markdown.preprocessors.Preprocessor):
    """django-wiki macro preprocessor - parse text for various [some_macro] and 
    [some_macro:arg] references. """

    def run(self, lines):
        new_text = []
        for line in lines:
            m = MACRO_RE.match(line)
            if m:
                macro = m.group('macro').strip()
                arg = m.group('arg')
                if arg:
                    arg = arg.strip()
                try:
                    line = getattr(self, macro)(arg)
                except AttributeError:
                    pass       
            if not line is None:
                new_text.append(line)
        return new_text

    def article_list(self, arg=None):
        html = render_to_string(
            "wiki/plugins/macros/article_list.html",
            Context({
                'article_children': self.markdown.article.get_children,
            })
        )
        return self.markdown.htmlStash.store(html, safe=True)
    

A wiki/plugins/macros/settings.py => wiki/plugins/macros/settings.py +5 -0
@@ 0,0 1,5 @@
from django.conf import settings as django_settings
from wiki.conf import settings as wiki_settings

SLUG = 'macros'
APP_LABEL = 'wiki'

A wiki/plugins/macros/templates/wiki/plugins/macros/article_list.html => wiki/plugins/macros/templates/wiki/plugins/macros/article_list.html +11 -0
@@ 0,0 1,11 @@
{% load i18n wiki_macro_tags %}

{% if article_children %}
    <div class="well wiki-article-list">
        <h3>{% trans "Articles" %}</h3>
        <br/>
            {% for child in article_children %}
                {% article_list child %}
            {% endfor %}
    </div>
{% endif %}

A wiki/plugins/macros/templates/wiki/plugins/templatetags/article_list.html => wiki/plugins/macros/templates/wiki/plugins/templatetags/article_list.html +13 -0
@@ 0,0 1,13 @@
{% load wiki_macro_tags %}
{% load url from future %}

<p><a href="{% url 'wiki:get' path=parent.path article_id=parent.article.id %}">{{ parent.article }}</a></p>
{% if parent.children.count %}
    <div class="wiki-article-sublist"> 
        <ul>
            {% for child in parent.children.all %}
                <li>{% article_list child %}</li>
            {% endfor %}
        </ul>
    </div>
{% endif %}

A wiki/plugins/macros/templatetags/__init__.py => wiki/plugins/macros/templatetags/__init__.py +0 -0
A wiki/plugins/macros/templatetags/wiki_macro_tags.py => wiki/plugins/macros/templatetags/wiki_macro_tags.py +14 -0
@@ 0,0 1,14 @@
from django import template

register = template.Library()


@register.inclusion_tag(
    'wiki/plugins/templatetags/article_list.html',
    takes_context=True
)
def article_list(context, urlpath):
    context['parent'] = urlpath 
    return context 



A wiki/plugins/macros/wiki_plugin.py => wiki/plugins/macros/wiki_plugin.py +17 -0
@@ 0,0 1,17 @@
# -*- coding: utf-8 -*-
from wiki.core.plugins import registry
from wiki.core.plugins.base import BasePlugin
from wiki.plugins.macros import settings
from wiki.plugins.macros.markdown_extensions import MacroExtension


class MacroPlugin(BasePlugin):
    
    slug = settings.SLUG
    
    markdown_extensions = [MacroExtension()]
    
    def __init__(self):
        pass
    
registry.register(MacroPlugin)