page.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. if ( !window.frameElement ) {
  2. // If the page is not yet displayed as an iframe of the index page (navigation panel/working links),
  3. // redirect to the index page (using the current URL without extension as the new fragment).
  4. // If this URL itself has a fragment, append it with a dot (since '#' in an URL fragment is not allowed).
  5. var hash = window.location.hash.substr( 1 );
  6. if (hash !== '') {
  7. hash = '.' + hash;
  8. }
  9. window.location.replace( window.location.origin + '/docs/#' + window.location.pathname.slice(6, -5) + hash );
  10. }
  11. var onDocumentLoad = function ( event ) {
  12. var path;
  13. var pathname = window.location.pathname;
  14. var section = /\/(manual|api|examples)\//.exec( pathname )[ 1 ].toString().split( '.html' )[ 0 ];
  15. var name = /[\-A-z0-9]+\.html/.exec( pathname ).toString().split( '.html' )[ 0 ];
  16. switch ( section ) {
  17. case 'api':
  18. path = /\/api\/[A-z0-9\/]+/.exec( pathname ).toString().substr( 5 );
  19. break;
  20. case 'examples':
  21. path = /\/examples\/[A-z0-9\/]+/.exec( pathname ).toString().substr( 10 );
  22. break;
  23. case 'manual':
  24. name = name.replace( /\-/g, ' ' );
  25. path = pathname.replace( /\ /g, '-' );
  26. path = /\/manual\/[-A-z0-9\/]+/.exec( path ).toString().substr( 8 );
  27. break;
  28. }
  29. var text = document.body.innerHTML;
  30. text = text.replace( /\[name\]/gi, name );
  31. text = text.replace( /\[path\]/gi, path );
  32. text = text.replace( /\[page:([\w\.]+)\]/gi, "[page:$1 $1]" ); // [page:name] to [page:name title]
  33. text = text.replace( /\[page:\.([\w\.]+) ([\w\.\s]+)\]/gi, "[page:" + name + ".$1 $2]" ); // [page:.member title] to [page:name.member title]
  34. text = text.replace( /\[page:([\w\.]+) ([\w\.\s]+)\]/gi, "<a onclick=\"window.parent.setUrlFragment('$1')\" title=\"$1\">$2</a>" ); // [page:name title]
  35. // text = text.replace( /\[member:.([\w]+) ([\w\.\s]+)\]/gi, "<a onclick=\"window.parent.setUrlFragment('" + name + ".$1')\" title=\"$1\">$2</a>" );
  36. text = text.replace( /\[(?:member|property|method):([\w]+)\]/gi, "[member:$1 $1]" ); // [member:name] to [member:name title]
  37. text = text.replace( /\[(?:member|property|method):([\w]+) ([\w\.\s]+)\]/gi, "<a onclick=\"window.parent.setUrlFragment('" + name + ".$2')\" target=\"_parent\" title=\"" + name + ".$2\" class=\"permalink\">#</a> .<a onclick=\"window.parent.setUrlFragment('$1')\" title=\"$1\" id=\"$2\">$2</a> " );
  38. text = text.replace( /\[link:([\w|\:|\/|\.|\-|\_]+)\]/gi, "[link:$1 $1]" ); // [link:url] to [link:url title]
  39. text = text.replace( /\[link:([\w|\:|\/|\.|\-|\_|\(|\)|\#]+) ([\w|\:|\/|\.|\-|\_|\s]+)\]/gi, "<a href=\"$1\" target=\"_blank\">$2</a>" ); // [link:url title]
  40. text = text.replace( /\*([\w|\d|\"|\-|\(][\w|\d|\ |\-|\/|\+|\-|\(|\)|\=|\,|\.\"]*[\w|\d|\"|\)]|\w)\*/gi, "<strong>$1</strong>" ); // *
  41. text = text.replace( /\[example:([\w\_]+)\]/gi, "[example:$1 $1]" ); // [example:name] to [example:name title]
  42. text = text.replace( /\[example:([\w\_]+) ([\w\:\/\.\-\_ \s]+)\]/gi, "<a href=\"../examples/#$1\" target=\"_blank\">$2</a>" ); // [example:name title]
  43. document.body.innerHTML = text;
  44. // handle code snippets formatting
  45. var elements = document.getElementsByTagName( 'code' );
  46. for ( var i = 0; i < elements.length; i ++ ) {
  47. var element = elements[ i ];
  48. text = element.textContent.trim();
  49. text = text.replace( /^\t\t/gm, '' );
  50. element.textContent = text;
  51. }
  52. // Edit button
  53. var button = document.createElement( 'div' );
  54. button.id = 'button';
  55. button.textContent = 'Edit';
  56. button.addEventListener( 'click', function ( event ) {
  57. window.open( 'https://github.com/mrdoob/three.js/blob/dev/docs/' + section + '/' + path + '.html' );
  58. }, false );
  59. document.body.appendChild( button );
  60. // Syntax highlighting
  61. var styleBase = document.createElement( 'link' );
  62. styleBase.href = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/prettify.css';
  63. styleBase.rel = 'stylesheet';
  64. var styleCustom = document.createElement( 'link' );
  65. styleCustom.href = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/threejs.css';
  66. styleCustom.rel = 'stylesheet';
  67. document.head.appendChild( styleBase );
  68. document.head.appendChild( styleCustom );
  69. var prettify = document.createElement( 'script' );
  70. prettify.src = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/prettify.js';
  71. prettify.onload = function () {
  72. var elements = document.getElementsByTagName( 'code' );
  73. for ( var i = 0; i < elements.length; i ++ ) {
  74. var e = elements[ i ];
  75. e.className += ' prettyprint';
  76. }
  77. prettyPrint();
  78. };
  79. document.head.appendChild( prettify );
  80. };
  81. document.addEventListener( 'DOMContentLoaded', onDocumentLoad, false );