From 22486f51d0682730e459bd9a787c5337a8610563 Mon Sep 17 00:00:00 2001 From: tschmidt-dev Date: Fri, 1 Feb 2013 15:28:05 -0800 Subject: [PATCH] added kwargs logic to macros plugin and depth kwarg to article_list macro --- wiki/plugins/macros/markdown_extensions.py | 21 ++++++++++--------- .../wiki/plugins/macros/article_list.html | 2 +- .../plugins/templatetags/article_list.html | 4 ++-- .../macros/templatetags/wiki_macro_tags.py | 3 ++- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/wiki/plugins/macros/markdown_extensions.py b/wiki/plugins/macros/markdown_extensions.py index 2d57b75c..c1823e14 100644 --- a/wiki/plugins/macros/markdown_extensions.py +++ b/wiki/plugins/macros/markdown_extensions.py @@ -4,7 +4,8 @@ import re from django.template.loader import render_to_string from django.template import Context -MACRO_RE = re.compile(r'.*(\[(?P\w+)(\:(?P\w+))?\]).*', re.IGNORECASE) +MACRO_RE = re.compile(r'.*(\[(?P\w+)(?P\s\w+\:.+)*\]).*', re.IGNORECASE) +KWARG_RE = re.compile(r'([^ |:]+):([^ |$]+)', re.IGNORECASE) class MacroExtension(markdown.Extension): @@ -17,7 +18,7 @@ class MacroExtension(markdown.Extension): class MacroPreprocessor(markdown.preprocessors.Preprocessor): """django-wiki macro preprocessor - parse text for various [some_macro] and - [some_macro:arg] references. """ + [some_macro (kw:arg)*] references. """ allowed_methods = ('article_list',) @@ -29,22 +30,22 @@ class MacroPreprocessor(markdown.preprocessors.Preprocessor): macro = m.group('macro').strip() if not macro in MacroPreprocessor.allowed_methods: continue - arg = m.group('arg') - if arg: - arg = arg.strip() - try: - line = getattr(self, macro)(arg) - except AttributeError: - pass + kwargs = m.group('kwargs') + if kwargs: + kwargs = eval('{' + KWARG_RE.sub(r'"\1":"\2",', kwargs) + '}') + line = getattr(self, macro)(**kwargs) + else: + line = getattr(self, macro)() if not line is None: new_text.append(line) return new_text - def article_list(self, arg=None): + def article_list(self, depth=2): html = render_to_string( "wiki/plugins/macros/article_list.html", Context({ 'article_children': self.markdown.article.get_children, + 'depth': int(depth) + 1, }) ) return self.markdown.htmlStash.store(html, safe=True) diff --git a/wiki/plugins/macros/templates/wiki/plugins/macros/article_list.html b/wiki/plugins/macros/templates/wiki/plugins/macros/article_list.html index 3caca13d..373cfafd 100644 --- a/wiki/plugins/macros/templates/wiki/plugins/macros/article_list.html +++ b/wiki/plugins/macros/templates/wiki/plugins/macros/article_list.html @@ -5,7 +5,7 @@

{% trans "Articles" %}


{% for child in article_children %} - {% article_list child %} + {% article_list child depth %} {% endfor %} {% endif %} diff --git a/wiki/plugins/macros/templates/wiki/plugins/templatetags/article_list.html b/wiki/plugins/macros/templates/wiki/plugins/templatetags/article_list.html index c3bc5b78..8419e6a9 100644 --- a/wiki/plugins/macros/templates/wiki/plugins/templatetags/article_list.html +++ b/wiki/plugins/macros/templates/wiki/plugins/templatetags/article_list.html @@ -2,11 +2,11 @@ {% load url from future %}

{{ parent.article }}

-{% if parent.children.count %} +{% if parent.children.count and parent.level < depth %}
    {% for child in parent.children.all %} -
  • {% article_list child %}
  • +
  • {% article_list child depth %}
  • {% endfor %}
diff --git a/wiki/plugins/macros/templatetags/wiki_macro_tags.py b/wiki/plugins/macros/templatetags/wiki_macro_tags.py index 3aee6bf7..7f98f8d8 100644 --- a/wiki/plugins/macros/templatetags/wiki_macro_tags.py +++ b/wiki/plugins/macros/templatetags/wiki_macro_tags.py @@ -7,8 +7,9 @@ register = template.Library() 'wiki/plugins/templatetags/article_list.html', takes_context=True ) -def article_list(context, urlpath): +def article_list(context, urlpath, depth): context['parent'] = urlpath + context['depth'] = depth return context -- 2.45.2