@cocalc/project
Version:
CoCalc: project daemon
126 lines (113 loc) • 3.85 kB
JavaScript
// Generated by CoffeeScript 2.5.1
(function() {
//########################################################################
// This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
// License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
//########################################################################
//##############################################
// Printing an individual file to pdf
//##############################################
var async, defaults, fs, message, misc, misc_node, print_sagews, required, temp;
async = require('async');
fs = require('fs');
temp = require('temp');
misc = require('@cocalc/util/misc');
misc_node = require('@cocalc/backend/misc_node');
message = require('@cocalc/util/message');
({defaults, required} = misc);
print_sagews = function(opts) {
var args, extra_data_file;
opts = defaults(opts, {
path: required,
outfile: required,
title: required,
author: required,
date: required,
contents: required,
subdir: required, // 'true' or 'false', if true, then workdir is a generated subdirectory which will retain the temporary tex files
base_url: void 0, // the base_url for downloading blobs/images
extra_data: void 0, // extra data that is useful for displaying certain things in the worksheet.
timeout: 90,
cb: required
});
extra_data_file = void 0;
args = [opts.path, '--outfile', opts.outfile, '--title', opts.title, '--author', opts.author, '--date', opts.date, '--subdir', opts.subdir, '--contents', opts.contents];
if (opts.base_url) {
args = args.concat(['--base_url', opts.base_url]);
}
return async.series([
function(cb) {
if (opts.extra_data == null) {
cb();
return;
}
extra_data_file = temp.path() + '.json';
args.push('--extra_data_file');
args.push(extra_data_file);
// NOTE: extra_data is a string that is *already* in JSON format.
return fs.writeFile(extra_data_file,
opts.extra_data,
cb);
},
function(cb) {
// run the converter script
return misc_node.execute_code({
command: "smc-sagews2pdf",
args: args,
err_on_exit: true,
bash: false,
timeout: opts.timeout,
cb: cb
});
}
], (err) => {
if (extra_data_file != null) {
fs.unlink(extra_data_file); // no need to wait for completion before calling opts.cb
}
return opts.cb(err);
});
};
exports.print_to_pdf = function(socket, mesg) {
var ext, pdf;
ext = misc.filename_extension(mesg.path);
if (ext) {
pdf = `${mesg.path.slice(0, mesg.path.length - ext.length)}pdf`;
} else {
pdf = mesg.path + '.pdf';
}
return async.series([
function(cb) {
switch (ext) {
case 'sagews':
return print_sagews({
path: mesg.path,
outfile: pdf,
title: mesg.options.title,
author: mesg.options.author,
date: mesg.options.date,
contents: mesg.options.contents,
subdir: mesg.options.subdir,
extra_data: mesg.options.extra_data,
timeout: mesg.options.timeout,
cb: cb
});
default:
return cb(`unable to print file of type '${ext}'`);
}
}
], function(err) {
if (err) {
return socket.write_mesg('json', message.error({
id: mesg.id,
error: err
}));
} else {
return socket.write_mesg('json', message.printed_to_pdf({
id: mesg.id,
path: pdf
}));
}
});
};
}).call(this);
//# sourceMappingURL=print_to_pdf.js.map