1 var index = [];2 3 window.addEventListener('load', function(e) {4 5 loadCSS('http://softwaremaniacs.org/media/soft/highlight/styles/googlecode.css');6 loadCSS('http://www.treeweb.es/ShareCode/preview/3e22d6194d8361f412d0623aa30516b4/css');7 8 loadJS('http://softwaremaniacs.org/media/soft/highlight/highlight.pack.js', function(e) {9 processALL();10 });11 12 13 }, true);14 15 function processALL() {16 index = [];17 18 traverseDOM(document.body, function(item) {19 var tagName = item.tagName;20 switch (tagName) {21 case 'CODE': processCODE(item); break;22 case 'H1': processH(item, 1); break;23 case 'H2': processH(item, 2); break;24 case 'H3': processH(item, 3); break;25 case 'H4': processH(item, 4); break;26 case 'H5': processH(item, 5); break;27 case 'H6': processH(item, 6); break;28 }29 });30 31 createIndex();32 };33 34 function createIndex() { 35 var current_level = 0;36 var result = '';37 38 var identation = [];39 40 var i = 0;41 while (i<index.length) {42 var level = index[i].level;43 if (current_level < level) {44 identation.push(0);45 result+=('<ol>');46 current_level++;47 } else if (current_level > level) {48 identation.pop();49 result+=('</ol>');50 current_level--;51 } else {52 identation.push(1+identation.pop());53 54 var item = index[i].item;55 item.innerHTML = '<span class="identation">'+identation.join('.')+' </span>'+item.innerHTML;56 item.id = 'title-'+i;57 result+=('<li><a href="#'+item.id+'">'+item.innerHTML+'</a>');58 59 i++;60 }61 }62 63 var div = document.createElement('div');64 div.className = 'index';65 div.innerHTML = result;66 67 document.body.insertBefore(div, document.body.firstChild);68 }69 70 function processCODE(item) {71 72 var frame = document.createElement('div');73 frame.className = 'code-frame';74 item.parentNode.insertBefore(frame, item);75 76 var title = document.createElement('div');77 title.className = 'code-title';78 frame.appendChild(title);79 80 var pre = document.createElement('pre');81 pre.appendChild(item);82 frame.appendChild(pre);83 84 hljs.highlightBlock(item, null, false);85 title.innerHTML = item.getAttribute('class');86 };87 88 function processH(item, level) {89 index.push({90 item:item,91 level:level92 });93 }94 95 function traverseDOM(item, callback) {96 var children = item.children;97 for (var i=0; i<children.length; i++) {98 traverseDOM(children[i], callback);99 }100 callback(item);101 }102 103 104 105 function loadJS(url, callback) {106 var script = document.createElement('script');107 script.setAttribute('type', 'text/javascript');108 script.setAttribute('src', url);109 if (callback) {110 script.addEventListener('load', callback, true);111 }112 document.head.appendChild(script);113 }114 115 function loadCSS(url, callback) {116 var link = document.createElement('link');117 link.setAttribute('rel', 'stylesheet');118 link.setAttribute('type', 'text/css');119 link.setAttribute('title', 'default');120 link.setAttribute('href', url);121 if (callback) {122 link.addEventListener('load', callback, true);123 }124 document.head.appendChild(link);125 }
Enlace
El enlace para compartir es: