This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
honeycomb_markdown.py
150 lines (124 loc) · 4.91 KB
/
honeycomb_markdown.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
144
145
146
147
148
149
150
import hashlib
import re
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from markdown.preprocessors import Preprocessor
from markdown.extensions import Extension
from markdown.inlinepatterns import Pattern
from markdown.postprocessors import Postprocessor
from markdown.util import etree
TABLEFIX_RE = re.compile(r'<table>')
DLFIX_RE = re.compile(r'<dl>')
ABBRFIX_RE = re.compile(r'<abbr title="([^"]+)">([^</abbr>]+)</abbr>')
USERNAME_PATTERN_NAME = r'~([\w-]+)'
USERNAME_PATTERN_ICON = r'@!([\w-]+)'
USERNAME_PATTERN_ICONNAME = r'@([\w-]+)'
POETRY_PATTERN = r"'''([^''']+)'''"
class Poetry(Preprocessor):
RE = re.compile(
r'''(?P<quotes>'{3})(?P<body>.*?)(?P=quotes)''',
re.DOTALL)
def repl(self, m):
return '<div style="white-space:pre-wrap">{}</div>'.format(
m.group('body'))
def run(self, lines):
text = '\n'.join(lines)
text = re.sub(self.RE, self.repl, text)
return text.split('\n')
class HoneycombUserName(Pattern):
def handleMatch(self, m):
el = etree.Element('a')
el.set('href', reverse('usermgmt:view_profile', args=(m.group(2),)))
el.set('class', 'name-user-link')
el.text = '~{}'.format(m.group(2))
return el
class HoneycombUserIcon(Pattern):
fetched_users = {}
def handleMatch(self, m):
try:
if m.group(2) in self.fetched_users:
user = self.fetched_users[m.group(2)]
else:
user = User.objects.get(username=m.group(2))
self.fetched_users[m.group(2)] = user
el = etree.Element('a')
el.set('href', reverse('usermgmt:view_profile',
args=(m.group(2),)))
el.set('class', 'icon-user-link')
el.text = ('![gravatar]'
'(https://www.gravatar.com/avatar/{}?s=50)').format(
hashlib.md5(
user.email.strip().lower().encode('utf-8')
).hexdigest(),
m.group(2))
return el
except User.DoesNotExist:
el = etree.Element('a')
el.set('href', reverse('usermgmt:view_profile',
args=(m.group(3),)))
el.set('class', 'user-link')
el.text = m.group(2)
return el
class HoneycombUserIconName(Pattern):
fetched_users = {}
def handleMatch(self, m):
try:
if m.group(2) in self.fetched_users:
user = self.fetched_users[m.group(2)]
else:
user = User.objects.get(username=m.group(2))
self.fetched_users[m.group(2)] = user
el = etree.Element('a')
el.set('href', reverse('usermgmt:view_profile',
args=(m.group(2),)))
el.set('class', 'icon-name-user-link')
el.text = ('![gravatar](https://www.gravatar.com/avatar/{}?s=50) '
'{}').format(
hashlib.md5(
user.email.strip().lower().encode('utf-8')
).hexdigest(),
m.group(2))
return el
except User.DoesNotExist:
el = etree.Element('a')
el.set('href', reverse('usermgmt:view_profile',
args=(m.group(3),)))
el.set('class', 'user-link')
el.text = m.group(2)
return el
class TableFix(Postprocessor):
def run(self, text):
return TABLEFIX_RE.sub(
r'<table class="table table-striped table-hover">', text)
class DLFix(Postprocessor):
def run(self, text):
return DLFIX_RE.sub(r'<dl class="dl-indent">', text)
class ABBRFix(Postprocessor):
def run(self, text):
return re.sub(
ABBRFIX_RE,
r'<abbr data-toggle="tooltip" '
r'data-placement="bottom" '
r'title="\1">\2</abbr>', text)
class HoneycombMarkdown(Extension):
def extendMarkdown(self, md, md_globals):
md.preprocessors['poetry'] = Poetry(md)
md.inlinePatterns.add(
'honeycomb_user_icon',
HoneycombUserIcon(USERNAME_PATTERN_ICON),
'<image_link')
md.inlinePatterns.add(
'honeycomb_user_iconname',
HoneycombUserIconName(USERNAME_PATTERN_ICONNAME),
'<image_link')
md.inlinePatterns['honeycomb_user_name'] = HoneycombUserName(
USERNAME_PATTERN_NAME)
md.postprocessors['tablefix'] = TableFix(md)
md.postprocessors['dlfix'] = DLFix(md)
md.postprocessors['abbrfix'] = ABBRFix(md)
class HoneycombMarkdownLite(Extension):
def extendMarkdown(self, md, md_globals):
md.preprocessors['poetry'] = Poetry(md)
md.postprocessors['tablefix'] = TableFix(md)
md.postprocessors['dlfix'] = DLFix(md)
md.postprocessors['abbrfix'] = ABBRFix(md)