Skip to content

Commit 792bec5

Browse files
Merge pull request #8618 from sagemathinc/latex-output-fixes4
frontend/latex: add more file types to the list of dependencies
2 parents 3a49d4d + 1e3ff14 commit 792bec5

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

src/packages/frontend/cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"respawns",
4747
"revealjs",
4848
"Rmarkdown",
49+
"rtex",
4950
"rtypes",
5051
"Sagemath",
5152
"sagetex",

src/packages/frontend/frame-editors/latex-editor/latex-log-parser.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { trimEnd } from "lodash";
2323

2424
import { normalize as path_normalize } from "path";
2525

26+
import { filename_extension } from "@cocalc/util/misc";
27+
2628
// Define some constants
2729
const LOG_WRAP_LIMIT = 79;
2830
const LATEX_WARNING_REGEX = /^LaTeX Warning: (.*)$/;
@@ -33,6 +35,26 @@ const LINES_REGEX = /lines? ([0-9]+)/;
3335
// This is used to parse the package name from the package warnings
3436
const PACKAGE_REGEX = /^(?:Package|Class|Module) (\b.+\b) Warning/;
3537

38+
// Whitelist of text file extensions that can be used with \input{} or \include{}
39+
const ALLOWED_DEP_EXTENSIONS = [
40+
"bbx",
41+
"bib",
42+
"bst",
43+
"cbx",
44+
"cfg",
45+
"cls",
46+
"def",
47+
"lbx",
48+
"md",
49+
"pgf",
50+
"rnw",
51+
"rtex",
52+
"sty",
53+
"tex",
54+
"tikz",
55+
"txt",
56+
] as const;
57+
3658
class LogText {
3759
private lines: string[];
3860
private row: number;
@@ -224,14 +246,25 @@ export class LatexParser {
224246

225247
addDeps(line: string): void {
226248
line = line.trim();
227-
// ignore absolute files
249+
// ignore absolute files (starting with /)
228250
if (line[0] === "/") return;
229251
if (line[line.length - 1] === "\\") {
230252
line = line.slice(0, line.length - 1);
231253
}
232-
// we only want to know about tex and bib files
233-
const pl = line.toLowerCase(); // could be name.TEX
234-
if (!pl.endsWith(".tex") && !pl.endsWith(".bib")) return;
254+
// Skip files that contain a colon (like "master.pdf :")
255+
if (line.includes(":")) return;
256+
257+
// Get the file extension (returns empty string if no extension)
258+
const ext = filename_extension(line).toLowerCase();
259+
260+
// If there's an extension, check if it's in the whitelist
261+
if (ext) {
262+
if (!ALLOWED_DEP_EXTENSIONS.includes(ext as any)) {
263+
return;
264+
}
265+
}
266+
// If no extension, include it (files without extensions are allowed)
267+
235268
this.deps.push(line);
236269
}
237270

@@ -306,7 +339,7 @@ export class LatexParser {
306339
const packageMatch = this.currentLine.match(PACKAGE_REGEX);
307340
if (!packageMatch) return;
308341
const packageName = packageMatch[1];
309-
// Regex to get rid of the unnecesary (packagename) prefix in most multi-line warnings
342+
// Regex to get rid of the unnecessary (packagename) prefix in most multi-line warnings
310343
const prefixRegex = new RegExp(`(?:\\(${packageName}\\))*[\\s]*(.*)`, "i");
311344
// After every warning message there's a blank line, let's use it
312345
while (!!(this.currentLine = this.log.nextLine())) {

src/packages/frontend/misc/latex-log-parser.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "fs";
2+
23
import { LatexParser } from "@cocalc/frontend/frame-editors/latex-editor/latex-log-parser";
34

45
describe("latex-log-parser", () => {
@@ -14,7 +15,7 @@ describe("latex-log-parser", () => {
1415
expect(parsed1.deps[1]).toEqual("subfile.tex");
1516
});
1617

17-
// This is an abbrivated log of https://github.com/sagemathinc/cocalc/issues/8089
18+
// This is an abbreviated log of https://github.com/sagemathinc/cocalc/issues/8089
1819
test("log2", () => {
1920
const log2 = fs.readFileSync("./misc/latex-logs/log2.txt", "utf-8");
2021
const parsed2 = new LatexParser(log2, {
@@ -23,7 +24,27 @@ describe("latex-log-parser", () => {
2324
const err0 = parsed2.all[0];
2425
expect(err0.file).toEqual("ch_Euclidean.tex");
2526
expect(err0.message).toEqual("Marginpar on page 1 moved.");
26-
expect(parsed2.deps).toEqual(["01.tex", "02.5.tex"]);
27+
expect(parsed2.deps).toEqual(["./livre1.bst", "01.tex", "02.5.tex"]);
2728
expect(parsed2.files).toEqual(["livre1.tex", "ch_Euclidean.tex"]);
2829
});
30+
31+
// Test for non-.tex/.bib files in dependencies (e.g., .txt files used via \input{})
32+
test("log3", () => {
33+
const log3 = fs.readFileSync("./misc/latex-logs/log3.txt", "utf-8");
34+
const parsed3 = new LatexParser(log3, {
35+
ignoreDuplicates: true,
36+
}).parse();
37+
expect(parsed3.deps).toEqual([
38+
"../subdir0/file2.md",
39+
"long-running.tex",
40+
"long-running.txt",
41+
"subdir/file_9",
42+
]);
43+
// No .tex or .bib files in parentheses, so files array is empty
44+
expect(parsed3.files).toEqual([]);
45+
expect(parsed3.errors).toEqual([]);
46+
expect(parsed3.warnings).toEqual([]);
47+
expect(parsed3.typesetting).toEqual([]);
48+
expect(parsed3.all).toEqual([]);
49+
});
2950
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(./long-running.txt) (./subdir/file_9)
2+
(../subdir0/file2.md) [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}]
3+
(./long-running.aux) )</usr/share/texlive/texmf-dist/fonts/type1/public/amsfont
4+
s/cm/cmr10.pfb>
5+
Output written on long-running.pdf (1 page, 15440 bytes).
6+
SyncTeX written on long-running.synctex.gz.
7+
Transcript written on long-running.log.
8+
Latexmk: Examining 'long-running.log'
9+
=== TeX engine is 'pdfTeX'
10+
Latexmk: applying rule 'pdflatex'...
11+
Latexmk: All targets (long-running.pdf) are up-to-date
12+
#===Dependents, and related info, for long-running.tex:
13+
long-running.pdf :\
14+
../subdir0/file2.md\
15+
/etc/texmf/web2c/texmf.cnf\
16+
/usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb\
17+
/usr/share/texlive/texmf-dist/tex/latex/base/article.cls\
18+
/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo\
19+
/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def\
20+
/usr/share/texlive/texmf-dist/web2c/texmf.cnf\
21+
/usr/share/texmf/web2c/texmf.cnf\
22+
/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map\
23+
/var/lib/texmf/web2c/pdftex/pdflatex.fmt\
24+
long-running.tex\
25+
long-running.txt\
26+
subdir/file_9
27+
#===End dependents for long-running.tex:

0 commit comments

Comments
 (0)