diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-21 10:53:15 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 11:46:14 +0100 |
commit | 7e21dbde1409471590c86c01196f596aa7e5d80f (patch) | |
tree | 8b3284a590356f298bd4d7626b349dedc5da22d1 | |
parent | a81a503ff0bbbf66f6fefb7090f8516071ae817a (diff) | |
download | gitbook-7e21dbde1409471590c86c01196f596aa7e5d80f.zip gitbook-7e21dbde1409471590c86c01196f596aa7e5d80f.tar.gz gitbook-7e21dbde1409471590c86c01196f596aa7e5d80f.tar.bz2 |
Fix parsing of summary
-rwxr-xr-x | packages/gitbook-asciidoc/lib/summary.js | 70 | ||||
-rwxr-xr-x | packages/gitbook-asciidoc/test/summary.js | 8 |
2 files changed, 72 insertions, 6 deletions
diff --git a/packages/gitbook-asciidoc/lib/summary.js b/packages/gitbook-asciidoc/lib/summary.js index 67e11f3..17c9834 100755 --- a/packages/gitbook-asciidoc/lib/summary.js +++ b/packages/gitbook-asciidoc/lib/summary.js @@ -3,11 +3,77 @@ var cheerio = require('cheerio'); var convert = require('./utils/convert'); + +// parse a ul list and return list of chapters recursvely +function parseList($ul, $) { + var articles = []; + + $ul.children("li").each(function() { + var article = {}; + + var $li = $(this); + var $p = $li.children("p"); + + article.title = $p.text(); + + // Parse link + var $a = $p.children("a"); + if ($a.length > 0) { + article.title = $a.first().text(); + article.path = $a.attr("href").replace(/\\/g, '/').replace(/^\/+/, '') + } + + // Sub articles + var $sub = $li.children(".olist").children("ol"); + article.articles = parseList($sub, $); + + articles.push(article); + }); + + return articles; +} + +function defaultChapterList(chapterList, entryPoint) { + var first = _.first(chapterList); + + // Check if introduction node was specified in SUMMARY.md + if (first && first.path == entryPoint) { + return chapterList; + } + + // It wasn't specified, so add in default + return [ + { + path: entryPoint, + title: 'Introduction' + } + ].concat(chapterList); +} + +function parseChaptersLevel(chapterList, level, base) { + var i = base || 0; + return _.map(chapterList, function(chapter) { + chapter.level = (level? [level || "", i] : [i]).join("."); + chapter.article = parseChaptersLevel(chapter.articles || [], chapter.level, 1); + + i = i + 1; + return chapter; + }); +}; + function parseSummary(src, entryPoint) { + entryPoint = entryPoint || "README.adoc"; + var html = convert(src); $ = cheerio.load(html); - console.log(html); - return []; + + var chapters = parseList($("ol").first(), $); + chapters = defaultChapterList(chapters, entryPoint); + chapters = parseChaptersLevel(chapters); + + return { + chapters: chapters + }; } function parseEntries (src) { diff --git a/packages/gitbook-asciidoc/test/summary.js b/packages/gitbook-asciidoc/test/summary.js index 5c9bbcc..5115bb1 100755 --- a/packages/gitbook-asciidoc/test/summary.js +++ b/packages/gitbook-asciidoc/test/summary.js @@ -35,10 +35,10 @@ describe('Summary parsing', function () { }); it('should normalize paths from .md', function() { - assert.equal(LEXED.chapters[0].path,'README.md'); - assert.equal(LEXED.chapters[1].path,'chapter-1/README.md'); - assert.equal(LEXED.chapters[2].path,'chapter-2/README.md'); - assert.equal(LEXED.chapters[3].path,'chapter-3/README.md'); + assert.equal(LEXED.chapters[0].path,'README.adoc'); + assert.equal(LEXED.chapters[1].path,'chapter-1/README.adoc'); + assert.equal(LEXED.chapters[2].path,'chapter-2/README.adoc'); + assert.equal(LEXED.chapters[3].path,'chapter-3/README.adoc'); }); it('should detect levels correctly', function() { |