1 /* This work is licensed under Creative Commons GNU LGPL License.
3 License: http://creativecommons.org/licenses/LGPL/2.1/
5 Author: Stefan Goessner/2005-06
6 Web: http://goessner.net/
14 "Wiky.rules.nonwikiblocks",
15 "Wiky.rules.wikiblocks",
19 { rex:/(\r?\n)/g, tmplt:"\xB6" }, // replace line breaks with '¶' ..
22 { rex:/(^\xB6)|(\xB6$)/g, tmplt:"" }, // .. remove linebreaks at BOS and EOS ..
23 { rex:/@([0-9]+)@/g, tmplt:function($0,$1){return Wiky.restore($1);} }, // resolve blocks ..
24 { rex:/\xB6/g, tmplt:"\n" } // replace '¶' with line breaks ..
27 { rex:/\\([%])/g, tmplt:function($0,$1){return Wiky.store($1);} },
28 { rex:/\[(?:\{([^}]*)\})?(?:\(([^)]*)\))?%(.*?)%\]/g, tmplt:function($0,$1,$2,$3){return ":p]"+Wiky.store("<pre"+($2?(" lang=\"x-"+Wiky.attr($2)+"\""):"")+Wiky.style($1)+">" + Wiky.apply($3, $2?Wiky.rules.lang[Wiky.attr($2)]:Wiky.rules.code) + "</pre>")+"[p:";} } //programm code block
31 "Wiky.rules.nonwikiinlines",
33 { rex:/(?:^|\xB6)(={1,6})(.*?)[=]*(?=\xB6|$)/g, tmplt:function($0,$1,$2){ var h=$1.length; return ":p]\xB6<h"+h+">"+$2+"</h"+h+">\xB6[p:";} }, // <h1> .. <h6>
34 { rex:/(?:^|\xB6)[-]{4}(?:\xB6|$)/g, tmplt:"\xB6<hr/>\xB6" }, // horizontal ruler ..
35 { rex:/\\\\([ \xB6])/g, tmplt:"<br/>$1" }, // forced line break ..
36 { rex:/(^|\xB6)([*01aAiIg]*[\.*])[ ]/g, tmplt:function($0,$1,$2){var state=$2.replace(/([*])/g,"u").replace(/([\.])/,"");return ":"+state+"]"+$1+"["+state+":";}},
37 { rex:/(?:^|\xB6);[ ](.*?):[ ]/g, tmplt:"\xB6:l][l:$1:d][d:"}, // ; term : definition
38 { rex:/\[(?:\{([^}]*)\})?(?:\(([^)]*)\))?\"/g, tmplt:function($0,$1,$2){return ":p]<blockquote"+Wiky.attr($2,"cite",0)+Wiky.attr($2,"title",1)+Wiky.style($1)+">[p:"; } }, // block quotation start
39 { rex:/\"\]/g, tmplt:":p]</blockquote>[p:" }, // block quotation end
40 { rex:/\[(\{[^}]*\})?\|/g, tmplt:":t]$1[r:" }, // .. start table ..
41 { rex:/\|\]/g, tmplt:":r][t:" }, // .. end table ..
42 { rex:/\|\xB6[ ]?\|/g, tmplt:":r]\xB6[r:" }, // .. end/start table row ..
43 { rex:/\|/g, tmplt:":c][c:" }, // .. end/start table cell ..
44 { rex:/^(.*)$/g, tmplt:"[p:$1:p]" }, // start paragraph '[p:' at BOS .. end paragraph ':p]' at EOS ..
45 { rex:/(([\xB6])([ \t\f\v\xB6]*?)){2,}/g, tmplt:":p]$1[p:" }, // .. separate paragraphs at blank lines ..
46 { rex:/\[([01AIacdgilprtu]+)[:](.*?)[:]([01AIacdgilprtu]+)\]/g, tmplt:function($0,$1,$2,$3){return Wiky.sectionRule($1==undefined?"":$1,"",Wiky.apply($2,Wiky.rules.wikiinlines),!$3?"":$3);} },
47 { rex:/\[[01AIacdgilprtu]+[:]|[:][01AIacdgilprtu]+\]/g, tmplt:"" }, // .. remove singular section delimiters (they frequently exist with incomplete documents while typing) ..
48 { rex:/<td>(?:([0-9]*)[>])?([ ]?)(.*?)([ ]?)<\/td>/g, tmplt:function($0,$1,$2,$3,$4){return "<td"+($1?" colspan=\""+$1+"\"":"")+($2==" "?(" style=\"text-align:"+($2==$4?"center":"right")+";\""):($4==" "?" style=\"text-align:left;\"":""))+">"+$2+$3+$4+"</td>";} },
49 { rex:/<(p|table)>(?:\xB6)?(?:\{(.*?)\})/g, tmplt:function($0,$1,$2){return "<"+$1+Wiky.style($2)+">";} },
50 { rex:/<p>([ \t\f\v\xB6]*?)<\/p>/g, tmplt:"$1" }, // .. remove empty paragraphs ..
51 "Wiky.rules.shortcuts"
54 { rex:/%(?:\{([^}]*)\})?(?:\(([^)]*)\))?(.*?)%/g, tmplt:function($0,$1,$2,$3){return Wiky.store("<code"+($2?(" lang=\"x-"+Wiky.attr($2)+"\""):"")+Wiky.style($1)+">" + Wiky.apply($3, $2?Wiky.rules.lang[Wiky.attr($2)]:Wiky.rules.code) + "</code>");} }, // inline code
55 { rex:/%(.*?)%/g, tmplt:function($0,$1){return Wiky.store("<code>" + Wiky.apply($2, Wiky.rules.code) + "</code>");} }
58 { rex:/\*([^*]+)\*/g, tmplt:"<strong>$1</strong>" }, // .. strong ..
59 { rex:/_([^_]+)_/g, tmplt:"<em>$1</em>" },
60 { rex:/\^([^^]+)\^/g, tmplt:"<sup>$1</sup>" },
61 { rex:/~([^~]+)~/g, tmplt:"<sub>$1</sub>" },
62 { rex:/\(-(.+?)-\)/g, tmplt:"<del>$1</del>" },
63 { rex:/\?([^ \t\f\v\xB6]+)\((.+)\)\?/g, tmplt:"<abbr title=\"$2\">$1</abbr>" }, // .. abbreviation ..
64 { rex:/\[(?:\{([^}]*)\})?[Ii]ma?ge?\:([^ ,\]]*)(?:[, ]([^\]]*))?\]/g, tmplt:function($0,$1,$2,$3){return Wiky.store("<img"+Wiky.style($1)+" src=\""+$2+"\" alt=\""+($3?$3:$2)+"\" title=\""+($3?$3:$2)+"\"/>");} }, // wikimedia image style ..
65 { rex:/\[([^ ,]+)[, ]([^\]]*)\]/g, tmplt:function($0,$1,$2){return Wiky.store("<a href=\""+$1+"\">"+$2+"</a>");}}, // wiki block style uri's ..
66 { rex:/(((http(s?))\:\/\/)?[A-Za-z0-9\._\/~\-:]+\.(?:png|jpg|jpeg|gif|bmp))/g, tmplt:function($0,$1,$2){return Wiky.store("<img src=\""+$1+"\" alt=\""+$1+"\"/>");} }, // simple images ..
67 { rex:/((mailto\:|javascript\:|(news|file|(ht|f)tp(s?))\:\/\/)[A-Za-z0-9\.:_\/~%\-+&#?!=()@\x80-\xB5\xB7\xFF]+)/g, tmplt:"<a href=\"$1\">$1</a>" } // simple uri's ..
70 { rex:/\\([|*_~\^])/g, tmplt:function($0,$1){return Wiky.store($1);} },
71 { rex:/\\&/g, tmplt:"&" },
72 { rex:/\\>/g, tmplt:">" },
73 { rex:/\\</g, tmplt:"<" }
76 { rex:/---/g, tmplt:"—" }, // —
77 { rex:/--/g, tmplt:"–" }, // –
78 { rex:/[\.]{3}/g, tmplt:"…"}, // …
79 { rex:/<->/g, tmplt:"↔"}, // $harr;
80 { rex:/<-/g, tmplt:"←"}, // ←
81 { rex:/->/g, tmplt:"→"}, //→
84 { rex:/&/g, tmplt:"&"},
85 { rex:/</g, tmplt:"<"},
86 { rex:/>/g, tmplt:">"}
94 "Wiky.inverse.nonwikiblocks",
95 "Wiky.inverse.wikiblocks",
99 { rex:/(\r?\n)/g, tmplt:"\xB6" } // replace line breaks with '¶' ..
102 { rex:/@([0-9]+)@/g, tmplt:function($0,$1){return Wiky.restore($1);} }, // resolve blocks ..
103 { rex:/\xB6/g, tmplt:"\n" } // replace '¶' with line breaks ..
106 { rex:/<pre([^>]*)>(.*?)<\/pre>/mgi, tmplt:function($0,$1,$2){return Wiky.store("["+Wiky.invStyle($1)+Wiky.invAttr($1,["lang"]).replace(/x\-/,"")+"%"+Wiky.apply($2, Wiky.hasAttr($1,"lang")?Wiky.inverse.lang[Wiky.attrVal($1,"lang").substr(2)]:Wiky.inverse.code)+"%]");} } //code block
109 "Wiky.inverse.nonwikiinlines",
110 "Wiky.inverse.escapes",
111 "Wiky.inverse.wikiinlines",
112 { rex:/<h1>(.*?)<\/h1>/mgi, tmplt:"=$1=" },
113 { rex:/<h2>(.*?)<\/h2>/mgi, tmplt:"==$1==" },
114 { rex:/<h3>(.*?)<\/h3>/mgi, tmplt:"===$1===" },
115 { rex:/<h4>(.*?)<\/h4>/mgi, tmplt:"====$1====" },
116 { rex:/<h5>(.*?)<\/h5>/mgi, tmplt:"=====$1=====" },
117 { rex:/<h6>(.*?)<\/h6>/mgi, tmplt:"======$1======" },
118 { rex:/<(p|table)[^>]+(style=\"[^\"]*\")[^>]*>/mgi, tmplt:function($0,$1,$2){return "<"+$1+">"+Wiky.invStyle($2);} },
119 { rex:/\xB6{2}<li/mgi, tmplt:"\xB6<li" }, // ie6 only ..
120 { rex:/<li class=\"?([^ >\"]*)\"?[^>]*?>([^<]*)/mgi, tmplt:function($0,$1,$2){return $1.replace(/u/g,"*").replace(/([01aAiIg])$/,"$1.")+" "+$2;}}, // list items ..
121 { rex:/(^|\xB6)<(u|o)l[^>]*?>\xB6/mgi, tmplt:"$1" }, // only outer level list start at BOL ...
122 { rex:/(<\/(?:dl|ol|ul|p)>[ \xB6]*<(?:p)>)/gi, tmplt:"\xB6\xB6" },
123 { rex:/<dt>(.*?)<\/dt>[ \f\n\r\t\v]*<dd>/mgi, tmplt:"; $1: " },
124 { rex:/<blockquote([^>]*)>/mgi, tmplt:function($0,$1){return Wiky.store("["+Wiky.invStyle($1)+Wiky.invAttr($1,["cite","title"])+"\"");} },
125 { rex:/<\/blockquote>/mgi, tmplt:"\"]" },
126 { rex:/<td class=\"?lft\"?>\xB6*[ ]?|<\/tr>/mgi, tmplt:"|" }, // ie6 only ..
127 { rex:/\xB6<tr(?:[^>]*?)>/mgi, tmplt:"\xB6" },
128 { rex:/<td colspan=\"([0-9]+)\"(?:[^>]*?)>/mgi, tmplt:"|$1>" },
129 { rex:/<td(?:[^>]*?)>/mgi, tmplt:"|" },
130 { rex:/<table>/mgi, tmplt:"[" },
131 { rex:/<\/table>/mgi, tmplt:"]" },
132 { rex:/<tr(?:[^>]*?)>\xB6*|<\/td>\xB6*|<tbody>\xB6*|<\/tbody>/mgi, tmplt:"" },
133 { rex:/<hr\/?>/mgi, tmplt:"----" },
134 { rex:/<br\/?>/mgi, tmplt:"\\\\" },
135 { rex:/(<p>|<(d|o|u)l[^>]*>|<\/(dl|ol|ul|p)>|<\/(li|dd)>)/mgi, tmplt:"" },
136 "Wiky.inverse.shortcuts"
139 { rex:/<code>(.*?)<\/code>/g, tmplt:function($0,$1){return Wiky.store("%"+Wiky.apply($1, Wiky.inverse["code"])+"%");} }
142 { rex:/<strong[^>]*?>(.*?)<\/strong>/mgi, tmplt:"*$1*" },
143 { rex:/<b[^>]*?>(.*?)<\/b>/mgi, tmplt:"*$1*" },
144 { rex:/<em[^>]*?>(.*?)<\/em>/mgi, tmplt:"_$1_" },
145 { rex:/<i[^>]*?>(.*?)<\/i>/mgi, tmplt:"_$1_" },
146 { rex:/<sup[^>]*?>(.*?)<\/sup>/mgi, tmplt:"^$1^" },
147 { rex:/<sub[^>]*?>(.*?)<\/sub>/mgi, tmplt:"~$1~" },
148 { rex:/<del[^>]*?>(.*?)<\/del>/mgi, tmplt:"(-$1-)" },
149 { rex:/<abbr title=\"([^\"]*)\">(.*?)<\/abbr>/mgi, tmplt:"?$2($1)?" },
150 { rex:/<a href=\"([^\"]*)\"[^>]*?>(.*?)<\/a>/mgi, tmplt:function($0,$1,$2){return $1==$2?$1:"["+$1+","+$2+"]";}},
151 { rex:/<img([^>]*)\/>/mgi, tmplt:function($0,$1){var a=Wiky.attrVal($1,"alt"),h=Wiky.attrVal($1,"src"),t=Wiky.attrVal($1,"title"),s=Wiky.attrVal($1,"style");return s||(t&&h!=t)?("["+Wiky.invStyle($1)+"img:"+h+(t&&(","+t))+"]"):h;}},
154 { rex:/([|*_~%\^])/g, tmplt:"\\$1" },
155 { rex:/&/g, tmplt:"\\&" },
156 { rex:/>/g, tmplt:"\\>" },
157 { rex:/</g, tmplt:"\\<" }
160 { rex:/–|\u2013/g, tmplt:"--"},
161 { rex:/—|\u2014/g, tmplt:"---"},
162 { rex:/…|\u2026/g, tmplt:"..."},
163 { rex:/↔|\u2194/g, tmplt:"<->"},
164 { rex:/←|\u2190/g, tmplt:"<-"},
165 { rex:/→|\u2192/g, tmplt:"->"}
168 { rex:/&/g, tmplt:"&"},
169 { rex:/</g, tmplt:"<"},
170 { rex:/>/g, tmplt:">"}
175 toHtml: function(str) {
177 return Wiky.apply(str, Wiky.rules.all);
180 toWiki: function(str) {
182 return Wiky.apply(str, Wiky.inverse.all);
185 apply: function(str, rules) {
187 for (var i in rules) {
188 if (typeof(rules[i]) == "string")
189 str = Wiky.apply(str, eval(rules[i]));
191 str = str.replace(rules[i].rex, rules[i].tmplt);
195 store: function(str, unresolved) {
196 return unresolved ? "@" + (Wiky.blocks.push(str)-1) + "@"
197 : "@" + (Wiky.blocks.push(str.replace(/@([0-9]+)@/g, function($0,$1){return Wiky.restore($1);}))-1) + "@";
199 restore: function(idx) {
200 return Wiky.blocks[idx];
202 attr: function(str, name, idx) {
203 var a = str && str.split(",")[idx||0];
204 return a ? (name ? (" "+name+"=\""+a+"\"") : a) : "";
206 hasAttr: function(str, name) {
207 return new RegExp(name+"=").test(str);
209 attrVal: function(str, name) {
210 return str.replace(new RegExp("^.*?"+name+"=\"(.*?)\".*?$"), "$1");
212 invAttr: function(str, names) {
215 if (str.indexOf(names[i]+"=")>=0)
216 a.push(str.replace(new RegExp("^.*?"+names[i]+"=\"(.*?)\".*?$"), "$1"));
217 return a.length ? ("("+a.join(",")+")") : "";
219 style: function(str) {
220 var s = str && str.split(/,|;/), p, style = "";
223 if (p[0] == ">") style += "margin-left:4em;";
224 else if (p[0] == "<") style += "margin-right:4em;";
225 else if (p[0] == ">>") style += "float:right;";
226 else if (p[0] == "<<") style += "float:left;";
227 else if (p[0] == "=") style += "display:block;margin:0 auto;";
228 else if (p[0] == "_") style += "text-decoration:underline;";
229 else if (p[0] == "b") style += "border:solid 1px;";
230 else if (p[0] == "c") style += "color:"+p[1]+";";
231 else if (p[0] == "C") style += "background:"+p[1]+";";
232 else if (p[0] == "w") style += "width:"+p[1]+";";
233 else style += p[0]+":"+p[1]+";";
235 return style ? " style=\""+style+"\"" : "";
237 invStyle: function(str) {
238 var s = /style=/.test(str) ? str.replace(/^.*?style=\"(.*?)\".*?$/, "$1") : "",
239 p = s && s.split(";"), pi, prop = [];
241 pi = p[i].split(":");
242 if (pi[0] == "margin-left" && pi[1]=="4em") prop.push(">");
243 else if (pi[0] == "margin-right" && pi[1]=="4em") prop.push("<");
244 else if (pi[0] == "float" && pi[1]=="right") prop.push(">>");
245 else if (pi[0] == "float" && pi[1]=="left") prop.push("<<");
246 else if (pi[0] == "margin" && pi[1]=="0 auto") prop.push("=");
247 else if (pi[0] == "display" && pi[1]=="block") ;
248 else if (pi[0] == "text-decoration" && pi[1]=="underline") prop.push("_");
249 else if (pi[0] == "border" && pi[1]=="solid 1px") prop.push("b");
250 else if (pi[0] == "color") prop.push("c:"+pi[1]);
251 else if (pi[0] == "background") prop.push("C:"+pi[1]);
252 else if (pi[0] == "width") prop.push("w:"+pi[1]);
253 else if (pi[0]) prop.push(pi[0]+":"+pi[1]);
255 return prop.length ? ("{" + prop.join(",") + "}") : "";
257 sectionRule: function(fromLevel, style, content, toLevel) {
258 var trf = { p_p: "<p>$1</p>",
259 p_u: "<p>$1</p><ul$3>",
260 p_o: "<p>$1</p><ol$3>",
263 u_p: "<li$2>$1</li></ul>",
264 u_c: "<li$2>$1</li></ul></td>",
265 u_r: "<li$2>$1</li></ul></td></tr>",
266 uu_p: "<li$2>$1</li></ul></li></ul>",
267 uo_p: "<li$2>$1</li></ol></li></ul>",
268 uuu_p: "<li$2>$1</li></ul></li></ul></li></ul>",
269 uou_p: "<li$2>$1</li></ul></li></ol></li></ul>",
270 uuo_p: "<li$2>$1</li></ol></li></ul></li></ul>",
271 uoo_p: "<li$2>$1</li></ol></li></ol></li></ul>",
273 u_u: "<li$2>$1</li>",
274 uu_u: "<li$2>$1</li></ul></li>",
275 uo_u: "<li$2>$1</li></ol></li>",
276 uuu_u: "<li$2>$1</li></ul></li></ul></li>",
277 uou_u: "<li$2>$1</li></ul></li></ol></li>",
278 uuo_u: "<li$2>$1</li></ol></li></ul></li>",
279 uoo_u: "<li$2>$1</li></ol></li></ol></li>",
280 u_uu: "<li$2>$1<ul$3>",
282 u_o: "<li$2>$1</li></ul><ol$3>",
283 uu_o: "<li$2>$1</li></ul></li></ul><ol$3>",
284 uo_o: "<li$2>$1</li></ol></li></ul><ol$3>",
285 uuu_o: "<li$2>$1</li></ul></li></ul></li></ul><ol$3>",
286 uou_o: "<li$2>$1</li></ul></li></ol></li></ul><ol$3>",
287 uuo_o: "<li$2>$1</li></ol></li></ul></li></ul><ol$3>",
288 uoo_o: "<li$2>$1</li></ol></li></ol></li></ul><ol$3>",
289 u_uo: "<li$2>$1<ol$3>",
291 o_p: "<li$2>$1</li></ol>",
292 oo_p: "<li$2>$1</li></ol></li></ol>",
293 ou_p: "<li$2>$1</li></ul></li></ol>",
294 ooo_p: "<li$2>$1</li></ol></li></ol>",
295 ouo_p: "<li$2>$1</li></ol></li></ul></li></ol>",
296 oou_p: "<li$2>$1</li></ul></li></ol></li></ol>",
297 ouu_p: "<li$2>$1</li></ul></li></ul></li></ol>",
299 o_u: "<li$2>$1</li></ol><ul$3>",
300 oo_u: "<li$2>$1</li></ol></li></ol><ul$3>",
301 ou_u: "<li$2>$1</li></ul></li></ol><ul$3>",
302 ooo_u: "<li$2>$1</li></ol></li></ol></li></ol><ul$3>",
303 ouo_u: "<li$2>$1</li></ol></li></ul></li></ol><ul$3>",
304 oou_u: "<li$2>$1</li></ul></li></ol></li></ol><ul$3>",
305 ouu_u: "<li$2>$1</li></ul></li></ul></li></ol><ul$3>",
306 o_ou: "<li$2>$1<ul$3>",
308 o_o: "<li$2>$1</li>",
309 oo_o: "<li$2>$1</li></ol></li>",
310 ou_o: "<li$2>$1</li></ul></li>",
311 ooo_o: "<li$2>$1</li></ol></li></ol></li>",
312 ouo_o: "<li$2>$1</li></ol></li></ul></li>",
313 oou_o: "<li$2>$1</li></ul></li></ol></li>",
314 ouu_o: "<li$2>$1</li></ul></li></ul></li>",
315 o_oo: "<li$2>$1<ol$3>",
319 d_u: "<dd>$1</dd></dl><ul>",
320 d_o: "<dd>$1</dd></dl><ol>",
321 p_l: "<p>$1</p><dl>",
322 u_l: "<li$2>$1</li></ul><dl>",
323 o_l: "<li$2>$1</li></ol><dl>",
324 uu_l: "<li$2>$1</li></ul></li></ul><dl>",
325 uo_l: "<li$2>$1</li></ol></li></ul><dl>",
326 ou_l: "<li$2>$1</li></ul></li></ol><dl>",
327 oo_l: "<li$2>$1</li></ol></li></ol><dl>",
328 d_p: "<dd>$1</dd></dl>",
330 p_t: "<p>$1</p><table>",
331 p_r: "<p>$1</p></td></tr>",
332 p_c: "<p>$1</p></td>",
333 t_p: "</table><p>$1</p>",
334 r_r: "<tr><td>$1</td></tr>",
335 r_p: "<tr><td><p>$1</p>",
336 r_c: "<tr><td>$1</td>",
337 r_u: "<tr><td>$1<ul>",
338 c_p: "<td><p>$1</p>",
339 c_r: "<td>$1</td></tr>",
341 // c_u: "<td>$1<ul>",
342 u_t: "<li$2>$1</li></ul><table>",
343 o_t: "<li$2>$1</li></ol><table>",
344 d_t: "<dd>$1</dd></dl><table>",
345 t_u: "</table><p>$1</p><ul>",
346 t_o: "</table><p>$1</p><ol>",
347 t_l: "</table><p>$1</p><dl>"
349 var type = { "0": "decimal-leading-zero",
355 "g": "lower-greek" };
357 var from = "", to = "", maxlen = Math.max(fromLevel.length, toLevel.length), sync = true, sectiontype = type[toLevel.charAt(toLevel.length-1)], transition;
359 for (var i=0; i<maxlen; i++)
360 if (fromLevel.charAt(i+1) != toLevel.charAt(i+1) || !sync || i == maxlen-1)
362 from += fromLevel.charAt(i) == undefined ? " " : fromLevel.charAt(i);
363 to += toLevel.charAt(i) == undefined ? " " : toLevel.charAt(i);
366 transition = (from + "_" + to).replace(/([01AIagi])/g, "o");
367 return !trf[transition] ? ("?(" + transition + ")") // error string !
368 : trf[transition].replace(/\$2/, " class=\"" + fromLevel + "\"")
369 .replace(/\$3/, !sectiontype ? "" : (" style=\"list-style-type:" + sectiontype + ";\""))
370 .replace(/\$1/, content)
371 .replace(/<p><\/p>/, "");