From 58bee15f376e5e56477d4e083493d9b6213fbc63 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Sat, 5 Apr 2014 20:04:24 -0700 Subject: [PATCH] Add basic code language normalization --- lib/parse/page.js | 5 ++++- lib/utils/index.js | 3 +++ lib/utils/lang.js | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 lib/utils/index.js create mode 100644 lib/utils/lang.js diff --git a/lib/parse/page.js b/lib/parse/page.js index cfe1da801..75eca9db0 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) { @@ -101,7 +104,7 @@ function parsePage(src, options) { }); // Languages in code blocks - var langs = _.pluck(codeNodes, 'lang'); + var langs = _.pluck(codeNodes, 'lang').map(lnormalize); // Check that they are all the same var validLangs = _.all(_.map(langs, function(lang) { diff --git a/lib/utils/index.js b/lib/utils/index.js new file mode 100644 index 000000000..155e72303 --- /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 000000000..7fd71e1fe --- /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 +};