page.js 4.8 KB

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