Skip to content

Commit

Permalink
[runmode.node addon] Integrate with node's require mechanism
Browse files Browse the repository at this point in the history
Mode dependencies are now loaded in a saner way.
  • Loading branch information
marijnh committed Feb 6, 2014
1 parent a506d85 commit 7fbe538
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 2 additions & 0 deletions addon/runmode/runmode.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,5 @@ exports.runMode = function(string, modespec, callback, options) {
}
}
};

require.cache[require.resolve("../../lib/codemirror")] = require.cache[require.resolve("./runmode.node")];
16 changes: 3 additions & 13 deletions bin/source-highlight
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

var fs = require("fs");

CodeMirror = require("../addon/runmode/runmode.node.js");
var CodeMirror = require("../addon/runmode/runmode.node.js");
require("../mode/meta.js");

var sPos = process.argv.indexOf("-s");
Expand All @@ -26,18 +26,8 @@ CodeMirror.modeInfo.forEach(function(info) {
}
});

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);
if (!CodeMirror.modes[modeName])
require("../mode/" + modeName + "/" + modeName + ".js");

function esc(str) {
return str.replace(/[<&]/g, function(ch) { return ch == "&" ? "&amp;" : "&lt;"; });
Expand Down
13 changes: 12 additions & 1 deletion mode/meta.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";

CodeMirror.modeInfo = [
{name: 'APL', mime: 'text/apl', mode: 'apl'},
{name: 'Asterisk', mime: 'text/x-asterisk', mode: 'asterisk'},
Expand Down Expand Up @@ -85,8 +95,9 @@ CodeMirror.modeInfo = [
{name: 'Velocity', mime: 'text/velocity', mode: 'velocity'},
{name: 'Verilog', mime: 'text/x-verilog', mode: 'verilog'},
{name: 'XML', mime: 'application/xml', mode: 'xml'},
{name: 'HTML', mime: 'text/html', mode: 'xml'},
{name: 'XQuery', mime: 'application/xquery', mode: 'xquery'},
{name: 'YAML', mime: 'text/x-yaml', mode: 'yaml'},
{name: 'Z80', mime: 'text/x-z80', mode: 'z80'}
];

});

0 comments on commit 7fbe538

Please sign in to comment.