Show More
@@ -1,152 +1,164 | |||||
1 | # coding=utf-8 |
|
1 | # coding=utf-8 | |
2 |
|
2 | |||
3 | import re |
|
3 | import re | |
4 | import bbcode |
|
4 | import bbcode | |
5 |
|
5 | |||
6 | import boards |
|
6 | import boards | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | __author__ = 'neko259' |
|
9 | __author__ = 'neko259' | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | REFLINK_PATTERN = re.compile(r'\d+') |
|
12 | REFLINK_PATTERN = re.compile(r'\d+') | |
|
13 | MULTI_NEWLINES_PATTERN = re.compile(r'(\r?\n){2,}') | |||
|
14 | ONE_NEWLINE = '\n' | |||
13 |
|
15 | |||
14 |
|
16 | |||
15 | class TextFormatter(): |
|
17 | class TextFormatter(): | |
16 | """ |
|
18 | """ | |
17 | An interface for formatter that can be used in the text format panel |
|
19 | An interface for formatter that can be used in the text format panel | |
18 | """ |
|
20 | """ | |
19 |
|
21 | |||
20 | def __init__(self): |
|
22 | def __init__(self): | |
21 | pass |
|
23 | pass | |
22 |
|
24 | |||
23 | name = '' |
|
25 | name = '' | |
24 |
|
26 | |||
25 | # Left and right tags for the button preview |
|
27 | # Left and right tags for the button preview | |
26 | preview_left = '' |
|
28 | preview_left = '' | |
27 | preview_right = '' |
|
29 | preview_right = '' | |
28 |
|
30 | |||
29 | # Left and right characters for the textarea input |
|
31 | # Left and right characters for the textarea input | |
30 | format_left = '' |
|
32 | format_left = '' | |
31 | format_right = '' |
|
33 | format_right = '' | |
32 |
|
34 | |||
33 |
|
35 | |||
34 | class AutolinkPattern(): |
|
36 | class AutolinkPattern(): | |
35 | def handleMatch(self, m): |
|
37 | def handleMatch(self, m): | |
36 | link_element = etree.Element('a') |
|
38 | link_element = etree.Element('a') | |
37 | href = m.group(2) |
|
39 | href = m.group(2) | |
38 | link_element.set('href', href) |
|
40 | link_element.set('href', href) | |
39 | link_element.text = href |
|
41 | link_element.text = href | |
40 |
|
42 | |||
41 | return link_element |
|
43 | return link_element | |
42 |
|
44 | |||
43 |
|
45 | |||
44 | class QuotePattern(TextFormatter): |
|
46 | class QuotePattern(TextFormatter): | |
45 | name = 'q' |
|
47 | name = 'q' | |
46 | preview_left = '<span class="multiquote">' |
|
48 | preview_left = '<span class="multiquote">' | |
47 | preview_right = '</span>' |
|
49 | preview_right = '</span>' | |
48 |
|
50 | |||
49 | format_left = '[quote]' |
|
51 | format_left = '[quote]' | |
50 | format_right = '[/quote]' |
|
52 | format_right = '[/quote]' | |
51 |
|
53 | |||
52 |
|
54 | |||
53 | class SpoilerPattern(TextFormatter): |
|
55 | class SpoilerPattern(TextFormatter): | |
54 | name = 'spoiler' |
|
56 | name = 'spoiler' | |
55 | preview_left = '<span class="spoiler">' |
|
57 | preview_left = '<span class="spoiler">' | |
56 | preview_right = '</span>' |
|
58 | preview_right = '</span>' | |
57 |
|
59 | |||
58 | format_left = '[spoiler]' |
|
60 | format_left = '[spoiler]' | |
59 | format_right = '[/spoiler]' |
|
61 | format_right = '[/spoiler]' | |
60 |
|
62 | |||
61 | def handleMatch(self, m): |
|
63 | def handleMatch(self, m): | |
62 | quote_element = etree.Element('span') |
|
64 | quote_element = etree.Element('span') | |
63 | quote_element.set('class', 'spoiler') |
|
65 | quote_element.set('class', 'spoiler') | |
64 | quote_element.text = m.group(2) |
|
66 | quote_element.text = m.group(2) | |
65 |
|
67 | |||
66 | return quote_element |
|
68 | return quote_element | |
67 |
|
69 | |||
68 |
|
70 | |||
69 | class CommentPattern(TextFormatter): |
|
71 | class CommentPattern(TextFormatter): | |
70 | name = '' |
|
72 | name = '' | |
71 | preview_left = '<span class="comment">// ' |
|
73 | preview_left = '<span class="comment">// ' | |
72 | preview_right = '</span>' |
|
74 | preview_right = '</span>' | |
73 |
|
75 | |||
74 | format_left = '[comment]' |
|
76 | format_left = '[comment]' | |
75 | format_right = '[/comment]' |
|
77 | format_right = '[/comment]' | |
76 |
|
78 | |||
77 |
|
79 | |||
78 | class StrikeThroughPattern(TextFormatter): |
|
80 | class StrikeThroughPattern(TextFormatter): | |
79 | name = 's' |
|
81 | name = 's' | |
80 | preview_left = '<span class="strikethrough">' |
|
82 | preview_left = '<span class="strikethrough">' | |
81 | preview_right = '</span>' |
|
83 | preview_right = '</span>' | |
82 |
|
84 | |||
83 | format_left = '[s]' |
|
85 | format_left = '[s]' | |
84 | format_right = '[/s]' |
|
86 | format_right = '[/s]' | |
85 |
|
87 | |||
86 |
|
88 | |||
87 | class ItalicPattern(TextFormatter): |
|
89 | class ItalicPattern(TextFormatter): | |
88 | name = 'i' |
|
90 | name = 'i' | |
89 | preview_left = '<i>' |
|
91 | preview_left = '<i>' | |
90 | preview_right = '</i>' |
|
92 | preview_right = '</i>' | |
91 |
|
93 | |||
92 | format_left = '[i]' |
|
94 | format_left = '[i]' | |
93 | format_right = '[/i]' |
|
95 | format_right = '[/i]' | |
94 |
|
96 | |||
95 |
|
97 | |||
96 | class BoldPattern(TextFormatter): |
|
98 | class BoldPattern(TextFormatter): | |
97 | name = 'b' |
|
99 | name = 'b' | |
98 | preview_left = '<b>' |
|
100 | preview_left = '<b>' | |
99 | preview_right = '</b>' |
|
101 | preview_right = '</b>' | |
100 |
|
102 | |||
101 | format_left = '[b]' |
|
103 | format_left = '[b]' | |
102 | format_right = '[/b]' |
|
104 | format_right = '[/b]' | |
103 |
|
105 | |||
104 |
|
106 | |||
105 | class CodePattern(TextFormatter): |
|
107 | class CodePattern(TextFormatter): | |
106 | name = 'code' |
|
108 | name = 'code' | |
107 | preview_left = '<code>' |
|
109 | preview_left = '<code>' | |
108 | preview_right = '</code>' |
|
110 | preview_right = '</code>' | |
109 |
|
111 | |||
110 | format_left = '[code]' |
|
112 | format_left = '[code]' | |
111 | format_right = '[/code]' |
|
113 | format_right = '[/code]' | |
112 |
|
114 | |||
113 |
|
115 | |||
114 | def render_reflink(tag_name, value, options, parent, context): |
|
116 | def render_reflink(tag_name, value, options, parent, context): | |
115 | if not REFLINK_PATTERN.match(value): |
|
117 | if not REFLINK_PATTERN.match(value): | |
116 | return u'>>%s' % value |
|
118 | return u'>>%s' % value | |
117 |
|
119 | |||
118 | post_id = int(value) |
|
120 | post_id = int(value) | |
119 |
|
121 | |||
120 | posts = boards.models.Post.objects.filter(id=post_id) |
|
122 | posts = boards.models.Post.objects.filter(id=post_id) | |
121 | if posts.exists(): |
|
123 | if posts.exists(): | |
122 | post = posts[0] |
|
124 | post = posts[0] | |
123 |
|
125 | |||
124 | return u'<a href=%s>>>%s</a>' % (post.get_url(), post_id) |
|
126 | return u'<a href=%s>>>%s</a>' % (post.get_url(), post_id) | |
125 | else: |
|
127 | else: | |
126 | return u'>>%s' % value |
|
128 | return u'>>%s' % value | |
127 |
|
129 | |||
128 |
|
130 | |||
|
131 | def preparse_text(text): | |||
|
132 | """ | |||
|
133 | Performs manual parsing before the bbcode parser is used. | |||
|
134 | """ | |||
|
135 | ||||
|
136 | return MULTI_NEWLINES_PATTERN.sub(ONE_NEWLINE, text) | |||
|
137 | ||||
|
138 | ||||
129 | def bbcode_extended(markup): |
|
139 | def bbcode_extended(markup): | |
130 | parser = bbcode.Parser() |
|
140 | parser = bbcode.Parser() | |
131 | parser.add_formatter('post', render_reflink, strip=True) |
|
141 | parser.add_formatter('post', render_reflink, strip=True) | |
132 | parser.add_simple_formatter('quote', |
|
142 | parser.add_simple_formatter('quote', | |
133 | u'<span class="multiquote">%(value)s</span>', strip=True) |
|
143 | u'<span class="multiquote">%(value)s</span>', strip=True) | |
134 | parser.add_simple_formatter('comment', |
|
144 | parser.add_simple_formatter('comment', | |
135 | u'<span class="comment">//%(value)s</span>') |
|
145 | u'<span class="comment">//%(value)s</span>') | |
136 | parser.add_simple_formatter('spoiler', |
|
146 | parser.add_simple_formatter('spoiler', | |
137 | u'<span class="spoiler">%(value)s</span>') |
|
147 | u'<span class="spoiler">%(value)s</span>') | |
138 | parser.add_simple_formatter('s', |
|
148 | parser.add_simple_formatter('s', | |
139 | u'<span class="strikethrough">%(value)s</span>') |
|
149 | u'<span class="strikethrough">%(value)s</span>') | |
140 | parser.add_simple_formatter('code', |
|
150 | parser.add_simple_formatter('code', | |
141 | u'<pre><code>%(value)s</pre></code>') |
|
151 | u'<pre><code>%(value)s</pre></code>') | |
142 | return parser.format(markup) |
|
152 | ||
|
153 | text = preparse_text(markup) | |||
|
154 | return parser.format(text) | |||
143 |
|
155 | |||
144 | formatters = [ |
|
156 | formatters = [ | |
145 | QuotePattern, |
|
157 | QuotePattern, | |
146 | SpoilerPattern, |
|
158 | SpoilerPattern, | |
147 | ItalicPattern, |
|
159 | ItalicPattern, | |
148 | BoldPattern, |
|
160 | BoldPattern, | |
149 | CommentPattern, |
|
161 | CommentPattern, | |
150 | StrikeThroughPattern, |
|
162 | StrikeThroughPattern, | |
151 | CodePattern, |
|
163 | CodePattern, | |
152 | ] |
|
164 | ] |
General Comments 0
You need to be logged in to leave comments.
Login now