Use browserify and "modules"

Reviewers: xymostech

Reviewed By: xymostech

Differential Revision: http://phabricator.benalpert.com/D37
This commit is contained in:
Ben Alpert
2013-07-05 21:44:44 -07:00
parent 97dc1bf1da
commit afb29f5df3
6 changed files with 102 additions and 100 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
parser.js
build
node_modules
parser.js

View File

@@ -1,14 +1,14 @@
var MJLite = (function() {
var parser = require("./parser");
var buildExpression = function(expression) {
var buildExpression = function(expression) {
return _.map(expression, function(ex, i) {
var prev = i > 0 ? expression[i-1] : null;
return buildGroup(ex, prev);
});
};
};
var makeSpan = function(className, children) {
var makeSpan = function(className, children) {
var span = document.createElement("span");
span.className = className;
@@ -21,9 +21,9 @@ var MJLite = (function() {
}
return span;
};
};
var buildGroup = function(group, prev) {
var buildGroup = function(group, prev) {
if (group.type === "ord") {
return makeSpan("mord", mathit(group.value));
} else if (group.type === "bin") {
@@ -59,9 +59,9 @@ var MJLite = (function() {
} else {
console.log("Unknown type:", group.type);
}
};
};
var charLookup = {
var charLookup = {
"*": "\u2217",
"-": "\u2212",
"cdot": "\u22C5",
@@ -69,16 +69,16 @@ var MJLite = (function() {
"rvert": "|",
"pm": "\u00b1",
"div": "\u00f7"
};
};
var textit = function(value) {
var textit = function(value) {
if (value in charLookup) {
value = charLookup[value];
}
return document.createTextNode(value);
};
};
var mathit = function(value) {
var mathit = function(value) {
var text = textit(value);
if (/[a-zA-Z]/.test(value)) {
@@ -86,26 +86,24 @@ var MJLite = (function() {
} else {
return text;
}
};
};
var clearNode = function(node) {
var clearNode = function(node) {
if ("textContent" in node) {
node.textContent = "";
} else {
node.innerText = "";
}
};
};
var process = function(toParse, baseElem) {
var process = function(toParse, baseElem) {
var tree = parser.parse(toParse);
clearNode(baseElem);
_.each(buildExpression(tree), function(elem) {
baseElem.appendChild(elem);
});
};
};
return {
module.exports = {
process: process
};
})();
};

View File

@@ -1,6 +1,6 @@
FILES=parser.js style.css build.js index.html
.PHONY: build ship copy
.PHONY: build ship copy watch
build: parser.js
ship: build
@@ -13,3 +13,6 @@ copy: build
parser.js: parser.jison
./node_modules/.bin/jison parser.jison
watch:
./node_modules/.bin/watchify MJLite.js --standalone MJLite -o build/MJLite.js

0
build/.gitkeep Normal file
View File

View File

@@ -3,8 +3,7 @@
<head>
<title>MJLite Test</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js" type="text/javascript"></script>
<script src="parser.js" type="text/javascript"></script>
<script src="MJLite.js" type="text/javascript"></script>
<script src="build/MJLite.js" type="text/javascript"></script>
<script src="main.js" type="text/javascript"></script>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

View File

@@ -2,6 +2,7 @@
"name": "mjlite",
"version": "0.0.1",
"devDependencies": {
"jison": "~0.4.6"
"jison": "~0.4.6",
"watchify": "~0.1.0"
}
}