forked from dellsystem/wikinotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx_superscript.py
56 lines (39 loc) · 1.8 KB
/
mdx_superscript.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
"""Superscipt extension for Markdown. From https://github.com/sgraber/markdown.superscript
To superscript something, place a carat symbol, '^', before and after the
text that you would like in superscript: 6.02 x 10^23^
The '23' in this example will be superscripted. See below.
Examples:
>>> import markdown
>>> md = markdown.Markdown(extensions=['superscript'])
>>> md.convert('This is a reference to a footnote^1^.')
u'<p>This is a reference to a footnote<sup>1</sup>.</p>'
>>> md.convert('This is scientific notation: 6.02 x 10^23^')
u'<p>This is scientific notation: 6.02 x 10<sup>23</sup></p>'
>>> md.convert('This is scientific notation: 6.02 x 10^23. Note lack of second carat.')
u'<p>This is scientific notation: 6.02 x 10^23. Note lack of second carat.</p>'
>>> md.convert('Scientific notation: 6.02 x 10^23. Add carat at end of sentence.^')
u'<p>Scientific notation: 6.02 x 10<sup>23. Add a carat at the end of sentence.</sup>.</p>'
Paragraph breaks will nullify superscripts across paragraphs. Line breaks
within paragraphs will not.
"""
import markdown
# Global Vars
SUPERSCRIPT_RE = r'(\^)([^\^]*)\2' # the number is a superscript^2^
class SuperscriptPattern(markdown.inlinepatterns.Pattern):
""" Return a superscript Element (`word^2^`). """
def handleMatch(self, m):
supr = m.group(3)
text = supr
el = markdown.etree.Element("sup")
el.text = markdown.AtomicString(text)
return el
class SuperscriptExtension(markdown.Extension):
""" Superscript Extension for Python-Markdown. """
def extendMarkdown(self, md, md_globals):
""" Replace superscript with SuperscriptPattern """
md.inlinePatterns['superscript'] = SuperscriptPattern(SUPERSCRIPT_RE, md)
def makeExtension(configs=None):
return SuperscriptExtension(configs=configs)
if __name__ == "__main__":
import doctest
doctest.testmod()