Nuestro conocimiento compartido. Nuestro tesoro compartido. Wikipedia.
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1 var index = [];2 3 window.addEventListener('load', function(e) {4 5  loadCSS('http://softwaremaniacs.org/media/soft/highlight/styles/googlecode.css');6 7  loadCSS('http://www.treeweb.es/ShareCode/preview/fc9ad45551a6e4b4d06e038fe9a6250b/css', function(e){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  addMetaMobile();17  18  index = [];19  20  traverseDOM(document.body, function(item) {21  var tagName = item.tagName;22  switch (tagName) {23  case 'CODE': processCODE(item); break;24  case 'H1': processH(item, 1); break;25  case 'H2': processH(item, 2); break;26  case 'H3': processH(item, 3); break;27  case 'H4': processH(item, 4); break;28  case 'H5': processH(item, 5); break;29  case 'H6': processH(item, 6); break;30  }31  });32  33  createIndex();34  addDocumentTitle();35  36  searchForHash();37 };38 39 function addMetaMobile() {40  // <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">41  var meta = document.createElement('meta');42  meta.setAttribute('name', 'viewport');43  meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');44  document.head.appendChild(meta);45 }46 47 /**48  * If URL has a hash, navigate to it49 */50 function searchForHash() {51  var hash = location.hash;52  if ('' != hash) {53  location.href = hash;54  }55 }56 57 function addDocumentTitle() {58  var documentTitle = document.title;59  if ('' != documentTitle) {60  var div = document.createElement('div');61  div.className = 'first-page';62  document.body.insertBefore(div, document.body.firstChild);63  64  var title = document.createElement('div');65  title.className = 'document-title';66  title.innerHTML = documentTitle;67  div.appendChild(title);68  69  var listMeta = document.getElementsByTagName('meta');70  console.log(listMeta);71  var metas = [];72  for (var i=0; i<listMeta.length; i++) {73  var meta = listMeta[i];74  var meta_name = meta.getAttribute('name');75  if (null != meta_name) {76  meta_name = meta_name.toLowerCase();77  if (null == metas[meta_name]) {78  metas[meta_name] = [];79  }80  var content = meta.getAttribute('content');81  metas[meta_name][content] = 0;82  }83  }84  85  // Build metas:86  var divMetas = document.createElement('table');87  divMetas.className = 'document-meta';88  div.appendChild(divMetas);89  for (var meta in metas) {90  var row = document.createElement('tr');91  row.className = 'meta meta-'+meta;92  divMetas.appendChild(row);93  94  var rowTitle = document.createElement('td');95  rowTitle.className = 'title';96  rowTitle.innerHTML = meta;97  row.appendChild(rowTitle);98  99  var rowContents = document.createElement('td');100  rowContents.className = 'contents';101  row.appendChild(rowContents);102  103  for (var c in metas[meta]) {104  var content = document.createElement('div');105  content.className = 'content';106  content.innerHTML = c;107  rowContents.appendChild(content);108  }109  110  }111  }112 }113 114 function createIndex() { 115  var current_level = 0;116  var result = '';117  118  var identation = [];119  120  var i = 0;121  while (i<index.length) {122  var level = index[i].level;123  if (current_level < level) {124  identation.push(0);125  result+=('<ol>');126  current_level++;127  } else if (current_level > level) {128  identation.pop();129  result+=('</ol>');130  current_level--;131  } else {132  identation.push(1+identation.pop());133  134  var item = index[i].item;135  item.id = 'title-'+identation.join('.');136  item.innerHTML = '<a href="#'+item.id+'"><span class="identation">'+identation.join('.')+'</span>'+item.innerHTML+'</a>';137  result+=('<li>'+item.innerHTML+'');138  139  i++;140  }141  }142  143  var div = document.createElement('div');144  div.className = 'index';145  div.innerHTML = result;146  147  document.body.insertBefore(div, document.body.firstChild);148 }149 150 function processCODE(item) {151  152  var frame = document.createElement('div');153  frame.className = 'code-frame';154  item.parentNode.insertBefore(frame, item);155  156  var title = document.createElement('div');157  title.className = 'code-title';158  frame.appendChild(title);159  160  var pre = document.createElement('pre');161  pre.appendChild(item);162  frame.appendChild(pre);163  164  var lang = '';165  if (null !== item.getAttribute('lang')) {166  lang = item.getAttribute('lang');167  } else if (null != item.getAttribute('language')) {168  lang = item.getAttribute('language');169  }170  if ('' != lang) {171  item.className = lang;172  }173  174  hljs.highlightBlock(item, null, false);175  176  title.innerHTML = item.className;177 };178 179 function processH(item, level) {180  index.push({181  item:item,182  level:level183  });184 }185 186 function traverseDOM(item, callback) {187  var children = item.children;188  for (var i=0; i<children.length; i++) {189  traverseDOM(children[i], callback);190  }191  callback(item);192 }193 194 195 196 function loadJS(url, callback) {197  var script = document.createElement('script');198  script.setAttribute('type', 'text/javascript');199  script.setAttribute('src', url);200  if (callback) {201  script.addEventListener('load', callback, true);202  }203  document.head.appendChild(script);204 }205 206 function loadCSS(url, callback) {207  var link = document.createElement('link');208  link.setAttribute('rel', 'stylesheet');209  link.setAttribute('type', 'text/css');210  link.setAttribute('title', 'default');211  link.setAttribute('href', url);212  if (callback) {213  link.addEventListener('load', callback, true);214  }215  document.head.appendChild(link);216 }
Enlace
El enlace para compartir es: