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+2;
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);
190 var lcase = text.toLowerCase();
191 var ucase = text.toUpperCase();
193 if(lcase == text) repl = ucase; /* was lowercase */
194 else if(ucase != text) repl = lcase; /* neither lower- or upper-case */
195 else { /* upper case -> camel-case */
196 var words = $(lcase.split(/\s/)).map(function() {
197 if(this.length > 0) {
198 return this[0].toUpperCase() + this.slice(1);
203 text = words.join(' ');
206 done(text, move_forward);
210 this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, done) {
212 var verses = text.split('\n');
213 text = ''; var buf = ''; var ebuf = '';
216 for(var i=0; i < verses.length; i++) {
217 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
219 text += (buf ? buf + '/\n' : '') + ebuf;
220 buf = (first ? '<strofa>\n' : '') + verses[i];
224 ebuf += '\n' + verses[i];
227 text = text + buf + '\n</strofa>' + ebuf;
230 move_forward += params.tag.length + 2;
233 done(text, move_forward);
238 ScriptletCenter.prototype.callInteractive = function(opts) {
239 $progress = $('<span>Executing script</span>');
242 /* This won't work, 'cause the JS below might be synchronous :( */
243 /* var timer = setTimeout(function() {
244 $.blockUI({message: $progress});
248 $.blockUI({message: $progress, showOverlay: false});
250 var input = self.XMLEditorSelectedText(opts.context);
251 self.scriptlets[opts.action](opts.context, opts.extra, input, 0, function(output, move_forward){
255 if (input != output) {
256 self.XMLEditorReplaceSelectedText(opts.context, output)
260 self.XMLEditorMoveCursorForward(opts.context, move_forward)
264 $.unblockUI(); // done
268 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
270 return editor.selection();
273 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
275 $progress.html("Replacing text");
276 editor.replaceSelection(replacement);
279 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
280 var pos = panel.cursorPosition();
281 panel.selectLines(pos.line, pos.character + n);
287 scriptletCenter = new ScriptletCenter();