javascript 代码:【复制】var Highlight = { analyze: function(code, lang) { if (!lang) return null; var result = []; var find = true; var last = ""; while (find && code) { find = false; var j; for (var i = 0; i < lang.syntaxs.length; i++) { if (lang.syntaxs[i].start) { var exists = false; for (j = 0; j < lang.syntaxs[i].start.length; j++) { if (last == lang.syntaxs[i].start[j]) { exists = true; break; } } if (!exists) continue; } var match = lang.syntaxs[i].pattren.exec(code); if (match) { var methods = lang.syntaxs[i].methods; if (methods) { for (j = 0; j < methods.length; j++) { var method = methods[j]; if (!method) continue; if (!match[j + 1]) continue; if (this[method] && this[method].syntaxs) { var items = this.analyze(match[j + 1], this[method]); for (var k = 0; k < items.length; k++) { result.push(items[k]); } } else result.push({ name: method, text: match[j + 1] }); } } else { if (lang.syntaxs[i].name) { var item = { name: lang.syntaxs[i].name, text: match[0] }; if (item.name == "Identifier" && lang.identifiers) { for (j = 0; j < lang.identifiers.length; j++) if (lang.identifiers[j].pattren.test(item.text)) item.name = lang.identifiers[j].name; } if (!/^(Whitespace|LineComment|MultiComment)$/i.test(item.name) && !/)$/.test(item.text)) last = item.name; result.push(item); } } code = code.substr(match[0].length); find = true; break; } } } if (!find && code) result.push({ name: "Unknown", text: code }); return result; } , exportHtml: function(code, lang) { var items = this.analyze(code, lang); var html = []; html.push("<pre class="Code"><code>"); for (var i = 0; i < items.length; i++) html.push("<span class="" + items[i].name + "">" + items[i].text.replace(/</g, "<").replace(/>/g, ">") + "</span>"); html.push("</pre></code>"); return html.join(""); } , attribute: { syntaxs: [ { pattren: /^s+/, name: "Whitespace" } , { pattren: /^(=|:|;|/)/, name: "Symbol" } , { pattren: /^('[^']*'|"[^"]*")/, name: "String" } , { pattren: /^[w/-_]+/, name: "Variant" } ] } , property: { syntaxs: [ { pattren: /^s+/, name: "Whitespace" } , { pattren: /^/*[sS]*?*//, name: "MultiComment" } , { pattren: /^([a-zA-Z0-9_-]+)(s*)(:)(s*)([^;]*)(;|$)/, methods: ["Variant", "Whitespace", "Symbol", "Whitespace", "String", "Symbol"] } ] } , css: { syntaxs: [ { pattren: /^s+/, name: "Whitespace" } , { pattren: /^/*[sS]*?*//, name: "MultiComment" } , { pattren: /^(((.|#)[a-zA-Z0-9_-]+[s,]*)+)({)([sS]*?)(})/, methods: ["Function", null, null, "Symbol", "property", "Symbol"] } ] } , javascript: { syntaxs: [ { pattren: /^s+/, name: "Whitespace" } , { pattren: /^//.*/, name: "LineComment" } , { pattren: /^/*[sS]*?*//, name: "MultiComment" } , { pattren: /^/([^\/nr]*(\.)*)*/(i|m|g)*/, name: "Regex", start: ["", "Symbol"] } , { pattren: /^(~|+|-|*|(|)|[|]|{|}|||;|:|=|<|>|.|,|%|!|&|?|^)+/, name: "Symbol" } , { pattren: /^//, name: "Symbol" } , { pattren: /^(d+(?!.|x|e|d|m)u?)|^0x([da-fA-F]+(?!.|x|m)u?)|^NaN/, name: "Number" } , { pattren: /^(d+)?.d+((+|-)?ed+)?(m|d|f)?|^d+((+|-)?e(+|-)?d+)?(m|d|f)?/, name: "Float" } , { pattren: /^"([^\"]*(\.)*)*"/, name: "String" } , { pattren: /^'([^\']*(\.)*)*'/, name: "String" } , { pattren: /^[w$_]+/, name: "Identifier" } ] , identifiers: [ { pattren: /^(break|delete|function|return|typeof|case|do|if|switch|var|catch|else|in|this|void|continue|false|instanceof|throw|while|debugger)$/, name: "Reserved" } , { pattren: /^(finally|new|true|with|default|for|null|try|abstract|double|goto|native|static|boolean|enum|implements|package|super|byte|prototype)$/, name: "Reserved" } , { pattren: /^(export|import|private|synchronized|char|extends|int|protected|throws|class|final|interface|public|transient|const|float|long|short|volatile)$/, name: "Reserved" } , { pattren: /^(Array|String|Boolean|undefined|Object|Enumerator|Error|Math)$/, name: "Type" } ] } , xhtml: { syntaxs: [ { pattren: /^s+/, name: "Whitespace" } , { pattren: /^<!--[sS]*?-->/, name: "MultiComment" } , { pattren: /^(<!)([sS]*?)(>)/, methods: ["Symbol", "attribute", "Symbol"] } , { pattren: /^(<?)(xmlb)([sS]*?)(?>)/, methods: ["Symbol", "Reserved", "attribute", "Symbol"] } , { pattren: /^(<?)(phpb)(.*?)(?>)/, methods: ["Symbol", "Reserved", "php", "Symbol"] } , { pattren: /^(<?)(phpb)([sS]*?)(n?>|$)/, methods: ["Symbol", "Reserved", "php", "Symbol"] } , { pattren: /^(<%@)([sS]*?)(%>)/, methods: ["Symbol", "attribute", "Symbol"] } , { pattren: /^(<%)([sS]*?)(%>)/, methods: ["Symbol", "Text", "Symbol"] } , { pattren: /^(<)(styleb)(([^>"']*(".*?"|'.*?')*)*?)(>)([sS]*?)(</)(style)(>)/i, methods: ["Symbol", "Type", "attribute", null, null, "Symbol", "css", "Symbol", "Type", "Symbol"] } , { pattren: /^(<)(scriptb)(([^>"']*(".*?"|'.*?')*)*?)(>)([sS]*?)(</)(script)(>)/i, methods: ["Symbol", "Type", "attribute", null, null, "Symbol", "javascript", "Symbol", "Type", "Symbol"] } , { pattren: /^(</?)([w-]+)(([^>"']*(".*?"|'.*?')*)*?)(>)/i, methods: ["Symbol", "Type", "attribute", null, null, "Symbol"] } , { pattren: /^[^<]+/i, name: "Text" } ] } , php: { syntaxs: [ { pattren: /^s+/, name: "Whitespace" } , { pattren: /^(//|#).*/, name: "LineComment" } , { pattren: /^/*[sS]*?*//, name: "MultiComment" } , { pattren: /^(~|+|-|*|(|)|[|]|{|}|||;|:|=|<|>|.|,|%|!|&|?|^|@)+/, name: "Symbol" } , { pattren: /^//, name: "Symbol" } , { pattren: /^(d+(?!.|x|e|d|m)u?)|^0x([da-fA-F]+(?!.|x|m)u?)|^NaN/, name: "Number" } , { pattren: /^(d+)?.d+((+|-)?ed+)?(m|d|f)?|^d+((+|-)?e(+|-)?d+)?(m|d|f)?/, name: "Float" } , { pattren: /^"([^\"]*(\.)*)*"/, name: "String" } , { pattren: /^'([^\']*(\.)*)*'/, name: "String" } , { pattren: /^$[w_]*/, name: "Variant" } , { pattren: /^[w_]+/, name: "Identifier" } ] , identifiers: [ { pattren: /^(require_once|include|and|or|xor|__FILE__|exception|php_user_filter|__LINE__|array|as|break|case|cfunction|class|const|continue|declare|default|die|do|each|echo|else|elseif|empty)$/, name: "Reserved" } , { pattren: /^(enddeclare|endfor|endforeach|endif|endswitch|endwhile|eval|exit|extends|for|foreach|function|global|if|isset|list|new|old_function|print|return|static|switch|unset|use|var|while|__FUNCTION__|__CLASS__|__METHOD__)$/, name: "Reserved" } , { pattren: /^(NULL|FALSE|TRUE)$/, name: "Variant" } ] } };