Permalink
Browse files

Working server side version

  • Loading branch information...
1 parent ac48469 commit 3ea77cb72d318d428aad09c5653bb88c041183b5 @ForbesLindesay committed Jan 5, 2013
Showing with 551 additions and 39 deletions.
  1. +17 −14 .gitignore
  2. +40 −4 README.md
  3. +17 −0 component.json
  4. +32 −21 index.js
  5. +273 −0 lib/escape.js
  6. +107 −0 lib/math-ml.js
  7. +15 −0 package.json
  8. +30 −0 test/index.html
  9. +18 −0 test/index.js
  10. +2 −0 test/server-generated.html
View
@@ -1,14 +1,17 @@
-lib-cov
-*.seed
-*.log
-*.csv
-*.dat
-*.out
-*.pid
-*.gz
-
-pids
-logs
-results
-
-npm-debug.log
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+npm-debug.log
+node_modules
+components
+build
View
@@ -1,4 +1,40 @@
-ascii-math
-==========
-
-node.js version of http://www1.chapman.edu/~jipsen/mathml/asciimath.html
+# ascii-math
+
+ This is an implimentation of [asciimath](http://www1.chapman.edu/~jipsen/mathml/asciimath.html). It lets you write plain text maths and have it be nicely formatted for you before display. Since it's a server side sollution it offers a much nicer user experience than MathJax or the original asciimath did. You can also use this function on the client if you prefer.
+
+## Installation
+
+ Server:
+
+ $ npm install ascii-math
+
+ Client:
+
+ $ component install ForbesLindesay/ascii-math
+
+## Server side usage
+
+ On the server you can generate text that can be placed inline straight into an html document.
+
+```javascript
+var parse = require('ascii-math');
+
+var res = parse('e^(i pi)=-1').toString();
+// => "<math title="e^(i pi)=-1"><msup><mi>e</mi><mrow><mi>i</mi><mi>&pi;</mi></mrow></msup><mo>=</mo><mo>-</mo><mn>1</mn></math>"
+```
+
+## Client side usage
+
+ On the client it is recommended that you use `.toElement()` but `.toString()` still works.
+
+```javascript
+var parse = require('ascii-math');
+
+var resA = parse('e^(i pi)=-1').toString();
+// => "<math title="e^(i pi)=-1"><msup><mi>e</mi><mrow><mi>i</mi><mi>&pi;</mi></mrow></msup><mo>=</mo><mo>-</mo><mn>1</mn></math>"
+div.innerHTML = resA;
+
+var resB = parse('e^(i pi)=-1').toElement();//this method fails on the server
+// => <math title="e^(i pi)=-1"><msup><mi>e</mi><mrow><mi>i</mi><mi>&pi;</mi></mrow></msup><mo>=</mo><mo>-</mo><mn>1</mn></math>
+div.appendChild(resB);
+```
View
@@ -0,0 +1,17 @@
+{
+ "name": "ascii-math",
+ "repo": "ForbesLindesay/ascii-math",
+ "description": "",
+ "version": "0.0.1",
+ "keywords": [],
+ "dependencies": {},
+ "development": {},
+ "license": "MIT",
+ "scripts": [
+ "index.js",
+ "lib/symbols.js",
+ "lib/token-types.js",
+ "lib/math-ml.js",
+ "lib/escape.js"
+ ]
+}
View
@@ -1,3 +1,24 @@
+var ml = require('./lib/math-ml');
+
+var tokenTypes = require('./lib/token-types');
+var CONST = tokenTypes.CONST;
+var UNARY = tokenTypes.UNARY;
+var BINARY = tokenTypes.BINARY;
+var INFIX = tokenTypes.INFIX;
+var LEFTBRACKET = tokenTypes.LEFTBRACKET;
+var RIGHTBRACKET = tokenTypes.RIGHTBRACKET;
+var SPACE = tokenTypes.SPACE;
+var UNDEROVER = tokenTypes.UNDEROVER;
+var DEFINITION = tokenTypes.DEFINITION;
+var LEFTRIGHT = tokenTypes.LEFTRIGHT;
+var TEXT = tokenTypes.TEXT;
+
+var AMsymbols = require('./lib/symbols');
+var AMnames = AMsymbols.map(function (symbol) {
+ return symbol.input;
+}); //list of input symbols
+
+module.exports = parseMath;
function parseMath(str) {
var frag, node;
AMnestingDepth = 0;
@@ -7,16 +28,17 @@ function parseMath(str) {
return node;
}
-module.exports = parseMath;
+var useFakes = true;
function createMmlNode(t, frag) {
- var node = document.createElementNS("http://www.w3.org/1998/Math/MathML", t);
- if (typeof frag === 'string') frag = document.createTextNode(frag);
+ var node = useFakes ? new ml.Node(t) : document.createElementNS("http://www.w3.org/1998/Math/MathML", t);
+ if (typeof frag === 'string') frag = useFakes ? new ml.Text(frag) : document.createTextNode(frag);
if (frag) node.appendChild(frag);
return node;
}
+
function createFragment() {
- return document.createDocumentFragment();
+ return useFakes ? new ml.Node('fragment') : document.createDocumentFragment();
}
var AMquote = {
@@ -27,21 +49,7 @@ var AMquote = {
ttype: TEXT
};
-var tokenTypes = require('./token-types');
-var CONST = tokenTypes.CONST;
-var UNARY = tokenTypes.UNARY;
-var BINARY = tokenTypes.BINARY;
-var INFIX = tokenTypes.INFIX;
-var LEFTBRACKET = tokenTypes.LEFTBRACKET;
-var RIGHTBRACKET = tokenTypes.RIGHTBRACKET;
-var SPACE = tokenTypes.SPACE;
-var UNDEROVER = tokenTypes.UNDEROVER;
-var DEFINITION = tokenTypes.DEFINITION;
-var LEFTRIGHT = tokenTypes.LEFTRIGHT;
-var TEXT = tokenTypes.TEXT;
-var AMsymbols = require('./symbols');
-var AMnames = AMsymbols.map(function (symbol) { return symbol.input; }); //list of input symbols
function AMremoveCharsAndBlanks(str, n) {
//remove n characters and any following blanks
@@ -350,7 +358,7 @@ function AMparseIexpr(str) {
function AMparseExpr(str, rightbracket) {
var symbol, node, result, i,
- newFrag = createFragment();
+ newFrag = createFragment();
do {
str = AMremoveCharsAndBlanks(str, 0);
result = AMparseIexpr(str);
@@ -385,8 +393,11 @@ function AMparseExpr(str, rightbracket) {
pos[i] = [];
node = newFrag.childNodes[i];
if (matrix) matrix = node.nodeName == "mrow" && (i == m - 1 || node.nextSibling.nodeName == "mo" && node.nextSibling.firstChild.nodeValue == ",") && node.firstChild.firstChild.nodeValue == left && node.lastChild.firstChild.nodeValue == right;
- if (matrix) for (var j = 0; j < node.childNodes.length; j++)
- if (node.childNodes[j].firstChild.nodeValue == ",") pos[i][pos[i].length] = j;
+ if (matrix) {
+ for (var j = 0; j < node.childNodes.length; j++) {
+ if (node.childNodes[j].firstChild.nodeValue == ",") pos[i][pos[i].length] = j;
+ }
+ }
if (matrix && i > 1) matrix = pos[i].length == pos[i - 2].length;
}
if (matrix) {
Oops, something went wrong.

0 comments on commit 3ea77cb

Please sign in to comment.