Skip to content

Commit

Permalink
Add option to specify external CSS
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
bfirsh committed Jun 18, 2018
1 parent 5186c18 commit 751d31b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 34 deletions.
7 changes: 6 additions & 1 deletion bin/engrafo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ program
"--no-post-processing",
"Raw LaTeXML output, without filters or postprocessors (for debugging)"
)
.option(
"--css <path>",
"URL or path to external CSS to use instead of the default CSS. If passed, the defaut CSS is not copied to the output directory."
)
.parse(process.argv);

if (program.args.length != 1) {
Expand All @@ -28,7 +32,8 @@ var output =
var options = {
input: input,
output: output,
postProcessing: program.postProcessing
postProcessing: program.postProcessing,
externalCSS: program.css
};

engrafo
Expand Down
20 changes: 14 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const fs = require("fs-extra");
const jsdom = require("jsdom");
const path = require("path");

const io = require("./io");
const latexml = require("./latexml");
const math = require("./math");
const postprocessors = require("./postprocessor");

// Run postprocessing against a string of HTML
exports.postprocess = htmlString => {
exports.postprocess = (htmlString, options) => {
var dom = jsdom.jsdom(htmlString, {
features: { ProcessExternalResources: false, FetchExternalResources: false }
});

// Run all processing on document.
postprocessors.css(dom, options);
postprocessors.footer(dom);
postprocessors.links(dom);
postprocessors.math(dom);
Expand All @@ -21,17 +23,17 @@ exports.postprocess = htmlString => {
};

// Do all processing on the file that LaTeXML produces
async function processHTML(htmlPath) {
async function processHTML(htmlPath, options) {
let htmlString = await fs.readFile(htmlPath, "utf8");
htmlString = exports.postprocess(htmlString);
htmlString = exports.postprocess(htmlString, options);
htmlString = await math.renderMath(htmlString);
await fs.writeFile(htmlPath, htmlString);
}

// Render and postprocess a LaTeX file into outputDir (created if does not
// exist). Calls callback with an error on failure or a path to an HTML file
// on success.
async function render({ input, output, postProcessing }) {
async function render({ input, output, postProcessing, externalCSS }) {
if (postProcessing === undefined) {
postProcessing = true;
}
Expand All @@ -40,10 +42,16 @@ async function render({ input, output, postProcessing }) {
const texPath = await io.pickLatexFile(inputDir);
const outputDir = await io.prepareOutputDirectory(output);

// If there is external CSS, don't let LaTeXML copy it to the output
// directory - we will handle it ourselves
const cssPath = externalCSS
? null
: path.join(__dirname, "../dist/index.css");

console.log(`Rendering tex file ${texPath} to ${outputDir}`);
const htmlPath = await latexml.render({ texPath, outputDir });
const htmlPath = await latexml.render({ texPath, outputDir, cssPath });

await processHTML(htmlPath);
await processHTML(htmlPath, { externalCSS });

if (output.startsWith("s3://")) {
await io.uploadOutputToS3(outputDir, output);
Expand Down
55 changes: 28 additions & 27 deletions src/latexml.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,37 @@ function unlinkIfExists(path) {
}

// render a document with latexml
function render({ texPath, outputDir }) {
var htmlPath = path.join(outputDir, "index.html");
function render({ texPath, outputDir, cssPath }) {
const htmlPath = path.join(outputDir, "index.html");

var latexmlc = childProcess.spawn(
"latexmlc",
[
"--dest",
htmlPath,
"--format",
"html5",
"--nodefaultresources",
"--css",
path.join(__dirname, "../dist/index.css"),
"--mathtex",
"--svg",
"--verbose",
"--preload",
"/app/latexml/engrafo.ltxml",
"--preload",
"/usr/src/latexml/lib/LaTeXML/Package/hyperref.sty.ltxml",
texPath
],
{
cwd: path.dirname(texPath)
}
);
const args = [
"--dest",
htmlPath,
"--format",
"html5",
"--nodefaultresources",
"--mathtex",
"--svg",
"--verbose",
"--preload",
"/app/latexml/engrafo.ltxml",
"--preload",
"/usr/src/latexml/lib/LaTeXML/Package/hyperref.sty.ltxml"
];

if (cssPath) {
args.push("--css", cssPath);
}

args.push(texPath);

const latexmlc = childProcess.spawn("latexmlc", args, {
cwd: path.dirname(texPath)
});

var stdoutReadline = readline.createInterface({ input: latexmlc.stdout });
const stdoutReadline = readline.createInterface({ input: latexmlc.stdout });
stdoutReadline.on("line", console.log);
var stderrReadline = readline.createInterface({ input: latexmlc.stderr });
const stderrReadline = readline.createInterface({ input: latexmlc.stderr });
stderrReadline.on("line", console.error);

return new Promise((resolve, reject) => {
Expand Down
13 changes: 13 additions & 0 deletions src/postprocessor/css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const utils = require("./utils");

module.exports = function(dom, { externalCSS }) {
if (externalCSS) {
const head = dom.querySelector("head");
const link = utils.nodeFromString(
dom,
'<link type="text/css" rel="stylesheet">'
);
link.setAttribute("href", externalCSS);
head.appendChild(link);
}
};
1 change: 1 addition & 0 deletions src/postprocessor/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
css: require("./css"),
footer: require("./footer"),
links: require("./links"),
math: require("./math")
Expand Down
14 changes: 14 additions & 0 deletions src/postprocessor/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.removeAll = function(els) {
Array.from(els).forEach(el => {
el.parentNode.removeChild(el);
});
};

exports.nodeFromString = function(dom, str) {
var div = dom.createElement("div");
div.innerHTML = str;
if (!div.firstChild) {
throw new Error("Invalid HTML passed to nodeFromString");
}
return div.firstChild;
};

0 comments on commit 751d31b

Please sign in to comment.