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