UNPKG

@cocalc/project

Version:
62 lines 2.29 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ Object.defineProperty(exports, "__esModule", { value: true }); exports.latex_format = void 0; const fs_1 = require("fs"); const tmp_1 = require("tmp"); const awaiting_1 = require("awaiting"); const child_process_1 = require("child_process"); const misc_1 = require("@cocalc/util/misc"); function close(proc, cb) { proc.on("close", (code) => cb(undefined, code)); } async function latex_format(input, options) { // create input temp file const input_path = await (0, awaiting_1.callback)(tmp_1.file, { postfix: ".tex" }); try { await (0, awaiting_1.callback)(fs_1.writeFile, input_path, input); // spawn the latexindent script. const latexindent = (0, child_process_1.spawn)("latexindent", [input_path]); let output = ""; // read data as it is produced. latexindent.stdout.on("data", (data) => (output += data.toString())); // wait for subprocess to close. const code = await (0, awaiting_1.callback)(close, latexindent); if (code) { throw Error(`latexindent exited with code ${code}`); } // now process the result according to the options. if (!options.useTabs) { // replace tabs by spaces let tab_width = 2; if (options.tabWidth !== undefined) { tab_width = options.tabWidth; } let SPACES = ""; for (let i = 0; i < tab_width; i++) { SPACES += " "; } output = (0, misc_1.replace_all)(output, "\t", SPACES); } // latexindent also annoyingly introduces a lot of whitespace lines. const lines = output.split("\n"), lines2 = []; for (let i = 0; i < lines.length; i++) { if (/\S/.test(lines[i])) { lines2.push(lines[i]); } else { lines2.push(""); } output = lines2.join("\n"); } return output; } finally { (0, fs_1.unlink)(input_path, () => { }); } } exports.latex_format = latex_format; //# sourceMappingURL=latex-format.js.map