Tip: Click lines to highlight, hold ctrl/cmd to multi-select

http://codedumper.com/iteci (5-Aug @ 07:50)

Syntax Highlighted Code

  1. (function () {
  2.  
  3. var blank_iframe = '/index-blank.html';
  4. var example_jquery = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'; // latest from google
  5.  
  6. var re_opt = /options/i;
  7.  
  8. if (!window.jquerydocs) window.jquerydocs = {};
  9. if (!window.xmldoc) window.xmldoc = null;
  10.  
  11. window.loadDocs = function(data) {
  12.     $(document).trigger('api-loading');
  13.    
  14.     if (!xmldoc && typeof data != "undefined") {
  15.         jquerydocs = data;
  16.         attachFind(jquerydocs);
  17.         $(document).trigger('api-load-success');
  18.         $(document).trigger('api-load-complete');
  19.     } else {
  20.         // parser
  21.         $.ajax({
  22.             url: xmldoc || 'jquery-docs.xml', // generated from jquery source: /tools/wikiapi2xml/createjQueryXMLDocs.py
  23.             dataType: 'xml',
  24.             success: parse,
  25.             error: function () {
  26.                 $(document).trigger('api-load-error');
  27.             },
  28.             complete: function () {
  29.                 $(document).trigger('api-load-complete');
  30.             }
  31.         });
  32.     }
  33. };
  34.  
  35. function parse(xml) {
  36.     var docinfo = $('docs', xml);
  37.     var guid = 0; // TODO upgrade guid to a combo of fn name and params - like Jorn's browser
  38.  
  39.     jquerydocs.version = docinfo.attr('version');
  40.     jquerydocs.timestamp = docinfo.attr('timestamp');
  41.     jquerydocs.startdoc = docinfo.attr('startdoc');
  42.    
  43.     var letters = []; // holder before sorting and inserting
  44.     jquerydocs.letters = [];
  45.  
  46.     jquerydocs.data = {};
  47.     jquerydocs.searchNames = [];
  48.     jquerydocs.categories = [];
  49.  
  50.     // loop through all types collecting data
  51.     $('cat', xml).each(function (i) {
  52.         var catName = this.getAttribute('value');
  53.         var category = {};
  54.         category.name = catName;
  55.         category.subcategories = [];
  56.        
  57.         $('subcat', this).each(function (i) {
  58.             var subcatName = this.getAttribute('value');
  59.             category.subcategories.push(subcatName);
  60.            
  61.             $('function,property,selector', this).each(function () {
  62.                 var data = {};
  63.                 guid++;
  64.                
  65.                 // some function names have spaces around them - so trim
  66.                 var name = this.getAttribute('name').replace( /^\s+|\s+$/g, '');
  67.                
  68.                 var searchName = name.toLowerCase().replace(/^jquery\./, '');
  69.                 letters.push(name.toLowerCase().substr(0,1));
  70.  
  71.                 name = name.replace(/^jquery\./i, '$.');
  72.                
  73.                 jquerydocs.searchNames.push(searchName + guid);
  74.                
  75.                 data['id'] = guid;
  76.                 data['searchname'] = searchName;
  77.                 data['name'] = name;
  78.                 data['type'] = this.nodeName.toLowerCase();
  79.                 data['category'] = this.getAttribute('cat');
  80.                 data['subcategory'] = subcatName;
  81.                 data['return'] = escapeHTML(this.getAttribute('return'));
  82.                 data['added'] = $('added', this).text();
  83.                 data['sample'] = $('> sample', this).text();
  84.                 data['desc'] = $('> desc', this).text();
  85.                 data['longdesc'] = deWikify($('> longdesc', this).text());
  86.                
  87.                 // silly hack because of conversion issue from wiki to text (the .ready function
  88.                 // has HTML in the description), but also includes HTML that should be printed,
  89.                 // in particular the body tag :-(
  90.                 data.longdesc = data.longdesc.replace(/<body>/, '&lt;body&gt;');
  91.                
  92.                 // some descs are in HTML format, some aren't
  93.                 if (!(/<p>/).test(data.longdesc)) {
  94.                     data.longdesc = '<p>' + data.longdesc.split(/\n\n/).join('</p><p>') + '</p>';
  95.                 }
  96.  
  97.                 // strip our empty p tag if there was no description
  98.                 if (data.longdesc == '<p></p>') {
  99.                     data.longdesc = '';
  100.                 }
  101.                
  102.                 /** params - we'll also search for Options to decide whether we need to parse */
  103.                 var readOptions = false;
  104.                 data.params = [];
  105.                 $('params', this).each(function (i) {
  106.                     var type = escapeHTML(this.getAttribute('type'));
  107.                     var name = this.getAttribute('name');
  108.                     var opt = this.getAttribute('optional') || "";
  109.                     var desc = $('desc', this).text();
  110.                    
  111.                     if (re_opt.test(type)) {
  112.                         readOptions = true;
  113.                     }
  114.                    
  115.                     data.params.push({
  116.                         optional : (/true/i).test(opt), // bool
  117.                         name : name,
  118.                         type : type,
  119.                         desc : desc
  120.                     });
  121.                 });
  122.                
  123.                 if (readOptions) {
  124.                     data.options = [];
  125.                     $('option', this).each(function () {
  126.                         var option = {};
  127.                         option['name'] = this.getAttribute('name');
  128.                         option['default'] = this.getAttribute('default') || '';
  129.                         option['type'] = escapeHTML(this.getAttribute('type'));
  130.                         option['desc'] = deWikify($('desc', this).text());
  131.  
  132.                         data.options.push(option);
  133.                     });
  134.                 }
  135.  
  136.                 data.examples = [];
  137.                 /** examples */
  138.                 $('example', this).each(function (i) {
  139.                     var iframe = '', exampleId = '';
  140.                     var example = {};
  141.  
  142.                     example['code'] = $('code', this).text();
  143.                     example['htmlCode'] = escapeHTML(example.code);
  144.                     example['desc'] = deWikify(escapeHTML($('desc', this).text()));
  145.                     example['css']  = $('css', this).text() || '';
  146.                     example['inhead'] = $('inhead', this).text() || '';
  147.                     example['html'] = $('html', this).text() || '';
  148.  
  149.                     exampleId = guid + 'iframeExample' + i;
  150.                     example['exampleId'] = exampleId;
  151.                    
  152.                     if (example.html) {
  153.  
  154.                         iframe = '<iframe id="' + exampleId + '" class="example" src="' + blank_iframe + '"></iframe>';
  155.  
  156.                         // we're storing the example iframe source to insert in to
  157.                         // the iframe only once it's inserted in to the DOM.
  158.                         example['runCode'] = iframeTemplate().replace(/%([a-z]*)%/ig, function (m, l) {
  159.                             return example[l] || "";
  160.                         });
  161.                     } else {
  162.                         example.runCode = '';
  163.                     }
  164.  
  165.                     data.examples.push(example);
  166.                 });
  167.  
  168.                 jquerydocs.data[searchName + data.id] = data;
  169.             });
  170.         });
  171.  
  172.         jquerydocs.categories.push(category); // FIXME should I warn if this exists?
  173.     });
  174.  
  175.     jquerydocs.letters = unique($.map(letters.sort(), function (i) {
  176.         return i.substr(0,1);
  177.     }));
  178.    
  179.     // attachFind(jquerydocs);
  180.  
  181.     $(document).trigger('api-load-success');
  182. }
  183.  
  184. // helpers
  185.  
  186. function attachFind(o) {
  187.     o.find = function (s, by) {
  188.         var found = [],
  189.             tmp = {},
  190.             tmpNames = [],
  191.             lettersLK = {},
  192.             letters = [],
  193.             catsLK = {},
  194.             cats = [],
  195.             catPointer = 0,
  196.             subLK = {},
  197.             sub = [],
  198.             data = {};
  199.            
  200.         var i = 0;
  201.         s = s.toLowerCase();
  202.         by = (by || 'searchname').toLowerCase();
  203.        
  204.         if (by == 'name') by = 'searchname'; // search without the $.
  205.        
  206.         for (i = 0; i < jquerydocs.searchNames.length; i++) {
  207.             if (jquerydocs.data[jquerydocs.searchNames[i]][by] && jquerydocs.data[jquerydocs.searchNames[i]][by].toLowerCase().indexOf(s) == 0) {
  208.                 data = tmp[jquerydocs.searchNames[i]] = jquerydocs.data[jquerydocs.searchNames[i]];
  209.                 tmpNames.push(jquerydocs.searchNames[i]);
  210.                
  211.                 if (!lettersLK[jquerydocs.searchNames[i].substr(0, 1)]) {
  212.                     lettersLK[jquerydocs.searchNames[i].substr(0, 1)] = true;
  213.                     letters.push(jquerydocs.searchNames[i].substr(0, 1));
  214.                 }
  215.                
  216.                 if (typeof catsLK[data.category] == 'undefined') {
  217.                     catsLK[data.category] = catPointer;
  218.                     cats.push({ name : data.category, subcategories : [] });
  219.                     catPointer++;
  220.                 }
  221.                
  222.                 if (!subLK[data.subcategory]) {
  223.                     subLK[data.subcategory] = true;
  224.                    
  225.                     cats[catsLK[data.category]].subcategories.push(data.subcategory);
  226.                 }
  227.             }
  228.         }
  229.        
  230.         tmpNames = tmpNames.sort().reverse(); // never sure if this is faster with the reverse
  231.         i = tmpNames.length;
  232.         while (i--) {
  233.             found.push(tmp[tmpNames[i]]);
  234.         }
  235.        
  236.         // this is kind of noddy, but returns the same object as we queried - which is cool!
  237.         found.letters = letters;
  238.         found.categories = cats;
  239.         found.data = tmp;
  240.         found.searchNames = tmpNames;
  241.         attachFind(found);
  242.        
  243.         return found;
  244.     };
  245. }
  246.  
  247. function fieldMap() {
  248.     return {
  249.        
  250.     }
  251. }
  252.  
  253. function unique(a) {
  254.     var ret = [], done = {};
  255.  
  256.     try {
  257.         for ( var i = 0, length = a.length; i < length; i++ ) {
  258.             var id = a[ i ] ;
  259.  
  260.             if ( !done[ id ] ) {
  261.                 done[ id ] = true;
  262.                 ret.push( a[ i ] );
  263.             }
  264.         }
  265.  
  266.     } catch( e ) {
  267.         ret = a;
  268.     }
  269.  
  270.     return ret;
  271. }
  272.  
  273. function iframeTemplate() {
  274.     // array so that we maintain some formatting
  275.     return [
  276.         '<!' + 'DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"',
  277.         '    "http://www.w3.org/TR/html4/loose.dtd">',
  278.         '<' + 'html>',
  279.         '<' + 'head>',
  280.         '<base href="http://docs.jquery.com" />',
  281.         '<' + 'script src="' + example_jquery + '"><' + '/script>',
  282.         '%inhead%',
  283.         '<' + 'script>',
  284.         '$(document).ready(function(){', '%code%', '  });',
  285.         '<' + '/script>',
  286.         '<' + 'style>',
  287.         '%css%',
  288.         '<' + '/style>',
  289.         '<' + '/head>',
  290.         '<' + 'body>',
  291.         '%html%',
  292.         '<' + '/body>',
  293.         '<' + '/html>'
  294.     ].join("\n");
  295. }
  296.  
  297.  
  298.  
  299. /** public utility functions */
  300.  
  301. window.escapeHTML = function (s) {
  302.     // converts null to string
  303.     return (s+"").replace(/[<>]/g, function (m) {
  304.         if (m == '<') return '&lt;';
  305.         else if (m == '>') return '&gt;';
  306.     });
  307. };
  308.  
  309. window.cleanSelector = function(s) {
  310.     return (s+"").replace(/[\$\.]/g, function (m) {
  311.         // handle escaping characters that break the selector engine
  312.         if (m == '$') {
  313.             return '\\$';
  314.         } else if (m == '.') {
  315.             return '\\.';
  316.         }
  317.     });
  318. };
  319.  
  320. window.linkifyTypes = function(type) {
  321.     // cheeky way to avoid doing a massive if (m == x || m == y || m == etc) - we just do an .indexOf()
  322.     var nodocs = '|jQuery|XMLHttpRequest|Plugins|Validator|Validation|undefined|or|Any|DOM|Map|top|left|lt|gt|\(s\)||'; // note we purposely include an empty match
  323.  
  324.     return type ? $.map(type.replace(/DOMElement/g, 'DOM Element').split(/, /), function (n) {
  325.         // match words and linkify, then italic to the optionals
  326.         return n.replace(/boolean/, 'Boolean').replace(/\b[a-z]*\b/gi, function (m, l) {
  327.             // special case
  328.             if (m == 'Elements') {
  329.                 return '<a href="http://docs.jquery.com/Types#Element">Element</a>s';
  330.             // no specific documentation for these types
  331.             } else if (nodocs.indexOf('|' + m + '|') !== -1) {
  332.                 return m;
  333.             } else {
  334.                 return '<a href="http://docs.jquery.com/Types#' + m + '">' + m + '</a>';
  335.             }
  336.         });
  337.     }).join(', ') : "";
  338. };
  339.  
  340. window.deWikify = function (s) {
  341.     return (""+s).replace(/'''.*?'''/g, function (m) {
  342.         return '<strong>' + m.replace(/'''/g, '') + '</strong>';
  343.    }).replace(/''.*?''/g, function (m) {
  344.        return '<em>' + m.replace(/''/g, '') + '</em>';
  345.    }).replace(/\[http.*?\]/, function (m) {
  346.        var p = m.replace(/^\[/, '').replace(/\]$/, '').split(/ /);
  347.        return '<a href="' + p[0] + '">' + (p.length == 2 ? p[1] : p[0]) + '</a>';
  348.    }).replace(/(((^|\n)(\*|[0-9]+.).*)+)/g, function (m) {
  349.        var type = 'ol';
  350.        // strip leading new line
  351.        m = m.replace( /^\s+|\s+$/g, "" );
  352.        if (m.match(/^\*/)) type = 'ul';
  353.        return '<' + type + '><li>' + m.replace(/\*?/g, '').split(/\n/).join("</li><li>") + '</li></' + type + '>';
  354.    });
  355. };
  356.  
  357. window.runExample = function(data) {
  358.    if (!data.examples || data.examples.length == 0) return;
  359.    
  360.    var i, win, example;
  361.  
  362.    for (i = 0; i < data.examples.length; i++) {
  363.        example = data.examples[i];
  364.        
  365.        win = $('#' + cleanSelector(example.exampleId)).get(0);
  366.        if (win) {
  367.            win = win.contentDocument || win.contentWindow.document;
  368.  
  369.            // from docs.jquery.com
  370.            win.write(example.runCode.replace("$(document).ready(function(){", "window.onload = (function(){try{")
  371.                .replace(/}\);\s*<\/sc/, "}catch(e){}});</sc")
  372.                .replace("</head>", "<style>html,body{border:0; margin:0; padding:0;}</style></head>")
  373.            );
  374.  
  375.            win.close();
  376.        }
  377.    }
  378. };
  379.  
  380. window.fixLinks = function (context) {
  381.    // since the source comes from the wiki, we need to adjust some of the links
  382.    $('a', context).each(function () {
  383.        var href = this.getAttribute('href');
  384.        if (href && !href.match(/http/) && !href.match(/^#/) && this.className != 'fnName') {
  385.            this.host = 'docs.jquery.com';
  386.            this.pathname = this.pathname.replace(window.location.pathname, '');
  387.        }
  388.    });
  389. };
  390.  
  391. })();

Plain Code

(function () {

var blank_iframe = '/index-blank.html';
var example_jquery = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'; // latest from google

var re_opt = /options/i;

if (!window.jquerydocs) window.jquerydocs = {};
if (!window.xmldoc) window.xmldoc = null;

window.loadDocs = function(data) {
    $(document).trigger('api-loading');
    
    if (!xmldoc && typeof data != "undefined") {
        jquerydocs = data;
        attachFind(jquerydocs);
        $(document).trigger('api-load-success');
        $(document).trigger('api-load-complete');
    } else {
        // parser
        $.ajax({
            url: xmldoc || 'jquery-docs.xml', // generated from jquery source: /tools/wikiapi2xml/createjQueryXMLDocs.py
            dataType: 'xml',
            success: parse,
            error: function () {
                $(document).trigger('api-load-error');
            },
            complete: function () {
                $(document).trigger('api-load-complete');
            }
        });
    }
};

function parse(xml) {
    var docinfo = $('docs', xml);
    var guid = 0; // TODO upgrade guid to a combo of fn name and params - like Jorn's browser

    jquerydocs.version = docinfo.attr('version');
    jquerydocs.timestamp = docinfo.attr('timestamp');
    jquerydocs.startdoc = docinfo.attr('startdoc');
    
    var letters = []; // holder before sorting and inserting
    jquerydocs.letters = [];

    jquerydocs.data = {};
    jquerydocs.searchNames = [];
    jquerydocs.categories = [];

    // loop through all types collecting data
    $('cat', xml).each(function (i) {
        var catName = this.getAttribute('value');
        var category = {};
        category.name = catName;
        category.subcategories = [];
        
        $('subcat', this).each(function (i) {
            var subcatName = this.getAttribute('value');
            category.subcategories.push(subcatName);
            
            $('function,property,selector', this).each(function () {
                var data = {};
                guid++;
                
                // some function names have spaces around them - so trim
                var name = this.getAttribute('name').replace( /^\s+|\s+$/g, '');
                
                var searchName = name.toLowerCase().replace(/^jquery\./, '');
                letters.push(name.toLowerCase().substr(0,1));

                name = name.replace(/^jquery\./i, '$.');
                
                jquerydocs.searchNames.push(searchName + guid);
                
                data['id'] = guid;
                data['searchname'] = searchName;
                data['name'] = name;
                data['type'] = this.nodeName.toLowerCase();
                data['category'] = this.getAttribute('cat');
                data['subcategory'] = subcatName;
                data['return'] = escapeHTML(this.getAttribute('return'));
                data['added'] = $('added', this).text();
                data['sample'] = $('> sample', this).text();
                data['desc'] = $('> desc', this).text();
                data['longdesc'] = deWikify($('> longdesc', this).text());
                
                // silly hack because of conversion issue from wiki to text (the .ready function 
                // has HTML in the description), but also includes HTML that should be printed, 
                // in particular the body tag :-(
                data.longdesc = data.longdesc.replace(/<body>/, '&lt;body&gt;');
                
                // some descs are in HTML format, some aren't
                if (!(/<p>/).test(data.longdesc)) {
                    data.longdesc = '<p>' + data.longdesc.split(/\n\n/).join('</p><p>') + '</p>';
                }

                // strip our empty p tag if there was no description
                if (data.longdesc == '<p></p>') {
                    data.longdesc = '';
                }
                
                /** params - we'll also search for Options to decide whether we need to parse */
                var readOptions = false;
                data.params = [];
                $('params', this).each(function (i) {
                    var type = escapeHTML(this.getAttribute('type'));
                    var name = this.getAttribute('name');
                    var opt = this.getAttribute('optional') || "";
                    var desc = $('desc', this).text();
                    
                    if (re_opt.test(type)) {
                        readOptions = true;
                    }
                    
                    data.params.push({
                        optional : (/true/i).test(opt), // bool
                        name : name,
                        type : type,
                        desc : desc
                    });
                });
                
                if (readOptions) {
                    data.options = [];
                    $('option', this).each(function () {
                        var option = {};
                        option['name'] = this.getAttribute('name');
                        option['default'] = this.getAttribute('default') || '';
                        option['type'] = escapeHTML(this.getAttribute('type'));
                        option['desc'] = deWikify($('desc', this).text());

                        data.options.push(option);
                    });
                }

                data.examples = [];
                /** examples */
                $('example', this).each(function (i) {
                    var iframe = '', exampleId = '';
                    var example = {};

                    example['code'] = $('code', this).text();
                    example['htmlCode'] = escapeHTML(example.code);
                    example['desc'] = deWikify(escapeHTML($('desc', this).text()));
                    example['css']  = $('css', this).text() || '';
                    example['inhead'] = $('inhead', this).text() || '';
                    example['html'] = $('html', this).text() || '';

                    exampleId = guid + 'iframeExample' + i;
                    example['exampleId'] = exampleId;
                    
                    if (example.html) {

                        iframe = '<iframe id="' + exampleId + '" class="example" src="' + blank_iframe + '"></iframe>';

                        // we're storing the example iframe source to insert in to 
                        // the iframe only once it's inserted in to the DOM.
                        example['runCode'] = iframeTemplate().replace(/%([a-z]*)%/ig, function (m, l) {
                            return example[l] || "";
                        });
                    } else {
                        example.runCode = '';
                    }

                    data.examples.push(example);
                });

                jquerydocs.data[searchName + data.id] = data;
            });
        });

        jquerydocs.categories.push(category); // FIXME should I warn if this exists?
    });

    jquerydocs.letters = unique($.map(letters.sort(), function (i) {
        return i.substr(0,1);
    }));
    
    // attachFind(jquerydocs);

    $(document).trigger('api-load-success');
}

// helpers

function attachFind(o) {
    o.find = function (s, by) {
        var found = [], 
            tmp = {}, 
            tmpNames = [], 
            lettersLK = {}, 
            letters = [],
            catsLK = {},
            cats = [],
            catPointer = 0,
            subLK = {},
            sub = [],
            data = {};
            
        var i = 0;
        s = s.toLowerCase();
        by = (by || 'searchname').toLowerCase();
        
        if (by == 'name') by = 'searchname'; // search without the $.
        
        for (i = 0; i < jquerydocs.searchNames.length; i++) {
            if (jquerydocs.data[jquerydocs.searchNames[i]][by] && jquerydocs.data[jquerydocs.searchNames[i]][by].toLowerCase().indexOf(s) == 0) {
                data = tmp[jquerydocs.searchNames[i]] = jquerydocs.data[jquerydocs.searchNames[i]];
                tmpNames.push(jquerydocs.searchNames[i]);
                
                if (!lettersLK[jquerydocs.searchNames[i].substr(0, 1)]) {
                    lettersLK[jquerydocs.searchNames[i].substr(0, 1)] = true;
                    letters.push(jquerydocs.searchNames[i].substr(0, 1));
                }
                
                if (typeof catsLK[data.category] == 'undefined') {
                    catsLK[data.category] = catPointer;
                    cats.push({ name : data.category, subcategories : [] });
                    catPointer++;
                }
                
                if (!subLK[data.subcategory]) {
                    subLK[data.subcategory] = true;
                    
                    cats[catsLK[data.category]].subcategories.push(data.subcategory);
                }
            }
        }
        
        tmpNames = tmpNames.sort().reverse(); // never sure if this is faster with the reverse
        i = tmpNames.length;
        while (i--) {
            found.push(tmp[tmpNames[i]]);
        }
        
        // this is kind of noddy, but returns the same object as we queried - which is cool!
        found.letters = letters;
        found.categories = cats;
        found.data = tmp;
        found.searchNames = tmpNames;
        attachFind(found);
        
        return found;
    };
}

function fieldMap() {
    return {
        
    }
}

function unique(a) {
    var ret = [], done = {};

    try {
        for ( var i = 0, length = a.length; i < length; i++ ) {
            var id = a[ i ] ;

            if ( !done[ id ] ) {
                done[ id ] = true;
                ret.push( a[ i ] );
            }
        }

    } catch( e ) {
        ret = a;
    }

    return ret;
}

function iframeTemplate() {
    // array so that we maintain some formatting
    return [
        '<!' + 'DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"', 
        '    "http://www.w3.org/TR/html4/loose.dtd">', 
        '<' + 'html>', 
        '<' + 'head>', 
        '<base href="http://docs.jquery.com" />', 
        '<' + 'script src="' + example_jquery + '"><' + '/script>', 
        '%inhead%', 
        '<' + 'script>', 
        '$(document).ready(function(){', '%code%', '  });', 
        '<' + '/script>', 
        '<' + 'style>', 
        '%css%', 
        '<' + '/style>', 
        '<' + '/head>', 
        '<' + 'body>', 
        '%html%', 
        '<' + '/body>', 
        '<' + '/html>'
    ].join("\n");
}



/** public utility functions */

window.escapeHTML = function (s) {
    // converts null to string
    return (s+"").replace(/[<>]/g, function (m) {
        if (m == '<') return '&lt;';
        else if (m == '>') return '&gt;';
    });
};

window.cleanSelector = function(s) {
    return (s+"").replace(/[\$\.]/g, function (m) {
        // handle escaping characters that break the selector engine
        if (m == '$') {
            return '\\$';
        } else if (m == '.') {
            return '\\.';
        }
    });
};

window.linkifyTypes = function(type) {
    // cheeky way to avoid doing a massive if (m == x || m == y || m == etc) - we just do an .indexOf()
    var nodocs = '|jQuery|XMLHttpRequest|Plugins|Validator|Validation|undefined|or|Any|DOM|Map|top|left|lt|gt|\(s\)||'; // note we purposely include an empty match

    return type ? $.map(type.replace(/DOMElement/g, 'DOM Element').split(/, /), function (n) {
        // match words and linkify, then italic to the optionals
        return n.replace(/boolean/, 'Boolean').replace(/\b[a-z]*\b/gi, function (m, l) {
            // special case
            if (m == 'Elements') {
                return '<a href="http://docs.jquery.com/Types#Element">Element</a>s';
            // no specific documentation for these types
            } else if (nodocs.indexOf('|' + m + '|') !== -1) {
                return m;
            } else {
                return '<a href="http://docs.jquery.com/Types#' + m + '">' + m + '</a>';
            }
        });
    }).join(', ') : "";
};

window.deWikify = function (s) {
    return (""+s).replace(/'''.*?'''/g, function (m) {
        return '<strong>' + m.replace(/'''/g, '') + '</strong>';
    }).replace(/''.*?''/g, function (m) {
        return '<em>' + m.replace(/''/g, '') + '</em>';
    }).replace(/\[http.*?\]/, function (m) {
        var p = m.replace(/^\[/, '').replace(/\]$/, '').split(/ /);
        return '<a href="' + p[0] + '">' + (p.length == 2 ? p[1] : p[0]) + '</a>';
    }).replace(/(((^|\n)(\*|[0-9]+.).*)+)/g, function (m) {
        var type = 'ol';
        // strip leading new line
        m = m.replace( /^\s+|\s+$/g, "" );
        if (m.match(/^\*/)) type = 'ul';
        return '<' + type + '><li>' + m.replace(/\*?/g, '').split(/\n/).join("</li><li>") + '</li></' + type + '>';
    });
};

window.runExample = function(data) {
    if (!data.examples || data.examples.length == 0) return;
    
    var i, win, example;

    for (i = 0; i < data.examples.length; i++) {
        example = data.examples[i];
        
        win = $('#' + cleanSelector(example.exampleId)).get(0);
        if (win) {
            win = win.contentDocument || win.contentWindow.document;

            // from docs.jquery.com
            win.write(example.runCode.replace("$(document).ready(function(){", "window.onload = (function(){try{")
                .replace(/}\);\s*<\/sc/, "}catch(e){}});</sc")
                .replace("</head>", "<style>html,body{border:0; margin:0; padding:0;}</style></head>")
            );

            win.close();
        }
    }
};

window.fixLinks = function (context) {
    // since the source comes from the wiki, we need to adjust some of the links
    $('a', context).each(function () {
        var href = this.getAttribute('href');
        if (href && !href.match(/http/) && !href.match(/^#/) && this.className != 'fnName') {
            this.host = 'docs.jquery.com';
            this.pathname = this.pathname.replace(window.location.pathname, '');
        }
    });
};

})();

Permalink: http://codedumper.com/iteci