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