summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-19 10:26:48 +0100
committerSamy Pessé <samypesse@gmail.com>2016-12-22 15:00:28 +0100
commit58b771ec31c556c83f4ae8f6f1e08610b6ced082 (patch)
tree462baee9a94c588af6c4178d9e38899b9697ff30
parent3f1041209582c26abc595be9c2c99189f5b85f3f (diff)
downloadgitbook-58b771ec31c556c83f4ae8f6f1e08610b6ced082.zip
gitbook-58b771ec31c556c83f4ae8f6f1e08610b6ced082.tar.gz
gitbook-58b771ec31c556c83f4ae8f6f1e08610b6ced082.tar.bz2
Add glossary parser and associated tests
-rw-r--r--packages/gitbook-markdown/lib/glossary.js48
-rw-r--r--packages/gitbook-markdown/lib/index.js3
-rw-r--r--packages/gitbook-markdown/test/fixtures/GLOSSARY.md30
-rw-r--r--packages/gitbook-markdown/test/glossary.js20
-rw-r--r--packages/gitbook-markdown/test/summary.js3
5 files changed, 100 insertions, 4 deletions
diff --git a/packages/gitbook-markdown/lib/glossary.js b/packages/gitbook-markdown/lib/glossary.js
new file mode 100644
index 0000000..549e9fd
--- /dev/null
+++ b/packages/gitbook-markdown/lib/glossary.js
@@ -0,0 +1,48 @@
+var _ = require('lodash');
+var kramed = require('kramed');
+
+// Get all the pairs of header + paragraph in a list of nodes
+function groups(nodes) {
+ // A list of next nodes
+ var next = nodes.slice(1).concat(null);
+
+ return _.reduce(nodes, function(accu, node, idx) {
+ // Skip
+ if(!(
+ node.type === 'heading' &&
+ (next[idx] && next[idx].type === 'paragraph')
+ )) {
+ return accu;
+ }
+
+ // Add group
+ accu.push([
+ node,
+ next[idx]
+ ]);
+
+ return accu;
+ }, []);
+}
+
+function parseGlossary(src) {
+ var nodes = kramed.lexer(src);
+
+ return groups(nodes)
+ .map(function(pair) {
+ // Simplify each group to a simple object with name/description
+ return {
+ name: pair[0].text,
+ id: entryId(pair[0].text),
+ description: pair[1].text,
+ };
+ });
+}
+
+// Normalizes a glossary entry's name to create an ID
+function entryId(name) {
+ return name.toLowerCase();
+}
+
+module.exports = parseGlossary;
+module.exports.entryId = entryId;
diff --git a/packages/gitbook-markdown/lib/index.js b/packages/gitbook-markdown/lib/index.js
index 15a7f54..54381a3 100644
--- a/packages/gitbook-markdown/lib/index.js
+++ b/packages/gitbook-markdown/lib/index.js
@@ -3,6 +3,7 @@ var _ = require("lodash");
module.exports = {
parse: {
- summary: require("./summary")
+ summary: require("./summary"),
+ glossary: require("./glossary")
}
};
diff --git a/packages/gitbook-markdown/test/fixtures/GLOSSARY.md b/packages/gitbook-markdown/test/fixtures/GLOSSARY.md
new file mode 100644
index 0000000..5969902
--- /dev/null
+++ b/packages/gitbook-markdown/test/fixtures/GLOSSARY.md
@@ -0,0 +1,30 @@
+# Magic
+Sufficiently advanced technology, beyond the understanding of the observer producing a sense of wonder.
+
+Hello, I am random noise in the middle of this beautiful Glossary. (Really astonishing !)
+
+# PHP
+An atrocious language, invented for the sole purpose of inflicting pain and suffering amongst the proframming wizards of this world.
+
+# Clojure
+Lisp re-invented for hipsters.
+
+# Go
+Go Go Google [Wow](https://www.google.com)
+
+Fantastic, I love code too ! :
+
+```py
+
+def f(x):
+ return x * 4
+
+# Wow this is some really awesome code
+# totally mind blowing
+# but we don't care, it shouldn't be in our glossary !
+print(f(9))
+```
+
+# Gitbook
+
+Awesome project. Really amazing, I'm really at a loss for words ...
diff --git a/packages/gitbook-markdown/test/glossary.js b/packages/gitbook-markdown/test/glossary.js
new file mode 100644
index 0000000..bf40e16
--- /dev/null
+++ b/packages/gitbook-markdown/test/glossary.js
@@ -0,0 +1,20 @@
+var fs = require('fs');
+var path = require('path');
+var assert = require('assert');
+
+var glossary = require('../').parse.glossary;
+
+var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/GLOSSARY.md'), 'utf8');
+var LEXED = glossary(CONTENT);
+
+describe('Glossary parsing', function () {
+ it('should only get heading + paragraph pairs', function() {
+ assert.equal(LEXED.length, 5);
+ });
+
+ it('should output simple name/description objects', function() {
+ assert.equal(true, !(LEXED.some(function(e) {
+ return !Boolean(e.name && e.description);
+ })));
+ });
+});
diff --git a/packages/gitbook-markdown/test/summary.js b/packages/gitbook-markdown/test/summary.js
index 2993817..18018b7 100644
--- a/packages/gitbook-markdown/test/summary.js
+++ b/packages/gitbook-markdown/test/summary.js
@@ -4,13 +4,10 @@ var assert = require('assert');
var summary = require('../').parse.summary;
-
var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/SUMMARY.md'), 'utf8');
var LEXED = summary(CONTENT);
-
describe('Summary parsing', function () {
-
it('should detect chapters', function() {
assert.equal(LEXED.chapters.length, 6);
});