1 function ScriptletCenter()
5 this.scriptlets['insert_tag'] = function(context, params)
7 var text = this.XMLEditorSelectedText(context);
8 var start_tag = '<'+params.tag;
10 for (var attr in params.attrs) {
11 start_tag += ' '+attr+'="' + params.attrs[attr] + '"';
15 var end_tag = '</'+params.tag+'>';
21 for(var index=0; index < text.length; index++)
23 if (text[index].match(/\s/)) { // whitespace
28 if(output == token) output += start_tag;
30 output += text[index];
34 if( output[output.length-1] == '\\' ) {
35 output = output.substr(0, output.length-1) + end_tag + '\\';
42 output = start_tag + end_tag;
45 if (text.length == 0) {
46 this.XMLEditorMoveCursorForward(context, params.tag.length + 2);
49 this.XMLEditorReplaceSelectedText(context, output);
52 this.scriptlets['lineregexp'] = function(context, params) {
54 var exprs = $.map(params.exprs, function(expr) {
59 rx: new RegExp(expr[0], opts),
65 var text = this.XMLEditorSelectedText(context);
69 var lines = text.split('\n');
70 var lines = $.map(lines, function(line) {
72 $(exprs).each(function() {
74 line = line.replace(expr.rx, expr.repl);
77 if(old_line != line) changed += 1;
82 this.XMLEditorReplaceSelectedText(context, lines.join('\n') );
86 this.scriptlets['codemirror_fontsize'] = function(context, params) {
87 var frameBody = this.XMLEditorBody(context);
90 frameBody.css('font-size', params.fontSize);
93 var old_size = parseInt(frameBody.css('font-size'));
94 frameBody.css('font-size', old_size + (params.change || 0) );
99 this.scriptlets['fulltextregexp'] = function(context, params) {
100 var exprs = $.map(params.exprs, function(expr) {
106 rx: new RegExp(expr[0], opts),
111 var text = this.XMLEditorSelectedText(context);
114 $(exprs).each(function() {
115 text = text.replace(this.rx, this.repl);
118 if( original != text) {
119 this.XMLEditorReplaceSelectedText(context, text);
123 this.scriptlets['macro'] = function(context, params) {
126 $(params).each(function() {
127 $.log(this[0], this[1]);
128 self.scriptlets[this[0]](context, this[1]);
132 this.scriptlets['lowercase'] = function(context, params)
134 var text = this.XMLEditorSelectedText(context);
139 var lcase = text.toLowerCase();
140 var ucase = text.toUpperCase();
142 if(lcase == text) repl = ucase; /* was lowercase */
143 else if(ucase != text) repl = lcase; /* neither lower- or upper-case */
144 else { /* upper case -> camel-case */
145 var words = $(lcase.split(/\s/)).map(function() {
146 if(this.length > 0) {
147 return this[0].toUpperCase() + this.slice(1);
152 repl = words.join(' ');
155 if(repl != text) this.XMLEditorReplaceSelectedText(context, repl);
159 this.scriptlets["insert_stanza"] = function(context, params) {
160 var text = this.XMLEditorSelectedText(context);
163 var verses = text.split('\n');
164 var text = ''; var buf = ''; var ebuf = '';
167 for(var i=0; i < verses.length; i++) {
168 verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
170 text += (buf ? buf + '\\\n' : '') + ebuf;
171 buf = (first ? '<strofa>\n' : '') + verses[i];
175 ebuf += '\n' + verses[i];
178 text = text + buf + '\n</strofa>' + ebuf;
179 this.XMLEditorReplaceSelectedText(context, text);
183 this.XMLEditorMoveCursorForward(context, params.tag.length + 2);
190 ScriptletCenter.prototype.XMLEditorSelectedText = function(panel) {
191 return panel.contentView.editor.selection();
194 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(panel, replacement)
196 panel.contentView.editor.replaceSelection(replacement);
197 /* TODO: fire the change event */
200 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
201 var pos = panel.contentView.editor.cursorPosition();
202 panel.contentView.editor.selectLines(pos.line, pos.character + n);
206 scriptletCenter = new ScriptletCenter();