diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-06 14:02:49 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-04-06 14:02:49 -0700 |
commit | c98ba22b08759f054748050945a467819e7481a6 (patch) | |
tree | e485f0f3356cb9c0a3027d65c2bed67dce96bfaf /lib/parse/page.js | |
parent | ef059c873753b3cdf42b102fc07b24767c5d0641 (diff) | |
download | gitbook-c98ba22b08759f054748050945a467819e7481a6.zip gitbook-c98ba22b08759f054748050945a467819e7481a6.tar.gz gitbook-c98ba22b08759f054748050945a467819e7481a6.tar.bz2 |
Move page lexer to separate module
Allow lexer to be used independently of page parser/renderer
Diffstat (limited to 'lib/parse/page.js')
-rw-r--r-- | lib/parse/page.js | 59 |
1 files changed, 3 insertions, 56 deletions
diff --git a/lib/parse/page.js b/lib/parse/page.js index b17f593..8bef308 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,31 +39,7 @@ 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; - }, []) + return lex(src) .map(function(section) { // Generate a uniqueId to identify this section in our code var id = _.uniqueId('gitbook_'); @@ -136,8 +84,7 @@ function parsePage(src, options) { type: section.type, content: render(section, options) }; - }) - .value(); + }); } // Exports |