Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
[main] prettify SVG output
Browse files Browse the repository at this point in the history
Fixes #11
  • Loading branch information
pkra committed Aug 29, 2017
1 parent b2815f4 commit f2240ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const speech = require('speech-rule-engine');
// speakText: // string with spoken annotation


const main = function(data, callback) {
const main = function (data, callback) {
let speechConfig = {
semantics: true,
domain: data.speakRuleset || 'mathspeak',
Expand All @@ -34,12 +34,12 @@ const main = function(data, callback) {
data.mmlNode = true;
data.mml = true;

mathjax.typeset(data, function(result, input) {
mathjax.typeset(data, function (result, input) {
postprocessor(speechConfig, result, input, callback);
})
};

const postprocessor = function(config, result, input, callback) {
const postprocessor = function (config, result, input, callback) {
if (result.error) throw result.error;
if (!result.mml) throw new Error('No MathML found. Please check the mathjax-node configuration');
if (!result.svgNode && !result.htmlNode && !result.mmlNode) throw new Error('No suitable output found. Please check the mathjax-node configuration');
Expand All @@ -62,6 +62,8 @@ const postprocessor = function(config, result, input, callback) {
// update serialization
// HACK add lost xlink namespaces TODO file jsdom bug
if (result.svg) result.svg = result.svgNode.outerHTML
.replace(/><([^/])/g, ">\n<$1")
.replace(/(<\/[a-z]*>)(?=<\/)/g, "$1\n")
.replace(/(<(?:use|image) [^>]*)(href=)/g, ' $1xlink:$2');
}
if (result.htmlNode) {
Expand All @@ -77,7 +79,7 @@ const postprocessor = function(config, result, input, callback) {
callback(result, input);
}

const preprocessor = function(data, callback) {
const preprocessor = function (data, callback) {
// setup SRE
let speechConfig = {
semantics: true,
Expand All @@ -100,7 +102,7 @@ const preprocessor = function(data, callback) {
format: data.format,
mml: true
};
mathjax.typeset(newdata, function(result) {
mathjax.typeset(newdata, function (result) {
if (result.error) throw result.error;
data.math = speech.toEnriched(result.mml).toString();
data.format = 'MathML';
Expand All @@ -113,12 +115,12 @@ const preprocessor = function(data, callback) {

exports.start = mathjax.start;
exports.config = mathjax.config;
exports.typeset = function(data, callback) {
exports.typeset = function (data, callback) {
if (data.svg) data.svgNode = true;
if (data.html) data.htmlNode = true;
if (data.mml) data.mmlNode = true;
if (data.enrich) {
preprocessor(data, function(result) {
preprocessor(data, function (result) {
main(result, callback);
})
} else main(data, callback);
Expand Down
10 changes: 10 additions & 0 deletions test/base-svg-prettify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var tape = require('tape');
var mjsre = require('../lib/main.js').typeset;

tape('Base: SVG prettify', function(t) {
t.plan(1);
var input = 'x';
mjsre({math: input, format: "TeX", svg: true,speakText: true},function(result){
t.ok(result.svg.indexOf('\n') !== -1, 'SVG string contains linebreaks');
});
});

0 comments on commit f2240ca

Please sign in to comment.