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, done)
53 var start_tag = '<'+params.tag;
54 var cursor_inside = false;
56 for (var attr in params.attrs) {
57 start_tag += ' '+attr+'="' + params.attrs[attr] + '"';
61 var end_tag = '</'+params.tag+'>';
67 for(var index=0; index < text.length; index++)
69 if (text[index].match(/\s/)) { // whitespace
74 if(output == token) output += start_tag;
76 output += text[index];
80 if( output[output.length-1] == '\\' ) {
81 output = output.substr(0, output.length-1) + end_tag + '\\';
87 // keep cursor inside tag if some previous scriptlet has already moved it
88 cursor_inside = move_forward != 0;
91 if(params.nocontent) {
92 output = "<"+params.tag +" />";
95 output = start_tag + end_tag;
101 move_forward -= params.tag.length+3;
104 done(output, move_forward);
107 this.scriptlets['lineregexp'] = function(context, params, text, move_forward, done) {
110 var exprs = $.map(params.exprs, function(expr) {
112 if(expr.length > 2) {
115 rx: new RegExp(expr[0], opts),
120 if(!text) done(text, move_forward);
123 var lines = text.split('\n');
125 nblck_map(lines, function(line, index) {
127 $(exprs).each(function() {
129 line = line.replace(expr.rx, expr.repl);
132 $progress.html(index);
134 if(old_line != line) changed += 1;
136 }, function(newlines) {
138 text = newlines.join('\n');
141 done(text, move_forward);
145 this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, done) {
148 var exprs = $.map(params.exprs, function(expr) {
150 if(expr.length > 2) {
154 rx: new RegExp(expr[0], opts),
159 if(!text) done(text, move_forward);
161 nblck_each(exprs, function(expr, index) {
162 $progress.html(600 + index);
163 text = text.replace(expr.rx, expr.repl);
165 done(text, move_forward);
169 this.scriptlets['macro'] = function(context, params, text, move_forward, done) {
173 function next(text, move_forward) {
174 if (i < params.length) {
177 self.scriptlets[e[0]](context, e[1], text, move_forward, next);
180 done(text, move_forward);
184 next(text, move_forward);
187 this.scriptlets['lowercase'] = function(context, params, text, move_forward, done)
189 if(!text) done(text, move_forward);
190 done(text.toLowerCase(), move_forward);
194 this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, done) {
196 var verses = text.split('\n');
197 text = ''; var buf = ''; var ebuf = '';
200 for(var i=0; i < verses.length; i++) {
201 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
203 text += (buf ? buf + '/\n' : '') + ebuf;
204 buf = (first ? '<strofa>\n' : '') + verses[i];
208 ebuf += '\n' + verses[i];
211 text = text + buf + '\n</strofa>' + ebuf;
214 text = "<strofa></strofa>"
215 move_forward -= "</strofa>".length;
218 done(text, move_forward);
222 this.scriptlets['autotag'] = function(context, params, text, move_forward, done)
224 if(!text.match(/^\n+$/)) done(text, move_forward);
226 function insert_done(output, mf) {
230 if (!params.split) params.split = 2;
231 if (!params.padding) params.padding = 3;
233 chunks = text.replace(/^\n+|\n+$/, '').split(new RegExp("\\n{"+params.split+",}"));
234 text = text.match(/^\n+/);
238 for(; params.padding; params.padding--) {
242 if (params.tag == 'strofa')
243 tagger = this.scriptlets['insert_stanza'];
245 tagger = this.scriptlets['insert_tag'];
249 if (params.tag == 'akap' && chunks[i].match(/^---/))
251 else tag = params.tag;
252 tagger(context, {tag: tag}, chunks[i], 0, insert_done);
257 done(text, move_forward);
262 ScriptletCenter.prototype.callInteractive = function(opts) {
263 $progress = $('<span>Executing script</span>');
266 /* This won't work, 'cause the JS below might be synchronous :( */
267 /* var timer = setTimeout(function() {
268 $.blockUI({message: $progress});
272 $.blockUI({message: $progress, showOverlay: false});
274 var input = self.XMLEditorSelectedText(opts.context);
275 self.scriptlets[opts.action](opts.context, opts.extra, input, 0, function(output, move_forward){
279 if (input != output) {
280 self.XMLEditorReplaceSelectedText(opts.context, output)
284 self.XMLEditorMoveCursorForward(opts.context, move_forward)
288 $.unblockUI(); // done
292 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
294 return editor.selection();
297 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
299 $progress.html("Replacing text");
300 editor.replaceSelection(replacement);
303 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
304 var pos = panel.cursorPosition();
305 panel.selectLines(pos.line, pos.character + n);
311 scriptletCenter = new ScriptletCenter();