summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2014-04-05 20:06:38 -0700
committerAaron O'Mullan <aaron.omullan@gmail.com>2014-04-05 20:06:38 -0700
commit4547ec73fda25f037bda2692dc9fc66a037cbde9 (patch)
tree85e7b33a023efc41d2a4b3acfe44ad168e2e204e
parent761cfb4a2c68cfcfa5557dd41ca912cb716df05b (diff)
parentcae62676653e9664fd876a37b6363aecd7cfaf5a (diff)
downloadgitbook-4547ec73fda25f037bda2692dc9fc66a037cbde9.zip
gitbook-4547ec73fda25f037bda2692dc9fc66a037cbde9.tar.gz
gitbook-4547ec73fda25f037bda2692dc9fc66a037cbde9.tar.bz2
Merge pull request #31 from GitbookIO/feature/lang
Feature/lang
-rw-r--r--lib/parse/page.js15
-rw-r--r--lib/utils/index.js3
-rw-r--r--lib/utils/lang.js16
-rw-r--r--test/page.js4
4 files changed, 38 insertions, 0 deletions
diff --git a/lib/parse/page.js b/lib/parse/page.js
index 937d84e..75eca9d 100644
--- a/lib/parse/page.js
+++ b/lib/parse/page.js
@@ -4,6 +4,9 @@ var hljs = require('highlight.js');
var renderer = require('./renderer');
+var lnormalize = require('../utils/lang').normalize;
+
+
// Synchronous highlighting with highlight.js
marked.setOptions({
highlight: function (code, lang) {
@@ -100,10 +103,22 @@ function parsePage(src, options) {
'type': 'code'
});
+ // Languages in code blocks
+ var langs = _.pluck(codeNodes, 'lang').map(lnormalize);
+
+ // Check that they are all the same
+ var validLangs = _.all(_.map(langs, function(lang) {
+ return lang && lang === langs[0];
+ }));
+
+ // Main language
+ var lang = validLangs ? langs[0] : null;
+
return {
id: id,
type: section.type,
content: render(nonCodeNodes),
+ lang: lang,
code: {
base: codeNodes[0].text,
solution: codeNodes[1].text,
diff --git a/lib/utils/index.js b/lib/utils/index.js
new file mode 100644
index 0000000..155e723
--- /dev/null
+++ b/lib/utils/index.js
@@ -0,0 +1,3 @@
+module.exports = {
+ lang: require('./lang'),
+};
diff --git a/lib/utils/lang.js b/lib/utils/lang.js
new file mode 100644
index 0000000..7fd71e1
--- /dev/null
+++ b/lib/utils/lang.js
@@ -0,0 +1,16 @@
+var MAP = {
+ 'py': 'python',
+ 'js': 'javascript',
+ 'rb': 'ruby',
+};
+
+function normalize(lang) {
+ var lower = lang.toLowerCase();
+ return MAP[lower] || lower;
+}
+
+// Exports
+module.exports = {
+ normalize: normalize,
+ MAP: MAP
+};
diff --git a/test/page.js b/test/page.js
index 68c0749..0f54809 100644
--- a/test/page.js
+++ b/test/page.js
@@ -45,6 +45,10 @@ describe('Page parsing', function() {
// HRs inserted correctly
assert.equal(HR_LEXED[0].content.match(/<hr>/g).length, 2);
});
+
+ it('should detect an exercise\'s language', function() {
+ assert.equal(LEXED[1].lang, 'python');
+ });
});