Commit c8d93079 by benjaoming

Fix [TOC] compatibility with custom ids and add support for [[WikiLink]] #179

parent c73d3310
......@@ -83,3 +83,12 @@ class MacroPreprocessor(markdown.preprocessors.Preprocessor):
example_code = _(u'[TOC]'),
args = {}
)
def wikilink(self):
return ""
toc.meta = dict(
short_description = _(u'WikiLinks'),
help_text = _(u'Insert a link to another wiki page with a short notation.'),
example_code = _(u'[[WikiLink]]'),
args = {}
)
......@@ -118,11 +118,6 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
c.append(anchor)
def build_toc_etree(self, div, toc_list):
# Add title to the div
if self.config["title"]:
header = etree.SubElement(div, "span")
header.attrib["class"] = "toctitle"
header.text = self.config["title"]
def build_etree_ul(toc_list, parent):
ul = etree.SubElement(parent, "ul")
......@@ -172,7 +167,7 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
p[i] = div
break
marker_found = True
if header_rgx.match(c.tag):
# Do not override pre-existing ids
......@@ -233,7 +228,7 @@ class TocExtension(markdown.Extension):
# by the header id extension) if both are used. Same goes for
# attr_list extension. This must come last because we don't want
# to redefine ids after toc is created. But we do want toc prettified.
md.treeprocessors.add("toc", tocext, "<prettify")
md.treeprocessors.add("toc", tocext, ">headerid")
def makeExtension(configs={}):
......
#!/usr/bin/env python
"""
Extend the shipped Markdown extension 'wikilinks'
"""
from markdown.extensions import wikilinks
import markdown
import re
def build_url(label, base, end, md):
""" Build a url from the label, a base, and an end. """
clean_label = re.sub(r'([ ]+_)|(_[ ]+)|([ ]+)', '_', label)
urlpaths = md.article.urlpath_set.all()
# Nevermind about the base we are fed, just keep the original
# call pattern from the wikilinks plugin for later...
base='/'
for urlpath in urlpaths:
if urlpath.children.filter(slug=clean_label).exists():
base=''
break
return '%s%s%s'% (base, clean_label, end)
class WikiLinkExtension(wikilinks.WikiLinkExtension):
def __init__(self, configs={}):
# set extension defaults
self.config = {
'base_url' : ['', 'String to append to beginning or URL.'],
'end_url' : ['/', 'String to append to end of URL.'],
'html_class' : ['wiki_wikilink', 'CSS hook. Leave blank for none.'],
'build_url' : [build_url, 'Callable formats URL from label.'],
}
# Override defaults with user settings
for key, value in configs :
self.setConfig(key, value)
def extendMarkdown(self, md, md_globals):
self.md = md
# append to end of inline patterns
WIKILINK_RE = r'\[\[([\w0-9_ -]+)\]\]'
wikilinkPattern = WikiLinks(WIKILINK_RE, self.getConfigs())
wikilinkPattern.md = md
md.inlinePatterns.add('wikilink', wikilinkPattern, "<not_strong")
class WikiLinks(wikilinks.WikiLinks):
def handleMatch(self, m):
if m.group(2).strip():
base_url, end_url, html_class = self._getMeta()
label = m.group(2).strip()
url = self.config['build_url'](label, base_url, end_url, self.md)
a = markdown.util.etree.Element('a')
a.text = label
a.set('href', url)
if html_class:
a.set('class', html_class)
else:
a = ''
return a
......@@ -7,6 +7,7 @@ from wiki.plugins.macros import settings
from wiki.plugins.macros.mdx.macro import MacroExtension
from wiki.plugins.macros.mdx.toc import WikiTocExtension
from wiki.plugins.macros.mdx.wikilinks import WikiLinkExtension
class MacroPlugin(BasePlugin):
......@@ -18,7 +19,7 @@ class MacroPlugin(BasePlugin):
'form_class': None,
'get_form_kwargs': (lambda a: {})}
markdown_extensions = [MacroExtension(), WikiTocExtension()]
markdown_extensions = [WikiLinkExtension(), MacroExtension(), WikiTocExtension()]
def __init__(self):
pass
......
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