Skip to content

Commit

Permalink
Merge pull request #7 from wikimedia/mathrm
Browse files Browse the repository at this point in the history
Use mathrm instead of mbox for text literals
  • Loading branch information
Marko Obrovac committed Oct 23, 2015
2 parents a773aae + 3ded7d7 commit e333ceb
Show file tree
Hide file tree
Showing 10 changed files with 548 additions and 388 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
node_js:
- "0.10"
- "0.8"
- "0.10"
- "0.12"
- "4.2"
- "iojs-v2.5.0"
before_install:
- npm install npm -g
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ Licensed under GPLv2.
[NPM1]: https://nodei.co/npm/texvcjs.svg
[NPM2]: https://nodei.co/npm/texvcjs/

[1]: https://travis-ci.org/cscott/texvcjs.svg
[2]: https://travis-ci.org/cscott/texvcjs
[3]: https://david-dm.org/cscott/texvcjs.svg
[4]: https://david-dm.org/cscott/texvcjs
[5]: https://david-dm.org/cscott/texvcjs/dev-status.svg
[6]: https://david-dm.org/cscott/texvcjs#info=devDependencies
[1]: https://travis-ci.org/wikimedia/texvcjs.svg
[2]: https://travis-ci.org/wikimedia/texvcjs
[3]: https://david-dm.org/wikimedia/texvcjs.svg
[4]: https://david-dm.org/wikimedia/texvcjs
[5]: https://david-dm.org/wikimedia/texvcjs/dev-status.svg
[6]: https://david-dm.org/wikimedia/texvcjs#info=devDependencies
3 changes: 2 additions & 1 deletion bin/texvcjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ program
.usage('[options] <tex input>')
.option('--rebuild', 'Rebuild PEGjs grammar before parsing')
.option('-v, --verbose', 'Show verbose error information')
.option('--usemathrm', 'Use \\mathrm instead of \\mbox to escape some text literals')
.option('-D, --debug', 'Show stack trace on failure');

