diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/generate/index.js | 1 | ||||
-rw-r--r-- | lib/generate/template.js | 3 | ||||
-rw-r--r-- | lib/parse/index.js | 2 | ||||
-rw-r--r-- | lib/parse/progress.js | 38 |
4 files changed, 41 insertions, 3 deletions
diff --git a/lib/generate/index.js b/lib/generate/index.js index b9edcc4..07b62d0 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -7,7 +7,6 @@ var fs = require("./fs"); var parse = require("../parse"); var template = require("./template"); - var generate = function(root, output, options) { var files, summary, navigation, tpl; diff --git a/lib/generate/template.js b/lib/generate/template.js index 0fad90c..d8c1f79 100644 --- a/lib/generate/template.js +++ b/lib/generate/template.js @@ -49,7 +49,8 @@ var initTemplate = function(options) { content: sections, basePath: basePath, staticBase: path.join(basePath, "gitbook"), - navigation: options.locals.allNavigation[_output] + navigation: options.locals.allNavigation[_output], + progress: parse.progress(options.locals.allNavigation, _output) }) ); }) diff --git a/lib/parse/index.js b/lib/parse/index.js index f6593ef..951e401 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -1,6 +1,6 @@ module.exports = { summary: require('./summary'), page: require('./page'), - + progress: require('./progress'), navigation: require('./navigation'), }; diff --git a/lib/parse/progress.js b/lib/parse/progress.js new file mode 100644 index 0000000..8088aa8 --- /dev/null +++ b/lib/parse/progress.js @@ -0,0 +1,38 @@ +var _ = require("lodash"); + +var calculProgress = function(navigation, current) { + var n = _.size(navigation); + var percent = 0; + var done = true; + + var chapters = _.chain(navigation) + .map(function(nav, path) { + nav.path = path; + return nav; + }) + .sortBy(function(nav) { + return nav.level; + }) + .map(function(nav, i) { + // Calcul percent + nav.percent = (i * 100) / Math.max((n - 1), 1); + + // Is it done + nav.done = done; + if (nav.path == current) { + percent = nav.percent; + done = false; + } + + return nav; + }) + .value(); + + + return { + percent: percent, + chapters: chapters + }; +} + +module.exports = calculProgress;
\ No newline at end of file |