##// 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 1 from django.core.urlresolvers import reverse
2 2 import markdown
3 3 from markdown.inlinepatterns import Pattern
4 4 from markdown.util import etree
5 5 import boards
6 6
7 7 __author__ = 'neko259'
8 8
9 9
10 10 AUTOLINK_PATTERN = r'(https?://\S+)'
11 11 QUOTE_PATTERN = r'^(?<!>)(>[^>].+)$'
12 12 REFLINK_PATTERN = r'((>>)(\d+))'
13 13 SPOILER_PATTERN = r'%%(.+)%%'
14 14 COMMENT_PATTERN = r'^(//(.+))'
15 15 STRIKETHROUGH_PATTERN = r'~(.+)~'
16 16
17 17
18 18 class TextFormatter():
19 19 """
20 20 An interface for formatter that can be used in the text format panel
21 21 """
22 22
23 23 name = ''
24 24
25 25 # Left and right tags for the button preview
26 26 preview_left = ''
27 27 preview_right = ''
28 28
29 29 # Left and right characters for the textarea input
30 30 format_left = ''
31 31 format_right = ''
32 32
33 33
34 34 class AutolinkPattern(Pattern):
35 35 def handleMatch(self, m):
36 36 link_element = etree.Element('a')
37 37 href = m.group(2)
38 38 link_element.set('href', href)
39 39 link_element.text = href
40 40
41 41 return link_element
42 42
43 43
44 44 class QuotePattern(Pattern, TextFormatter):
45 45 name = ''
46 46 preview_left = '<span class="quote">&gt; '
47 47 preview_right = '</span>'
48 48
49 49 format_left = '&gt;'
50 50
51 51 def handleMatch(self, m):
52 52 quote_element = etree.Element('span')
53 53 quote_element.set('class', 'quote')
54 54 quote_element.text = m.group(2)
55 55
56 56 return quote_element
57 57
58 58
59 59 class ReflinkPattern(Pattern):
60 60 def handleMatch(self, m):
61 61 post_id = m.group(4)
62 62
63 63 posts = boards.models.Post.objects.filter(id=post_id)
64 64 if posts.count() > 0:
65 65 ref_element = etree.Element('a')
66 66
67 67 post = posts[0]
68 68 if not post.is_opening():
69 69 link = reverse(boards.views.thread, kwargs={
70 70 'post_id': post.thread_new.get_opening_post().id})\
71 71 + '#' + post_id
72 72 else:
73 73 link = reverse(boards.views.thread, kwargs={'post_id': post_id})
74 74
75 75 ref_element.set('href', link)
76 76 ref_element.text = m.group(2)
77 77
78 78 return ref_element
79 79
80 80
81 81 class SpoilerPattern(Pattern, TextFormatter):
82 82 name = 's'
83 83 preview_left = '<span class="spoiler">'
84 84 preview_right = '</span>'
85 85
86 86 format_left = '%%'
87 87 format_right = '%%'
88 88
89 89 def handleMatch(self, m):
90 90 quote_element = etree.Element('span')
91 91 quote_element.set('class', 'spoiler')
92 92 quote_element.text = m.group(2)
93 93
94 94 return quote_element
95 95
96 96
97 97 class CommentPattern(Pattern, TextFormatter):
98 98 name = ''
99 99 preview_left = '<span class="comment">// '
100 100 preview_right = '</span>'
101 101
102 102 format_left = '//'
103 103
104 104 def handleMatch(self, m):
105 105 quote_element = etree.Element('span')
106 106 quote_element.set('class', 'comment')
107 107 quote_element.text = '//' + m.group(3)
108 108
109 109 return quote_element
110 110
111 111
112 112 class StrikeThroughPattern(Pattern, TextFormatter):
113 113 name = 's'
114 114 preview_left = '<span class="strikethrough">'
115 115 preview_right = '</span>'
116 116
117 117 format_left = '~'
118 118 format_right = '~'
119 119
120 120 def handleMatch(self, m):
121 121 quote_element = etree.Element('span')
122 122 quote_element.set('class', 'strikethrough')
123 123 quote_element.text = m.group(2)
124 124
125 125 return quote_element
126 126
127 127
128 128 class ItalicPattern(TextFormatter):
129 129 name = 'i'
130 130 preview_left = '<i>'
131 131 preview_right = '</i>'
132 132
133 133 format_left = '_'
134 134 format_right = '_'
135 135
136 136
137 137 class BoldPattern(TextFormatter):
138 138 name = 'b'
139 139 preview_left = '<b>'
140 140 preview_right = '</b>'
141 141
142 142 format_left = '__'
143 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 154 class NeboardMarkdown(markdown.Extension):
147 155 def extendMarkdown(self, md, md_globals):
148 156 self._add_neboard_patterns(md)
149 157 self._delete_patterns(md)
150 158
151 159 def _delete_patterns(self, md):
152 160 del md.parser.blockprocessors['quote']
153 161
154 162 del md.inlinePatterns['image_link']
155 163 del md.inlinePatterns['image_reference']
156 164
157 165 def _add_neboard_patterns(self, md):
158 166 autolink = AutolinkPattern(AUTOLINK_PATTERN, md)
159 167 quote = QuotePattern(QUOTE_PATTERN, md)
160 168 reflink = ReflinkPattern(REFLINK_PATTERN, md)
161 169 spoiler = SpoilerPattern(SPOILER_PATTERN, md)
162 170 comment = CommentPattern(COMMENT_PATTERN, md)
163 171 strikethrough = StrikeThroughPattern(STRIKETHROUGH_PATTERN, md)
164 172
165 173 md.inlinePatterns[u'autolink_ext'] = autolink
166 174 md.inlinePatterns[u'spoiler'] = spoiler
167 175 md.inlinePatterns[u'strikethrough'] = strikethrough
168 176 md.inlinePatterns[u'comment'] = comment
169 177 md.inlinePatterns[u'reflink'] = reflink
170 178 md.inlinePatterns[u'quote'] = quote
171 179
172 180
173 181 def makeExtension(configs=None):
174 182 return NeboardMarkdown(configs=configs)
175 183
176 184 neboard_extension = makeExtension()
177 185
178 186
179 187 def markdown_extended(markup):
180 188 return markdown.markdown(markup, [neboard_extension], safe_mode=True)
181 189
182 190 formatters = [
183 191 QuotePattern,
184 192 SpoilerPattern,
185 193 ItalicPattern,
186 194 BoldPattern,
187 195 CommentPattern,
188 196 StrikeThroughPattern,
197 CodePattern,
189 198 ]
@@ -1,42 +1,116 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 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 32 function addMarkToMsg(start, end) {
33 if (end.length == 0) {
34 return addTextToEachLineOfSelection(start);
35 }
36
27 37 var textarea = document.getElementsByTagName('textarea')[0];
28 38 if(!textarea) return;
29 39 if( document.selection ) {
30 40 textarea.focus();
31 41 sel = document.selection.createRange();
32 42 sel.text = start + sel.text + end;
33 43 } else if(textarea.selectionStart || textarea.selectionStart == '0') {
34 44 textarea.focus();
35 45 var startPos = textarea.selectionStart;
36 46 var endPos = textarea.selectionEnd;
37 47 textarea.value = textarea.value.substring(0, startPos) + start + textarea.value.substring(startPos, endPos) + end + textarea.value.substring( endPos, textarea.value.length );
38 48 } else {
39 49 textarea.value += start + end;
40 50 }
41 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 116 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now