Skip to content

Added support for glossaries / glossaries-extra #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ in reverse chronological order. Releases follow `semantic versioning
<https://anaconda.org/pytask/latex-dependency-scanner>`_.


0.1.2 - 2022-xx-xx
------------------

- :gh:`12` adds support for glossaries and glossaries-extra. Thanks to
:ghuser:`axtimhaus`!


0.1.1 - 2022-02-08
------------------

Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ in ``\includegraphics``), all possible candidates are returned.
PDF
~~~

``generate_pdf()`` allows to conveniently generate PDFs with Python. The function is
``compile_pdf()`` allows to conveniently generate PDFs with Python. The function is
mainly used for validating test cases.

.. code-block:: python

import latex_dependency_scanner as lds


lds.generate_pdf("document.tex", "document.pdf")
lds.compile_pdf("document.tex", "document.pdf")


Changes
Expand Down
12 changes: 9 additions & 3 deletions src/latex_dependency_scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@


REGEX_TEX = re.compile(
r"\\(?P<type>usepackage|RequirePackage|include|addbibresource|bibliography|putbib|"
r"includegraphics|input|(sub)?import|lstinputlisting)"
r"\\(?P<type>usepackage|RequirePackage|include|addbibresource|bibliography|putbib"
r"|includegraphics|input|(sub)?import|lstinputlisting|glsxtrresourcefile"
r"|GlsXtrLoadResources)"
r"(<[^<>]*>)?"
r"(\[[^\[\]]*\])?"
r"({(?P<relative_to>[^{}]*)})?{(?P<file>[^{}]*)}",
r"({(?P<relative_to>[^{}]*)})?(\[[^\[\]]*src=)?{(?P<file>[^{}]*)}",
re.M,
)
"""re.Pattern: The regular expression pattern to extract included files from a LaTeX
Expand Down Expand Up @@ -138,6 +139,11 @@ def yield_nodes_from_node(
common_extensions = [ext]
else:
common_extensions = COMMON_GRAPHICS_EXTENSIONS
elif match.group("type") in [
"glsxtrresourcefile",
"GlsXtrLoadResources",
]:
common_extensions = [".glstex", ".bib"] # .bib for bib2gls
elif match.group("type") == "lstinputlistings":
common_extensions = [""]
else:
Expand Down
1 change: 1 addition & 0 deletions tests/resources/acronyms.glstex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\newabbreviation{abc}{ABC}{AlphaBetaGamma}
4 changes: 4 additions & 0 deletions tests/resources/symbols.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@symbol{Ab,
name = {\ensuremath{A_b}},
description = {Symbol Ab},
}
8 changes: 8 additions & 0 deletions tests/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@
"\\addbibresource{bibfile}",
{"type": "addbibresource", "file": "bibfile", "relative_to": None},
),
(
"\\glsxtrresourcefile{glsfile}",
{"type": "glsxtrresourcefile", "file": "glsfile", "relative_to": None},
),
(
"\\GlsXtrLoadResources[src={glsfile}, selection={all}]",
{"type": "GlsXtrLoadResources", "file": "glsfile", "relative_to": None},
),
],
)
def test_regex_tex(text, expected):
Expand Down
73 changes: 73 additions & 0 deletions tests/test_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,76 @@ def test_biblatex_bibliography_without_extension_and_file(tmp_path):
nodes = scan(tmp_path / "document.tex")

assert nodes == [tmp_path / "document.tex", tmp_path / "bibliography.bib"]


@pytest.mark.end_to_end
def test_glossaries(tmp_path):
"""Test document with glossaries"""
source = """
\\documentclass{article}
\\usepackage{glossaries}
\\glsxtrresourcefile{symbols}
\\GlsXtrLoadResources[src={acronyms}]
\\begin{document}
\\printunsrtsymbols
\\printunsrtglossaries
\\end{document}
"""
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(source))
shutil.copy(TEST_RESOURCES / "symbols.bib", tmp_path / "symbols.bib")
shutil.copy(TEST_RESOURCES / "acronyms.glstex", tmp_path / "acronyms.glstex")

nodes = scan(tmp_path / "document.tex")

assert nodes == [
tmp_path / "document.tex",
tmp_path / "symbols.bib",
tmp_path / "acronyms.glstex",
]


@pytest.mark.end_to_end
def test_glossaries_both_extensions_present(tmp_path):
"""Test document with glossaries and present files symbols.bib AND symbols.glstex"""
source = """
\\documentclass{article}
\\usepackage{glossaries}
\\glsxtrresourcefile{symbols}
\\begin{document}
\\printunsrtsymbols
\\printunsrtglossaries
\\end{document}
"""
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(source))
shutil.copy(TEST_RESOURCES / "symbols.bib", tmp_path / "symbols.bib")
shutil.copy(TEST_RESOURCES / "acronyms.glstex", tmp_path / "symbols.glstex")

nodes = scan(tmp_path / "document.tex")

assert nodes == [tmp_path / "document.tex", tmp_path / "symbols.glstex"]


@pytest.mark.end_to_end
def test_glossaries_without_files(tmp_path):
"""Test document with glossaries"""
source = """
\\documentclass{article}
\\usepackage{glossaries}
\\glsxtrresourcefile{symbols}
\\GlsXtrLoadResources[src={acronyms}]
\\begin{document}
\\printunsrtsymbols
\\printunsrtglossaries
\\end{document}
"""
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(source))

nodes = scan(tmp_path / "document.tex")

assert nodes == [
tmp_path / "document.tex",
tmp_path / "symbols.glstex",
tmp_path / "symbols.bib",
tmp_path / "acronyms.glstex",
tmp_path / "acronyms.bib",
]