Skip to content

Commit

Permalink
get positional macros working
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinvwaran committed Jun 6, 2021
1 parent 1b7aee4 commit 7ce0baf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 40 deletions.
66 changes: 37 additions & 29 deletions src/wiki/plugins/macros/mdx/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
MACRO_RE = r"(?i)(\[(?P<macro>\w+)(?P<kwargs>(\s(\w+:)?(%s|[\w'`&!%%+/$-]+))*)\])" % re_sq_short
MACRO_RE_COMPILED = re.compile(MACRO_RE)
KWARG_RE = re.compile(
r"\s*(?P<arg>\w+)(:(?P<value>([^\']+|%s)))?" % re_sq_short, re.IGNORECASE
)
r'\s*((?P<arg>\w+):)?(?P<value>(%s|[^\s:]+))' %
re_sq_short,
re.IGNORECASE)

# Positional macros were deprecated in markdown 3.0, so we need to kwargize
# the existing positional macros during the preprocessing step. This dict
Expand All @@ -31,7 +32,9 @@ class MacroExtension(markdown.Extension):
""" Macro plugin markdown extension for django-wiki. """

def extendMarkdown(self, md):
md.inlinePatterns.add("dw-macros", MacroPattern(MACRO_RE, md), ">link")
md.preprocessors.register(SubstitutionPreprocessor(), 'escaper', 5)
md.inlinePatterns.register(MacroPattern(MACRO_RE, md), 'dw-macros', 5)
md.postprocessors.register(SubstitutionPostprocessor(), 'unescaper', 5)


# Escaping --------------------------------------------------------------------------------
Expand Down Expand Up @@ -73,21 +76,28 @@ def run(self, text):
# Macro implementation ----------------------------------------------------------------------
class MacroPattern(markdown.inlinepatterns.Pattern):
"""django-wiki macro preprocessor - parse text for various [some_macro] and
[some_macro (kw:arg)*] references."""
[some_macro (kw:arg)*] references. """

def handleMatch(self, m):
macro = m.group("macro").strip()
macro = m.group('macro').strip()
if macro not in settings.METHODS or not hasattr(self, macro):
return m.group(2)

kwargs = m.group("kwargs")
kwargs = m.group('kwargs')
if not kwargs:
return getattr(self, macro)()
kwargs_dict = {}
for kwarg in KWARG_RE.finditer(kwargs):
arg = kwarg.group("arg")
value = kwarg.group("value")
if value is None:
for i, kwarg in enumerate(KWARG_RE.finditer(kwargs)):

# Begin Xposition-specific hack: if there's no :, we have a positional macro
arg = kwarg.group('arg')
value = kwarg.group('value')
if arg is None and macro in POSITIONAL_MACROS:
arg = "arg" + str(i)
if len(value) > 2 and value[0] == '"' and value[-1] == '"':
value = value[1:-1]
elif arg is None:
arg = value
value = True
if isinstance(value, str):
# If value is enclosed with ': Remove and
Expand All @@ -104,40 +114,38 @@ def article_list(self, depth="2"):
html = render_to_string(
"wiki/plugins/macros/article_list.html",
context={
"article_children": self.markdown.article.get_children(
article__current_revision__deleted=False
),
"depth": int(depth) + 1,
},
)
'article_children': self.markdown.article.get_children(
article__current_revision__deleted=False),
'depth': int(depth) + 1,
})
return self.markdown.htmlStash.store(html)

article_list.meta = dict(
short_description=_("Article list"),
help_text=_("Insert a list of articles in this level."),
example_code="[article_list depth:2]",
args={"depth": _("Maximum depth to show levels for.")},
short_description=_('Article list'),
help_text=_('Insert a list of articles in this level.'),
example_code='[article_list depth:2]',
args={'depth': _('Maximum depth to show levels for.')}
)

def toc(self):
return "[TOC]"

toc.meta = dict(
short_description=_("Table of contents"),
help_text=_("Insert a table of contents matching the headings."),
example_code="[TOC]",
args={},
short_description=_('Table of contents'),
help_text=_('Insert a table of contents matching the headings.'),
example_code='[TOC]',
args={}
)

def wikilink(self):
return ""

wikilink.meta = dict(
short_description=_("WikiLinks"),
help_text=_("Insert a link to another wiki page with a short notation."),
example_code="[[WikiLink]]",
args={},
)
short_description=_('WikiLinks'),
help_text=_(
'Insert a link to another wiki page with a short notation.'),
example_code='[[WikiLink]]',
args={})

def errormsg(self):
return etree.fromstring('<span class="error">' + 'Macro Error: please see example usage' + '</span>')
Expand Down
21 changes: 13 additions & 8 deletions src/wiki/plugins/macros/settings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from django.conf import settings as django_settings

SLUG = "macros"
APP_LABEL = "wiki"
SLUG = 'macros'
APP_LABEL = 'wiki'

#: List of markdown extensions this plugin should support.
#: ``article_list`` inserts a list of articles from the current level.
#: ``toc`` inserts a table of contents matching the headings.
METHODS = getattr(
django_settings,
"WIKI_PLUGINS_METHODS",
(
"article_list",
"toc",
),
)
'WIKI_PLUGINS_METHODS',
('article_list',
'toc',
'wikilink',
'ex',
'gex',
'p',
'pspecial',
'ss',
'exref',
))
10 changes: 7 additions & 3 deletions src/wiki/plugins/metadata/templates/construal_article_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ <h4 id="usages">Usages ({{usages|length}})</h4>
</div>

</div>
<script src="{% static "wiki/js/jquery-3.4.1.min.js" %}"></script>


<script src="{% static "wiki/js/jquery-3.4.1.min.js" %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-markdown/2.10.0/js/bootstrap-markdown.js" integrity="sha512-pho6CWrO/GDwYDCNxcHT9ODBGjwDOEv7JXyumGxAkcaJYSizVmSIVTp4+u5PuVmYUgBhmNYxB0rOAejhebLOew==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>


<script>
$.ajax({
url: "/_table/construal/{{article.id}}",
Expand All @@ -60,8 +65,7 @@ <h4 id="usages">Usages ({{usages|length}})</h4>
})
</script>



{% endblock %}



Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "wiki/article.html" %}
{% load wiki_tags i18n sekizai_tags %}
{% load metadata_tags %}
{% load static %}

{% block wiki_articletitle %}
<i class="fa fa-globe fa-2x" aria-hidden="true" style="opacity: .1; position: absolute; left: -80px;"></i>
Expand Down Expand Up @@ -52,6 +53,14 @@ <h4>Construals with function</h4>
</div>
</div>
</div>
<script src="{% static "wiki/js/jquery.min.js" %}"></script>
<script src="{% static "wiki/js/jquery-3.4.1.min.js" %}"></script>
<script src="{% static "wiki/js/core.js" %}"></script>
<script src="{% static "wiki/js/popper.js" %}"></script>
<script src="{% static "wiki/bootstrap/js/bootstrap.bundle.min.js" %}"></script>
<script src="{% static "wiki/js/respond.min.js" %}"></script>


<script>
$.ajax({
url: "/_table/supersense/{{article.id}}",
Expand Down

0 comments on commit 7ce0baf

Please sign in to comment.