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);
286 this.scriptlets['slugify'] = function(context, params, text, move_forward, move_up, done)
288 done(slugify(text.replace(/_/g, '-')), move_forward, move_up);
293 ScriptletCenter.prototype.callInteractive = function(opts) {
294 $progress = $('<span>Executing script</span>');
297 /* This won't work, 'cause the JS below might be synchronous :( */
298 /* var timer = setTimeout(function() {
299 $.blockUI({message: $progress});
303 $.blockUI({message: $progress, showOverlay: false});
305 var input = self.XMLEditorSelectedText(opts.context);
306 self.scriptlets[opts.action](opts.context, opts.extra, input, 0, 0, function(output, move_forward, move_up){
310 if (input != output) {
311 self.XMLEditorReplaceSelectedText(opts.context, output)
313 if (move_forward || move_up) {
315 self.XMLEditorMoveCursorForward(opts.context, move_forward, move_up)
319 $.unblockUI(); // done
323 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
325 return editor.selection();
328 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
330 $progress.html("Replacing text");
331 editor.replaceSelection(replacement);
334 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, right, up) {
335 var pos = panel.cursorPosition();
339 line = panel.nextLine(line);
343 line = panel.prevLine(line);
346 len = panel.lineContent(line).length;
347 panel.selectLines(line, len + right);
350 panel.selectLines(pos.line, pos.character + right);
357 scriptletCenter = new ScriptletCenter();