publish.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. const env = require( 'jsdoc/env' );
  2. const fs = require( 'jsdoc/fs' );
  3. const helper = require( 'jsdoc/util/templateHelper' );
  4. const logger = require( 'jsdoc/util/logger' );
  5. const path = require( 'jsdoc/path' );
  6. const { taffy } = require( '@jsdoc/salty' );
  7. const template = require( 'jsdoc/template' );
  8. const util = require( 'util' );
  9. const htmlsafe = helper.htmlsafe;
  10. const linkto = helper.linkto;
  11. const resolveAuthorLinks = helper.resolveAuthorLinks;
  12. const hasOwnProp = Object.prototype.hasOwnProperty;
  13. let data;
  14. let view;
  15. const outdir = path.normalize( env.opts.destination );
  16. const themeOpts = ( env.opts.themeOpts ) || {};
  17. const categoryMap = {}; // Maps class names to their categories (Core, Addons, TSL)
  18. function mkdirSync( filepath ) {
  19. return fs.mkdirSync( filepath, { recursive: true } );
  20. }
  21. function find( spec ) {
  22. return helper.find( data, spec );
  23. }
  24. function getAncestorLinks( doclet ) {
  25. return helper.getAncestorLinks( data, doclet );
  26. }
  27. function hashToLink( doclet, hash ) {
  28. let url;
  29. if ( ! /^(#.+)/.test( hash ) ) {
  30. return hash;
  31. }
  32. url = helper.createLink( doclet );
  33. url = url.replace( /(#.+|$)/, hash );
  34. return `<a href="${url}">${hash}</a>`;
  35. }
  36. function needsSignature( { kind, type, meta } ) {
  37. let needsSig = false;
  38. // function and class definitions always get a signature
  39. if ( kind === 'function' || kind === 'class' ) {
  40. needsSig = true;
  41. } else if ( kind === 'typedef' && type && type.names && type.names.length ) {
  42. // typedefs that contain functions get a signature, too
  43. for ( let i = 0, l = type.names.length; i < l; i ++ ) {
  44. if ( type.names[ i ].toLowerCase() === 'function' ) {
  45. needsSig = true;
  46. break;
  47. }
  48. }
  49. } else if ( kind === 'namespace' && meta && meta.code && meta.code.type && meta.code.type.match( /[Ff]unction/ ) ) {
  50. // and namespaces that are functions get a signature (but finding them is a
  51. // bit messy)
  52. needsSig = true;
  53. }
  54. return needsSig;
  55. }
  56. function updateItemName( item ) {
  57. let itemName = item.name || '';
  58. if ( item.variable ) {
  59. itemName = `&hellip;${itemName}`;
  60. }
  61. return itemName;
  62. }
  63. function addParamAttributes( params ) {
  64. return params.filter( ( { name } ) => name && ! name.includes( '.' ) ).map( param => {
  65. let itemName = updateItemName( param );
  66. if ( param.type && param.type.names && param.type.names.length ) {
  67. const escapedTypes = param.type.names.map( name => linkto( name, htmlsafe( name ) ) );
  68. itemName += ' : <span class="param-type">' + escapedTypes.join( ' | ' ) + '</span>';
  69. }
  70. return itemName;
  71. } );
  72. }
  73. function buildItemTypeStrings( item ) {
  74. const types = [];
  75. if ( item && item.type && item.type.names ) {
  76. item.type.names.forEach( name => {
  77. types.push( linkto( name, htmlsafe( name ) ) );
  78. } );
  79. }
  80. return types;
  81. }
  82. function buildSearchListForData() {
  83. const categories = {
  84. 'Core': [],
  85. 'Addons': [],
  86. 'Global': [],
  87. 'TSL': []
  88. };
  89. data().each( ( item ) => {
  90. if ( item.kind !== 'package' && item.kind !== 'typedef' && ! item.inherited ) {
  91. // Extract the class name from the longname (e.g., "Animation#getAnimationLoop" -> "Animation")
  92. const parts = item.longname.split( /[#~]/ );
  93. const className = parts[ 0 ];
  94. // If this item is a member/method of a class, check if the parent class exists
  95. if ( parts.length > 1 ) {
  96. // Find the parent class/module
  97. const parentClass = find( { longname: className, kind: [ 'class', 'module' ] } );
  98. // Only include if parent exists and is not private
  99. if ( parentClass && parentClass.length > 0 && parentClass[ 0 ].access !== 'private' ) {
  100. const category = categoryMap[ className ];
  101. const entry = {
  102. title: item.longname,
  103. kind: item.kind
  104. };
  105. if ( category ) {
  106. categories[ category ].push( entry );
  107. }
  108. }
  109. } else {
  110. // This is a top-level class/module/function - include if not private
  111. if ( item.access !== 'private' ) {
  112. let category = categoryMap[ className ];
  113. // If not in categoryMap, determine category from @tsl tag
  114. if ( ! category ) {
  115. const hasTslTag = Array.isArray( item.tags ) && item.tags.some( tag => tag.title === 'tsl' );
  116. if ( hasTslTag ) {
  117. category = 'TSL';
  118. } else {
  119. category = 'Global';
  120. }
  121. }
  122. const entry = {
  123. title: item.longname,
  124. kind: item.kind
  125. };
  126. categories[ category ].push( entry );
  127. }
  128. }
  129. }
  130. } );
  131. return categories;
  132. }
  133. function buildAttribsString( attribs ) {
  134. let attribsString = '';
  135. if ( attribs && attribs.length ) {
  136. attribsString = htmlsafe( util.format( '(%s) ', attribs.join( ', ' ) ) );
  137. }
  138. return attribsString;
  139. }
  140. function addNonParamAttributes( items ) {
  141. let types = [];
  142. items.forEach( item => {
  143. types = types.concat( buildItemTypeStrings( item ) );
  144. } );
  145. return types;
  146. }
  147. function addSignatureParams( f ) {
  148. const params = f.params ? addParamAttributes( f.params ) : [];
  149. const paramsString = params.join( ', ' );
  150. f.signature = util.format( '%s(%s)', ( f.signature || '' ), paramsString ? ' ' + paramsString + ' ' : '' );
  151. }
  152. function addSignatureReturns( f ) {
  153. let returnTypes = [];
  154. let returnTypesString = '';
  155. const source = f.yields || f.returns;
  156. if ( source ) {
  157. returnTypes = addNonParamAttributes( source );
  158. }
  159. if ( returnTypes.length ) {
  160. returnTypesString = util.format( ' : %s', returnTypes.join( ' | ' ) );
  161. }
  162. f.signature = `<span class="signature">${f.signature || ''}</span>${returnTypesString ? `<span class="type-signature">${returnTypesString}</span>` : ''}`;
  163. }
  164. function addSignatureTypes( f ) {
  165. const types = f.type ? buildItemTypeStrings( f ) : [];
  166. f.signature = `${f.signature || ''}${types.length ? `<span class="type-signature"> : ${types.join( ' | ' )}</span>` : ''}`;
  167. }
  168. function addAttribs( f ) {
  169. const attribs = helper.getAttribs( f ).filter( attrib => attrib !== 'static' && attrib !== 'nullable' );
  170. const attribsString = buildAttribsString( attribs );
  171. f.attribs = attribsString ? util.format( '<span class="type-signature">%s</span>', attribsString ) : '';
  172. }
  173. function shortenPaths( files, commonPrefix ) {
  174. Object.keys( files ).forEach( file => {
  175. files[ file ].shortened = files[ file ].resolved.replace( commonPrefix, '' )
  176. // always use forward slashes
  177. .replace( /\\/g, '/' );
  178. } );
  179. return files;
  180. }
  181. function getPathFromDoclet( { meta } ) {
  182. if ( ! meta ) {
  183. return null;
  184. }
  185. return meta.path && meta.path !== 'null' ?
  186. path.join( meta.path, meta.filename ) :
  187. meta.filename;
  188. }
  189. function getFullAugmentsChain( doclet ) {
  190. const chain = [];
  191. if ( ! doclet || ! doclet.augments || ! doclet.augments.length ) {
  192. return chain;
  193. }
  194. // Start with the immediate parent
  195. const parentName = doclet.augments[0];
  196. chain.push( parentName );
  197. // Recursively find the parent's ancestors
  198. const parentDoclet = find( { longname: parentName } );
  199. if ( parentDoclet && parentDoclet.length > 0 ) {
  200. const parentChain = getFullAugmentsChain( parentDoclet[0] );
  201. chain.unshift( ...parentChain );
  202. }
  203. return chain;
  204. }
  205. function generate( title, docs, filename, resolveLinks ) {
  206. let html;
  207. resolveLinks = resolveLinks !== false;
  208. const docData = {
  209. env: env,
  210. title: title,
  211. docs: docs,
  212. augments: docs && docs[0] ? getFullAugmentsChain( docs[0] ) : null
  213. };
  214. // Put HTML files in pages/ subdirectory
  215. const pagesDir = path.join( outdir, 'pages' );
  216. mkdirSync( pagesDir );
  217. const outpath = path.join( pagesDir, filename );
  218. html = view.render( 'container.tmpl', docData );
  219. if ( resolveLinks ) {
  220. html = helper.resolveLinks( html ); // turn {@link foo} into <a href="foodoc.html">foo</a>
  221. }
  222. // Convert Prettify classes to Highlight.js format
  223. html = html.replace( /<pre class="prettyprint source linenums"><code>/g, '<pre><code>' );
  224. html = html.replace( /<pre class="prettyprint source lang-(\w+)"[^>]*><code>/g, '<pre><code class="language-$1">' );
  225. html = html.replace( /<pre class="prettyprint"><code>/g, '<pre><code>' );
  226. // Add target="_blank" to external links
  227. html = html.replace( /<a\s+([^>]*href=["'](https?:\/\/[^"']+)["'][^>]*)>/gi, '<a $1 target="_blank" rel="noopener">' );
  228. // Remove lines that only contain whitespace
  229. html = html.replace( /^\s*\n/gm, '' );
  230. fs.writeFileSync( outpath, html, 'utf8' );
  231. }
  232. function generateSourceFiles( sourceFiles, encoding = 'utf8' ) {
  233. Object.keys( sourceFiles ).forEach( file => {
  234. let source;
  235. // links are keyed to the shortened path in each doclet's `meta.shortpath` property
  236. const sourceOutfile = helper.getUniqueFilename( sourceFiles[ file ].shortened );
  237. helper.registerLink( sourceFiles[ file ].shortened, sourceOutfile );
  238. try {
  239. source = {
  240. kind: 'source',
  241. code: helper.htmlsafe( fs.readFileSync( sourceFiles[ file ].resolved, encoding ) )
  242. };
  243. } catch ( e ) {
  244. logger.error( 'Error while generating source file %s: %s', file, e.message );
  245. }
  246. generate( `Source: ${sourceFiles[ file ].shortened}`, [ source ], sourceOutfile,
  247. false );
  248. } );
  249. }
  250. function buildMainNav( items, itemsSeen, linktoFn ) {
  251. const coreDirectory = 'src';
  252. const addonsDirectory = 'examples/jsm';
  253. const hierarchy = new Map();
  254. hierarchy.set( 'Core', new Map() );
  255. hierarchy.set( 'Addons', new Map() );
  256. let nav = '';
  257. if ( items.length ) {
  258. items.forEach( item => {
  259. let displayName;
  260. let itemNav = '';
  261. if ( ! hasOwnProp.call( itemsSeen, item.longname ) ) {
  262. if ( env.conf.templates.default.useLongnameInNav ) {
  263. displayName = item.longname;
  264. } else {
  265. displayName = item.name;
  266. }
  267. itemNav += `<li>${linktoFn( item.longname, displayName.replace( /\b(module|event):/g, '' ) )}</li>\n`;
  268. itemsSeen[ item.longname ] = true;
  269. const path = item.meta.shortpath;
  270. if ( path.startsWith( coreDirectory ) ) {
  271. const subCategory = path.split( '/' )[ 1 ];
  272. pushNavItem( hierarchy, 'Core', subCategory, itemNav );
  273. categoryMap[ item.longname ] = 'Core';
  274. } else if ( path.startsWith( addonsDirectory ) ) {
  275. const subCategory = path.split( '/' )[ 2 ];
  276. pushNavItem( hierarchy, 'Addons', subCategory, itemNav );
  277. categoryMap[ item.longname ] = 'Addons';
  278. }
  279. }
  280. } );
  281. for ( const [ mainCategory, map ] of hierarchy ) {
  282. nav += `\t\t\t\t\t<h2>${mainCategory}</h2>\n`;
  283. const sortedMap = new Map( [ ...map.entries() ].sort() ); // sort sub categories
  284. for ( const [ subCategory, links ] of sortedMap ) {
  285. nav += `\t\t\t\t\t<h3>${subCategory}</h3>\n`;
  286. nav += '\t\t\t\t\t<ul>\n';
  287. links.sort();
  288. for ( const link of links ) {
  289. nav += '\t\t\t\t\t\t' + link;
  290. }
  291. nav += '\t\t\t\t\t</ul>\n';
  292. }
  293. }
  294. }
  295. return nav;
  296. }
  297. function buildGlobalsNav( globals, seen ) {
  298. let globalNav;
  299. let nav = '';
  300. if ( globals.length ) {
  301. // TSL
  302. let tslNav = '';
  303. globals.forEach( ( { kind, longname, name, tags } ) => {
  304. if ( kind !== 'typedef' && ! hasOwnProp.call( seen, longname ) ) {
  305. const hasTslTag = Array.isArray( tags ) && tags.some( tag => tag.title === 'tsl' );
  306. if ( hasTslTag ) {
  307. tslNav += `\t\t\t\t\t\t<li>${linkto( longname, name )}</li>\n`;
  308. seen[ longname ] = true;
  309. }
  310. }
  311. } );
  312. nav += '\t\t\t\t\t<h2>TSL</h2>\n';
  313. nav += '\t\t\t\t\t<ul>\n';
  314. nav += tslNav;
  315. nav += '\t\t\t\t\t</ul>\n';
  316. // Globals
  317. globalNav = '';
  318. globals.forEach( ( { kind, longname, name } ) => {
  319. if ( kind !== 'typedef' && ! hasOwnProp.call( seen, longname ) ) {
  320. globalNav += `\t\t\t\t\t\t<li>${linkto( longname, name )}</li>\n`;
  321. }
  322. seen[ longname ] = true;
  323. } );
  324. if ( ! globalNav ) {
  325. // turn the heading into a link so you can actually get to the global page
  326. nav += `\t\t\t\t\t<h3>${linkto( 'global', 'Global' )}</h3>\n`;
  327. } else {
  328. nav += '\t\t\t\t\t<h2>Global</h2>\n';
  329. nav += '\t\t\t\t\t<ul>\n';
  330. nav += globalNav;
  331. nav += '\t\t\t\t\t</ul>\n';
  332. }
  333. }
  334. return nav;
  335. }
  336. function pushNavItem( hierarchy, mainCategory, subCategory, itemNav ) {
  337. // Special case for TSL - keep it all uppercase
  338. if ( subCategory.toLowerCase() === 'tsl' ) {
  339. subCategory = 'TSL';
  340. } else {
  341. subCategory = subCategory[ 0 ].toUpperCase() + subCategory.slice( 1 ); // capitalize
  342. }
  343. if ( hierarchy.get( mainCategory ).get( subCategory ) === undefined ) {
  344. hierarchy.get( mainCategory ).set( subCategory, [] );
  345. }
  346. const categoryList = hierarchy.get( mainCategory ).get( subCategory );
  347. categoryList.push( itemNav );
  348. }
  349. /**
  350. * Create the navigation sidebar.
  351. * @param {Object} members The members that will be used to create the sidebar.
  352. * @return {string} The HTML for the navigation sidebar.
  353. */
  354. function buildNav( members ) {
  355. let nav = '\n';
  356. const seen = {};
  357. nav += buildMainNav( [ ...members.classes, ...members.modules ], seen, linkto );
  358. nav += buildGlobalsNav( members.globals, seen );
  359. return nav;
  360. }
  361. /**
  362. @param {TAFFY} taffyData See <http://taffydb.com/>.
  363. @param {Object} opts
  364. @param {Tutorial} tutorials
  365. */
  366. exports.publish = ( taffyData, opts, tutorials ) => {
  367. const sourceFilePaths = [];
  368. let sourceFiles = {};
  369. let staticFileFilter;
  370. let staticFilePaths;
  371. let staticFileScanner;
  372. data = taffyData;
  373. const conf = env.conf.templates || {};
  374. conf.default = conf.default || {};
  375. const templatePath = path.normalize( opts.template );
  376. view = new template.Template( path.join( templatePath, 'tmpl' ) );
  377. // claim some special filenames in advance, so the All-Powerful Overseer of Filename Uniqueness
  378. // doesn't try to hand them out later
  379. const indexUrl = helper.getUniqueFilename( 'index' );
  380. // don't call registerLink() on this one! 'index' is also a valid longname
  381. const globalUrl = helper.getUniqueFilename( 'global' );
  382. helper.registerLink( 'global', globalUrl );
  383. // set up templating
  384. view.layout = conf.default.layoutFile ?
  385. path.getResourcePath( path.dirname( conf.default.layoutFile ),
  386. path.basename( conf.default.layoutFile ) ) :
  387. 'layout.tmpl';
  388. // set up tutorials for helper
  389. helper.setTutorials( tutorials );
  390. data = helper.prune( data );
  391. data.sort( 'longname, version, since' );
  392. helper.addEventListeners( data );
  393. data().each( doclet => {
  394. let sourcePath;
  395. doclet.attribs = '';
  396. if ( doclet.see ) {
  397. doclet.see.forEach( ( seeItem, i ) => {
  398. doclet.see[ i ] = hashToLink( doclet, seeItem );
  399. } );
  400. }
  401. // build a list of source files
  402. if ( doclet.meta ) {
  403. sourcePath = getPathFromDoclet( doclet );
  404. sourceFiles[ sourcePath ] = {
  405. resolved: sourcePath,
  406. shortened: null
  407. };
  408. if ( ! sourceFilePaths.includes( sourcePath ) ) {
  409. sourceFilePaths.push( sourcePath );
  410. }
  411. }
  412. } );
  413. fs.mkPath( outdir );
  414. // copy the template's static files to outdir
  415. const fromDir = path.join( templatePath, 'static' );
  416. const staticFiles = fs.ls( fromDir, 3 );
  417. staticFiles.forEach( fileName => {
  418. const toDir = fs.toDir( fileName.replace( fromDir, outdir ) );
  419. fs.mkPath( toDir );
  420. fs.copyFileSync( fileName, toDir );
  421. } );
  422. // copy user-specified static files to outdir
  423. if ( conf.default.staticFiles ) {
  424. // The canonical property name is `include`. We accept `paths` for backwards compatibility
  425. // with a bug in JSDoc 3.2.x.
  426. staticFilePaths = conf.default.staticFiles.include ||
  427. conf.default.staticFiles.paths ||
  428. [];
  429. staticFileFilter = new ( require( 'jsdoc/src/filter' ).Filter )( conf.default.staticFiles );
  430. staticFileScanner = new ( require( 'jsdoc/src/scanner' ).Scanner )();
  431. staticFilePaths.forEach( filePath => {
  432. filePath = path.resolve( env.pwd, filePath );
  433. const extraStaticFiles = staticFileScanner.scan( [ filePath ], 10, staticFileFilter );
  434. extraStaticFiles.forEach( fileName => {
  435. const sourcePath = fs.toDir( filePath );
  436. const toDir = fs.toDir( fileName.replace( sourcePath, outdir ) );
  437. fs.mkPath( toDir );
  438. fs.copyFileSync( fileName, toDir );
  439. } );
  440. } );
  441. }
  442. if ( sourceFilePaths.length ) {
  443. sourceFiles = shortenPaths( sourceFiles, path.commonPrefix( sourceFilePaths ) );
  444. }
  445. data().each( doclet => {
  446. let docletPath;
  447. const url = helper.createLink( doclet );
  448. helper.registerLink( doclet.longname, url );
  449. // add a shortened version of the full path
  450. if ( doclet.meta ) {
  451. docletPath = getPathFromDoclet( doclet );
  452. docletPath = sourceFiles[ docletPath ].shortened;
  453. if ( docletPath ) {
  454. doclet.meta.shortpath = docletPath;
  455. }
  456. }
  457. } );
  458. data().each( doclet => {
  459. const url = helper.longnameToUrl[ doclet.longname ];
  460. if ( url.includes( '#' ) ) {
  461. doclet.id = helper.longnameToUrl[ doclet.longname ].split( /#/ ).pop();
  462. } else {
  463. doclet.id = doclet.name;
  464. }
  465. if ( needsSignature( doclet ) ) {
  466. addSignatureParams( doclet );
  467. addSignatureReturns( doclet );
  468. addAttribs( doclet );
  469. }
  470. } );
  471. // do this after the urls have all been generated
  472. data().each( doclet => {
  473. doclet.ancestors = getAncestorLinks( doclet );
  474. if ( doclet.kind === 'member' ) {
  475. addSignatureTypes( doclet );
  476. addAttribs( doclet );
  477. }
  478. if ( doclet.kind === 'constant' ) {
  479. addSignatureTypes( doclet );
  480. addAttribs( doclet );
  481. doclet.kind = 'member';
  482. }
  483. } );
  484. // prepare import statements, demo tags, and extract code examples
  485. data().each( doclet => {
  486. if ( doclet.kind === 'class' || doclet.kind === 'module' ) {
  487. const tags = doclet.tags;
  488. if ( Array.isArray( tags ) ) {
  489. const importTag = tags.find( tag => tag.title === 'three_import' );
  490. doclet.import = ( importTag !== undefined ) ? importTag.text : null;
  491. const demoTag = tags.find( tag => tag.title === 'demo' );
  492. doclet.demo = ( demoTag !== undefined ) ? demoTag.text : null;
  493. }
  494. // Extract code example from classdesc
  495. if ( doclet.classdesc ) {
  496. const codeBlockRegex = /<pre class="prettyprint source[^"]*"><code>([\s\S]*?)<\/code><\/pre>/;
  497. const match = doclet.classdesc.match( codeBlockRegex );
  498. if ( match ) {
  499. doclet.codeExample = match[ 0 ];
  500. // Remove the code example from classdesc
  501. doclet.classdesc = doclet.classdesc.replace( codeBlockRegex, '' ).trim();
  502. }
  503. }
  504. }
  505. } );
  506. const members = helper.getMembers( data );
  507. members.tutorials = tutorials.children;
  508. // output pretty-printed source files by default
  509. const outputSourceFiles = conf.default && conf.default.outputSourceFiles !== false;
  510. // add template helpers
  511. view.find = find;
  512. view.linkto = linkto;
  513. view.resolveAuthorLinks = resolveAuthorLinks;
  514. view.htmlsafe = htmlsafe;
  515. view.outputSourceFiles = outputSourceFiles;
  516. view.ignoreInheritedSymbols = themeOpts.ignoreInheritedSymbols;
  517. // Empty nav in templates - will be loaded from nav.html client-side
  518. view.nav = '';
  519. // generate the pretty-printed source files first so other pages can link to them
  520. if ( outputSourceFiles ) {
  521. generateSourceFiles( sourceFiles, opts.encoding );
  522. }
  523. if ( members.globals.length ) {
  524. // Split globals into TSL and non-TSL
  525. const tslGlobals = [];
  526. const nonTslGlobals = [];
  527. const originalGlobals = members.globals;
  528. originalGlobals.forEach( item => {
  529. const hasTslTag = Array.isArray( item.tags ) && item.tags.some( tag => tag.title === 'tsl' );
  530. if ( hasTslTag ) {
  531. tslGlobals.push( item );
  532. // Register each TSL item to link to TSL.html
  533. helper.registerLink( item.longname, 'TSL.html#' + item.name );
  534. } else {
  535. nonTslGlobals.push( item );
  536. }
  537. } );
  538. // Generate TSL.html for TSL functions
  539. if ( tslGlobals.length ) {
  540. generate( 'TSL', [ { kind: 'globalobj', isTSL: true } ], 'TSL.html' );
  541. }
  542. // Generate global.html for remaining globals
  543. if ( nonTslGlobals.length ) {
  544. generate( 'Global', [ { kind: 'globalobj' } ], globalUrl );
  545. }
  546. }
  547. // index page displays information from package.json and lists files
  548. const files = find( { kind: 'file' } );
  549. const packages = find( { kind: 'package' } );
  550. generate( '', // MODIFIED (Remove Home title)
  551. packages.concat(
  552. [ {
  553. kind: 'mainpage',
  554. readme: opts.readme,
  555. longname: ( opts.mainpagetitle ) ? opts.mainpagetitle : 'Main Page'
  556. } ]
  557. ).concat( files ), indexUrl );
  558. // set up the lists that we'll use to generate pages
  559. const classes = taffy( members.classes );
  560. const modules = taffy( members.modules );
  561. Object.keys( helper.longnameToUrl ).forEach( longname => {
  562. const myClasses = helper.find( classes, { longname: longname } );
  563. const myModules = helper.find( modules, { longname: longname } );
  564. if ( myClasses.length ) {
  565. generate( `${myClasses[ 0 ].name}`, myClasses, helper.longnameToUrl[ longname ] );
  566. }
  567. if ( myModules.length ) {
  568. generate( `${myModules[ 0 ].name}`, myModules, helper.longnameToUrl[ longname ] );
  569. }
  570. } );
  571. // Build navigation HTML
  572. const navHtml = buildNav( members );
  573. // Generate index.html with embedded navigation
  574. const indexTemplatePath = path.join( templatePath, 'static', 'index.html' );
  575. let indexHtml = fs.readFileSync( indexTemplatePath, 'utf8' );
  576. // Replace placeholder with actual navigation
  577. indexHtml = indexHtml.replace( '<!--NAV_PLACEHOLDER-->', navHtml );
  578. fs.writeFileSync(
  579. path.join( outdir, 'index.html' ),
  580. indexHtml,
  581. 'utf8'
  582. );
  583. // search
  584. const searchList = buildSearchListForData();
  585. fs.writeFileSync(
  586. path.join( outdir, 'search.json' ),
  587. JSON.stringify( searchList, null, '\t' )
  588. );
  589. };
粤ICP备19079148号