Show More
@@ -1,192 +1,192 b'' | |||
|
1 | 1 | import markdown |
|
2 | 2 | from markdown.inlinepatterns import Pattern, SubstituteTagPattern |
|
3 | 3 | from markdown.util import etree |
|
4 | 4 | import boards |
|
5 | 5 | |
|
6 | 6 | __author__ = 'neko259' |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | AUTOLINK_PATTERN = r'(https?://\S+)' |
|
10 | 10 | QUOTE_PATTERN = r'^(?<!>)(>[^>].*)$' |
|
11 | 11 | REFLINK_PATTERN = r'((>>)(\d+))' |
|
12 | 12 | SPOILER_PATTERN = r'%%([^(%%)]+)%%' |
|
13 | 13 | COMMENT_PATTERN = r'^(//(.+))' |
|
14 | 14 | STRIKETHROUGH_PATTERN = r'~(.+)~' |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | class TextFormatter(): |
|
18 | 18 | """ |
|
19 | 19 | An interface for formatter that can be used in the text format panel |
|
20 | 20 | """ |
|
21 | 21 | |
|
22 | 22 | name = '' |
|
23 | 23 | |
|
24 | 24 | # Left and right tags for the button preview |
|
25 | 25 | preview_left = '' |
|
26 | 26 | preview_right = '' |
|
27 | 27 | |
|
28 | 28 | # Left and right characters for the textarea input |
|
29 | 29 | format_left = '' |
|
30 | 30 | format_right = '' |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class AutolinkPattern(Pattern): |
|
34 | 34 | def handleMatch(self, m): |
|
35 | 35 | link_element = etree.Element('a') |
|
36 | 36 | href = m.group(2) |
|
37 | 37 | link_element.set('href', href) |
|
38 | 38 | link_element.text = href |
|
39 | 39 | |
|
40 | 40 | return link_element |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | class QuotePattern(Pattern, TextFormatter): |
|
44 | 44 | name = '' |
|
45 | 45 | preview_left = '<span class="quote">> ' |
|
46 | 46 | preview_right = '</span>' |
|
47 | 47 | |
|
48 | 48 | format_left = '>' |
|
49 | 49 | |
|
50 | 50 | def handleMatch(self, m): |
|
51 | 51 | quote_element = etree.Element('span') |
|
52 | 52 | quote_element.set('class', 'quote') |
|
53 | 53 | quote_element.text = m.group(2) |
|
54 | 54 | |
|
55 | 55 | return quote_element |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | class ReflinkPattern(Pattern): |
|
59 | 59 | def handleMatch(self, m): |
|
60 | 60 | post_id = m.group(4) |
|
61 | 61 | |
|
62 | 62 | posts = boards.models.Post.objects.filter(id=post_id) |
|
63 | 63 | if posts.count() > 0: |
|
64 | 64 | ref_element = etree.Element('a') |
|
65 | 65 | |
|
66 | 66 | post = posts[0] |
|
67 | 67 | |
|
68 | 68 | ref_element.set('href', post.get_url()) |
|
69 | 69 | ref_element.text = m.group(2) |
|
70 | 70 | |
|
71 | 71 | return ref_element |
|
72 | 72 | |
|
73 | 73 | |
|
74 | 74 | class SpoilerPattern(Pattern, TextFormatter): |
|
75 | 75 | name = 's' |
|
76 | 76 | preview_left = '<span class="spoiler">' |
|
77 | 77 | preview_right = '</span>' |
|
78 | 78 | |
|
79 | 79 | format_left = '%%' |
|
80 | 80 | format_right = '%%' |
|
81 | 81 | |
|
82 | 82 | def handleMatch(self, m): |
|
83 | 83 | quote_element = etree.Element('span') |
|
84 | 84 | quote_element.set('class', 'spoiler') |
|
85 | 85 | quote_element.text = m.group(2) |
|
86 | 86 | |
|
87 | 87 | return quote_element |
|
88 | 88 | |
|
89 | 89 | |
|
90 | 90 | class CommentPattern(Pattern, TextFormatter): |
|
91 | 91 | name = '' |
|
92 | 92 | preview_left = '<span class="comment">// ' |
|
93 | 93 | preview_right = '</span>' |
|
94 | 94 | |
|
95 | 95 | format_left = '//' |
|
96 | 96 | |
|
97 | 97 | def handleMatch(self, m): |
|
98 | 98 | quote_element = etree.Element('span') |
|
99 | 99 | quote_element.set('class', 'comment') |
|
100 | 100 | quote_element.text = '//' + m.group(3) |
|
101 | 101 | |
|
102 | 102 | return quote_element |
|
103 | 103 | |
|
104 | 104 | |
|
105 | 105 | class StrikeThroughPattern(Pattern, TextFormatter): |
|
106 | 106 | name = 's' |
|
107 | 107 | preview_left = '<span class="strikethrough">' |
|
108 | 108 | preview_right = '</span>' |
|
109 | 109 | |
|
110 | 110 | format_left = '~' |
|
111 | 111 | format_right = '~' |
|
112 | 112 | |
|
113 | 113 | def handleMatch(self, m): |
|
114 | 114 | quote_element = etree.Element('span') |
|
115 | 115 | quote_element.set('class', 'strikethrough') |
|
116 | 116 | quote_element.text = m.group(2) |
|
117 | 117 | |
|
118 | 118 | return quote_element |
|
119 | 119 | |
|
120 | 120 | |
|
121 | 121 | class ItalicPattern(TextFormatter): |
|
122 | 122 | name = 'i' |
|
123 | 123 | preview_left = '<i>' |
|
124 | 124 | preview_right = '</i>' |
|
125 | 125 | |
|
126 | 126 | format_left = '_' |
|
127 | 127 | format_right = '_' |
|
128 | 128 | |
|
129 | 129 | |
|
130 | 130 | class BoldPattern(TextFormatter): |
|
131 | 131 | name = 'b' |
|
132 | 132 | preview_left = '<b>' |
|
133 | 133 | preview_right = '</b>' |
|
134 | 134 | |
|
135 | 135 | format_left = '__' |
|
136 | 136 | format_right = '__' |
|
137 | 137 | |
|
138 | 138 | |
|
139 | 139 | class CodePattern(TextFormatter): |
|
140 | 140 | name = 'code' |
|
141 | 141 | preview_left = '<code>' |
|
142 | 142 | preview_right = '</code>' |
|
143 | 143 | |
|
144 | 144 | format_left = ' ' |
|
145 | 145 | |
|
146 | 146 | |
|
147 | 147 | class NeboardMarkdown(markdown.Extension): |
|
148 | 148 | def extendMarkdown(self, md, md_globals): |
|
149 | 149 | self._add_neboard_patterns(md) |
|
150 | 150 | self._delete_patterns(md) |
|
151 | 151 | |
|
152 | 152 | def _delete_patterns(self, md): |
|
153 | 153 | del md.parser.blockprocessors['quote'] |
|
154 | 154 | |
|
155 | 155 | del md.inlinePatterns['image_link'] |
|
156 | 156 | del md.inlinePatterns['image_reference'] |
|
157 | 157 | |
|
158 | 158 | def _add_neboard_patterns(self, md): |
|
159 | 159 | autolink = AutolinkPattern(AUTOLINK_PATTERN, md) |
|
160 | 160 | quote = QuotePattern(QUOTE_PATTERN, md) |
|
161 | 161 | reflink = ReflinkPattern(REFLINK_PATTERN, md) |
|
162 | 162 | spoiler = SpoilerPattern(SPOILER_PATTERN, md) |
|
163 | 163 | comment = CommentPattern(COMMENT_PATTERN, md) |
|
164 | 164 | strikethrough = StrikeThroughPattern(STRIKETHROUGH_PATTERN, md) |
|
165 | 165 | |
|
166 | 166 | md.inlinePatterns[u'autolink_ext'] = autolink |
|
167 | 167 | md.inlinePatterns[u'spoiler'] = spoiler |
|
168 | 168 | md.inlinePatterns[u'strikethrough'] = strikethrough |
|
169 | 169 | md.inlinePatterns[u'comment'] = comment |
|
170 | 170 | md.inlinePatterns[u'reflink'] = reflink |
|
171 | 171 | md.inlinePatterns[u'quote'] = quote |
|
172 | 172 | |
|
173 | 173 | |
|
174 | 174 | def make_extension(configs=None): |
|
175 | 175 | return NeboardMarkdown(configs=configs) |
|
176 | 176 | |
|
177 | 177 | neboard_extension = make_extension() |
|
178 | 178 | |
|
179 | 179 | |
|
180 | 180 | def markdown_extended(markup): |
|
181 | 181 | return markdown.markdown(markup, [neboard_extension, 'nl2br'], |
|
182 |
safe_mode= |
|
|
182 | safe_mode='escape') | |
|
183 | 183 | |
|
184 | 184 | formatters = [ |
|
185 | 185 | QuotePattern, |
|
186 | 186 | SpoilerPattern, |
|
187 | 187 | ItalicPattern, |
|
188 | 188 | BoldPattern, |
|
189 | 189 | CommentPattern, |
|
190 | 190 | StrikeThroughPattern, |
|
191 | 191 | CodePattern, |
|
192 | 192 | ] |
General Comments 0
You need to be logged in to leave comments.
Login now