[ Index ] |
|
Code source de Kupu-1.3.5 |
1 /***************************************************************************** 2 * 3 * Copyright (c) 2003-2004 Kupu Contributors. All rights reserved. 4 * 5 * This software is distributed under the terms of the Kupu 6 * License. See LICENSE.txt for license text. For a list of Kupu 7 * Contributors see CREDITS.txt. 8 * 9 *****************************************************************************/ 10 11 // $Id: kupueditor.js 7951 2004-12-21 15:06:38Z guido $ 12 13 // WARNING: this file can contain non-ascii characters, *always* make sure your 14 // text-editor uses 'UTF-8' as the character encoding!! 15 16 function CleanupExpressionsTool(actionselectid, performactionbuttonid) { 17 /* tool that allows global replace actions on the text contents 18 19 the actions will be presented to the user as a list of some 20 sort (e.g. select) of which the user can choose one, when (s)he 21 does the system will use a set of regular expressions and 22 replacements on the code, when a match of the expression is 23 encountered the match will be replaced with the replacement 24 25 matches and replacements can be configured from the XML, the 26 format is: 27 28 <config> 29 <cleanup_expressions> 30 <set> 31 <name>Convert single quotes to curly ones</name> 32 <expression> 33 <reg> 34 (\W)' 35 </reg> 36 <replacement> 37 \1‘ 38 </replacement> 39 </expression> 40 <expression> 41 <reg> 42 ' 43 </reg> 44 <replacement> 45 ’ 46 </replacement> 47 </expression> 48 </set> 49 <set> 50 <name>Reduce whitespace</name> 51 <expression> 52 <reg> 53 [ ]{2} 54 </reg> 55 <replacement> 56   57 </replacement> 58 </expression> 59 </set> 60 </cleanup_expressions> 61 </config> 62 63 */ 64 this.actionselect = document.getElementById(actionselectid); 65 this.performactionbutton = document.getElementById(performactionbuttonid); 66 }; 67 68 CleanupExpressionsTool.prototype = new KupuTool; 69 70 CleanupExpressionsTool.prototype.initialize = function(editor) { 71 /* initialize the tool, read the regexp sets into a mapping */ 72 this.editor = editor; 73 // mapping name -> [[regexp, replacement], ...] 74 this.expressions = this.generateExpressionsMapping(); 75 // populate action select 76 this.populateActionSelect(this.expressions); 77 // add the event handler to the button 78 addEventHandler(this.performactionbutton, 'click', this.performAction, 79 this); 80 }; 81 82 CleanupExpressionsTool.prototype.generateExpressionsMapping = function() { 83 /* convert the config struct to a somewhat simpler mapping */ 84 var ret = {}; 85 var expressions = this.editor.config['cleanup_expressions']; 86 if (!expressions) { 87 // no expressions in the XML config, bail out 88 alert('no cleanup expressions configured'); 89 return ret; 90 }; 91 var sets = expressions.set; 92 for (var i=0; i < sets.length; i++) { 93 var set = sets[i]; 94 var name = set.name; 95 ret[name] = []; 96 var exprs = set.expression; 97 // may be list type as well as object 98 if (exprs.length) { 99 for (var j=0; j < exprs.length; j++) { 100 var expr = exprs[j]; 101 var regexp = expr.reg.strip(); 102 var replacement = this._prepareReplacement(expr.replacement) 103 ret[name].push([regexp, replacement]); 104 }; 105 } else { 106 var regexp = exprs.reg.strip(); 107 var replacement = this._prepareReplacement(exprs.replacement); 108 ret[name].push([regexp, replacement]); 109 }; 110 }; 111 return ret; 112 }; 113 114 CleanupExpressionsTool.prototype._prepareReplacement = function(data) { 115 /* replace \x([0-9a-f]{2}) escapes with the unicode value of \1 */ 116 data = data.strip(); 117 var reg = /\\x([0-9a-f]{2})/g; 118 while (true) { 119 var match = reg.exec(data); 120 if (!match || !match.length) { 121 return data; 122 }; 123 data = data.replace(match[0], String.fromCharCode(parseInt(match[1], 16))); 124 }; 125 }; 126 127 CleanupExpressionsTool.prototype.populateActionSelect = function(mapping) { 128 /* populate the select with which the user can choose actions */ 129 for (var name in mapping) { 130 var option = document.createElement('option'); 131 option.value = name; 132 option.appendChild(document.createTextNode(name)); 133 this.actionselect.appendChild(option); 134 }; 135 this.actionselect.style.width = '100%'; 136 }; 137 138 CleanupExpressionsTool.prototype.performAction = function() { 139 /* perform a single action (set of regexps/replacements) to the whole body */ 140 var action = this.actionselect.options[ 141 this.actionselect.selectedIndex].value; 142 var sets = this.expressions[action]; 143 for (var i=0; i < sets.length; i++) { 144 var body = this.editor.getInnerDocument().getElementsByTagName('body')[0]; 145 var nodeiterator = new NodeIterator(body); 146 while (true) { 147 var current = nodeiterator.next(); 148 if (!current) { 149 break; 150 }; 151 if (current.nodeType == 3) { 152 var value = current.nodeValue; 153 if (value.strip()) { 154 this.performReplaceOnNode(current, sets[i][0], sets[i][1]); 155 }; 156 }; 157 }; 158 }; 159 alert('Cleanup done'); 160 }; 161 162 CleanupExpressionsTool.prototype.performReplaceOnNode = function(node, regexp, replacement) { 163 /* perform the replacement to the contents of a single node */ 164 var value = node.nodeValue; 165 while (true) { 166 var reg = new RegExp(regexp, 'g'); 167 var match = reg.exec(value); 168 if (!match || !match.length) { 169 node.nodeValue = value; 170 return; 171 }; 172 value = value.replace(match[0], this.createReplacement(replacement, match)); 173 }; 174 }; 175 176 CleanupExpressionsTool.prototype.createReplacement = function(pattern, interpolations) { 177 /* interpolate '\[0-9]' escapes, they will be replaced with interpolations[n] where 178 n is the number behind the backslash */ 179 var reg = /\\([0-9])/g; 180 while (true) { 181 var match = reg.exec(pattern); 182 if (!match || !match.length) { 183 return pattern; 184 }; 185 pattern = pattern.replace(match[0], interpolations[parseInt(match[1])]); 186 }; 187 };
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 15:30:41 2007 | par Balluche grâce à PHPXref 0.7 |