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