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.
83
from markdown import etree
85
from markdown.util import etree
94
(?:((?P<namespace>\w+):)?(?P<rel>[^\]#]+?) \s* ::)? \s*
96
(?:\| \s* (?P<label>.+?) \s*)?
101
def make_link(md, rel, target, label):
102
a = etree.Element('a')
103
a.set('href', target)
106
a.text = label or target
110
def make_wikilink(md, rel, target, label):
111
# Turns a link from Syntax [[ Page Name | linktext ]]
112
a = etree.Element('a')
113
a.set('href', '/wiki/' + target)
114
a.set('class', 'wikilink')
117
a.text = label or target
121
class SemanticWikiLinkExtension(markdown.Extension):
123
def __init__(self, *args, **kwargs):
125
'make_link': [make_wikilink, 'Callback to convert link parts into an HTML/etree element'],
126
'namespace': ['aa', 'Default namespace'],
128
super(SemanticWikiLinkExtension, self).__init__(*args, **kwargs)
130
def extendMarkdown(self, md, md_globals):
133
# append to end of inline patterns
134
ext = SemanticWikiLinkPattern(self.config, md)
135
md.inlinePatterns.add('semanticwikilink', ext, '<not_strong')
138
class SemanticWikiLinkPattern(markdown.inlinepatterns.Pattern):
140
def __init__(self, config, md=None):
141
markdown.inlinepatterns.Pattern.__init__(self, '', md)
142
self.compiled_re = re.compile(
143
'^(.*?)%s(.*?)$' % WIKILINK_RE, re.DOTALL | re.X)
146
def getCompiledRegExp(self):
147
return self.compiled_re
149
def handleMatch(self, m):
152
fn = self.config['make_link'][0]
153
namespace = d.get('namespace') or self.config['namespace'][0]
157
rel = '%s:%s' % (namespace, d.get('rel'))
159
return fn(self.markdown, rel, d.get('target'), d.get('label'))
162
def makeExtension(*args, **kwargs):
163
return SemanticWikiLinkExtension(*args, **kwargs)
166
if __name__ == '__main__':