summaryrefslogtreecommitdiffstats
path: root/lib/parse/lex.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-19 09:47:36 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-19 09:47:36 +0100
commitec586dd3cdf06e9567f5d3e4961022ddc3c94778 (patch)
treecc7825ab73110b4e6fbedee404427b052edffa17 /lib/parse/lex.js
parent80432161708357bdcf0e00533d9e6d327636dab6 (diff)
downloadgitbook-ec586dd3cdf06e9567f5d3e4961022ddc3c94778.zip
gitbook-ec586dd3cdf06e9567f5d3e4961022ddc3c94778.tar.gz
gitbook-ec586dd3cdf06e9567f5d3e4961022ddc3c94778.tar.bz2
Clear folder
Diffstat (limited to 'lib/parse/lex.js')
-rw-r--r--lib/parse/lex.js79
1 files changed, 0 insertions, 79 deletions
diff --git a/lib/parse/lex.js b/lib/parse/lex.js
deleted file mode 100644
index 3391acf..0000000
--- a/lib/parse/lex.js
+++ /dev/null
@@ -1,79 +0,0 @@
-var _ = require('lodash');
-var kramed = require('kramed');
-
-var isExercise = require('./is_exercise');
-var isQuiz = require('./is_quiz');
-
-// 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) {
- if(isExercise(nodes)) {
- return 'exercise';
- } else if(isQuiz(nodes)) {
- return 'quiz';
- }
-
- 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 = kramed.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;
- }, [])
- .map(function(section) {
- section.links = nodes.links;
- return section;
- })
- .value();
-}
-
-// Exports
-module.exports = lexPage;