From 642a0ec7397c0bbebafcdc7f241721bc1151ca58 Mon Sep 17 00:00:00 2001 From: Eric Laberge Date: Sun, 21 Jan 2018 13:01:53 -0500 Subject: [PATCH] Added an option to strip path prefixes. --- docco.js | 12 ++++++++---- docco.litcoffee | 9 +++++++-- resources/parallel/docco.jst | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/docco.js b/docco.js index e53b4de3..981d6936 100644 --- a/docco.js +++ b/docco.js @@ -220,9 +220,9 @@ // documentation file by passing the completed HTML sections into the template, // and rendering it to the specified output path. write = function(source, sections, config) { - var css, destination, first, firstSection, hasTitle, html, relative, title; + var css, destination, first, firstSection, hasTitle, html, prefix, relative, title; destination = function(file) { - return path.join(config.output, path.dirname(file), path.basename(file, path.extname(file)) + '.html'); + return path.join(config.output, path.dirname(file).replace(config.prefix, ''), path.basename(file, path.extname(file)) + '.html'); }; relative = function(file) { var from, to; @@ -241,6 +241,7 @@ hasTitle = first && first.type === 'heading' && first.depth === 1; title = hasTitle ? first.text : path.basename(source); css = relative(path.join(config.output, path.basename(config.css))); + prefix = config.prefix; html = config.template({ sources: config.sources, css, @@ -249,7 +250,8 @@ sections, path, destination, - relative + relative, + prefix }); console.log(`docco: ${source} -> ${destination(source)}`); return fs.outputFileSync(destination(source), html); @@ -263,6 +265,7 @@ defaults = { layout: 'parallel', output: 'docs', + prefix: '', template: null, css: null, extension: null, @@ -277,6 +280,7 @@ var config, dir; config = _.extend({}, defaults, _.pick(options, ..._.keys(defaults))); config.languages = buildMatchers(config.languages); + config.prefix = new RegExp(`^${config.prefix}(\/?)`); // The user is able to override the layout file used with the `--template` parameter. // In this case, it is also neccessary to explicitly specify a stylesheet file. // These custom templates are compiled exactly like the predefined ones, but the `public` folder @@ -375,7 +379,7 @@ run = function(args = process.argv) { var c; c = defaults; - commander.version(version).usage('[options] files').option('-L, --languages [file]', 'use a custom languages.json', _.compose(JSON.parse, fs.readFileSync)).option('-l, --layout [name]', 'choose a layout (parallel, linear or classic)', c.layout).option('-o, --output [path]', 'output to a given folder', c.output).option('-c, --css [file]', 'use a custom css file', c.css).option('-t, --template [file]', 'use a custom .jst template', c.template).option('-e, --extension [ext]', 'assume a file extension for all inputs', c.extension).option('-m, --marked [file]', 'use custom marked options', c.marked).parse(args).name = "docco"; + commander.version(version).usage('[options] files').option('-L, --languages [file]', 'use a custom languages.json', _.compose(JSON.parse, fs.readFileSync)).option('-l, --layout [name]', 'choose a layout (parallel, linear or classic)', c.layout).option('-o, --output [path]', 'output to a given folder', c.output).option('-p, --prefix [path]', 'trim file prefix', c.prefix).option('-c, --css [file]', 'use a custom css file', c.css).option('-t, --template [file]', 'use a custom .jst template', c.template).option('-e, --extension [ext]', 'assume a file extension for all inputs', c.extension).option('-m, --marked [file]', 'use custom marked options', c.marked).parse(args).name = "docco"; if (commander.args.length) { return document(commander); } else { diff --git a/docco.litcoffee b/docco.litcoffee index ca1eb3e7..ec3d2832 100644 --- a/docco.litcoffee +++ b/docco.litcoffee @@ -200,7 +200,7 @@ and rendering it to the specified output path. write = (source, sections, config) -> destination = (file) -> - path.join(config.output, path.dirname(file), path.basename(file, path.extname(file)) + '.html') + path.join(config.output, path.dirname(file).replace(config.prefix, ''), path.basename(file, path.extname(file)) + '.html') relative = (file) -> to = path.dirname(path.resolve(file)) @@ -216,9 +216,10 @@ name of the source file. hasTitle = first and first.type is 'heading' and first.depth is 1 title = if hasTitle then first.text else path.basename source css = relative path.join(config.output, path.basename(config.css)) + prefix = config.prefix html = config.template {sources: config.sources, css, - title, hasTitle, sections, path, destination, relative} + title, hasTitle, sections, path, destination, relative, prefix} console.log "docco: #{source} -> #{destination source}" fs.outputFileSync destination(source), html @@ -233,6 +234,7 @@ user-specified options. defaults = layout: 'parallel' output: 'docs' + prefix: '' template: null css: null extension: null @@ -248,6 +250,8 @@ source files for languages for which we have definitions. config.languages = buildMatchers config.languages + config.prefix = new RegExp "^#{config.prefix}(\/?)" + The user is able to override the layout file used with the `--template` parameter. In this case, it is also neccessary to explicitly specify a stylesheet file. These custom templates are compiled exactly like the predefined ones, but the `public` folder @@ -341,6 +345,7 @@ Parse options using [Commander](https://github.com/visionmedia/commander.js). .option('-L, --languages [file]', 'use a custom languages.json', _.compose JSON.parse, fs.readFileSync) .option('-l, --layout [name]', 'choose a layout (parallel, linear or classic)', c.layout) .option('-o, --output [path]', 'output to a given folder', c.output) + .option('-p, --prefix [path]', 'trim file prefix', c.prefix) .option('-c, --css [file]', 'use a custom css file', c.css) .option('-t, --template [file]', 'use a custom .jst template', c.template) .option('-e, --extension [ext]', 'assume a file extension for all inputs', c.extension) diff --git a/resources/parallel/docco.jst b/resources/parallel/docco.jst index 14bfbc72..03aa09b9 100644 --- a/resources/parallel/docco.jst +++ b/resources/parallel/docco.jst @@ -21,7 +21,7 @@ <% for (var i=0, l=sources.length; i <% var source = sources[i]; %> - <%= source %> + <%= source.replace(prefix, '') %> <% } %>