diff --git a/app.js b/app.js index 8796ce2..861bdf7 100644 --- a/app.js +++ b/app.js @@ -83,13 +83,16 @@ if (config.toc.template){ pmd.validatePathRef(config.toc.template, 'config.toc.template'); } +// Set helper variable so the engine know if the MenuTemplate is being used +config.toc.hasMenuTemplate = (typeof config.toc.menuTemplate !== 'undefined'); // Process data and write to file var objs = pmd.generateData(config); objs = pmd.mergeObjs(objs); +// The toc returns the configuration needed for generating the files (determine if we have a folder structured menu or flattened menu). pmd.generateToc(config, objs); -pmd.saveToFile(config, pmd.globalFiles); +pmd.saveToFile(config, pmd.menuStructure, pmd.globalFiles); // Check if the search library is enabled if(config.search) { diff --git a/lib/pmd.js b/lib/pmd.js index af2eb35..06f291c 100644 --- a/lib/pmd.js +++ b/lib/pmd.js @@ -623,11 +623,17 @@ pmd.mergeObjs = function(objs){ * * @param objs array of all data */ -pmd.saveToFile = function(config, objs){ +pmd.saveToFile = function(config, tocConfig, objs){ // Finally print out data objs.files.forEach(function(obj){ if(obj && obj.fileData && !obj.fileData.isFolder) { obj.fileData.folders = pmd.menuStructure.folders; + obj.fileData.hasMenuTemplate = tocConfig.hasMenuTemplate; + + // We use the old way of the menu, set the files variable corresponding to all the files we are generating + if(!obj.fileData.hasMenuTemplate) { + obj.fileData.files = objs.files; + } var markdown = obj.template(obj.fileData); let docExtName = path.extname(obj.folder.template); @@ -636,8 +642,8 @@ pmd.saveToFile = function(config, objs){ debug.logFile(obj.fileData.name + docExtName, markdown); } - // Register the list template - Handlebars.registerPartial( "list", fs.readFileSync(path.resolve(config.toc.menuTemplate),'utf8') ); + // Register the list template, use the same variable as used inside the toc, this is why we need this variable + Handlebars.registerPartial( "list", tocConfig.listPartialContent); fs.writeFileSync(path.resolve(obj.folder.output.path,obj.fileData.name + docExtName), markdown); } @@ -679,8 +685,13 @@ pmd.generateToc = function(config, objs){ var indexData = { folders: objs.children, + files: null, projectDispName: config.projectDispName, - search: config.search + search: config.search, + // Determine if we have a menu template if not we use the old flattened menu + hasMenuTemplate: config.toc.hasMenuTemplate, + // Stores the content of the menuTemplate if provided + listPartialContent: "" }, files = { files: [], @@ -707,14 +718,38 @@ pmd.generateToc = function(config, objs){ templateContent = fs.readFileSync(path.resolve(config.toc.template),'utf8'), template = Handlebars.compile(templateContent); - // Register the list template - Handlebars.registerPartial( "list", fs.readFileSync(path.resolve(config.toc.menuTemplate),'utf8') ); + // Check if we need to apply the flat menu structure or not + if(indexData.hasMenuTemplate) { + // Register the list template + indexData.listPartialContent = fs.readFileSync(path.resolve(config.toc.menuTemplate),'utf8'); + } + else { + var filesFlattened = []; + + // Iterate over the files and remove the folders out of it + files.files.forEach(function(file) { + // This was actually a folder remove it + if(file && file.docFileName) { + filesFlattened.push(file); + } + }); + + // Set the files to the flattened list + indexData.files = files.files = filesFlattened; + } + + // Register the list partial, this is needed when using it inside the template. When no structured menu is provided the listPartialContent is an empty string. + // That handlebar function is not being used than. When we have a structured menu it contains the content of file provided in the config.json (config.toc.menuTemplate) + Handlebars.registerPartial( "list", indexData.listPartialContent); + // Create the markdown file markdown = template(indexData); + // Set the global files needed when generating the docs for the objects pmd.globalFiles = files; pmd.menuStructure = indexData; + // Save the file to the filesystem fs.writeFileSync(path.resolve(config.folders[0].output.path, config.toc.fileName), markdown); }//config.templates.index }// generateToc diff --git a/templates/package.html b/templates/package.html index d8fc529..a53e081 100644 --- a/templates/package.html +++ b/templates/package.html @@ -235,22 +235,30 @@