page.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. if ( !window.frameElement && window.location.protocol !== 'file:' ) {
  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. function onDocumentLoad( event ) {
  17. var path, localizedPath;
  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. localizedPath = /\/api\/[A-z0-9\/]+/.exec( pathname ).toString().substr( 5 );
  24. // Remove localized part of the path (e.g. 'en/' or 'es-MX/'):
  25. path = localizedPath.replace( /^[A-z0-9-]+\//, '' );
  26. break;
  27. case 'examples':
  28. path = localizedPath = /\/examples\/[A-z0-9\/]+/.exec( pathname ).toString().substr( 10 );
  29. break;
  30. case 'manual':
  31. name = name.replace( /\-/g, ' ' );
  32. path = pathname.replace( /\ /g, '-' );
  33. path = localizedPath = /\/manual\/[-A-z0-9\/]+/.exec( path ).toString().substr( 8 );
  34. break;
  35. }
  36. var text = document.body.innerHTML;
  37. text = text.replace( /\[name\]/gi, name );
  38. text = text.replace( /\[path\]/gi, path );
  39. text = text.replace( /\[page:([\w\.]+)\]/gi, "[page:$1 $1]" ); // [page:name] to [page:name title]
  40. text = text.replace( /\[page:\.([\w\.]+) ([\w\.\s]+)\]/gi, "[page:" + name + ".$1 $2]" ); // [page:.member title] to [page:name.member title]
  41. text = text.replace( /\[page:([\w\.]+) ([\w\.\s]+)\]/gi, "<a onclick=\"window.parent.setUrlFragment('$1')\" title=\"$1\">$2</a>" ); // [page:name title]
  42. // text = text.replace( /\[member:.([\w]+) ([\w\.\s]+)\]/gi, "<a onclick=\"window.parent.setUrlFragment('" + name + ".$1')\" title=\"$1\">$2</a>" );
  43. text = text.replace( /\[(member|property|method|param):([\w]+)\]/gi, "[$1:$2 $2]" ); // [member:name] to [member:name title]
  44. text = text.replace( /\[(?:member|property|method):([\w]+) ([\w\.\s]+)\]\s*(\(.*\))?/gi, "<a onclick=\"window.parent.setUrlFragment('" + name + ".$2')\" target=\"_parent\" title=\"" + name + ".$2\" class=\"permalink\">#</a> .<a onclick=\"window.parent.setUrlFragment('" + name + ".$2')\" id=\"$2\">$2</a> $3 : <a class=\"param\" onclick=\"window.parent.setUrlFragment('$1')\">$1</a>" );
  45. text = text.replace( /\[param:([\w\.]+) ([\w\.\s]+)\]/gi, "$2 : <a class=\"param\" onclick=\"window.parent.setUrlFragment('$1')\">$1</a>" ); // [param:name title]
  46. text = text.replace( /\[link:([\w|\:|\/|\.|\-|\_]+)\]/gi, "[link:$1 $1]" ); // [link:url] to [link:url title]
  47. text = text.replace( /\[link:([\w|\:|\/|\.|\-|\_|\(|\)|\#|\=]+) ([\w|\:|\/|\.|\-|\_|\s]+)\]/gi, "<a href=\"$1\" target=\"_blank\">$2</a>" ); // [link:url title]
  48. text = text.replace( /\*([\w|\d|\"|\-|\(][\w|\d|\ |\-|\/|\+|\-|\(|\)|\=|\,|\.\"]*[\w|\d|\"|\)]|\w)\*/gi, "<strong>$1</strong>" ); // *
  49. text = text.replace( /\[example:([\w\_]+)\]/gi, "[example:$1 $1]" ); // [example:name] to [example:name title]
  50. text = text.replace( /\[example:([\w\_]+) ([\w\:\/\.\-\_ \s]+)\]/gi, "<a href=\"../examples/#$1\" target=\"_blank\">$2</a>" ); // [example:name title]
  51. text = text.replace( /<a class="param" onclick="window.parent.setUrlFragment\('\w+'\)">(null|this|Boolean|Object|Array|Number|String|Integer|Float|TypedArray|ArrayBuffer)<\/a>/gi, '<span class="param">$1</span>' ); // remove links to primitive types
  52. document.body.innerHTML = text;
  53. // handle code snippets formatting
  54. var elements = document.getElementsByTagName( 'code' );
  55. for ( var i = 0; i < elements.length; i ++ ) {
  56. var element = elements[ i ];
  57. text = element.textContent.trim();
  58. text = text.replace( /^\t\t/gm, '' );
  59. element.textContent = text;
  60. }
  61. // Edit button
  62. var button = document.createElement( 'div' );
  63. button.id = 'button';
  64. button.innerHTML = '<img src="../files/ic_mode_edit_black_24dp.svg">';
  65. button.addEventListener( 'click', function ( event ) {
  66. window.open( 'https://github.com/mrdoob/three.js/blob/dev/docs/' + section + '/' + localizedPath + '.html' );
  67. }, false );
  68. document.body.appendChild( button );
  69. // Syntax highlighting
  70. var styleBase = document.createElement( 'link' );
  71. styleBase.href = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/prettify.css';
  72. styleBase.rel = 'stylesheet';
  73. var styleCustom = document.createElement( 'link' );
  74. styleCustom.href = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/threejs.css';
  75. styleCustom.rel = 'stylesheet';
  76. document.head.appendChild( styleBase );
  77. document.head.appendChild( styleCustom );
  78. var prettify = document.createElement( 'script' );
  79. prettify.src = pathname.substring( 0, pathname.indexOf( 'docs' ) + 4 ) + '/prettify/prettify.js';
  80. prettify.onload = function () {
  81. var elements = document.getElementsByTagName( 'code' );
  82. for ( var i = 0; i < elements.length; i ++ ) {
  83. var e = elements[ i ];
  84. e.className += ' prettyprint';
  85. }
  86. prettyPrint();
  87. };
  88. document.head.appendChild( prettify );
  89. };
  90. document.addEventListener( 'DOMContentLoaded', onDocumentLoad, false );