@cocalc/project
Version:
CoCalc: project daemon
132 lines • 5.54 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.nbconvert = void 0;
/*
Node.js interface to nbconvert.
*/
const { execute_code } = require("@cocalc/backend/misc_node");
const async_utils_1 = require("@cocalc/util/async-utils");
const ipynb_to_html_1 = __importStar(require("./ipynb-to-html"));
const html_to_pdf_1 = __importDefault(require("./html-to-pdf"));
const util_1 = require("./util");
const path_1 = require("path");
const logger_1 = require("@cocalc/project/logger");
const sanitize_nbconvert_1 = require("@cocalc/util/sanitize-nbconvert");
const log = (0, logger_1.getLogger)("jupyter-nbconvert");
async function nbconvert(opts) {
log.debug("start", opts);
try {
if (!opts.timeout) {
opts.timeout = 60;
}
let { j, to } = (0, util_1.parseTo)(opts.args);
if (to == "cocalc-html" || to == "cocalc-pdf") {
// We use our own internal cocalc conversion, since I'm tired of weird subtle issues
// with upstream nbconvert...
const ipynb = (0, path_1.join)(opts.directory ?? "", (0, util_1.parseSource)(opts.args)); // make relative to home directory
const html = await (0, ipynb_to_html_1.default)(ipynb);
if (to == "cocalc-html") {
return;
}
if (to == "cocalc-pdf") {
await (0, html_to_pdf_1.default)(html, opts.timeout);
return;
}
throw Error("impossible");
}
let convertToPDF = false;
const originalSource = (0, util_1.parseSource)(opts.args); // before any mangling for the benefit of nbconvert.
if (to == "lab-pdf") {
for (let i = 0; i < opts.args.length; i++) {
if (opts.args[i] == "lab-pdf") {
opts.args[i] = "html";
break;
}
}
to = "html";
convertToPDF = true;
}
else if (to == "classic-pdf") {
for (let i = 0; i < opts.args.length; i++) {
if (opts.args[i] == "classic-pdf") {
opts.args[i] = "html";
break;
}
}
to = "html";
convertToPDF = true;
// Put --template argument at beginning -- path must be at the end.
opts.args = ["--template", "classic"].concat(opts.args);
}
let command;
let args;
if (to === "sagews") {
// support sagews converter, which is its own script, not in nbconvert.
// NOTE that if to is set, then j must be set.
command = "smc-ipynb2sagews";
args = opts.args.slice(0, j).concat(opts.args.slice(j + 3)); // j+3 cuts out --to and --.
}
else {
command = "jupyter";
args = ["nbconvert"].concat(opts.args);
// This is the **one and only case** where we sanitize the input filename. Doing so when not calling
// nbconvert would actually break everything.
args[args.length - 1] = (0, sanitize_nbconvert_1.sanitize_nbconvert_path)(args[args.length - 1]);
}
log.debug("running ", { command, args });
// Note about bash/ulimit_timeout below. This is critical since nbconvert
// could launch things like pdflatex that might run forever and without
// ulimit they do not get killed properly; this has happened in production!
const output = await (0, async_utils_1.callback_opts)(execute_code)({
command,
args,
path: opts.directory,
err_on_exit: false,
timeout: opts.timeout,
ulimit_timeout: true,
bash: true,
});
if (output.exit_code != 0) {
throw Error(output.stderr);
}
if (convertToPDF) {
// Important to use *unmangled* source here!
await (0, html_to_pdf_1.default)((0, ipynb_to_html_1.htmlPath)((0, path_1.join)(opts.directory ?? "", originalSource)));
}
}
finally {
log.debug("finished");
}
}
exports.nbconvert = nbconvert;
//# sourceMappingURL=index.js.map