summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@friendco.de>2014-03-30 21:30:33 -0700
committerAaron O'Mullan <aaron.omullan@friendco.de>2014-03-30 21:30:33 -0700
commit39a80421fc2bffe255cbfa06aea4d74876fb50b6 (patch)
treed3a4c5748cff888e33c99288323c8d48968f7330
parent1d28ab441a9f64357804ca7f983cbe884239e6a3 (diff)
downloadgitbook-39a80421fc2bffe255cbfa06aea4d74876fb50b6.zip
gitbook-39a80421fc2bffe255cbfa06aea4d74876fb50b6.tar.gz
gitbook-39a80421fc2bffe255cbfa06aea4d74876fb50b6.tar.bz2
Add summary parsing
-rw-r--r--lib/index.js3
-rw-r--r--lib/summary.js92
2 files changed, 95 insertions, 0 deletions
diff --git a/lib/index.js b/lib/index.js
new file mode 100644
index 0000000..8fbc605
--- /dev/null
+++ b/lib/index.js
@@ -0,0 +1,3 @@
+module.exports = {
+ summary: require('./summary')
+};
diff --git a/lib/summary.js b/lib/summary.js
new file mode 100644
index 0000000..37e920a
--- /dev/null
+++ b/lib/summary.js
@@ -0,0 +1,92 @@
+var _ = require('lodash');
+var marked = require('marked');
+
+
+// Utility function for splitting a list into groups
+function splitBy(list, starter, ender) {
+ var starts = 0;
+ var ends = 0;
+ var group = [];
+
+ // Groups
+ return _.reduce(list, function(groups, value) {
+ // Ignore start and end delimiters in resulted groups
+ if(starter(value)) {
+ starts++;
+ } else if(ender(value)) {
+ ends++;
+ }
+
+ // Add current value to group
+ group.push(value);
+
+ // We've got a matching
+ if(starts === ends && starts !== 0) {
+ // Add group to end groups
+ // (remove starter and ender token)
+ groups.push(group.slice(1, -1));
+
+ // Reset group
+ group = [];
+ }
+
+ return groups;
+ }, []);
+}
+
+function listSplit(nodes, start_type, end_type) {
+ return splitBy(nodes, function(el) {
+ return el.type === start_type;
+ }, function(el) {
+ return el.type === end_type;
+ });
+}
+
+// Get the biggest list
+// out of a list of marked nodes
+function filterList(nodes) {
+ return _.chain(nodes)
+ .toArray()
+ .rest(function(el) {
+ // Get everything after list_start
+ return el.type !== 'list_start';
+ })
+ .reverse()
+ .rest(function(el) {
+ // Get everything after list_end (remember we're reversed)
+ return el.type !== 'list_end';
+ })
+ .reverse()
+ .value().slice(1, -1);
+}
+
+function parseArticle(nodes) {
+ return _.first(nodes).text;
+}
+
+function parseChapter(nodes) {
+ return {
+ chapter: _.first(nodes).text,
+ articles: _.map(listSplit(filterList(nodes), 'list_item_start', 'list_item_end'), parseArticle)
+ };
+}
+
+function parseSummary(src) {
+ var nodes = marked.lexer(src);
+
+ // Get out list of chapters
+ var chapterList = filterList(nodes);
+
+ // Split out chapter sections
+ var chapters = _.chain(listSplit(chapterList, 'list_item_start', 'list_item_end'))
+ .map(parseChapter)
+ .value();
+
+ return {
+ chapters: chapters
+ };
+}
+
+
+// Exports
+module.exports = parseSummary;