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;
111 alert(move_forward + ' ' + move_up)
114 done(output, move_forward, move_up);
117 this.scriptlets['lineregexp'] = function(context, params, text, move_forward, move_up, done) {
120 var exprs = $.map(params.exprs, function(expr) {
122 if(expr.length > 2) {
125 rx: new RegExp(expr[0], opts),
130 if(!text) done(text, move_forward, move_up);
133 var lines = text.split('\n');
135 nblck_map(lines, function(line, index) {
137 $(exprs).each(function() {
139 line = line.replace(expr.rx, expr.repl);
142 $progress.html(index);
144 if(old_line != line) changed += 1;
146 }, function(newlines) {
148 text = newlines.join('\n');
151 done(text, move_forward, move_up);
155 this.scriptlets['fulltextregexp'] = function(context, params, text, move_forward, move_up, done) {
158 var exprs = $.map(params.exprs, function(expr) {
160 if(expr.length > 2) {
164 rx: new RegExp(expr[0], opts),
169 if(!text) done(text, move_forward, move_up);
171 nblck_each(exprs, function(expr, index) {
172 $progress.html(600 + index);
173 text = text.replace(expr.rx, expr.repl);
175 done(text, move_forward, move_up);
179 this.scriptlets['macro'] = function(context, params, text, move_forward, move_up, done) {
183 function next(text, move_forward, move_up) {
184 if (i < params.length) {
187 self.scriptlets[e[0]](context, e[1], text, move_forward, move_up, next);
190 done(text, move_forward, move_up);
194 next(text, move_forward, move_up);
197 this.scriptlets['lowercase'] = function(context, params, text, move_forward, move_up, done)
199 if(!text) done(text, move_forward, move_up);
200 done(text.toLowerCase(), move_forward, move_up);
204 this.scriptlets["insert_stanza"] = function(context, params, text, move_forward, move_up, done) {
205 var padding_top = '';
206 for (var i=params.padding_top; i; i--)
209 var padding_bottom = '';
210 for (var i=params.padding_bottom; i; i--)
211 padding_bottom += '\n';
214 var verses = text.split('\n');
215 text = ''; var buf = ''; var ebuf = '';
218 for(var i=0; i < verses.length; i++) {
219 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
221 text += (buf ? buf + '/\n' : '') + ebuf;
222 buf = (first ? '<strofa>\n' : '') + verses[i];
226 ebuf += '\n' + verses[i];
229 text = padding_top + text + buf + '\n</strofa>' + padding_bottom + ebuf;
232 text = padding_top + "<strofa></strofa>" + padding_bottom;
233 move_forward -= "</strofa>".length;
234 move_up += params.padding_bottom || 0;
237 done(text, move_forward, move_up);
241 this.scriptlets['autotag'] = function(context, params, text, move_forward, move_up, done)
243 if(!text.match(/^\n+$/)) done(text, move_forward, move_up);
245 function insert_done(output, mf) {
249 if (!params.split) params.split = 2;
250 if (!params.padding) params.padding = 3;
252 chunks = text.replace(/^\n+|\n+$/, '').split(new RegExp("\\n{"+params.split+",}"));
253 text = text.match(/^\n+/);
257 for(; params.padding; params.padding--) {
261 if (params.tag == 'strofa')
262 tagger = this.scriptlets['insert_stanza'];
264 tagger = this.scriptlets['insert_tag'];
268 if (params.tag == 'akap' && chunks[i].match(/^---/))
270 else tag = params.tag;
271 tagger(context, {tag: tag}, chunks[i], 0, insert_done);
276 done(text, move_forward, move_up);
281 ScriptletCenter.prototype.callInteractive = function(opts) {
282 $progress = $('<span>Executing script</span>');
285 /* This won't work, 'cause the JS below might be synchronous :( */
286 /* var timer = setTimeout(function() {
287 $.blockUI({message: $progress});
291 $.blockUI({message: $progress, showOverlay: false});
293 var input = self.XMLEditorSelectedText(opts.context);
294 self.scriptlets[opts.action](opts.context, opts.extra, input, 0, 0, function(output, move_forward, move_up){
298 if (input != output) {
299 self.XMLEditorReplaceSelectedText(opts.context, output)
301 if (move_forward || move_up) {
303 self.XMLEditorMoveCursorForward(opts.context, move_forward, move_up)
307 $.unblockUI(); // done
311 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
313 return editor.selection();
316 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
318 $progress.html("Replacing text");
319 editor.replaceSelection(replacement);
322 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, right, up) {
323 var pos = panel.cursorPosition();
327 line = panel.nextLine(line);
331 line = panel.prevLine(line);
334 len = panel.lineContent(line).length;
335 panel.selectLines(line, len + right);
338 panel.selectLines(pos.line, pos.character + right);
345 scriptletCenter = new ScriptletCenter();