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 move_cursor = 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 + '\\';
88 if(params.nocontent) {
89 output = "<"+params.tag +" />";
92 output = start_tag + end_tag;
98 move_forward -= params.tag.length+3;
101 done(output, move_forward);
104 this.scriptlets['lineregexp'] = function(context, params, text, move_forward, done) {
107 var exprs = $.map(params.exprs, function(expr) {
109 if(expr.length > 2) {
112 rx: new RegExp(expr[0], opts),
118 if(!text) done(text, move_forward);
121 var lines = text.split('\n');
123 nblck_map(lines, function(line, index) {
125 $(exprs).each(function() {
127 line = line.replace(expr.rx, expr.repl);
130 $progress.html(index);
132 if(old_line != line) changed += 1;
134 }, function(newlines) {
136 text = newlines.join('\n');
139 done(text, move_forward);
143 this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, done) {
146 var exprs = $.map(params.exprs, function(expr) {
148 if(expr.length > 2) {
152 rx: new RegExp(expr[0], opts),
157 if(!text) done(text, move_forward);
158 var original = text;$
160 nblck_each(exprs, function(expr, index) {
161 $progress.html(600 + index);
162 text = text.replace(expr.rx, expr.repl);
164 done(text, move_forward);
168 this.scriptlets['macro'] = function(context, params, text, move_forward, done) {
172 function next(text, move_forward) {
173 if (i < params.length) {
176 self.scriptlets[e[0]](context, e[1], text, move_forward, next);
179 done(text, move_forward);
183 next(text, move_forward);
186 this.scriptlets['lowercase'] = function(context, params, text, move_forward, done)
188 if(!text) done(text, move_forward);
189 done(text.toLowerCase(), move_forward);
193 this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, done) {
195 var verses = text.split('\n');
196 text = ''; var buf = ''; var ebuf = '';
199 for(var i=0; i < verses.length; i++) {
200 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
202 text += (buf ? buf + '/\n' : '') + ebuf;
203 buf = (first ? '<strofa>\n' : '') + verses[i];
207 ebuf += '\n' + verses[i];
210 text = text + buf + '\n</strofa>' + ebuf;
213 text = "<strofa></strofa>"
214 move_forward -= "</strofa>".length;
217 done(text, move_forward);
222 ScriptletCenter.prototype.callInteractive = function(opts) {
223 $progress = $('<span>Executing script</span>');
226 /* This won't work, 'cause the JS below might be synchronous :( */
227 /* var timer = setTimeout(function() {
228 $.blockUI({message: $progress});
232 $.blockUI({message: $progress, showOverlay: false});
234 var input = self.XMLEditorSelectedText(opts.context);
235 self.scriptlets[opts.action](opts.context, opts.extra, input, 0, function(output, move_forward){
239 if (input != output) {
240 self.XMLEditorReplaceSelectedText(opts.context, output)
244 self.XMLEditorMoveCursorForward(opts.context, move_forward)
248 $.unblockUI(); // done
252 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
254 return editor.selection();
257 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
259 $progress.html("Replacing text");
260 editor.replaceSelection(replacement);
263 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
264 var pos = panel.cursorPosition();
265 panel.selectLines(pos.line, pos.character + n);
271 scriptletCenter = new ScriptletCenter();