Commit 5a3c029c by tschmidt-dev

added core macros plugin with initial article_list macro

parent f14ecefa
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)
from django.conf import settings as django_settings
from wiki.conf import settings as wiki_settings
SLUG = 'macros'
APP_LABEL = 'wiki'
{% 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 %}
{% 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>
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
# -*- 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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment