Show More
@@ -1,6 +1,7 | |||||
1 | # coding=utf-8 |
|
1 | # coding=utf-8 | |
2 |
|
2 | |||
3 | import re |
|
3 | import re | |
|
4 | import random | |||
4 | import bbcode |
|
5 | import bbcode | |
5 |
|
6 | |||
6 | from urllib.parse import unquote |
|
7 | from urllib.parse import unquote | |
@@ -67,13 +68,6 class SpoilerPattern(TextFormatter): | |||||
67 | format_left = '[spoiler]' |
|
68 | format_left = '[spoiler]' | |
68 | format_right = '[/spoiler]' |
|
69 | format_right = '[/spoiler]' | |
69 |
|
70 | |||
70 | def handleMatch(self, m): |
|
|||
71 | quote_element = etree.Element('span') |
|
|||
72 | quote_element.set('class', 'spoiler') |
|
|||
73 | quote_element.text = m.group(2) |
|
|||
74 |
|
||||
75 | return quote_element |
|
|||
76 |
|
||||
77 |
|
71 | |||
78 | class CommentPattern(TextFormatter): |
|
72 | class CommentPattern(TextFormatter): | |
79 | name = '' |
|
73 | name = '' | |
@@ -173,6 +167,17 def render_tag(tag_name, value, options, | |||||
173 | return url |
|
167 | return url | |
174 |
|
168 | |||
175 |
|
169 | |||
|
170 | def render_spoiler(tag_name, value, options, parent, context): | |||
|
171 | text_len = len(value) | |||
|
172 | pre_spaces = ' ' * max(random.randint(0, text_len // 2), 2) | |||
|
173 | post_spaces = ' ' * max(random.randint(0, text_len // 2), 2) | |||
|
174 | return '<span class="spoiler">{}{}{}</span>'.format(pre_spaces, value, | |||
|
175 | post_spaces) | |||
|
176 | ||||
|
177 | return quote_element | |||
|
178 | ||||
|
179 | ||||
|
180 | ||||
176 | formatters = [ |
|
181 | formatters = [ | |
177 | QuotePattern, |
|
182 | QuotePattern, | |
178 | SpoilerPattern, |
|
183 | SpoilerPattern, | |
@@ -202,11 +207,10 class Parser: | |||||
202 | self.parser.add_formatter('quote', render_quote, strip=True) |
|
207 | self.parser.add_formatter('quote', render_quote, strip=True) | |
203 | self.parser.add_formatter('user', render_notification, strip=True) |
|
208 | self.parser.add_formatter('user', render_notification, strip=True) | |
204 | self.parser.add_formatter('tag', render_tag, strip=True) |
|
209 | self.parser.add_formatter('tag', render_tag, strip=True) | |
|
210 | self.parser.add_formatter('spoiler', render_spoiler, strip=True) | |||
205 | self.parser.add_simple_formatter( |
|
211 | self.parser.add_simple_formatter( | |
206 | 'comment', '<span class="comment">//%(value)s</span>') |
|
212 | 'comment', '<span class="comment">//%(value)s</span>') | |
207 | self.parser.add_simple_formatter( |
|
213 | self.parser.add_simple_formatter( | |
208 | 'spoiler', '<span class="spoiler">%(value)s</span>') |
|
|||
209 | self.parser.add_simple_formatter( |
|
|||
210 | 's', '<span class="strikethrough">%(value)s</span>') |
|
214 | 's', '<span class="strikethrough">%(value)s</span>') | |
211 | # TODO Why not use built-in tag? |
|
215 | # TODO Why not use built-in tag? | |
212 | self.parser.add_simple_formatter('code', |
|
216 | self.parser.add_simple_formatter('code', |
General Comments 0
You need to be logged in to leave comments.
Login now