diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-01 01:21:42 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-01 01:21:44 -0700 |
commit | 256d1ba35a74777b6bdd9d937809798a0cfe3c20 (patch) | |
tree | 7be827ab14a85c307afc40d6ff0a176b14116173 /lib | |
parent | cc340db0faa412edec47a7d37ce591501ab454fb (diff) | |
download | gitbook-256d1ba35a74777b6bdd9d937809798a0cfe3c20.zip gitbook-256d1ba35a74777b6bdd9d937809798a0cfe3c20.tar.gz gitbook-256d1ba35a74777b6bdd9d937809798a0cfe3c20.tar.bz2 |
Add progress calcul
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 |