2 var slice = Array.prototype.slice;
4 function update(array, args) {
5 var arrayLength = array.length, length = args.length;
6 while (length--) array[arrayLength + length] = args[length];
10 function merge(array, args) {
11 array = slice.call(array, 0);
12 return update(array, args);
15 Function.prototype.bind = function(context) {
16 if (arguments.length < 2 && typeof arguments[0] === 'undefined') {
20 var args = slice.call(arguments, 1);
22 var a = merge(args, arguments);
23 return __method.apply(context, a);
29 function nblck_each(array, body, after) {
30 $.each(array, function(i) {
37 function nblck_map(array, func, after) {
40 nblck_each(array, function(elem, index) {
41 acc.push(func(elem, index));
47 function ScriptletCenter()
51 this.scriptlets['insert_tag'] = function(context, params, text, move_forward, move_up, done)
54 for (var i=params.padding_top; i; i--)
57 var start_tag = '<'+params.tag;
58 var cursor_inside = false;
60 for (var attr in params.attrs) {
61 start_tag += ' '+attr+'="' + params.attrs[attr] + '"';
65 var end_tag = '</'+params.tag+'>';
67 var padding_bottom = '';
68 for (var i=params.padding_bottom; i; i--)
69 padding_bottom += '\n';
75 for(var index=0; index < text.length; index++)
77 if (text[index].match(/\s/)) { // whitespace
82 if(output == token) output += padding_top + start_tag;
84 output += text[index];
88 if( output[output.length-1] == '\\' ) {
89 output = output.substr(0, output.length-1) + end_tag + padding_bottom + '\\';
91 output += end_tag + padding_bottom;
95 // keep cursor inside tag if some previous scriptlet has already moved it
96 cursor_inside = move_forward != 0 || move_up != 0;
99 if(params.nocontent) {
100 output = padding_top + "<"+params.tag +" />" + padding_bottom;
103 output = padding_top + start_tag + end_tag + padding_bottom;
104 cursor_inside = true;
109 move_forward -= params.tag.length + 3;
110 move_up += params.padding_bottom || 0;
113 done(output, move_forward, move_up);
116 this.scriptlets['lineregexp'] = function(context, params, text, move_forward, move_up, done) {
119 var exprs = $.map(params.exprs, function(expr) {
121 if(expr.length > 2) {
124 rx: new RegExp(expr[0], opts),
129 if(!text) done(text, move_forward, move_up);
132 var lines = text.split('\n');
134 nblck_map(lines, function(line, index) {
136 $(exprs).each(function() {
138 line = line.replace(expr.rx, expr.repl);
141 $progress.html(index);
143 if(old_line != line) changed += 1;
145 }, function(newlines) {
147 text = newlines.join('\n');
150 done(text, move_forward, move_up);
154 this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, move_up, done) {
157 var exprs = $.map(params.exprs, function(expr) {
159 if(expr.length > 2) {
163 rx: new RegExp(expr[0], opts),
168 if(!text) done(text, move_forward, move_up);
170 nblck_each(exprs, function(expr, index) {
171 $progress.html(600 + index);
172 text = text.replace(expr.rx, expr.repl);
174 done(text, move_forward, move_up);
178 this.scriptlets['macro'] = function(context, params, text, move_forward, move_up, done) {
182 function next(text, move_forward, move_up) {
183 if (i < params.length) {
186 self.scriptlets[e[0]](context, e[1], text, move_forward, move_up, next);
189 done(text, move_forward, move_up);
193 next(text, move_forward, move_up);
196 this.scriptlets['lowercase'] = function(context, params, text, move_forward, move_up, done)
198 if(!text) done(text, move_forward, move_up);
199 done(text.toLowerCase(), move_forward, move_up);
203 this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, move_up, done) {
204 var padding_top = '';
205 for (var i=params.padding_top; i; i--)
208 var padding_bottom = '';
209 for (var i=params.padding_bottom; i; i--)
210 padding_bottom += '\n';
213 var verses = text.split('\n');
214 text = ''; var buf = ''; var ebuf = '';
217 for(var i=0; i < verses.length; i++) {
218 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
220 text += (buf ? buf + '/\n' : '') + ebuf;
221 buf = (first ? '<strofa>\n' : '') + verses[i];
225 ebuf += '\n' + verses[i];
228 text = padding_top + text + buf + '\n</strofa>' + padding_bottom + ebuf;
231 text = padding_top + "<strofa></strofa>" + padding_bottom;
232 move_forward -= "</strofa>".length;
233 move_up += params.padding_bottom || 0;
236 done(text, move_forward, move_up);
240 this.scriptlets['autotag'] = function(context, params, text, move_forward, move_up, done)
242 if(!text.match(/^\n+$/)) done(text, move_forward, move_up);
246 function insert_done(text, mf, mu) {
250 if (!params.split) params.split = 2;
251 if (!params.padding) params.padding = 3;
253 if (params.tag == 'strofa')
254 tagger = this.scriptlets['insert_stanza'];
256 tagger = this.scriptlets['insert_tag'];
258 var padding_top = text.match(/^\n+/)
259 output = padding_top ? padding_top[0] : '';
262 for(var i=params.padding; i; --i) {
266 text = text.substr(output.length);
267 var chunk_reg = new RegExp("^([\\s\\S]+?)(\\n{"+params.split+",}|$)");
268 while (match = text.match(chunk_reg)) {
269 if (params.tag == 'akap' && match[1].match(/^---/))
271 else tag = params.tag;
272 tagger(context, {tag: tag}, match[1], 0, 0, insert_done);
273 if (match[2].length > params.padding)
277 text = text.substr(match[0].length)
282 done(output, move_forward, move_up);
287 ScriptletCenter.prototype.callInteractive = function(opts) {
288 $progress = $('<span>Executing script</span>');
291 /* This won't work, 'cause the JS below might be synchronous :( */
292 /* var timer = setTimeout(function() {
293 $.blockUI({message: $progress});
297 $.blockUI({message: $progress, showOverlay: false});
299 var input = self.XMLEditorSelectedText(opts.context);
300 self.scriptlets[opts.action](opts.context, opts.extra, input, 0, 0, function(output, move_forward, move_up){
304 if (input != output) {
305 self.XMLEditorReplaceSelectedText(opts.context, output)
307 if (move_forward || move_up) {
309 self.XMLEditorMoveCursorForward(opts.context, move_forward, move_up)
313 $.unblockUI(); // done
317 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
319 return editor.selection();
322 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
324 $progress.html("Replacing text");
325 editor.replaceSelection(replacement);
328 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, right, up) {
329 var pos = panel.cursorPosition();
333 line = panel.nextLine(line);
337 line = panel.prevLine(line);
340 len = panel.lineContent(line).length;
341 panel.selectLines(line, len + right);
344 panel.selectLines(pos.line, pos.character + right);
351 scriptletCenter = new ScriptletCenter();