diff options
Diffstat (limited to 'lib/parse')
-rw-r--r-- | lib/parse/index.js | 1 | ||||
-rw-r--r-- | lib/parse/lex.js | 74 | ||||
-rw-r--r-- | lib/parse/page.js | 67 |
3 files changed, 81 insertions, 61 deletions
diff --git a/lib/parse/index.js b/lib/parse/index.js index ec98347..0e333e4 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -2,6 +2,7 @@ module.exports = { summary: require('./summary'), langs: require('./langs'), page: require('./page'), + lex: require('./lex'), progress: require('./progress'), navigation: require('./navigation'), }; diff --git a/lib/parse/lex.js b/lib/parse/lex.js new file mode 100644 index 0000000..6b3236e --- /dev/null +++ b/lib/parse/lex.js @@ -0,0 +1,74 @@ +var _ = require('lodash'); +var marked = require('marked'); + +// Split a page up into sections (lesson, exercises, ...) +function splitSections(nodes) { + var section = []; + + return _.reduce(nodes, function(sections, el) { + if(el.type === 'hr') { + sections.push(section); + section = []; + } else { + section.push(el); + } + + return sections; + }, []).concat([section]); // Add remaining nodes +} + +// What is the type of this section +function sectionType(nodes, idx) { + var codeNodes = _.filter(nodes, { + type: 'code' + }).length; + + if(codeNodes === 3 && (idx % 2) == 1) { + return 'exercise'; + } + + return 'normal'; +} + +// Generate a uniqueId to identify this section in our code +function sectionId(section, idx) { + return _.uniqueId('gitbook_'); +} + +function lexPage(src) { + // Lex file + var nodes = marked.lexer(src); + + return _.chain(splitSections(nodes)) + .map(function(section, idx) { + // Detect section type + section.type = sectionType(section, idx); + return section; + }) + .map(function(section, idx) { + // Give each section an ID + section.id = sectionId(section, idx); + return section; + + }) + .filter(function(section) { + return !_.isEmpty(section); + }) + .reduce(function(sections, section) { + var last = _.last(sections); + + // Merge normal sections together + if(last && last.type === section.type && last.type === 'normal') { + last.push.apply(last, [{'type': 'hr'}].concat(section)); + } else { + // Add to list of sections + sections.push(section); + } + + return sections; + }, []) + .value(); +} + +// Exports +module.exports = lexPage; diff --git a/lib/parse/page.js b/lib/parse/page.js index b17f593..56a9e60 100644 --- a/lib/parse/page.js +++ b/lib/parse/page.js @@ -2,6 +2,7 @@ var _ = require('lodash'); var marked = require('marked'); var hljs = require('highlight.js'); +var lex = require('./lex'); var renderer = require('./renderer'); var lnormalize = require('../utils/lang').normalize; @@ -19,35 +20,6 @@ marked.setOptions({ }); -// Split a page up into sections (lesson, exercises, ...) -function splitSections(nodes) { - var section = []; - - return _.reduce(nodes, function(sections, el) { - if(el.type === 'hr') { - sections.push(section); - section = []; - } else { - section.push(el); - } - - return sections; - }, []).concat([section]); // Add remaining nodes -} - -// What is the type of this section -function sectionType(nodes, idx) { - var codeNodes = _.filter(nodes, { - type: 'code' - }).length; - - if(codeNodes === 3 && (idx % 2) == 1) { - return 'exercise'; - } - - return 'normal'; -} - // Render a section using our custom renderer function render(section, _options) { // Copy section @@ -67,35 +39,9 @@ function render(section, _options) { function parsePage(src, options) { options = options || {}; - // Lex file - var nodes = marked.lexer(src); - - return _.chain(splitSections(nodes)) - .map(function(section, idx) { - // Detect section type - section.type = sectionType(section, idx); - return section; - }) - .filter(function(section) { - return !_.isEmpty(section); - }) - .reduce(function(sections, section) { - var last = _.last(sections); - - // Merge normal sections together - if(last && last.type === section.type && last.type === 'normal') { - last.push.apply(last, [{'type': 'hr'}].concat(section)); - } else { - // Add to list of sections - sections.push(section); - } - - return sections; - }, []) + // Lex if not already lexed + return (_.isArray(src) ? src : lex(src)) .map(function(section) { - // Generate a uniqueId to identify this section in our code - var id = _.uniqueId('gitbook_'); - // Transform given type if(section.type === 'exercise') { var nonCodeNodes = _.reject(section, { @@ -118,7 +64,7 @@ function parsePage(src, options) { var lang = validLangs ? langs[0] : null; return { - id: id, + id: section.id, type: section.type, content: render(nonCodeNodes), lang: lang, @@ -132,12 +78,11 @@ function parsePage(src, options) { // Render normal pages return { - id: id, + id: section.id, type: section.type, content: render(section, options) }; - }) - .value(); + }); } // Exports |