1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006 |
- var PR_keywords = {};
- (function () {
- var CPP_KEYWORDS = "abstract bool break case catch char class const " +
- "const_cast continue default delete deprecated dllexport dllimport do " +
- "double dynamic_cast else enum explicit extern false float for friend " +
- "goto if inline int long mutable naked namespace new noinline noreturn " +
- "nothrow novtable operator private property protected public register " +
- "reinterpret_cast return selectany short signed sizeof static " +
- "static_cast struct switch template this thread throw true try typedef " +
- "typeid typename union unsigned using declaration, directive uuid " +
- "virtual void volatile while typeof";
- var CSHARP_KEYWORDS = "as base by byte checked decimal delegate descending " +
- "event finally fixed foreach from group implicit in interface internal " +
- "into is lock null object out override orderby params readonly ref sbyte " +
- "sealed stackalloc string select uint ulong unchecked unsafe ushort var";
- var JAVA_KEYWORDS = "package synchronized boolean implements import throws " +
- "instanceof transient extends final strictfp native super";
- var JSCRIPT_KEYWORDS = "debugger export function with NaN Infinity";
- var PERL_KEYWORDS = "require sub unless until use elsif BEGIN END";
- var PYTHON_KEYWORDS = "and assert def del elif except exec global lambda " +
- "not or pass print raise yield False True None";
- var RUBY_KEYWORDS = "then end begin rescue ensure module when undef next " +
- "redo retry alias defined";
- var SH_KEYWORDS = "done fi";
- var KEYWORDS = [CPP_KEYWORDS, CSHARP_KEYWORDS, JAVA_KEYWORDS,
- JSCRIPT_KEYWORDS, PERL_KEYWORDS, PYTHON_KEYWORDS,
- RUBY_KEYWORDS, SH_KEYWORDS];
- for (var k = 0; k < KEYWORDS.length; k++) {
- var kw = KEYWORDS[k].split(' ');
- for (var i = 0; i < kw.length; i++) {
- if (kw[i]) { PR_keywords[kw[i]] = true; }
- }
- }
- }).call(this);
- var PR_STRING = 'str';
- var PR_KEYWORD = 'kwd';
- var PR_COMMENT = 'com';
- var PR_TYPE = 'typ';
- var PR_LITERAL = 'lit';
- var PR_PUNCTUATION = 'pun';
- var PR_PLAIN = 'pln';
- var PR_TAG = 'tag';
- var PR_DECLARATION = 'dec';
- var PR_SOURCE = 'src';
- var PR_ATTRIB_NAME = 'atn';
- var PR_ATTRIB_VALUE = 'atv';
- var PR_TAB_WIDTH = 4;
- function PR_isWordChar(ch) {
- return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
- }
- function PR_spliceArrayInto(
- inserted, container, containerPosition, countReplaced) {
- inserted.unshift(containerPosition, countReplaced || 0);
- try {
- container.splice.apply(container, inserted);
- } finally {
- inserted.splice(0, 2);
- }
- }
- var REGEXP_PRECEDER_PATTERN = (function () {
- var preceders = [
- "!", "!=", "!==", "#", "%", "%=", "&", "&&", "&&=",
- "&=", "(", "*", "*=", "+=", ",", "-=",
- "->", "/", "/=", ":", "::", ";",
- "<", "<<", "<<=", "<=", "=", "==", "===", ">",
- ">=", ">>", ">>=", ">>>", ">>>=", "?", "@", "[",
- "^", "^=", "^^", "^^=", "{", "|", "|=", "||",
- "||=", "~", "break", "case", "continue", "delete",
- "do", "else", "finally", "instanceof",
- "return", "throw", "try", "typeof"
- ];
- var pattern = '(?:' +
- '(?:(?:^|[^0-9\.])\\.{1,3})|' +
- '(?:(?:^|[^\\+])\\+)|' +
- '(?:(?:^|[^\\-])-)'
- ;
- for (var i = 0; i < preceders.length; ++i) {
- var preceder = preceders[i];
- if (PR_isWordChar(preceder.charAt(0))) {
- pattern += '|\\b' + preceder;
- } else {
- pattern += '|' + preceder.replace(/([^=<>:&])/g, '\\$1');
- }
- }
- pattern += '|^)\\s*$';
- return new RegExp(pattern);
-
-
-
-
-
- })();
- var pr_amp = /&/g;
- var pr_lt = /</g;
- var pr_gt = />/g;
- var pr_quot = /\"/g;
- function PR_attribToHtml(str) {
- return str.replace(pr_amp, '&')
- .replace(pr_lt, '<')
- .replace(pr_gt, '>')
- .replace(pr_quot, '"');
- }
- function PR_textToHtml(str) {
- return str.replace(pr_amp, '&')
- .replace(pr_lt, '<')
- .replace(pr_gt, '>');
- }
- var pr_ltEnt = /</g;
- var pr_gtEnt = />/g;
- var pr_aposEnt = /'/g;
- var pr_quotEnt = /"/g;
- var pr_ampEnt = /&/g;
- function PR_htmlToText(html) {
- var pos = html.indexOf('&');
- if (pos < 0) { return html; }
-
-
-
- for (--pos; (pos = html.indexOf('&#', pos + 1)) >= 0;) {
- var end = html.indexOf(';', pos);
- if (end >= 0) {
- var num = html.substring(pos + 3, end);
- var radix = 10;
- if (num && num.charAt(0) == 'x') {
- num = num.substring(1);
- radix = 16;
- }
- var codePoint = parseInt(num, radix);
- if (!isNaN(codePoint)) {
- html = (html.substring(0, pos) + String.fromCharCode(codePoint) +
- html.substring(end + 1));
- }
- }
- }
- return html.replace(pr_ltEnt, '<')
- .replace(pr_gtEnt, '>')
- .replace(pr_aposEnt, "'")
- .replace(pr_quotEnt, '"')
- .replace(pr_ampEnt, '&');
- }
- function PR_isRawContent(node) {
- return 'XMP' == node.tagName;
- }
- var PR_innerHtmlWorks = null;
- function PR_getInnerHtml(node) {
-
-
-
- if (null === PR_innerHtmlWorks) {
- var testNode = document.createElement('PRE');
- testNode.appendChild(
- document.createTextNode('<!DOCTYPE foo PUBLIC "foo bar">\n<foo />'));
- PR_innerHtmlWorks = !/</.test(testNode.innerHTML);
- }
- if (PR_innerHtmlWorks) {
- var content = node.innerHTML;
-
- if (PR_isRawContent(node)) {
- content = PR_textToHtml(content);
- }
- return content;
- }
- var out = [];
- for (var child = node.firstChild; child; child = child.nextSibling) {
- PR_normalizedHtml(child, out);
- }
- return out.join('');
- }
- function PR_normalizedHtml(node, out) {
- switch (node.nodeType) {
- case 1:
- var name = node.tagName.toLowerCase();
- out.push('\074', name);
- for (var i = 0; i < node.attributes.length; ++i) {
- var attr = node.attributes[i];
- if (!attr.specified) { continue; }
- out.push(' ');
- PR_normalizedHtml(attr, out);
- }
- out.push('>');
- for (var child = node.firstChild; child; child = child.nextSibling) {
- PR_normalizedHtml(child, out);
- }
- if (node.firstChild || !/^(?:br|link|img)$/.test(name)) {
- out.push('<\/', name, '>');
- }
- break;
- case 2:
- out.push(node.name.toLowerCase(), '="', PR_attribToHtml(node.value), '"');
- break;
- case 3: case 4:
- out.push(PR_textToHtml(node.nodeValue));
- break;
- }
- }
- function PR_tabExpander(tabWidth) {
- var SPACES = ' ';
- var charInLine = 0;
- return function (plainText) {
-
-
-
- var out = null;
- var pos = 0;
- for (var i = 0, n = plainText.length; i < n; ++i) {
- var ch = plainText.charAt(i);
- switch (ch) {
- case '\t':
- if (!out) { out = []; }
- out.push(plainText.substring(pos, i));
-
-
-
-
- var nSpaces = tabWidth - (charInLine % tabWidth);
- charInLine += nSpaces;
- for (; nSpaces >= 0; nSpaces -= SPACES.length) {
- out.push(SPACES.substring(0, nSpaces));
- }
- pos = i + 1;
- break;
- case '\n':
- charInLine = 0;
- break;
- default:
- ++charInLine;
- }
- }
- if (!out) { return plainText; }
- out.push(plainText.substring(pos));
- return out.join('');
- };
- }
- var pr_chunkPattern =
- /(?:[^<]+|<!--[\s\S]*?-->|<!\[CDATA\[([\s\S]*?)\]\]>|<\/?[a-zA-Z][^>]*>|<)/g;
- var pr_commentPrefix = /^<!--/;
- var pr_cdataPrefix = /^<\[CDATA\[/;
- var pr_brPrefix = /^<br\b/i;
- function PR_extractTags(s) {
-
-
-
- var matches = s.match(pr_chunkPattern);
- var sourceBuf = [];
- var sourceBufLen = 0;
- var extractedTags = [];
- if (matches) {
- for (var i = 0, n = matches.length; i < n; ++i) {
- var match = matches[i];
- if (match.length > 1 && match.charAt(0) === '<') {
- if (pr_commentPrefix.test(match)) { continue; }
- if (pr_cdataPrefix.test(match)) {
-
- sourceBuf.push(match.substring(9, match.length - 3));
- sourceBufLen += match.length - 12;
- } else if (pr_brPrefix.test(match)) {
-
-
-
- sourceBuf.push('\n');
- sourceBufLen += 1;
- } else {
- extractedTags.push(sourceBufLen, match);
- }
- } else {
- var literalText = PR_htmlToText(match);
- sourceBuf.push(literalText);
- sourceBufLen += literalText.length;
- }
- }
- }
- return { source: sourceBuf.join(''), tags: extractedTags };
- }
- function PR_createSimpleLexer(shortcutStylePatterns,
- fallthroughStylePatterns) {
- var shortcuts = {};
- (function () {
- var allPatterns = shortcutStylePatterns.concat(fallthroughStylePatterns);
- for (var i = allPatterns.length; --i >= 0;) {
- var patternParts = allPatterns[i];
- var shortcutChars = patternParts[3];
- if (shortcutChars) {
- for (var c = shortcutChars.length; --c >= 0;) {
- shortcuts[shortcutChars.charAt(c)] = patternParts;
- }
- }
- }
- })();
- var nPatterns = fallthroughStylePatterns.length;
- return function (sourceCode, opt_basePos) {
- opt_basePos = opt_basePos || 0;
- var decorations = [opt_basePos, PR_PLAIN];
- var lastToken = '';
- var pos = 0;
- var tail = sourceCode;
- while (tail.length) {
- var style;
- var token = null;
- var patternParts = shortcuts[tail.charAt(0)];
- if (patternParts) {
- var match = tail.match(patternParts[1]);
- token = match[0];
- style = patternParts[0];
- } else {
- for (var i = 0; i < nPatterns; ++i) {
- patternParts = fallthroughStylePatterns[i];
- var contextPattern = patternParts[2];
- if (contextPattern && !contextPattern.test(lastToken)) {
-
- continue;
- }
- var match = tail.match(patternParts[1]);
- if (match) {
- token = match[0];
- style = patternParts[0];
- break;
- }
- }
- if (!token) {
- style = PR_PLAIN;
- token = tail.substring(0, 1);
- }
- }
- decorations.push(opt_basePos + pos, style);
- pos += token.length;
- tail = tail.substring(token.length);
- if (style !== PR_COMMENT && /\S/.test(token)) { lastToken = token; }
- }
- return decorations;
- };
- }
- var PR_C_STYLE_STRING_AND_COMMENT_LEXER = PR_createSimpleLexer([
- [PR_STRING, /^\'(?:[^\\\']|\\[\s\S])*(?:\'|$)/, null, "'"],
- [PR_STRING, /^\"(?:[^\\\"]|\\[\s\S])*(?:\"|$)/, null, '"'],
- [PR_STRING, /^\`(?:[^\\\`]|\\[\s\S])*(?:\`|$)/, null, '`']
- ], [
- [PR_PLAIN, /^(?:[^\'\"\`\/\#]+)/, null, ' \r\n'],
- [PR_COMMENT, /^#[^\r\n]*/, null, '#'],
- [PR_COMMENT, /^\/\/[^\r\n]*/, null],
- [PR_STRING, /^\/(?:[^\\\*\/]|\\[\s\S])+(?:\/|$)/, REGEXP_PRECEDER_PATTERN],
- [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]
- ]);
- /** splits the given string into comment, string, and "other" tokens.
- * @param {String} sourceCode as plain text
- * @return {Array.<Number|String>} a decoration list.
- * @private
- */
- function PR_splitStringAndCommentTokens(sourceCode) {
- return PR_C_STYLE_STRING_AND_COMMENT_LEXER(sourceCode);
- }
- var PR_C_STYLE_LITERAL_IDENTIFIER_PUNC_RECOGNIZER = PR_createSimpleLexer([], [
- [PR_PLAIN, /^\s+/, null, ' \r\n'],
- // TODO(mikesamuel): recognize non-latin letters and numerals in identifiers
- [PR_PLAIN, /^[a-z_$@][a-z_$@0-9]*/i, null],
- // A hex number
- [PR_LITERAL, /^0x[a-f0-9]+[a-z]/i, null],
- // An octal or decimal number, possibly in scientific notation
- [PR_LITERAL, /^(?:\d(?:_\d+)*\d*(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?[a-z]*/i,
- null, '123456789'],
- [PR_PUNCTUATION, /^[^\s\w\.$@]+/, null]
- // Fallback will handle decimal points not adjacent to a digit
- ]);
-
- /** splits plain text tokens into more specific tokens, and then tries to
- * recognize keywords, and types.
- * @private
- */
- function PR_splitNonStringNonCommentTokens(source, decorations) {
- for (var i = 0; i < decorations.length; i += 2) {
- var style = decorations[i + 1];
- if (style === PR_PLAIN) {
- var start = decorations[i];
- var end = i + 2 < decorations.length ? decorations[i + 2] : source.length;
- var chunk = source.substring(start, end);
- var subDecs = PR_C_STYLE_LITERAL_IDENTIFIER_PUNC_RECOGNIZER(chunk, start);
- for (var j = 0, m = subDecs.length; j < m; j += 2) {
- var subStyle = subDecs[j + 1];
- if (subStyle === PR_PLAIN) {
- var subStart = subDecs[j];
- var subEnd = j + 2 < m ? subDecs[j + 2] : chunk.length;
- var token = source.substring(subStart, subEnd);
- if (token == '.') {
- subDecs[j + 1] = PR_PUNCTUATION;
- } else if (token in PR_keywords) {
- subDecs[j + 1] = PR_KEYWORD;
- } else if (/^@?[A-Z][A-Z$]*[a-z][A-Za-z$]*$/.test(token)) {
- // classify types and annotations using Java's style conventions
- subDecs[j + 1] = token.charAt(0) == '@' ? PR_LITERAL : PR_TYPE;
- }
- }
- }
- PR_spliceArrayInto(subDecs, decorations, i, 2);
- i += subDecs.length - 2;
- }
- }
- return decorations;
- }
- var PR_MARKUP_LEXER = PR_createSimpleLexer([], [
- [PR_PLAIN, /^[^<]+/, null],
- [PR_DECLARATION, /^<!\w[^>]*(?:>|$)/, null],
- [PR_COMMENT, /^<!--[\s\S]*?(?:-->|$)/, null],
- [PR_SOURCE, /^<\?[\s\S]*?(?:\?>|$)/, null],
- [PR_SOURCE, /^<%[\s\S]*?(?:%>|$)/, null],
- [PR_SOURCE,
- // Tags whose content is not escaped, and which contain source code.
- /^<(script|style|xmp)\b[^>]*>[\s\S]*?<\/\1\b[^>]*>/i, null],
- [PR_TAG, /^<\/?\w[^<>]*>/, null]
- ]);
- // Splits any of the source|style|xmp entries above into a start tag,
- // source content, and end tag.
- var PR_SOURCE_CHUNK_PARTS = /^(<[^>]*>)([\s\S]*)(<\/[^>]*>)$/;
- /** split markup on tags, comments, application directives, and other top level
- * constructs. Tags are returned as a single token - attributes are not yet
- * broken out.
- * @private
- */
- function PR_tokenizeMarkup(source) {
- var decorations = PR_MARKUP_LEXER(source);
- for (var i = 0; i < decorations.length; i += 2) {
- if (decorations[i + 1] === PR_SOURCE) {
- var start = decorations[i];
- var end = i + 2 < decorations.length ? decorations[i + 2] : source.length;
- // Split out start and end script tags as actual tags, and leave the body
- // with style SCRIPT.
- var sourceChunk = source.substring(start, end);
- var match = (sourceChunk.match(PR_SOURCE_CHUNK_PARTS)
- //|| sourceChunk.match(/^(<[?%])([\s\S]*)([?%]>)$/)
- );
- if (match) {
- decorations.splice(
- i, 2,
- start, PR_TAG, // the open chunk
- start + match[1].length, PR_SOURCE,
- start + match[1].length + (match[2] || '').length, PR_TAG);
- }
- }
- }
- return decorations;
- }
- var PR_TAG_LEXER = PR_createSimpleLexer([
- [PR_ATTRIB_VALUE, /^\'[^\']*(?:\'|$)/, null, "'"],
- [PR_ATTRIB_VALUE, /^\"[^\"]*(?:\"|$)/, null, '"'],
- [PR_PUNCTUATION, /^[<>\/=]+/, null, '<>/=']
- ], [
- [PR_TAG, /^[\w-]+/, /^</],
- [PR_ATTRIB_VALUE, /^[\w-]+/, /^=/],
- [PR_ATTRIB_NAME, /^[\w-]+/, null],
- [PR_PLAIN, /^\s+/, null, ' \r\n']
- ]);
- /** split tags attributes and their values out from the tag name, and
- * recursively lex source chunks.
- * @private
- */
- function PR_splitTagAttributes(source, decorations) {
- for (var i = 0; i < decorations.length; i += 2) {
- var style = decorations[i + 1];
- if (style === PR_TAG) {
- var start = decorations[i];
- var end = i + 2 < decorations.length ? decorations[i + 2] : source.length;
- var chunk = source.substring(start, end);
- var subDecorations = PR_TAG_LEXER(chunk, start);
- PR_spliceArrayInto(subDecorations, decorations, i, 2);
- i += subDecorations.length - 2;
- }
- }
- return decorations;
- }
- /** identify regions of markup that are really source code, and recursivley
- * lex them.
- * @private
- */
- function PR_splitSourceNodes(source, decorations) {
- for (var i = 0; i < decorations.length; i += 2) {
- var style = decorations[i + 1];
- if (style == PR_SOURCE) {
- // Recurse using the non-markup lexer
- var start = decorations[i];
- var end = i + 2 < decorations.length ? decorations[i + 2] : source.length;
- var subDecorations = PR_decorateSource(source.substring(start, end));
- for (var j = 0, m = subDecorations.length; j < m; j += 2) {
- subDecorations[j] += start;
- }
- PR_spliceArrayInto(subDecorations, decorations, i, 2);
- i += subDecorations.length - 2;
- }
- }
- return decorations;
- }
- /** identify attribute values that really contain source code and recursively
- * lex them.
- * @private
- */
- function PR_splitSourceAttributes(source, decorations) {
- var nextValueIsSource = false;
- for (var i = 0; i < decorations.length; i += 2) {
- var style = decorations[i + 1];
- if (style === PR_ATTRIB_NAME) {
- var start = decorations[i];
- var end = i + 2 < decorations.length ? decorations[i + 2] : source.length;
- nextValueIsSource = /^on|^style$/i.test(source.substring(start, end));
- } else if (style == PR_ATTRIB_VALUE) {
- if (nextValueIsSource) {
- var start = decorations[i];
- var end
- = i + 2 < decorations.length ? decorations[i + 2] : source.length;
- var attribValue = source.substring(start, end);
- var attribLen = attribValue.length;
- var quoted =
- (attribLen >= 2 && /^[\"\']/.test(attribValue) &&
- attribValue.charAt(0) === attribValue.charAt(attribLen - 1));
- var attribSource;
- var attribSourceStart;
- var attribSourceEnd;
- if (quoted) {
- attribSourceStart = start + 1;
- attribSourceEnd = end - 1;
- attribSource = attribValue;
- } else {
- attribSourceStart = start + 1;
- attribSourceEnd = end - 1;
- attribSource = attribValue.substring(1, attribValue.length - 1);
- }
- var attribSourceDecorations = PR_decorateSource(attribSource);
- for (var j = 0, m = attribSourceDecorations.length; j < m; j += 2) {
- attribSourceDecorations[j] += attribSourceStart;
- }
- if (quoted) {
- attribSourceDecorations.push(attribSourceEnd, PR_ATTRIB_VALUE);
- PR_spliceArrayInto(attribSourceDecorations, decorations, i + 2, 0);
- } else {
- PR_spliceArrayInto(attribSourceDecorations, decorations, i, 2);
- }
- }
- nextValueIsSource = false;
- }
- }
- return decorations;
- }
- /** returns a list of decorations, where even entries
- *
- * This code treats ", ', and ` as string delimiters, and \ as a string escape.
- * It does not recognize perl's qq() style strings. It has no special handling
- * for double delimiter escapes as in basic, or tje tripled delimiters used in
- * python, but should work on those regardless although in those cases a single
- * string literal may be broken up into multiple adjacent string literals.
- *
- * It recognizes C, C++, and shell style comments.
- *
- * @param {String} sourceCode as plain text
- * @return {Array.<String,Number>} a decoration list
- */
- function PR_decorateSource(sourceCode) {
- // Split into strings, comments, and other.
- // We do this because strings and comments are easily recognizable and can
- // contain stuff that looks like other tokens, so we want to mark those early
- // so we don't recurse into them.
- var decorations = PR_splitStringAndCommentTokens(sourceCode);
-
-
- decorations = PR_splitNonStringNonCommentTokens(sourceCode, decorations);
-
- return decorations;
- }
- function PR_decorateMarkup(sourceCode) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var decorations = PR_tokenizeMarkup(sourceCode);
- decorations = PR_splitTagAttributes(sourceCode, decorations);
- decorations = PR_splitSourceNodes(sourceCode, decorations);
- decorations = PR_splitSourceAttributes(sourceCode, decorations);
- return decorations;
- }
- function PR_recombineTagsAndDecorations(
- sourceText, extractedTags, decorations) {
- var html = [];
- var outputIdx = 0;
- var openDecoration = null;
- var currentDecoration = null;
- var tagPos = 0;
- var decPos = 0;
- var tabExpander = PR_tabExpander(PR_TAB_WIDTH);
-
-
- function emitTextUpTo(sourceIdx) {
- if (sourceIdx > outputIdx) {
- if (openDecoration && openDecoration !== currentDecoration) {
-
- html.push('</span>');
- openDecoration = null;
- }
- if (!openDecoration && currentDecoration) {
- openDecoration = currentDecoration;
- html.push('<span class="', openDecoration, '">');
- }
-
-
-
-
-
-
-
- var htmlChunk = PR_textToHtml(
- tabExpander(sourceText.substring(outputIdx, sourceIdx)))
- .replace(/(\r\n?|\n| ) /g, '$1 ')
- .replace(/\r\n?|\n/g, '<br>');
- html.push(htmlChunk);
- outputIdx = sourceIdx;
- }
- }
- while (true) {
-
-
- var outputTag;
- if (tagPos < extractedTags.length) {
- if (decPos < decorations.length) {
-
-
-
- outputTag = extractedTags[tagPos] <= decorations[decPos];
- } else {
- outputTag = true;
- }
- } else {
- outputTag = false;
- }
-
- if (outputTag) {
- emitTextUpTo(extractedTags[tagPos]);
- if (openDecoration) {
-
- html.push('</span>');
- openDecoration = null;
- }
- html.push(extractedTags[tagPos + 1]);
- tagPos += 2;
- } else if (decPos < decorations.length) {
- emitTextUpTo(decorations[decPos]);
- currentDecoration = decorations[decPos + 1];
- decPos += 2;
- } else {
- break;
- }
- }
- emitTextUpTo(sourceText.length);
- if (openDecoration) {
- html.push('</span>');
- }
- return html.join('');
- }
- function prettyPrintOne(sourceCodeHtml) {
- try {
-
- var sourceAndExtractedTags = PR_extractTags(sourceCodeHtml);
-
- var source = sourceAndExtractedTags.source;
-
- var extractedTags = sourceAndExtractedTags.tags;
-
-
- var isMarkup = /^\s*</.test(source) && />\s*$/.test(source);
-
- var decorations = isMarkup
- ? PR_decorateMarkup(source)
- : PR_decorateSource(source);
-
-
- return PR_recombineTagsAndDecorations(source, extractedTags, decorations);
- } catch (e) {
- if ('console' in window) {
- console.log(e);
- console.trace();
- }
- return sourceCodeHtml;
- }
- }
- var PR_SHOULD_USE_CONTINUATION = true;
- function prettyPrint(opt_whenDone) {
-
- var codeSegments = [
- document.getElementsByTagName('pre'),
- document.getElementsByTagName('code'),
- document.getElementsByTagName('xmp') ];
- var elements = [];
- for (var i = 0; i < codeSegments.length; ++i) {
- for (var j = 0; j < codeSegments[i].length; ++j) {
- elements.push(codeSegments[i][j]);
- }
- }
- codeSegments = null;
-
-
- var k = 0;
- function doWork() {
- var endTime = (PR_SHOULD_USE_CONTINUATION
- ? new Date().getTime() + 250
- : Infinity);
- for (; k < elements.length && new Date().getTime() < endTime; k++) {
- var cs = elements[k];
- if (cs.className && cs.className.indexOf('prettyprint') >= 0 || true) {
-
- var nested = false;
- for (var p = cs.parentNode; p != null; p = p.parentNode) {
- if ((p.tagName == 'pre' || p.tagName == 'code' ||
- p.tagName == 'xmp') &&
- (p.className && p.className.indexOf('prettyprint') >= 0 || true) ) {
- nested = true;
- break;
- }
- }
- if (!nested) {
-
-
- var content = PR_getInnerHtml(cs);
- content = content.replace(/(?:\r\n?|\n)$/, '');
-
- var newContent = prettyPrintOne(content);
-
- if (!PR_isRawContent(cs)) {
-
- cs.innerHTML = newContent;
- } else {
-
-
-
- var pre = document.createElement('PRE');
- for (var i = 0; i < cs.attributes.length; ++i) {
- var a = cs.attributes[i];
- if (a.specified) {
- pre.setAttribute(a.name, a.value);
- }
- }
- pre.innerHTML = newContent;
-
- cs.parentNode.replaceChild(pre, cs);
- }
- }
- }
- }
- if (k < elements.length) {
-
- setTimeout(doWork, 250);
- } else if (opt_whenDone) {
- opt_whenDone();
- }
- }
- doWork();
- }
|