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);
244 function insert_done(output, mf) {
248 if (!params.split) params.split = 2;
249 if (!params.padding) params.padding = 3;
251 chunks = text.replace(/^\n+|\n+$/, '').split(new RegExp("\\n{"+params.split+",}"));
252 text = text.match(/^\n+/);
256 for(; params.padding; params.padding--) {
260 if (params.tag == 'strofa')
261 tagger = this.scriptlets['insert_stanza'];
263 tagger = this.scriptlets['insert_tag'];
267 if (params.tag == 'akap' && chunks[i].match(/^---/))
269 else tag = params.tag;
270 tagger(context, {tag: tag}, chunks[i], 0, 0, insert_done);
275 done(text, move_forward, move_up);
280 ScriptletCenter.prototype.callInteractive = function(opts) {
281 $progress = $('<span>Executing script</span>');
284 /* This won't work, 'cause the JS below might be synchronous :( */
285 /* var timer = setTimeout(function() {
286 $.blockUI({message: $progress});
290 $.blockUI({message: $progress, showOverlay: false});
292 var input = self.XMLEditorSelectedText(opts.context);
293 self.scriptlets[opts.action](opts.context, opts.extra, input, 0, 0, function(output, move_forward, move_up){
297 if (input != output) {
298 self.XMLEditorReplaceSelectedText(opts.context, output)
300 if (move_forward || move_up) {
302 self.XMLEditorMoveCursorForward(opts.context, move_forward, move_up)
306 $.unblockUI(); // done
310 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
312 return editor.selection();
315 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
317 $progress.html("Replacing text");
318 editor.replaceSelection(replacement);
321 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, right, up) {
322 var pos = panel.cursorPosition();
326 line = panel.nextLine(line);
330 line = panel.prevLine(line);
333 len = panel.lineContent(line).length;
334 panel.selectLines(line, len + right);
337 panel.selectLines(pos.line, pos.character + right);
344 scriptletCenter = new ScriptletCenter();