5
SemanticWikiLinks Extension for Python-Markdown
6
===============================================
8
Adds support for semantic (wiki)links (RDFa).
10
Converts links of style `[[rel :: target | label ]]`, where `rel` and `label`
13
Customizable with `make_link` option as to what the actual element is.
19
>>> text = "Some text with a [[WikiLink]]."
20
>>> html = markdown.markdown(text, ['semanticwikilinks'])
22
<p>Some text with a <a href="WikiLink">WikiLink</a>.</p>
24
>>> text = "[[http://activearchives.org/]], [[#id|anchor]], [[../index.html|a relative link]], [[/|an absolute link]], [[/index.html|another absolute link]]"
25
>>> html = markdown.markdown(text, ['semanticwikilinks'])
27
<p><a href="http://activearchives.org/">http://activearchives.org/</a>, <a href="#id">anchor</a>, <a href="../index.html">a relative link</a>, <a href="/">an absolute link</a>, <a href="/index.html">another absolute link</a></p>
29
Define a custom URL builder:
31
>>> from markdown.util import etree
32
>>> def make_rdfa(md, rel, target, label):
33
... # `md` is the Markdown instance
34
... elt = etree.Element("span")
35
... elt.set("property", rel)
36
... elt.set("value", target)
37
... elt.text = label or target
40
>>> md = markdown.Markdown(extensions=['semanticwikilinks'],
41
... extension_configs={'semanticwikilinks' : [('make_link', make_rdfa)]})
42
>>> html = md.convert('[[ Speaker :: Sherry Turkle | Second Self ]]')
44
<p><span property="aa:Speaker" value="Sherry Turkle">Second Self</span></p>
46
Change the default namespace (which is "aa"):
48
>>> md = markdown.Markdown(extensions=['semanticwikilinks'],
49
... extension_configs={'semanticwikilinks' : [('namespace', 'mynamespace')]})
50
>>> html = md.convert('[[ Speaker :: Sherry Turkle | Second Self ]]')
52
<p><a href="Sherry Turkle" rel="mynamespace:Speaker">Second Self</a></p>
57
- An optional function to wikify names? (It is already possible to achieve
58
this with the custom `make_link` function).
64
* [Markdown 2.0+](http://www.freewisdom.org/projects/python-markdown/)
70
2011, 2012 [The active archives contributors](http://activearchives.org/)
71
2011, 2012 [Michael Murtaugh](http://automatist.org/)
72
2011, 2012 [Alexandre Leray](http://stdin.fr/)
76
This software is released under the modified BSD License.
77
See LICENSE.md for details.
82
try: from markdown import etree
83
except ImportError: from markdown.util import etree
92
(?:((?P<namespace>\w+):)?(?P<rel>[^\]#]+?) \s* ::)? \s*
94
(?:\| \s* (?P<label>.+?) \s*)?
99
def make_link(md, rel, target, label):
100
a = etree.Element('a')
101
a.set('href', target)
104
a.text = label or target
107
def make_wikilink(md, rel, target, label):
108
# Turns a link from Syntax [[ Page Name | linktext ]]
109
a = etree.Element('a')
110
a.set('href', '/wiki/' + target)
111
a.set('class', 'wikilink')
117
class SemanticWikiLinkExtension(markdown.Extension):
118
def __init__(self, *args, **kwargs):
120
'make_link': [make_wikilink, 'Callback to convert link parts into an HTML/etree element'],
121
'namespace': ['aa', 'Default namespace'],
123
super(SemanticWikiLinkExtension, self).__init__(*args, **kwargs)
125
def extendMarkdown(self, md, md_globals):
128
# append to end of inline patterns
129
ext = SemanticWikiLinkPattern(self.config, md)
130
md.inlinePatterns.add('semanticwikilink', ext, "<not_strong")
133
class SemanticWikiLinkPattern(markdown.inlinepatterns.Pattern):
134
def __init__(self, config, md=None):
135
markdown.inlinepatterns.Pattern.__init__(self, '', md)
136
self.compiled_re = re.compile("^(.*?)%s(.*?)$" % WIKILINK_RE, re.DOTALL | re.X)
139
def getCompiledRegExp(self):
140
return self.compiled_re
142
def handleMatch(self, m):
143
""" Returns etree """
145
fn = self.config['make_link'][0]
146
namespace = d.get("namespace") or self.config['namespace'][0]
150
rel = "%s:%s" % (namespace, d.get("rel"))
152
return fn(self.markdown, rel, d.get("target"), d.get("label"))
155
def makeExtension(*args, **kwargs):
156
return SemanticWikiLinkExtension(*args, **kwargs)
159
if __name__ == "__main__":