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