~netlandish/django-wiki

5a3c029c2e646cbde56819e189cf9e31b4278a72 — tschmidt-dev 11 years ago f14ecef
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 +10 -0
@@ 0,0 1,10 @@
{% load wiki_macro_tags %}
{% if article_children %}
    <div class="well">
        <h3>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 +11 -0
@@ 0,0 1,11 @@
{% load wiki_macro_tags %}
<p><a href="{% url wiki:get path=urlpath.path %}">{{ urlpath.article }}</a></p>
<div> 
    <ul>
        <li>
            {% for urlpath in urlpath.children.all %}
                {% article_list urlpath %}
            {% endfor %}
        </li>
    </ul>
</div>

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['urlpath'] = 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)