diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-06 15:03:27 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-06 15:03:27 -0700 |
commit | 74edc4f279c8748f072efe3b090a2414b351d697 (patch) | |
tree | 8e06317bc24bbfef142a01d6eadb9ef32d333c2f /lib/parse/lex.js | |
parent | 52ccc2b46dbcec3fac7dd412ca05ccb8c2e26dc5 (diff) | |
parent | 696cbdc3b13905498e2832d43790567ac48799d0 (diff) | |
download | gitbook-74edc4f279c8748f072efe3b090a2414b351d697.zip gitbook-74edc4f279c8748f072efe3b090a2414b351d697.tar.gz gitbook-74edc4f279c8748f072efe3b090a2414b351d697.tar.bz2 |
Merge pull request #39 from GitbookIO/feature/search
Feature/search
Diffstat (limited to 'lib/parse/lex.js')
-rw-r--r-- | lib/parse/lex.js | 74 |
1 files changed, 74 insertions, 0 deletions
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; |