page.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var onDocumentLoad = function ( event ) {
  2. var section = /\/(manual|api)\//.exec( window.location.pathname )[ 1 ].toString().split( '.html' )[ 0 ];
  3. var name = /[-A-z0-9]+\.html/.exec( window.location.pathname ).toString().split( '.html' )[ 0 ];
  4. var path;
  5. if ( section == 'manual' ) {
  6. name = name.replace(/\-/g, ' ');
  7. path = window.location.pathname.replace( /\ /g, '-' );
  8. path = /\/manual\/[-a-z0-9\/]+/.exec( path ).toString().substr( 8 );
  9. } else {
  10. path = /\/api\/[A-z0-9\/]+/.exec( window.location.pathname ).toString().substr( 5 );
  11. }
  12. var text = document.body.innerHTML;
  13. text = text.replace(/\[name\]/gi, name);
  14. text = text.replace(/\[path\]/gi, path);
  15. text = text.replace(/\[page:(\w+)\]/gi, "[page:$1 $1]" ); // [page:name] to [page:name title]
  16. text = text.replace(/\[page:(\w+) ([\w|\.]+)\]/gi, "<a href=\"javascript:window.parent.goTo('$1')\" title=\"$1\">$2</a>" ); // [page:name title]
  17. text = text.replace(/\[link:([\w|\:|\/|\.|\-|\_]+)\]/gi, "[link:$1 $1]" ); // [link:url] to [link:url title]
  18. text = text.replace(/\[link:([\w|\:|\/|\.|\-|\_]+) ([\w|\:|\/|\.|\-|\_]+)\]/gi, "<a href=\"$1\" target=\"_blank\">$2</a>" ); // [link:url title]
  19. text = text.replace(/\*([\w|\d|\"|\-|\(][\w|\d|\ |\-|\/|\+|\-|\(|\)|\=|\,|\.\"]*[\w|\d|\"|\)]|\w)\*/gi, "<strong>$1</strong>" ); // *
  20. document.body.innerHTML = text;
  21. // Edit button
  22. var button = document.createElement( 'div' );
  23. button.id = 'button';
  24. button.textContent = 'Edit this page';
  25. button.addEventListener( 'click', function ( event ) {
  26. window.open( 'https://github.com/mrdoob/three.js/blob/dev/docs/' + section + '/' + path + '.html' );
  27. }, false );
  28. document.body.appendChild( button );
  29. // Syntax highlighting
  30. var styleBase = document.createElement( 'link' );
  31. styleBase.href = '../../prettify/prettify.css';
  32. styleBase.rel = 'stylesheet';
  33. var styleCustom = document.createElement( 'link' );
  34. styleCustom.href = '../../prettify/threejs.css';
  35. styleCustom.rel = 'stylesheet';
  36. document.head.appendChild( styleBase );
  37. document.head.appendChild( styleCustom );
  38. var prettify = document.createElement( 'script' );
  39. prettify.src = '../../prettify/prettify.js';
  40. prettify.onload = function () {
  41. var elements = document.getElementsByTagName( 'code' );
  42. for ( var i = 0; i < elements.length; i ++ ) {
  43. var e = elements[ i ];
  44. e.className += ' prettyprint';
  45. }
  46. prettyPrint();
  47. }
  48. document.head.appendChild( prettify );
  49. };
  50. document.addEventListener( 'DOMContentLoaded', onDocumentLoad, false );