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