From b336586129112de5dea2a6e5512cad4f280d54b9 Mon Sep 17 00:00:00 2001 From: benjaoming Date: Sun, 3 Feb 2013 14:08:57 +0100 Subject: [PATCH] Add help sidebar for macros and make allowed methods configurable --- wiki/plugins/macros/markdown_extensions.py | 11 ++++++++-- wiki/plugins/macros/settings.py | 3 ++- .../wiki/plugins/macros/sidebar.html | 20 +++++++++++++++++++ .../macros/templatetags/wiki_macro_tags.py | 10 ++++++++++ wiki/plugins/macros/wiki_plugin.py | 8 ++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 wiki/plugins/macros/templates/wiki/plugins/macros/sidebar.html diff --git a/wiki/plugins/macros/markdown_extensions.py b/wiki/plugins/macros/markdown_extensions.py index 2d57b75c..f5b43c44 100644 --- a/wiki/plugins/macros/markdown_extensions.py +++ b/wiki/plugins/macros/markdown_extensions.py @@ -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\w+)(\:(?P\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.')} + ) diff --git a/wiki/plugins/macros/settings.py b/wiki/plugins/macros/settings.py index 48ae0421..bfc262e0 100644 --- a/wiki/plugins/macros/settings.py +++ b/wiki/plugins/macros/settings.py @@ -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 diff --git a/wiki/plugins/macros/templates/wiki/plugins/macros/sidebar.html b/wiki/plugins/macros/templates/wiki/plugins/macros/sidebar.html new file mode 100644 index 00000000..d1a8f4b8 --- /dev/null +++ b/wiki/plugins/macros/templates/wiki/plugins/macros/sidebar.html @@ -0,0 +1,20 @@ +{% load i18n wiki_macro_tags %} +{% allowed_macros as macros %} + +{% for macro in macros %} +

{{ macro.short_description }}

+{{ macro.help_text|linebreaks|safe }} +{% if macro.example_code %} +
{{ macro.example_code }}
+{% endif %} +{% if macro.args %} + +{% for arg,description in macro.args.items %} + + + + +{% endfor %} +
{{ arg }}{{ description }}
+{% endif %} +{% endfor %} diff --git a/wiki/plugins/macros/templatetags/wiki_macro_tags.py b/wiki/plugins/macros/templatetags/wiki_macro_tags.py index 3aee6bf7..d4c18a7b 100644 --- a/wiki/plugins/macros/templatetags/wiki_macro_tags.py +++ b/wiki/plugins/macros/templatetags/wiki_macro_tags.py @@ -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 diff --git a/wiki/plugins/macros/wiki_plugin.py b/wiki/plugins/macros/wiki_plugin.py index 6cfcdcf7..821a3cbb 100644 --- a/wiki/plugins/macros/wiki_plugin.py +++ b/wiki/plugins/macros/wiki_plugin.py @@ -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): -- 2.45.2