##// END OF EJS Templates
Added proper formatting when several lines are selected
neko259 -
r440:4330ff5a 1.4.1 default
parent child Browse files
Show More
@@ -1,189 +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">&gt; '
46 preview_left = '<span class="quote">&gt; '
47 preview_right = '</span>'
47 preview_right = '</span>'
48
48
49 format_left = '&gt;'
49 format_left = '&gt;'
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):
147 name = 'code'
148 preview_left = '<code>'
149 preview_right = '</code>'
150
151 format_left = ' '
152
153
146 class NeboardMarkdown(markdown.Extension):
154 class NeboardMarkdown(markdown.Extension):
147 def extendMarkdown(self, md, md_globals):
155 def extendMarkdown(self, md, md_globals):
148 self._add_neboard_patterns(md)
156 self._add_neboard_patterns(md)
149 self._delete_patterns(md)
157 self._delete_patterns(md)
150
158
151 def _delete_patterns(self, md):
159 def _delete_patterns(self, md):
152 del md.parser.blockprocessors['quote']
160 del md.parser.blockprocessors['quote']
153
161
154 del md.inlinePatterns['image_link']
162 del md.inlinePatterns['image_link']
155 del md.inlinePatterns['image_reference']
163 del md.inlinePatterns['image_reference']
156
164
157 def _add_neboard_patterns(self, md):
165 def _add_neboard_patterns(self, md):
158 autolink = AutolinkPattern(AUTOLINK_PATTERN, md)
166 autolink = AutolinkPattern(AUTOLINK_PATTERN, md)
159 quote = QuotePattern(QUOTE_PATTERN, md)
167 quote = QuotePattern(QUOTE_PATTERN, md)
160 reflink = ReflinkPattern(REFLINK_PATTERN, md)
168 reflink = ReflinkPattern(REFLINK_PATTERN, md)
161 spoiler = SpoilerPattern(SPOILER_PATTERN, md)
169 spoiler = SpoilerPattern(SPOILER_PATTERN, md)
162 comment = CommentPattern(COMMENT_PATTERN, md)
170 comment = CommentPattern(COMMENT_PATTERN, md)
163 strikethrough = StrikeThroughPattern(STRIKETHROUGH_PATTERN, md)
171 strikethrough = StrikeThroughPattern(STRIKETHROUGH_PATTERN, md)
164
172
165 md.inlinePatterns[u'autolink_ext'] = autolink
173 md.inlinePatterns[u'autolink_ext'] = autolink
166 md.inlinePatterns[u'spoiler'] = spoiler
174 md.inlinePatterns[u'spoiler'] = spoiler
167 md.inlinePatterns[u'strikethrough'] = strikethrough
175 md.inlinePatterns[u'strikethrough'] = strikethrough
168 md.inlinePatterns[u'comment'] = comment
176 md.inlinePatterns[u'comment'] = comment
169 md.inlinePatterns[u'reflink'] = reflink
177 md.inlinePatterns[u'reflink'] = reflink
170 md.inlinePatterns[u'quote'] = quote
178 md.inlinePatterns[u'quote'] = quote
171
179
172
180
173 def makeExtension(configs=None):
181 def makeExtension(configs=None):
174 return NeboardMarkdown(configs=configs)
182 return NeboardMarkdown(configs=configs)
175
183
176 neboard_extension = makeExtension()
184 neboard_extension = makeExtension()
177
185
178
186
179 def markdown_extended(markup):
187 def markdown_extended(markup):
180 return markdown.markdown(markup, [neboard_extension], safe_mode=True)
188 return markdown.markdown(markup, [neboard_extension], safe_mode=True)
181
189
182 formatters = [
190 formatters = [
183 QuotePattern,
191 QuotePattern,
184 SpoilerPattern,
192 SpoilerPattern,
185 ItalicPattern,
193 ItalicPattern,
186 BoldPattern,
194 BoldPattern,
187 CommentPattern,
195 CommentPattern,
188 StrikeThroughPattern,
196 StrikeThroughPattern,
197 CodePattern,
189 ]
198 ]
@@ -1,42 +1,116 b''
1 /*
1 /*
2 @licstart The following is the entire license notice for the
2 @licstart The following is the entire license notice for the
3 JavaScript code in this page.
3 JavaScript code in this page.
4
4
5
5
6 Copyright (C) 2013 neko259
6 Copyright (C) 2013 neko259
7
7
8 The JavaScript code in this page is free software: you can
8 The JavaScript code in this page is free software: you can
9 redistribute it and/or modify it under the terms of the GNU
9 redistribute it and/or modify it under the terms of the GNU
10 General Public License (GNU GPL) as published by the Free Software
10 General Public License (GNU GPL) as published by the Free Software
11 Foundation, either version 3 of the License, or (at your option)
11 Foundation, either version 3 of the License, or (at your option)
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 without even the implied warranty of MERCHANTABILITY or FITNESS
13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15
15
16 As additional permission under GNU GPL version 3 section 7, you
16 As additional permission under GNU GPL version 3 section 7, you
17 may distribute non-source (e.g., minimized or compacted) forms of
17 may distribute non-source (e.g., minimized or compacted) forms of
18 that code without the copy of the GNU GPL normally required by
18 that code without the copy of the GNU GPL normally required by
19 section 4, provided you include this license notice and a URL
19 section 4, provided you include this license notice and a URL
20 through which recipients can access the Corresponding Source.
20 through which recipients can access the Corresponding Source.
21
21
22 @licend The above is the entire license notice
22 @licend The above is the entire license notice
23 for the JavaScript code in this page.
23 for the JavaScript code in this page.
24 */
24 */
25
25
26 /**
27 * Add the desired characters to the start and end of selection.
28 * @param start Start (left) text
29 * @param end End (right) text
30 * @returns {boolean}
31 */
26 function addMarkToMsg(start, end) {
32 function addMarkToMsg(start, end) {
33 if (end.length == 0) {
34 return addTextToEachLineOfSelection(start);
35 }
36
27 var textarea = document.getElementsByTagName('textarea')[0];
37 var textarea = document.getElementsByTagName('textarea')[0];
28 if(!textarea) return;
38 if(!textarea) return;
29 if( document.selection ) {
39 if( document.selection ) {
30 textarea.focus();
40 textarea.focus();
31 sel = document.selection.createRange();
41 sel = document.selection.createRange();
32 sel.text = start + sel.text + end;
42 sel.text = start + sel.text + end;
33 } else if(textarea.selectionStart || textarea.selectionStart == '0') {
43 } else if(textarea.selectionStart || textarea.selectionStart == '0') {
34 textarea.focus();
44 textarea.focus();
35 var startPos = textarea.selectionStart;
45 var startPos = textarea.selectionStart;
36 var endPos = textarea.selectionEnd;
46 var endPos = textarea.selectionEnd;
37 textarea.value = textarea.value.substring(0, startPos) + start + textarea.value.substring(startPos, endPos) + end + textarea.value.substring( endPos, textarea.value.length );
47 textarea.value = textarea.value.substring(0, startPos) + start + textarea.value.substring(startPos, endPos) + end + textarea.value.substring( endPos, textarea.value.length );
38 } else {
48 } else {
39 textarea.value += start + end;
49 textarea.value += start + end;
40 }
50 }
41 return false;
51 return false;
52 }
53
54 /**
55 * Add text to the beginning of each selected line. Partially selected lines
56 * are included
57 * @param textToAdd
58 * @returns {*}
59 */
60 function addTextToEachLineOfSelection(textToAdd) {
61 var editor, end, newValue, start, value, _ref, _ref1;
62 editor = document.getElementsByTagName('textarea')[0];
63 _ref = [editor.selectionStart, editor.selectionEnd], start = _ref[0], end = _ref[1];
64 if (start == null) {
65 return;
66 }
67 if (start === end) {
68 return;
69 }
70 console.log("Selection range: start=" + start + " end=" + end);
71 value = editor.value;
72 _ref1 = getLinesRange(start, end, value), start = _ref1[0], end = _ref1[1];
73 newValue = replaceLines(start, end, value, textToAdd);
74 return editor.value = newValue;
75 }
76
77 function replaceLines(start, end, value, textToAdd) {
78 var line, replacedText, text;
79 text = value.slice(start, end);
80 replacedText = ((function() {
81 var _i, _len, _ref, _results;
82 _ref = text.split("\n");
83 _results = [];
84 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
85 line = _ref[_i];
86 _results.push(textToAdd + line);
87 }
88 return _results;
89 })()).join("\n");
90 return replaceSubstring(start, end, value, replacedText);
91 }
92
93 function replaceSubstring(start, end, string, replacingString) {
94 return string.slice(0, start) + replacingString + string.slice(end);
95 }
96
97 function getLinesRange(start, end, value) {
98 var i, rangeEnd, rangeStart, _i, _j, _ref, _ref1;
99 if (value[start] === "\n") {
100 start = start - 1;
101 }
102 _ref = [start, end], rangeStart = _ref[0], rangeEnd = _ref[1];
103 for (i = _i = start; start <= 0 ? _i <= 0 : _i >= 0; i = start <= 0 ? ++_i : --_i) {
104 if (value[i] === "\n") {
105 break;
106 }
107 rangeStart = i;
108 }
109 for (i = _j = end, _ref1 = value.length; end <= _ref1 ? _j < _ref1 : _j > _ref1; i = end <= _ref1 ? ++_j : --_j) {
110 if (value[i] === "\n") {
111 break;
112 }
113 rangeEnd = i;
114 }
115 return [rangeStart, rangeEnd];
42 } No newline at end of file
116 }
General Comments 0
You need to be logged in to leave comments. Login now