X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;ds=sidebyside;f=to-be-integrated%2Fthird-party%2Fcodemirror-3.15%2Fbin%2Fsource-highlight;fp=to-be-integrated%2Fthird-party%2Fcodemirror-3.15%2Fbin%2Fsource-highlight;h=7596ed776ca04d432d05b52867cdfbd99ce62cc2;hb=31540dd504798e0aca69e10d8144fbedc5b16af8;hp=0000000000000000000000000000000000000000;hpb=937653fd70bbf7d95bcf870e7f2b46b4a8fec486;p=myslice.git diff --git a/to-be-integrated/third-party/codemirror-3.15/bin/source-highlight b/to-be-integrated/third-party/codemirror-3.15/bin/source-highlight new file mode 100755 index 00000000..7596ed77 --- /dev/null +++ b/to-be-integrated/third-party/codemirror-3.15/bin/source-highlight @@ -0,0 +1,61 @@ +#!/usr/bin/env node + +// Simple command-line code highlighting tool. Reads code from stdin, +// spits html to stdout. For example: +// +// echo 'function foo(a) { return a; }' | bin/source-highlight -s javascript +// bin/source-highlight -s + +var fs = require("fs"); + +CodeMirror = require("../addon/runmode/runmode.node.js"); +require("../mode/meta.js"); + +var sPos = process.argv.indexOf("-s"); +if (sPos == -1 || sPos == process.argv.length - 1) { + console.error("Usage: source-highlight -s language"); + process.exit(1); +} +var lang = process.argv[sPos + 1].toLowerCase(), modeName = lang; +CodeMirror.modeInfo.forEach(function(info) { + if (info.mime == lang) { + modeName = info.mode; + } else if (info.name.toLowerCase() == lang) { + modeName = info.mode; + lang = info.mime; + } +}); + +function ensureMode(name) { + if (CodeMirror.modes[name] || name == "null") return; + try { + require("../mode/" + name + "/" + name + ".js"); + } catch(e) { + console.error("Could not load mode " + name + "."); + process.exit(1); + } + var obj = CodeMirror.modes[name]; + if (obj.dependencies) obj.dependencies.forEach(ensureMode); +} +ensureMode(modeName); + +function esc(str) { + return str.replace(/[<&]/, function(ch) { return ch == "&" ? "&" : "<"; }); +} + +var code = fs.readFileSync("/dev/stdin", "utf8"); +var curStyle = null, accum = ""; +function flush() { + if (curStyle) process.stdout.write("" + esc(accum) + ""); + else process.stdout.write(esc(accum)); +} + +CodeMirror.runMode(code, lang, function(text, style) { + if (style != curStyle) { + flush(); + curStyle = style; accum = text; + } else { + accum += text; + } +}); +flush();