1 function ScriptletCenter()
5 this.scriptlets['insert_tag'] = function(context, params)
7 var text = this.XMLEditorSelectedText(context);
8 var start_tag = '<'+params.tag;
9 var move_cursor = false;
11 for (var attr in params.attrs) {
12 start_tag += ' '+attr+'="' + params.attrs[attr] + '"';
16 var end_tag = '</'+params.tag+'>';
22 for(var index=0; index < text.length; index++)
24 if (text[index].match(/\s/)) { // whitespace
29 if(output == token) output += start_tag;
31 output += text[index];
35 if( output[output.length-1] == '\\' ) {
36 output = output.substr(0, output.length-1) + end_tag + '\\';
43 if(params.nocontent) {
44 output = "<"+params.tag +" />";
47 output = start_tag + end_tag;
52 this.XMLEditorReplaceSelectedText(context, output);
56 this.XMLEditorMoveCursorForward(context, params.tag.length+2);
62 this.scriptlets['lineregexp'] = function(context, params) {
64 var exprs = $.map(params.exprs, function(expr) {
69 rx: new RegExp(expr[0], opts),
75 var text = this.XMLEditorSelectedText(context);
79 var lines = text.split('\n');
80 lines = $.map(lines, function(line) {
82 $(exprs).each(function() {
84 line = line.replace(expr.rx, expr.repl);
87 if(old_line != line) changed += 1;
92 this.XMLEditorReplaceSelectedText(context, lines.join('\n') );
96 this.scriptlets['codemirror_fontsize'] = function(context, params) {
97 var frameBody = this.XMLEditorBody(context);
100 frameBody.css('font-size', params.fontSize);
103 var old_size = parseInt(frameBody.css('font-size'), 10);
104 frameBody.css('font-size', old_size + (params.change || 0) );
109 this.scriptlets['fulltextregexp'] = function(context, params) {
110 var exprs = $.map(params.exprs, function(expr) {
112 if(expr.length > 2) {
116 rx: new RegExp(expr[0], opts),
121 var text = this.XMLEditorSelectedText(context);
124 $(exprs).each(function() {
125 text = text.replace(this.rx, this.repl);
128 if( original != text) {
129 this.XMLEditorReplaceSelectedText(context, text);
133 this.scriptlets['macro'] = function(context, params) {
136 $(params).each(function() {
137 $.log(this[0], this[1]);
138 self.scriptlets[this[0]](context, this[1]);
142 this.scriptlets['lowercase'] = function(context, params)
144 var text = this.XMLEditorSelectedText(context);
149 var lcase = text.toLowerCase();
150 var ucase = text.toUpperCase();
152 if(lcase == text) repl = ucase; /* was lowercase */
153 else if(ucase != text) repl = lcase; /* neither lower- or upper-case */
154 else { /* upper case -> camel-case */
155 var words = $(lcase.split(/\s/)).map(function() {
156 if(this.length > 0) {
157 return this[0].toUpperCase() + this.slice(1);
162 repl = words.join(' ');
165 if(repl != text) this.XMLEditorReplaceSelectedText(context, repl);
169 this.scriptlets["insert_stanza"] = function(context, params) {
170 var text = this.XMLEditorSelectedText(context);
173 var verses = text.split('\n');
174 text = ''; var buf = ''; var ebuf = '';
177 for(var i=0; i < verses.length; i++) {
178 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
180 text += (buf ? buf + '/\n' : '') + ebuf;
181 buf = (first ? '<strofa>\n' : '') + verses[i];
185 ebuf += '\n' + verses[i];
188 text = text + buf + '\n</strofa>' + ebuf;
189 this.XMLEditorReplaceSelectedText(context, text);
193 this.XMLEditorMoveCursorForward(context, params.tag.length + 2);
200 ScriptletCenter.prototype.XMLEditorSelectedText = function(panel) {
201 return panel.contentView.editor.selection();
204 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(panel, replacement)
206 panel.contentView.editor.replaceSelection(replacement);
207 // Tell XML view that it's data has changed
208 panel.contentView.editorDataChanged();
211 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
212 var pos = panel.contentView.editor.cursorPosition();
213 panel.contentView.editor.selectLines(pos.line, pos.character + n);
219 scriptletCenter = new ScriptletCenter();