-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmdx_textalign.py
More file actions
69 lines (47 loc) · 1.71 KB
/
mdx_textalign.py
File metadata and controls
69 lines (47 loc) · 1.71 KB
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
#!/usr/bin/env python
'''
Text Alignment Extension for Python-Markdown
============================================
Allows markdown to center and right-align text.
Usage
-----
>>> import markdown
>>> text = """->This text is centered<- ->and this text is right-aligned->"""
>>> html = markdown.markdown(text,["textalign"])
>>> print(html)
<p>
<div style="display:block;text-align:center;">This text is centered</div>
<div style="display:block;text-align:right;">and this text is right-aligned</div>
</p>
Dependencies
------------
* [Markdown 2.0+](http://www.freewisdom.org/projects/python-markdown/)
'''
import markdown
from markdown.inlinepatterns import Pattern
from markdown.util import etree
CENTR_RE = r"(\-\>)(.+?)(\<\-)"
RIGHT_RE = r"(\-\>)(.+?)(\-\>)"
class CenterAlignPattern(Pattern):
def handleMatch(self,m):
txt = etree.Element("div")
txt.set("style","display:block;text-align:center;")
txt.text = m.group(3)
return txt
class RightAlignPattern(Pattern):
def handleMatch(self,m):
txt = etree.Element("div")
txt.set("style","display:block;text-align:right;")
txt.text = m.group(3)
return txt
class TextAlignExtension(markdown.extensions.Extension):
"""Adds textalign extension to Markdown class."""
def extendMarkdown(self, md, md_globals):
"""Modifies inline patterns."""
md.inlinePatterns.add("center", CenterAlignPattern(CENTR_RE), "<not_strong")
md.inlinePatterns.add("right", RightAlignPattern( RIGHT_RE), "<not_strong")
def makeExtension(configs={}):
return TextAlignExtension(configs=dict(configs))
if __name__ == "__main__":
import doctest
doctest.testmod()