<commit_before>/*
Convert LaTeX file to PDF using latexmk.
*/

import { exec, ExecOutput } from "../generic/client";
import { path_split, change_filename_extension } from "smc-util/misc2";

export async function latexmk(
  project_id: string,
  path: string,
  build_command: string | string[],
  time: number | undefined, // (ms since epoch)  used to aggregate multiple calls into one across all users.
  status: Function
): Promise<ExecOutput> {
  const x = path_split(path);
  let command: string;
  let args: string[] | undefined;
  if (typeof build_command === "string") {
    command = build_command;
    args = undefined;
    status(command);
  } else {
    command = build_command[0];
    args = build_command.slice(1);
    status([command].concat(args).join(" "));
  }
  return await exec({
    bash: true,    // we use ulimit so that the timeout on the backend is *enforced* via ulimit!!
    allow_post: false, // definitely could take a long time to fully run latex
    timeout: 60,
    command: command,
    args: args,
    project_id: project_id,
    path: x.head,
    err_on_exit: false,
    aggregate: time
  });
}

export type Engine = "PDFLaTeX" | "XeLaTeX" | "LuaTex";

export function build_command(
  engine: Engine,
  filename: string,
  knitr: boolean
): string[] {
  /*
  errorstopmode recommended by
  http://tex.stackexchange.com/questions/114805/pdflatex-nonstopmode-with-tikz-stops-compiling
  since in some cases things will hang (using )
  return "pdflatex -synctex=1 -interact=errorstopmode '#{@filename_tex}'"
  However, users hate nostopmode, so we use nonstopmode, which can hang in rare cases with tikz.
  See https://github.com/sagemathinc/cocalc/issues/156<commit_msg>webapp/latex: 4 min timeout latexmk<commit_after>/*
Convert LaTeX file to PDF using latexmk.
*/

import { exec, ExecOutput } from "../generic/client";
import { path_split, change_filename_extension } from "smc-util/misc2";

export async function latexmk(
  project_id: string,
  path: string,
  build_command: string | string[],
  time: number | undefined, // (ms since epoch)  used to aggregate multiple calls into one across all users.
  status: Function
): Promise<ExecOutput> {
  const x = path_split(path);
  let command: string;
  let args: string[] | undefined;
  if (typeof build_command === "string") {
    command = build_command;
    args = undefined;
    status(command);
  } else {
    command = build_command[0];
    args = build_command.slice(1);
    status([command].concat(args).join(" "));
  }
  return await exec({
    bash: true, // we use ulimit so that the timeout on the backend is *enforced* via ulimit!!
    allow_post: false, // definitely could take a long time to fully run latex
    timeout: 4 * 60, // 4 minutes, on par with Overleaf
    command: command,
    args: args,
    project_id: project_id,
    path: x.head,
    err_on_exit: false,
    aggregate: time
  });
}

export type Engine = "PDFLaTeX" | "XeLaTeX" | "LuaTex";

export function build_command(
  engine: Engine,
  filename: string,
  knitr: boolean
): string[] {
  /*
  errorstopmode recommended by
  http://tex.stackexchange.com/questions/114805/pdflatex-nonstopmode-with-tikz-stops-compiling
  since in some cases things will hang (using )
  return "pdflatex -synctex=1 -interact=errorstopmode '#{@filename_tex}'"
  However, users hate nostopmode, so we use nonstopmode, which can hang in rare cases with tikz.
  See https://github.com/sagemathinc/cocalc/issues/156
{'fraction_non_alphanumeric': 0.1142527409117138, 'fraction_numerical': 0.009521061742642816, 'mean_word_length': 3.749315068493151, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4735265', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1102, 'n_words': 348}