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),
117 if(!text) done(text, move_forward);
120 var lines = text.split('\n');
122 nblck_map(lines, function(line, index) {
124 $(exprs).each(function() {
126 line = line.replace(expr.rx, expr.repl);
129 $progress.html(index);
131 if(old_line != line) changed += 1;
133 }, function(newlines) {
135 text = newlines.join('\n');
138 done(text, move_forward);
142 this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, done) {
145 var exprs = $.map(params.exprs, function(expr) {
147 if(expr.length > 2) {
151 rx: new RegExp(expr[0], opts),
156 if(!text) done(text, move_forward);
158 nblck_each(exprs, function(expr, index) {
159 $progress.html(600 + index);
160 text = text.replace(expr.rx, expr.repl);
162 done(text, move_forward);
166 this.scriptlets['macro'] = function(context, params, text, move_forward, done) {
170 function next(text, move_forward) {
171 if (i < params.length) {
174 self.scriptlets[e[0]](context, e[1], text, move_forward, next);
177 done(text, move_forward);
181 next(text, move_forward);
184 this.scriptlets['lowercase'] = function(context, params, text, move_forward, done)
186 if(!text) done(text, move_forward);
187 done(text.toLowerCase(), move_forward);
191 this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, done) {
193 var verses = text.split('\n');
194 text = ''; var buf = ''; var ebuf = '';
197 for(var i=0; i < verses.length; i++) {
198 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
200 text += (buf ? buf + '/\n' : '') + ebuf;
201 buf = (first ? '<strofa>\n' : '') + verses[i];
205 ebuf += '\n' + verses[i];
208 text = text + buf + '\n</strofa>' + ebuf;
211 text = "<strofa></strofa>"
212 move_forward -= "</strofa>".length;
215 done(text, move_forward);
219 this.scriptlets['autotag'] = function(context, params, text, move_forward, done)
221 if(!text.match(/^\n+$/)) done(text, move_forward);
223 function insert_done(output, mf) {
227 if (!params.split) params.split = 2;
228 if (!params.padding) params.padding = 3;
230 chunks = text.replace(/^\n+|\n+$/, '').split(new RegExp("\\n{"+params.split+",}"));
231 text = text.match(/^\n+/);
235 for(; params.padding; params.padding--) {
239 if (params.tag == 'strofa')
240 tagger = this.scriptlets['insert_stanza'];
242 tagger = this.scriptlets['insert_tag'];
246 if (params.tag == 'akap' && chunks[i].match(/^---/))
248 else tag = params.tag;
249 tagger(context, {tag: tag}, chunks[i], 0, insert_done);
254 done(text, move_forward);
259 ScriptletCenter.prototype.callInteractive = function(opts) {
260 $progress = $('<span>Executing script</span>');
263 /* This won't work, 'cause the JS below might be synchronous :( */
264 /* var timer = setTimeout(function() {
265 $.blockUI({message: $progress});
269 $.blockUI({message: $progress, showOverlay: false});
271 var input = self.XMLEditorSelectedText(opts.context);
272 self.scriptlets[opts.action](opts.context, opts.extra, input, 0, function(output, move_forward){
276 if (input != output) {
277 self.XMLEditorReplaceSelectedText(opts.context, output)
281 self.XMLEditorMoveCursorForward(opts.context, move_forward)
285 $.unblockUI(); // done
289 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
291 return editor.selection();
294 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
296 $progress.html("Replacing text");
297 editor.replaceSelection(replacement);
300 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
301 var pos = panel.cursorPosition();
302 panel.selectLines(pos.line, pos.character + n);
308 scriptletCenter = new ScriptletCenter();