forked from sgraber/markdown.subscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscript.py
46 lines (33 loc) · 1.35 KB
/
subscript.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
"""Subscript extension for Markdown.
To subscript something, place a tilde symbol, '~', before and after the
text that you would like in subscript: C~6~H~12~O~6~
The numbers in this example will be subscripted. See below for more:
Examples:
>>> import markdown
>>> md = markdown.Markdown(extensions=['subscript'])
>>> md.convert('This is sugar: C~6~H~12~O~6~')
u'<p>This is sugar: C<sub>6</sub>H<sub>12</sub>O<sub>6</sub></p>'
Paragraph breaks will nullify subscripts across paragraphs. Line breaks
within paragraphs will not.
"""
import markdown
# Global Vars
SUBSCRIPT_RE = r'(\~)([^\~]*)\2' # the number is subscript~2~
class SubscriptPattern(markdown.inlinepatterns.Pattern):
""" Return a subscript Element: `C~6~H~12~O~6~' """
def handleMatch(self, m):
subsc = m.group(3)
text = subsc
el = markdown.etree.Element("sub")
el.text = markdown.AtomicString(text)
return el
class SubscriptExtension(markdown.Extension):
""" Subscript Extension for Python-Markdown. """
def extendMarkdown(self, md, md_globals):
""" Replace subscript with SubscriptPattern """
md.inlinePatterns['subscript'] = SubscriptPattern(SUBSCRIPT_RE, md)
def makeExtension(configs=None):
return SubscriptExtension(configs=configs)
if __name__ == "__main__":
import doctest
doctest.testmod()