~netlandish/django-wiki

b336586129112de5dea2a6e5512cad4f280d54b9 — benjaoming 11 years ago 0751cc8
Add help sidebar for macros and make allowed methods configurable
M wiki/plugins/macros/markdown_extensions.py => wiki/plugins/macros/markdown_extensions.py +9 -2
@@ 1,11 1,13 @@
import markdown
import re

from django.utils.translation import ugettext as _
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)

from wiki.plugins.macros import settings

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


@@ 19,7 21,7 @@ class MacroPreprocessor(markdown.preprocessors.Preprocessor):
    """django-wiki macro preprocessor - parse text for various [some_macro] and 
    [some_macro:arg] references. """
    
    allowed_methods = ('article_list',)
    allowed_methods = settings.METHODS
    
    def run(self, lines):
        new_text = []


@@ 48,4 50,9 @@ class MacroPreprocessor(markdown.preprocessors.Preprocessor):
            })
        )
        return self.markdown.htmlStash.store(html, safe=True)
    
    article_list.meta = dict(
        short_description = _(u'Article list'),
        help_text = _(u'Insert a list of articles in this level.'),
        example_code = _(u'[article_list:depth=2]'),
        args = {'depth': _('Maximum depth to show levels for.')}
    )

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

SLUG = 'macros'
APP_LABEL = 'wiki'

METHODS = getattr(django_settings, 'WIKI_PLUGINS_METHODS', ('article_list',))
\ No newline at end of file

A wiki/plugins/macros/templates/wiki/plugins/macros/sidebar.html => wiki/plugins/macros/templates/wiki/plugins/macros/sidebar.html +20 -0
@@ 0,0 1,20 @@
{% load i18n wiki_macro_tags %}
{% allowed_macros as macros %}

{% for macro in macros %}
<h4>{{ macro.short_description }}</h4>
{{ macro.help_text|linebreaks|safe }}
{% if macro.example_code %}
<pre>{{ macro.example_code }}</pre>
{% endif %}
{% if macro.args %}
<table class="table table-compact">
{% for arg,description in macro.args.items %}
  <tr>
    <th>{{ arg }}</th>
    <td>{{ description }}</td>
  </tr>
{% endfor %}
</table>
{% endif %}
{% endfor %}

M wiki/plugins/macros/templatetags/wiki_macro_tags.py => wiki/plugins/macros/templatetags/wiki_macro_tags.py +10 -0
@@ 1,4 1,5 @@
from django import template
from wiki.plugins.macros import settings, markdown_extensions

register = template.Library()



@@ 12,3 13,12 @@ def article_list(context, urlpath):
    return context 


@register.assignment_tag
def allowed_macros():
    for method in settings.METHODS:
        try:
            yield getattr(markdown_extensions.MacroPreprocessor, method).meta
        except AttributeError:
            continue
    
    
\ No newline at end of file

M wiki/plugins/macros/wiki_plugin.py => wiki/plugins/macros/wiki_plugin.py +8 -0
@@ 1,4 1,6 @@
# -*- coding: utf-8 -*-
from django.utils.translation import ugettext as _

from wiki.core.plugins import registry
from wiki.core.plugins.base import BasePlugin
from wiki.plugins.macros import settings


@@ 9,6 11,12 @@ class MacroPlugin(BasePlugin):
    
    slug = settings.SLUG
    
    sidebar = {'headline': _('Macros'),
               'icon_class': 'icon-play',
               'template': 'wiki/plugins/macros/sidebar.html',
               'form_class': None,
               'get_form_kwargs': (lambda a: {})}
    
    markdown_extensions = [MacroExtension()]
    
    def __init__(self):