/* This work is licensed under Creative Commons GNU LGPL License. License: http://creativecommons.org/licenses/LGPL/2.1/ Author: Stefan Goessner/2005-06 Web: http://goessner.net/ inspired by: http://xml-maiden.com/ */ Wiky.rules.math = { version: 0.95, preshortcuts: [ // { rex:/[ ]/g, tmplt:"`"}, // omit due to charset support ie6 { rex:/\+\-/g, tmplt:"±"}, { rex:/\/O|\\Oslash/g, tmplt:"Ø"}, { rex:/\/o|\\oslash/g, tmplt:"ø"}, { rex:/<->|\\harr/g, tmplt:"↔"}, { rex:/<-|\\larr/g, tmplt:"←"}, { rex:/->|\\rarr/g, tmplt:"→"}, { rex:/<=>|\\hArr/g, tmplt:"⇔"}, { rex:/=>|\\rArr/g, tmplt:"⇒"}, { rex:/-=|\\equiv/g, tmplt:"≡"}, { rex:/<=|\\le/g, tmplt:"≤"}, { rex:/>=|\\ge/g, tmplt:"≥"}, { rex://g, tmplt:">"} ], postshortcuts: [ { rex:/\*|\\middot/g, tmplt:"·"}, { rex:/\\x|\\times/g, tmplt:"×"}, { rex:/~=|\\cong/g, tmplt:"≅"}, { rex:/~~|\\asymp/g, tmplt:"≈"}, { rex:/~|\\sim/g, tmplt:"∼"}, { rex:/!=|\\neq|\\ne/g, tmplt:"≠"}, { rex:/\.\.\.|\\ldots/g, tmplt:"…"}, { rex:/\\in|\\isin/g, tmplt:"∈"}, { rex:/([0-9])x([0-9])/g, tmplt:"$1×$2"}, { rex:/([A-Za-z]) x ([A-Za-z])/g, tmplt:"$1×$2"}, // { rex:/[`]{4}/g, tmplt:" "}, // omit due to charset support ie6 // { rex:/[`]{3}/g, tmplt:" "}, // { rex:/[`]{2}/g, tmplt:" "}, // { rex:/[`]/g, tmplt:" "}, { rex:/\{/g, tmplt:"‎"}, // unvisible left-to-right mark, { rex:/\}/g, tmplt:"‏"} // unvisible right-to-left mark, ], expr: [ { rex:/\^\^/g, tmplt:"^^"}, // ^ overindex { rex:/(\\sum|\\prod|\\int)_([-]?[a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})\^([-]?[a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})/g, tmplt:"$3$1$2"}, // over-/underscript (\sum, \prod, \int) { rex:/(\\sum|\\prod|\\int)\^([-]?[a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})/g, tmplt:"$2$1 "}, { rex:/(\\sum|\\prod|\\int)_([-]?[a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})/g, tmplt:" $1$2"}, { rex:/_([-]?[a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})\^([-]?[a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})/g, tmplt:"$2$1"}, // over-/underindex { rex:/\^([-]?[a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})/g, tmplt:"$1"}, // overindex { rex:/_([-]?[a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})/g, tmplt:"$1"}, // underindex { rex:/-/g, tmplt:"−"}, { rex:/([a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})\/([a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})/g, tmplt:"$1$2"}, // fraction { rex:/([a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})\/\/([a-zA-Z0-9\.&;#\\]+|\{@[0-9]+@\})/g, tmplt:"$1$2"}, // fraction { rex:/\[((\[(([^,\]]+[,]){1,}[^\]]+)\][ \n]*){1,})\]/g, tmplt:function($0,$1){var m=Wiky.math.transpose($1.replace(/(^\[|\]$)/g,"").replace(/(\][ \n]*\[)/g,"|").split("|")),sz=" style=\"font-size:"+(m.len)+"00%;\"";/*alert("{("+m.mat.join(")}\n{(").split(",").join(")(")+")}");*/ return ""+Wiky.math.fence()+""+m.mat.join("\n").split(",").join("")+""+Wiky.math.fence()+"";}}, // matrix { rex:/\[((?:[^,\]]){1,}[^\]]+)\]/g, tmplt:function($0,$1){var v=$1.split(","),sz=" style=\"font-size:"+v.length+"00%;\""; return ""+Wiky.math.fence()+""+v.join("")+""+Wiky.math.fence()+"";}}, // vector { rex:/!([a-zA-Z0-9\.&;]+)/g, tmplt:"$1" }, // bold vector symbol .. { rex:/\\prod/g, tmplt:""}, { rex:/\\sum/g, tmplt:""}, { rex:/\\int/g, tmplt:""}, "Wiky.rules.math.postshortcuts" ], symbols: [ { rex:/\\Alpha/g, tmplt:"Α"}, { rex:/\\Beta/g, tmplt:"Β"}, { rex:/\\Gamma/g, tmplt:"Γ"}, { rex:/\\Delta/g, tmplt:"Δ"}, { rex:/\\Epsilon/g, tmplt:"Ε"}, { rex:/\\Zeta/g, tmplt:"Ζ"}, { rex:/\\Eta/g, tmplt:"Η"}, { rex:/\\Theta/g, tmplt:"Θ"}, { rex:/\\Iota/g, tmplt:"Ι"}, { rex:/\\Kappa/g, tmplt:"Κ"}, { rex:/\\Lambda/g, tmplt:"Λ"}, { rex:/\\Mu/g, tmplt:"Μ"}, { rex:/\\Nu/g, tmplt:"Ν"}, { rex:/\\Xi/g, tmplt:"Ξ"}, { rex:/\\Omicron/g, tmplt:"Ο"}, { rex:/\\Pi/g, tmplt:"Π"}, { rex:/\\Rho/g, tmplt:"Ρ"}, { rex:/\\Sigma/g, tmplt:"Σ"}, { rex:/\\Tau/g, tmplt:"Τ"}, { rex:/\\Upsilon/g, tmplt:"Υ"}, { rex:/\\Phi/g, tmplt:"Φ"}, { rex:/\\Chi/g, tmplt:"Χ"}, { rex:/\\Psi/g, tmplt:"Ψ"}, { rex:/\\Omega/g, tmplt:"Ω"}, { rex:/\\alpha/g, tmplt:"α"}, { rex:/\\beta/g, tmplt:"β"}, { rex:/\\gamma/g, tmplt:"γ"}, { rex:/\\delta/g, tmplt:"δ"}, { rex:/\\epsilon/g, tmplt:"ε"}, { rex:/\\zeta/g, tmplt:"ζ"}, { rex:/\\eta/g, tmplt:"η"}, { rex:/\\thetasym/g, tmplt:"ϑ"}, { rex:/\\theta/g, tmplt:"θ"}, { rex:/\\iota/g, tmplt:"ι"}, { rex:/\\kappa/g, tmplt:"κ"}, { rex:/\\lambda/g, tmplt:"λ"}, { rex:/\\mu/g, tmplt:"μ"}, { rex:/\\nu/g, tmplt:"ν"}, { rex:/\\xi/g, tmplt:"ξ"}, { rex:/\\omicron/g, tmplt:"ο"}, { rex:/\\piv/g, tmplt:"ϖ"}, { rex:/\\pi/g, tmplt:"π"}, { rex:/\\rho/g, tmplt:"ρ"}, { rex:/\\sigmaf/g, tmplt:"ς"}, { rex:/\\sigma/g, tmplt:"σ"}, { rex:/\\tau/g, tmplt:"τ"}, { rex:/\\upsilon/g, tmplt:"υ"}, { rex:/\\phi/g, tmplt:"φ"}, { rex:/\\chi/g, tmplt:"χ"}, { rex:/\\psi/g, tmplt:"ψ"}, { rex:/\\omega/g, tmplt:"ω"}, { rex:/\\upsih/g, tmplt:"ϒ"}, // miscellaneous symbols { rex:/\\bull/g, tmplt:"•"}, { rex:/\\uarr/g, tmplt:"↑"}, { rex:/\\darr/g, tmplt:"↓"}, { rex:/\\crarr/g, tmplt:"↵"}, { rex:/\\lArr/g, tmplt:"⇐"}, { rex:/\\uArr/g, tmplt:"⇑"}, { rex:/\\dArr/g, tmplt:"⇓"}, { rex:/\\forall/g, tmplt:"∀"}, { rex:/\\part/g, tmplt:"∂"}, { rex:/\\exist/g, tmplt:"∃"}, { rex:/\\empty/g, tmplt:"∅"}, { rex:/\\nabla/g, tmplt:"∇"}, { rex:/\\notin/g, tmplt:"∉"}, { rex:/\\ni/g, tmplt:"∋"}, { rex:/\\minus/g, tmplt:"−"}, { rex:/\\lowast/g, tmplt:"∗"}, { rex:/\\sqrt|\\radic/g, tmplt:"√"}, { rex:/\\prop/g, tmplt:"∝"}, { rex:/\\infin/g, tmplt:"∞"}, { rex:/\\ang/g, tmplt:"∠"}, { rex:/\\and/g, tmplt:"∧"}, { rex:/\\or/g, tmplt:"∨"}, { rex:/\\cap/g, tmplt:"∩"}, { rex:/\\cup/g, tmplt:"∪"}, { rex:/\\there4/g, tmplt:"∴"}, { rex:/\\sub/g, tmplt:"⊂"}, { rex:/\\sup/g, tmplt:"⊃"}, { rex:/\\nsub/g, tmplt:"⊄"}, { rex:/\\sube/g, tmplt:"⊆"}, { rex:/\\supe/g, tmplt:"⊇"}, { rex:/\\oplus/g, tmplt:"⊕"}, { rex:/\\otimes/g, tmplt:"⊗"}, { rex:/\\perp/g, tmplt:"⊥"}, { rex:/\\sdot/g, tmplt:"⋅"} ] }; Wiky.inverse.math = { pre: [ { rex:/−|\u2212/g, tmplt:"-"}, { rex:/ |\u2009/g, tmplt:" "}, { rex:/‎|\u200E/g, tmplt:"{"}, { rex:/‏|\u200F/g, tmplt:"}"} ], post: [ // { rex:/([$])/g, tmplt:"\\$1" }, { rex:/^|\x5E/g, tmplt:"^"}, { rex:/</g, tmplt:"<"}, { rex:/>/g, tmplt:">"} ], shortcuts: [ // { rex:/ |\u2003/g, tmplt:" "}, // omit due to charset support ie6 // { rex:/ |\u2002/g, tmplt:" "}, // { rex:/ |\u2009/g, tmplt:" "}, { rex:/±|\xB1/g, tmplt:"+-"}, { rex:/·|\xB7/g, tmplt:"*"}, { rex:/×|\xD7/g, tmplt:"\\x"}, { rex:/Ø|\xD8/g, tmplt:"/O"}, { rex:/ø|\xF8/g, tmplt:"/o"}, { rex:/←|\u2190/g, tmplt:"<-"}, { rex:/→|\u2192/g, tmplt:"->"}, { rex:/↔|\u2194/g, tmplt:"<->"}, { rex:/⇒|\u21D2/g, tmplt:"=>"}, { rex:/⇔|\u21D4/g, tmplt:"<=>"}, { rex:/∼|\u223C/g, tmplt:"~"}, { rex:/≅|\u2245/g, tmplt:"~="}, { rex:/≈|\u2248/g, tmplt:"~~"}, { rex:/≠|\u2260/g, tmplt:"!="}, { rex:/…/g, tmplt:"..."}, { rex:/≡|\u2261/g, tmplt:"-="}, { rex:/≤|\u2264/g, tmplt:"<="}, { rex:/≥|\u2265/g, tmplt:">="} ], expr: [ { rex:/(\{?@[0-9]+@\}?)<\/span>(\{?@[0-9]+@\}?)<\/span><\/span>/g, tmplt:"_$2^$1"}, // superscript + subscript { rex:/(\{?@[0-9]+@\}?)<\/span>(\\prod|\\sum|\\int)(\{?@[0-9]+@\}?)<\/span><\/span>/g, tmplt:"$2_$3^$1"}, // overscript + underscript { rex:/@[0-9]+@<\/span>(\\prod|\\sum|\\int)(\{?@[0-9]+@\}?)<\/span><\/span>/mgi, tmplt:"$1_$2", dbg:true}, // underscript { rex:/(\{?@[0-9]+@\}?)<\/span>(\\prod|\\sum|\\int)@[0-9]+@<\/span><\/span>/mgi, tmplt:"$2^$1"}, // overscript { rex:/(\{?@[0-9]+@\}?)<\/span>(\{?@[0-9]+@\}?)<\/span><\/span>/mgi, tmplt:"$1/$2"}, // fraction { rex:/]*>&[^;]+;<\/span>((?:[^>]*<\/span>){2,})<\/span>]*>&[^;]+;<\/span>/mgi, tmplt:function($0,$1){return "["+$1.replace(/(?:^|<\/span>$)/g,"").replace(/<\/span>/g,",")+"]";}}, // vector .. { rex:/]*>&[^;]+;<\/span>((?:(?:(?:[^>]*<\/span>){2,})<\/span>[^>]*){2,})]*>&[^;]+;<\/span>/mgi, tmplt:function($0,$1){return "[["+Wiky.math.transpose($1.replace(/(?:^|<\/span><\/span>$)/g,"").replace(/<\/span>/g,",").replace(/<\/span><\/span>[^>]*/g,"|").split("|")).mat.join("][")+"]]";}}, // matrix .. { rex:/(@[0-9]+@)<\/span>/mgi, tmplt:"!$1"}, // bold vector .. { rex:/(\{?@[0-9]+@\}?)<\/sup>⁄(\{?@[0-9]+@\}?)<\/sub>/mgi, tmplt:"$1//$2"}, { rex:/(\{?@[0-9]+@\}?)<\/sup>/mgi, tmplt:"^$1" }, { rex:/(\{?@[0-9]+@\}?)<\/sub>/mgi, tmplt:"_$1" } ], symbols: [ // greek symbols { rex:/Α|\u391/g, tmplt:"\\Alpha"}, { rex:/Β|\u392/g, tmplt:"\\Beta"}, { rex:/Γ|\u393/g, tmplt:"\\Gamma"}, { rex:/Δ|\u394/g, tmplt:"\\Delta"}, { rex:/Ε|\u395/g, tmplt:"\\Epsilon"}, { rex:/Ζ|\u396/g, tmplt:"\\Zeta"}, { rex:/Η|\u397/g, tmplt:"\\Eta"}, { rex:/Θ|\u398/g, tmplt:"\\Theta"}, { rex:/Ι|\u399/g, tmplt:"\\Iota"}, { rex:/Κ|\u39A/g, tmplt:"\\Kappa"}, { rex:/Λ|\u39B/g, tmplt:"\\Lambda"}, { rex:/Μ|\u39C/g, tmplt:"\\Mu"}, { rex:/Ν|\u39D/g, tmplt:"\\Nu"}, { rex:/Ξ|\u39E/g, tmplt:"\\Xi"}, { rex:/Ο|\u39F/g, tmplt:"\\Omicron"}, { rex:/Π|\u3A0/g, tmplt:"\\Pi"}, { rex:/Ρ|\u3A1/g, tmplt:"\\Rho"}, { rex:/Σ|\u3A3/g, tmplt:"\\Sigma"}, { rex:/Τ|\u3A4/g, tmplt:"\\Tau"}, { rex:/Υ|\u3A5/g, tmplt:"\\Upsilon"}, { rex:/Φ|\u3A6/g, tmplt:"\\Phi"}, { rex:/Χ|\u3A7/g, tmplt:"\\Chi"}, { rex:/Ψ|\u3A8/g, tmplt:"\\Psi"}, { rex:/Ω|\u3A9/g, tmplt:"\\Omega"}, { rex:/α|\u3B1/g, tmplt:"\\alpha"}, { rex:/β|\u3B2/g, tmplt:"\\beta"}, { rex:/γ|\u3B3/g, tmplt:"\\gamma"}, { rex:/δ|\u3B4/g, tmplt:"\\delta"}, { rex:/ε|\u3B5/g, tmplt:"\\epsilon"}, { rex:/ζ|\u3B6/g, tmplt:"\\zeta"}, { rex:/η|\u3B7/g, tmplt:"\\eta"}, { rex:/ϑ|\u3D1/g, tmplt:"\\thetasym"}, { rex:/θ|\u3B8/g, tmplt:"\\theta"}, { rex:/ι|\u3B9/g, tmplt:"\\iota"}, { rex:/κ|\u3BA/g, tmplt:"\\kappa"}, { rex:/λ|\u3BB/g, tmplt:"\\lambda"}, { rex:/μ|\u3BC/g, tmplt:"\\mu"}, { rex:/ν|\u3BD/g, tmplt:"\\nu"}, { rex:/ξ|\u3BE/g, tmplt:"\\xi"}, { rex:/ο|\u3BF/g, tmplt:"\\omicron"}, { rex:/π|\u3C0/g, tmplt:"\\pi"}, { rex:/ρ|\u3C1/g, tmplt:"\\rho"}, { rex:/ς|\u3C2/g, tmplt:"\\sigmaf"}, { rex:/σ|\u3C3/g, tmplt:"\\sigma"}, { rex:/τ|\u3C4/g, tmplt:"\\tau"}, { rex:/υ|\u3C5/g, tmplt:"\\upsilon"}, { rex:/φ|\u3C6/g, tmplt:"\\phi"}, { rex:/χ|\u3C7/g, tmplt:"\\chi"}, { rex:/ψ|\u3C8/g, tmplt:"\\psi"}, { rex:/ω|\u3C9/g, tmplt:"\\omega"}, // miscellaneous symbols { rex:/ϒ|\u3D2/g, tmplt:"\\upsih"}, { rex:/ϖ|\u3D6/g, tmplt:"\\piv"}, { rex:/•|\u2022/g, tmplt:"\\bull"}, { rex:/↑|\u2191/g, tmplt:"\\uarr"}, { rex:/↓|\u2193/g, tmplt:"\\darr"}, { rex:/↵|\u21B5/g, tmplt:"\\crarr"}, { rex:/⇐|\u21D0/g, tmplt:"\\lArr"}, { rex:/⇑|\u21D1/g, tmplt:"\\uArr"}, { rex:/⇓|\u21D3/g, tmplt:"\\dArr"}, { rex:/∀|\u2200/g, tmplt:"\\forall"}, { rex:/∂|\u2202/g, tmplt:"\\part"}, { rex:/∃|\u2203/g, tmplt:"\\exist"}, { rex:/∅|\u2205/g, tmplt:"\\empty"}, { rex:/∇|\u2207/g, tmplt:"\\nabla"}, { rex:/∈|\u2208/g, tmplt:"\\isin"}, { rex:/∉|\u2209/g, tmplt:"\\notin"}, { rex:/∋|\u220B/g, tmplt:"\\ni"}, { rex:/(∏|\u220F)<\/span>/g, tmplt:"\\prod"}, { rex:/(∑|\u2211)<\/span>/g, tmplt:"\\sum"}, { rex:/∗|\u2217/g, tmplt:"\\lowast"}, { rex:/√|\u221A/g, tmplt:"\\sqrt"}, { rex:/∝|\u221D/g, tmplt:"\\prop"}, { rex:/∞|\u221E/g, tmplt:"\\infin"}, { rex:/∠|\u2220/g, tmplt:"\\ang"}, { rex:/∧|\u2227/g, tmplt:"\\and"}, { rex:/∨|\u2228/g, tmplt:"\\or"}, { rex:/∩|\u2229/g, tmplt:"\\cap"}, { rex:/∪|\u222A/g, tmplt:"\\cup"}, { rex:/(?:∫|\u222B)<\/span>/g, tmplt:"\\int"}, { rex:/∴|\u2234/g, tmplt:"\\there4"}, { rex:/⊂|\u2282/g, tmplt:"\\sub"}, { rex:/⊃|\u2283/g, tmplt:"\\sup"}, { rex:/⊄|\u2284/g, tmplt:"\\nsub"}, { rex:/⊆|\u2286/g, tmplt:"\\sube"}, { rex:/⊇|\u2287/g, tmplt:"\\supe"}, { rex:/⊕|\u2295/g, tmplt:"\\oplus"}, { rex:/⊗|\u2297/g, tmplt:"\\otimes"}, { rex:/⊥|\u22A5/g, tmplt:"\\perp"}, { rex:/⋅|\u22C5/g, tmplt:"\\sdot"} ] }; Wiky.math = { toHtml: function(str) { var expr = function(itr) { // region from "{" to "}", nesting allowed .. var s = ""; for (var c = itr.str.charAt(itr.pos++); itr.pos <= itr.str.length && c != "}"; c = itr.str.charAt(itr.pos++)) s += (c == "{") ? ("{"+expr(itr)+"}") : c; return Wiky.store(Wiky.apply(s, Wiky.rules.math.expr)); }; str = Wiky.apply(str, Wiky.rules.math.preshortcuts); str = Wiky.apply(str, Wiky.rules.math.symbols); str = expr({str:str,pos:0}); return str; }, toWiki: function(str) { var parseTree = function(itr, endtag) { var c, s="",gt,nam,idxof=function(s,c,p){var i=s.indexOf(c,p);return i>=0?i:s.length;} for (itr.buf=itr.str.substr(itr.pos,endtag.length); itr.pos",itr.pos)) < idxof(itr.str,"/",itr.pos)) { // start tags .. no empty elements or endtags .. nam = itr.str.substring(itr.pos+1,Math.min(idxof(itr.str," ",itr.pos),gt)); s += itr.str.substring(itr.pos,itr.pos=gt+1) + parseTree(itr, "") + ""; itr.pos += nam.length+3; } else s += c; } itr.pos--; return Wiky.store(s, true); }; str = Wiky.apply(str, Wiky.inverse.math.pre); str = Wiky.apply(str, Wiky.inverse.math.symbols); str = parseTree({str:str,pos:0,buf:null}, ""); while (str.match(/@[0-9]+@/g) != null) str = Wiky.apply(str.replace(/@([0-9]+)@/g, function($0,$1){return Wiky.restore($1);}), Wiky.inverse.math.expr); str = Wiky.apply(str, Wiky.inverse.math.shortcuts); str = Wiky.apply(str, Wiky.inverse.math.post); return str; }, fence: function(str) { return window && window.ActiveXObject ? " " : " "; }, transpose: function (m) { var t=[]; for (var i in m) { m[i] = m[i].split(","); for (var j in m[i]) { if (!t[j]) t[j]=[]; t[j][i] = m[i][j]; } } for (var i in t) t[i] = t[i].join(","); return {mat:t, len:m.length}; } }; Wiky.rules.pre = Wiky.rules.pre.concat({ rex:/\\([$])/g, tmplt:function($0,$1){return Wiky.store($1);} }); Wiky.rules.nonwikiblocks = Wiky.rules.nonwikiblocks.concat( [ { rex:/\[\(([a-zA-Z0-9\.-]+)\)\$([^$]*)\$\]/g, tmplt:function($0,$1,$2){return ":p]
("+$1+")" + Wiky.math.toHtml($2) + "
[p:";} }, // numbered equation { rex:/\[\$([^$]*)\$\]/g, tmplt:function($0,$1){return ":p]
" + Wiky.math.toHtml($1) + "
[p:";} }, // equation ]); Wiky.rules.nonwikiinlines = Wiky.rules.nonwikiinlines.concat( { rex:/\$([^$]*)\$/g, tmplt:function($0,$1){return "" + Wiky.math.toHtml($1) + "";} } // inline equation ); Wiky.inverse.pre = Wiky.inverse.pre.concat({ rex:/([\$])/g, tmplt:"\\$1" }); Wiky.inverse.nonwikiblocks = Wiky.inverse.nonwikiblocks.concat( [ { rex:/