PACKAGES.forEach(function(pkg) {
Expand All @@ -33,7 +34,7 @@ if (program.rebuild) {
}

var texvcjs = require('../');
var result = texvcjs.check(input, { debug: program.debug });
var result = texvcjs.check(input, { debug: program.debug, usemathrm:program.usemathrm });

// check required packages
PACKAGES.forEach(function(pkg) {
Expand Down
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ var check = module.exports.check = function(input, options) {
* command ...
*/
try {
if( typeof options === "undefined" ){
options = {};
options.usemathrm = false;
}
// allow user to pass a parsed AST as input, as well as a string
if (typeof(input)==='string') {
input = Parser.parse(input);
input = Parser.parse(input, {usemathrm:options.usemathrm});
}
var output = render(input);
var result = { status: '+', output: output };
Expand Down
841 changes: 480 additions & 361 deletions lib/parser.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions lib/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,18 @@ LITERAL
{ return ast.RenderT.TEX_ONLY("\\operatorname {" + f.slice(1) + "}" + c); }
/ f:generic_func &{ return tu.other_literals1[f]; } _ // from Texutil.find(...)
{ return ast.RenderT.TEX_ONLY(f + " "); }
/ f:generic_func &{ return options.usemathrm && tu.other_literals2[f]; } _ // from Texutil.find(...)
{ return ast.RenderT.TEX_ONLY("\\mathrm {" + f + "} "); }
/ mathrm:generic_func &{ return options.usemathrm && mathrm === "\\mathrm"; } _
"{" f:generic_func &{ return options.usemathrm && tu.other_literals2[f]; } _ "}" _
/* make sure we can parse what we emit */
{ return options.usemathrm && ast.RenderT.TEX_ONLY("\\mathrm {" + f + "} "); }
/ f:generic_func &{ return tu.other_literals2[f]; } _ // from Texutil.find(...)
{ return ast.RenderT.TEX_ONLY("\\mbox{" + f + "} "); }
/ mbox:generic_func &{ return mbox === "\\mbox"; } _
"{" f:generic_func &{ return tu.other_literals2[f]; } _ "}" _
/* make sure we can parse what we emit */
{ return ast.RenderT.TEX_ONLY("\\mbox{" + f + "} "); }
/* make sure we can parse what we emit */
{ return ast.RenderT.TEX_ONLY("\\mbox{" + f + "} "); }
/ f:(COLOR / DEFINECOLOR)
{ return ast.RenderT.TEX_ONLY(f); }
/ "\\" c:[, ;!_#%$&] _
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"url": "https:/cscott/texvcjs/issues"
},
"dependencies": {
"commander": "~2.5.0"
"commander": "~2.9.0"
},
"devDependencies": {
"jshint": "~2.5.2",
"mocha": "~2.0.1",
"jshint": "~2.8.0",
"mocha": "~2.3.3",
"pegjs": "~0.8.0"
},
"bin": {
Expand Down
30 changes: 29 additions & 1 deletion test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,34 @@ describe('Comprehensive test cases', function() {
'\\mbox{\\varstigma} ',
skipOcaml: true
},
'Literals (2) MJ': {
usemathrm: true,
input:
'\\AA\\Coppa\\coppa\\Digamma\\euro\\geneuro\\geneuronarrow' +
'\\geneurowide\\Koppa\\koppa\\officialeuro\\Sampi\\sampi' +
'\\Stigma\\stigma\\textvisiblespace\\varstigma',
output:
'\\mathrm {\\AA} \\mathrm {\\Coppa} \\mathrm {\\coppa} ' +
'\\mathrm {\\Digamma} \\mathrm {\\euro} \\mathrm {\\geneuro} ' +
'\\mathrm {\\geneuronarrow} \\mathrm {\\geneurowide} ' +
'\\mathrm {\\Koppa} \\mathrm {\\koppa} \\mathrm {\\officialeuro} ' +
'\\mathrm {\\Sampi} \\mathrm {\\sampi} \\mathrm {\\Stigma} ' +
'\\mathrm {\\stigma} \\mathrm {\\textvisiblespace} ' +
'\\mathrm {\\varstigma} '
},
'Literals (2\') MJ': {
usemathrm:true,
/* We can parse what we emit (but the ocaml version can't) */
input:
'\\mathrm {\\AA} \\mathrm {\\Coppa} \\mathrm {\\coppa} ' +
'\\mathrm {\\Digamma} \\mathrm {\\euro} \\mathrm {\\geneuro} ' +
'\\mathrm {\\geneuronarrow} \\mathrm {\\geneurowide} ' +
'\\mathrm {\\Koppa} \\mathrm {\\koppa} \\mathrm {\\officialeuro} ' +
'\\mathrm {\\Sampi} \\mathrm {\\sampi} \\mathrm {\\Stigma} ' +
'\\mathrm {\\stigma} \\mathrm {\\textvisiblespace} ' +
'\\mathrm {\\varstigma} ',
skipOcaml: true
},
'Literals (3)': {
input:
'\\C\\H\\N\\Q\\R\\Z\\alef\\alefsym\\Alpha\\and\\ang\\Beta' +
Expand Down Expand Up @@ -422,7 +450,7 @@ describe('Comprehensive test cases', function() {
tc.output = tc.output || tc.input;
if (!tc.skipJs) {
it('output should be correct', function() {
var result = texvcjs.check(tc.input, { debug: true });
var result = texvcjs.check(tc.input, { debug: true, usemathrm:tc.usemathrm});
assert.equal(result.status, '+');
assert.equal(result.output, tc.output);
});
Expand Down
2 changes: 1 addition & 1 deletion test/mathjax-texvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Run test for all mathjax-texvc commands:', function () {
formulae.forEach(function (testcase) {
if(testcase.ignore !== true ) {
it(testcase.id+"$"+testcase.input+"$", function () {
var result = texvcjs.check(testcase.input);
var result = texvcjs.check(testcase.input,{usemathrm:true});
assert.equal(result.output, testcase.texvcjs,
JSON.stringify({
id: testcase.id,
Expand Down
20 changes: 10 additions & 10 deletions test/mathjax-texvc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
{
"id": 2,
"input": "\\koppa",
"texvcjs": "\\mbox{\\koppa} "
"texvcjs": "\\mathrm {\\koppa} "
},
{
"id": 3,
"input": "\\stigma",
"texvcjs": "\\mbox{\\stigma} "
"texvcjs": "\\mathrm {\\stigma} "
},
{
"id": 4,
"input": "\\coppa",
"texvcjs": "\\mbox{\\coppa} "
"texvcjs": "\\mathrm {\\coppa} "
},
{
"id": 5,
Expand Down Expand Up @@ -87,7 +87,7 @@
{
"id": 18,
"input": "\\AA",
"texvcjs": "\\mbox{\\AA} "
"texvcjs": "\\mathrm {\\AA} "
},
{
"id": 19,
Expand Down Expand Up @@ -162,7 +162,7 @@
{
"id": 33,
"input": "\\textvisiblespace",
"texvcjs": "\\mbox{\\textvisiblespace} "
"texvcjs": "\\mathrm {\\textvisiblespace} "
},
{
"id": 34,
Expand Down Expand Up @@ -365,17 +365,17 @@
{
"id": 73,
"input": "\\Koppa",
"texvcjs": "\\mbox{\\Koppa} "
"texvcjs": "\\mathrm {\\Koppa} "
},
{
"id": 74,
"input": "\\Stigma",
"texvcjs": "\\mbox{\\Stigma} "
"texvcjs": "\\mathrm {\\Stigma} "
},
{
"id": 75,
"input": "\\Coppa",
"texvcjs": "\\mbox{\\Coppa} "
"texvcjs": "\\mathrm {\\Coppa} "
},
{
"id": 76,
Expand Down Expand Up @@ -451,8 +451,8 @@
},
{
"id": 90,
"input": "\\pagecolor{red}",
"texvcjs": "\\pagecolor {red}"
"input": "\\pagecolor{red}x",
"texvcjs": "\\pagecolor {red}x"
},
{
"id": 91,
Expand Down

0 comments on commit e333ceb

Please sign in to comment.