Skip to content

Commit f748069

Browse files
committed
extended scan for glossaries
1 parent 2ef36bb commit f748069

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed

src/latex_dependency_scanner/scanner.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
COMMON_TEX_EXTENSIONS = [".ltx", ".tex"]
1010
"""List[str]: List of typical file extensions that contain latex"""
1111

12-
1312
COMMON_GRAPHICS_EXTENSIONS = [
1413
# Image formats.
1514
".eps",
@@ -20,21 +19,19 @@
2019
]
2120
"""List[str]: List of typical image extensions contained in LaTeX files."""
2221

23-
2422
COMMON_EXTENSIONS_IN_TEX = (
25-
[
26-
# No extension if the extension is provided.
27-
"",
28-
# TeX formats.
29-
".bib",
30-
".sty",
31-
]
32-
+ COMMON_GRAPHICS_EXTENSIONS
33-
+ COMMON_TEX_EXTENSIONS
23+
[
24+
# No extension if the extension is provided.
25+
"",
26+
# TeX formats.
27+
".bib",
28+
".sty",
29+
]
30+
+ COMMON_GRAPHICS_EXTENSIONS
31+
+ COMMON_TEX_EXTENSIONS
3432
)
3533
"""List[str]: List of typical file extensions included in latex files"""
3634

37-
3835
REGEX_TEX = re.compile(
3936
r"\\(?P<type>usepackage|RequirePackage|include|addbibresource|bibliography|putbib|"
4037
r"includegraphics|input|(sub)?import|lstinputlisting|glsxtrresourcefile|GlsXtrLoadResources)"
@@ -69,9 +66,9 @@ def scan(paths: Union[Path, List[Path]]):
6966

7067

7168
def yield_nodes_from_node(
72-
node: Path,
73-
nodes: List[Path],
74-
relative_to: Optional[Path] = None,
69+
node: Path,
70+
nodes: List[Path],
71+
relative_to: Optional[Path] = None,
7572
):
7673
r"""Yield nodes from node.
7774
@@ -137,6 +134,8 @@ def yield_nodes_from_node(
137134
common_extensions = [ext]
138135
else:
139136
common_extensions = COMMON_GRAPHICS_EXTENSIONS
137+
elif match.group("type") in ["glsxtrresourcefile", "GlsXtrLoadResources"]:
138+
common_extensions = [".glstex", ".bib"] # .bib for bib2gls
140139
elif match.group("type") == "lstinputlistings":
141140
common_extensions = [""]
142141
else:

tests/resources/acronyms.glstex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\newabbreviation{abc}{ABC}{AlphaBetaGamma}

tests/resources/symbols.bib

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@symbol{Ab,
2+
name = {\ensuremath{A_b}},
3+
description = {Symbol Ab},
4+
}
5+

tests/test_scan.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,68 @@ def test_biblatex_bibliography_without_extension_and_file(tmp_path):
390390
nodes = scan(tmp_path / "document.tex")
391391

392392
assert nodes == [tmp_path / "document.tex", tmp_path / "bibliography.bib"]
393+
394+
395+
@pytest.mark.end_to_end
396+
def test_glossaries(tmp_path):
397+
"""Test document with glossaries"""
398+
source = """
399+
\\documentclass{article}
400+
\\usepackage{glossaries}
401+
\\glsxtrresourcefile{symbols}
402+
\\GlsXtrLoadResources[src={acronyms}]
403+
\\begin{document}
404+
\\printunsrtsymbols
405+
\\printunsrtglossaries
406+
\\end{document}
407+
"""
408+
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(source))
409+
shutil.copy(TEST_RESOURCES / "symbols.bib", tmp_path / "symbols.bib")
410+
shutil.copy(TEST_RESOURCES / "acronyms.glstex", tmp_path / "acronyms.glstex")
411+
412+
nodes = scan(tmp_path / "document.tex")
413+
414+
assert nodes == [tmp_path / "document.tex", tmp_path / "symbols.bib", tmp_path / "acronyms.glstex"]
415+
416+
417+
@pytest.mark.end_to_end
418+
def test_glossaries_both_extensions_present(tmp_path):
419+
"""Test document with glossaries and present files symbols.bib AND symbols.glstex"""
420+
source = """
421+
\\documentclass{article}
422+
\\usepackage{glossaries}
423+
\\glsxtrresourcefile{symbols}
424+
\\begin{document}
425+
\\printunsrtsymbols
426+
\\printunsrtglossaries
427+
\\end{document}
428+
"""
429+
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(source))
430+
shutil.copy(TEST_RESOURCES / "symbols.bib", tmp_path / "symbols.bib")
431+
shutil.copy(TEST_RESOURCES / "acronyms.glstex", tmp_path / "symbols.glstex")
432+
433+
nodes = scan(tmp_path / "document.tex")
434+
print(nodes)
435+
436+
assert nodes == [tmp_path / "document.tex", tmp_path / "symbols.glstex"]
437+
438+
439+
@pytest.mark.end_to_end
440+
def test_glossaries_without_files(tmp_path):
441+
"""Test document with glossaries"""
442+
source = """
443+
\\documentclass{article}
444+
\\usepackage{glossaries}
445+
\\glsxtrresourcefile{symbols}
446+
\\GlsXtrLoadResources[src={acronyms}]
447+
\\begin{document}
448+
\\printunsrtsymbols
449+
\\printunsrtglossaries
450+
\\end{document}
451+
"""
452+
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(source))
453+
454+
nodes = scan(tmp_path / "document.tex")
455+
456+
assert nodes == [tmp_path / "document.tex", tmp_path / "symbols.glstex", tmp_path / "symbols.bib",
457+
tmp_path / "acronyms.glstex", tmp_path / "acronyms.bib"]

0 commit comments

Comments
 (0)