Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ASCIIMathTeXImg.js UMD compatible and export as NodeJS package #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@ simple calculator-style math expressions on a webpage to MathML.
The resulting page can be displayed with any browser that can render MathML.
Currently that includes any recent version of Firefox (3+).


NodeJS/CommonJS/Webpack Usage
-----------------------------

```js
// You can convert ASCIIMath to LaTeX using NodeJS/CommonJS/Webpack!
const am = require("asciimath")
am.parse("a / b") // Returns equivalent latex '\\frac{{a}}{{b}}'

// Changing decimal sign
// (only relevant property from config, everything else relates to DOM)
am.parse("1,2/3") // Parsed as two numbers '{1},\\frac{{2}}{{3}}', i.e. 1, 2/3
am.config.decimalsign = ","
am.parse("1,2/3") // Parsed as one number '\\frac{{1,2}}{{3}}', i.e. 1.2 / 3 or 1,2 / 3
```
43 changes: 39 additions & 4 deletions asciimath-based/ASCIIMathTeXImg.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,24 @@ THE SOFTWARE.
*/

//var AMTcgiloc = ''; //set to the URL of your LaTex renderer
var noMathRender = false;

(function() {
// UMD export from https://github.com/umdjs/umd/blob/master/templates/returnExports.js
// if the module has no dependencies, the above pattern can be simplified to
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {

var config = {
translateOnLoad: true, //true to autotranslate
mathcolor: "", // defaults to back, or specify any other color
Expand Down Expand Up @@ -873,6 +888,28 @@ function AMTparseAMtoTeX(str) {
return AMTparseExpr(str.replace(/^\s+/g,""),false)[0];
}

AMinitSymbols();

return {
parse: AMTparseAMtoTeX,
config: config,
}

}));


(function() {

// Return if AMD, CommonJS or not browser
// I.e. only run when imported using "script" tag
if (typeof define === 'function' && define.amd) return;
if (typeof module === 'object' && module.exports) return;
if (typeof window === 'undefined') return; // Not browser

// "Import" UMD exports. Exported as window.* later
var AMTparseAMtoTeX = window.returnExports.parse;
var config = window.returnExports.config;

function AMparseMath(str) {
//DLMOD to remove  , which editor adds on multiple spaces
str = str.replace(/( |\u00a0| )/g,"");
Expand Down Expand Up @@ -1015,8 +1052,6 @@ var AMbody;
var AMtranslated = false;
var AMnoMathML = true;

AMinitSymbols();

window.translate = translate;
window.AMTconfig = config;
window.AMprocessNode = AMprocessNode;
Expand Down
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "asciimath",
"version": "1.0.0",
"description": "ASCIIMath parser and TeX renderer",
"main": "asciimath-based/ASCIIMathTeXImg.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/asciimath/asciimathml.git"
},
"keywords": [
"ASCIIMath",
"LaTeX",
"math"
],
"author": "David Lippman",
"license": "MIT",
"bugs": {
"url": "https://github.com/asciimath/asciimathml/issues"
},
"homepage": "https://github.com/asciimath/asciimathml#readme"
}