function prettyPrint()

in content/resources/js/prettify.js [1347:1438]


    function prettyPrint(opt_whenDone) {
        function byTagName(tn) { return document.getElementsByTagName(tn); }
        // fetch a list of nodes to rewrite
        var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')];
        var elements = [];
        for (var i = 0; i < codeSegments.length; ++i) {
            for (var j = 0, n = codeSegments[i].length; j < n; ++j) {
                elements.push(codeSegments[i][j]);
            }
        }
        codeSegments = null;

        var clock = Date;
        if (!clock['now']) {
            clock = { 'now': function () { return +(new Date); } };
        }

        // The loop is broken into a series of continuations to make sure that we
        // don't make the browser unresponsive when rewriting a large page.
        var k = 0;
        var prettyPrintingJob;

        var langExtensionRe = /\blang(?:uage)?-([\w.]+)(?!\S)/;
        var prettyPrintRe = /\bprettyprint\b/;

        function doWork() {
            var endTime = (window['PR_SHOULD_USE_CONTINUATION'] ?
                clock['now']() + 250 /* ms */ :
                Infinity);
            for (; k < elements.length && clock['now']() < endTime; k++) {
                var cs = elements[k];
                var className = cs.className;
                if (className.indexOf('prettyprint') >= 0) {
                    // If the classes includes a language extensions, use it.
                    // Language extensions can be specified like
                    //     <pre class="prettyprint lang-cpp">
                    // the language extension "cpp" is used to find a language handler as
                    // passed to PR.registerLangHandler.
                    // HTML5 recommends that a language be specified using "language-"
                    // as the prefix instead.  Google Code Prettify supports both.
                    // http://dev.w3.org/html5/spec-author-view/the-code-element.html
                    var langExtension = className.match(langExtensionRe);
                    // Support <pre class="prettyprint"><code class="language-c">
                    var wrapper;
                    if (!langExtension && (wrapper = childContentWrapper(cs))
                        && "CODE" === wrapper.tagName) {
                        langExtension = wrapper.className.match(langExtensionRe);
                    }

                    if (langExtension) {
                        langExtension = langExtension[1];
                    }

                    // make sure this is not nested in an already prettified element
                    var nested = false;
                    for (var p = cs.parentNode; p; p = p.parentNode) {
                        if ((p.tagName === 'pre' || p.tagName === 'code' ||
                            p.tagName === 'xmp') &&
                            p.className && p.className.indexOf('prettyprint') >= 0) {
                            nested = true;
                            break;
                        }
                    }
                    if (!nested) {
                        // Look for a class like linenums or linenums:<n> where <n> is the
                        // 1-indexed number of the first line.
                        var lineNums = cs.className.match(/\blinenums\b(?::(\d+))?/);
                        lineNums = lineNums
                            ? lineNums[1] && lineNums[1].length ? +lineNums[1] : true
                            : false;
                        if (lineNums) { numberLines(cs, lineNums); }

                        // do the pretty printing
                        prettyPrintingJob = {
                            langExtension: langExtension,
                            sourceNode: cs,
                            numberLines: lineNums
                        };
                        applyDecorator(prettyPrintingJob);
                    }
                }
            }
            if (k < elements.length) {
                // finish up in a continuation
                setTimeout(doWork, 250);
            } else if (opt_whenDone) {
                opt_whenDone();
            }
        }

        doWork();
    }