Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/formatters/bib-format.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { callback } from "awaiting";6import { readFile, unlink, writeFile } from "node:fs";7import * as tmp from "tmp";89import { executeCode } from "@cocalc/backend/execute-code";1011interface ParserOptions {12parser: string;13}1415// ref: man biber1617async function biber(input_path, output_path) {18const args = [19"--tool",20"--output-align",21"--output-indent=2",22"--output-fieldcase=lower",23"--output-file",24output_path,25input_path,26];2728return await executeCode({29command: "biber",30args: args,31err_on_exit: false,32bash: false,33timeout: 20,34});35}3637export async function bib_format(38input: string,39options: ParserOptions,40logger: any41): Promise<string> {42// create input temp file43const input_path: string = await callback(tmp.file);44const output_path: string = await callback(tmp.file);45try {46await callback(writeFile, input_path, input);4748// spawn the bibtex formatter49let bib_formatter;50try {51switch (options.parser) {52case "bib-biber":53bib_formatter = await biber(input_path, output_path);54break;55default:56throw Error(`Unknown XML formatter utility '${options.parser}'`);57}58} catch (e) {59logger.debug(`Calling Bibtex formatter raised ${e}`);60throw new Error(61`Bibtex formatter broken or not available. Is '${options.parser}' installed?`62);63}6465const { exit_code, stdout, stderr } = bib_formatter;66const code = exit_code;6768const problem = code >= 1;69if (problem) {70const msg = `Bibtex formatter "${options.parser}" exited with code ${code}\nOutput:\n${stdout}\n${stderr}`;71throw Error(msg);72}7374// all fine, we read from the temp file75const output: Buffer = await callback(readFile, output_path);76const s: string = output.toString("utf-8");77return s;78} finally {79// logger.debug(`bibtex formatter done, unlinking ${input_path}`);80unlink(input_path, () => {});81unlink(output_path, () => {});82}83}848586