-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdx_semanticdata.py
144 lines (103 loc) · 4.45 KB
/
mdx_semanticdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
'''
Semantic data Extension for Python-Markdown
===========================================
Adds support for semantic data (RDFa).
Converts `%% property :: content | label %%` structures into `span` elements
with a `property` and `content` attributes. `label` is optional;
Customizable with `make_elt` option as to what the actual element is.
Usage
-----
>>> import markdown
>>> text = "%%dc:author :: Sherry Turkle | Turkle's%% %%dc:title::Second Self%% was an early book on the social aspects of computation."
>>> html = markdown.markdown(text, ['semanticdata'])
>>> print(html)
<p><span content="Sherry Turkle" property="dc:author">Turkle's</span> <span content="Second Self" property="dc:title">Second Self</span> was an early book on the social aspects of computation.</p>
Custom tree element:
>>> def make_elt (md, rel, target, label):
... # `md` is the Markdown instance
... if rel == "dc:title":
... elt = markdown.util.etree.Element('cite')
... else:
... elt = markdown.util.etree.Element('span')
... elt.set('content', target)
... elt.text = label or target
... if rel:
... elt.set('property', rel)
... return elt
>>> md = markdown.Markdown(extensions=['semanticdata'],
... extension_configs={'semanticdata' : [('make_elt', make_elt)]})
>>> html = md.convert(text)
>>> print(html)
<p><span content="Sherry Turkle" property="dc:author">Turkle's</span> <cite content="Second Self" property="dc:title">Second Self</cite> was an early book on the social aspects of computation.</p>
Custom default namespace:
>>> text = "%%author :: Sherry Turkle | Turkle's%% %%title::Second Self%% was an early book on the social aspects of computation."
>>> md = markdown.Markdown(extensions=['semanticdata'],
... extension_configs={'semanticdata' : [('namespace', 'dc')]})
>>> html = md.convert(text)
>>> print(html)
<p><span content="Sherry Turkle" property="dc:author">Turkle's</span> <span content="Second Self" property="dc:title">Second Self</span> was an early book on the social aspects of computation.</p>
Dependencies
------------
* [Markdown 2.0+](http://www.freewisdom.org/projects/python-markdown/)
Copyright
---------
2011, 2012 [The active archives contributors](http://activearchives.org/)
2011, 2012 [Michael Murtaugh](http://automatist.org/)
2011, 2012 [Alexandre Leray](http://stdin.fr/)
All rights reserved.
This software is released under the modified BSD License.
See LICENSE.md for details.
'''
import markdown
import re
__version__ = "1.1"
pattern = r"""
\%\%\s*
(?:((?P<namespace>\w+):)?(?P<rel>[^\%#]+?) \s* ::)? \s*
(?P<target>.+?) \s*
(?:\| \s* (?P<label>[^\]]+?) \s*)?
\%\%
""".strip()
def make_elt (md, rel, target, label):
elt = markdown.util.etree.Element('span')
elt.set('content', target)
elt.text = label or target
if rel:
elt.set('property', rel)
return elt
class SemanticDataExtension(markdown.Extension):
def __init__(self, configs):
self.config = {
'make_elt' : [make_elt, 'Callback to convert parts into an HTML/etree element (default <span>)'],
'namespace' : ['aa', 'Default namespace'],
}
# 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
pat = SemanticDataPattern(self.config, md)
md.inlinePatterns.add('semanticdata', pat, "<not_strong")
class SemanticDataPattern(markdown.inlinepatterns.Pattern):
def __init__(self, config, md=None):
markdown.inlinepatterns.Pattern.__init__(self, '', md)
self.compiled_re = re.compile("^(.*?)%s(.*?)$" % pattern, re.DOTALL | re.X)
self.config = config
def getCompiledRegExp (self):
return self.compiled_re
def handleMatch(self, m):
""" Returns etree """
d = m.groupdict()
fn = self.config['make_elt'][0]
namespace = d.get("namespace") or self.config['namespace'][0]
rel = d.get("rel")
if rel:
rel = "%s:%s" % (namespace, d.get("rel"))
return fn(self.markdown, rel, d.get("target"), d.get("label"))
def makeExtension(configs={}) :
return SemanticDataExtension(configs=configs)
if __name__ == "__main__":
import doctest
doctest.testmod()