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 this.XMLEditorReplaceSelectedText(context, output);
47 if (text.length == 0) {
48 this.XMLEditorMoveCursorForward(context, -params.tag.length-3);
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 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'), 10);
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) {
102 if(expr.length > 2) {
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 text = ''; var buf = ''; var ebuf = '';
167 for(var i=0; i < verses.length; i++) {
168 var 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 // Tell XML view that it's data has changed
198 panel.contentView.editorDataChanged();
201 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
202 var pos = panel.contentView.editor.cursorPosition();
203 panel.contentView.editor.selectLines(pos.line, pos.character + n);
209 scriptletCenter = new ScriptletCenter();