From 2ef36bbdd99d7d537e45c626d52783b5e9e9c877 Mon Sep 17 00:00:00 2001 From: Max Weiner Date: Fri, 19 Nov 2021 14:54:55 +0100 Subject: [PATCH 1/5] extended regex for glossaries --- src/latex_dependency_scanner/scanner.py | 4 ++-- tests/test_regex.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/latex_dependency_scanner/scanner.py b/src/latex_dependency_scanner/scanner.py index 2bccea5..ff6039d 100644 --- a/src/latex_dependency_scanner/scanner.py +++ b/src/latex_dependency_scanner/scanner.py @@ -37,10 +37,10 @@ REGEX_TEX = re.compile( r"\\(?Pusepackage|RequirePackage|include|addbibresource|bibliography|putbib|" - r"includegraphics|input|(sub)?import|lstinputlisting)" + r"includegraphics|input|(sub)?import|lstinputlisting|glsxtrresourcefile|GlsXtrLoadResources)" r"(<[^<>]*>)?" r"(\[[^\[\]]*\])?" - r"({(?P[^{}]*)})?{(?P[^{}]*)}", + r"({(?P[^{}]*)})?(\[[^\[\]]*src=)?{(?P[^{}]*)}", re.M, ) """re.Pattern: The regular expression pattern to extract included files from a LaTeX diff --git a/tests/test_regex.py b/tests/test_regex.py index e2be846..d88329d 100644 --- a/tests/test_regex.py +++ b/tests/test_regex.py @@ -57,6 +57,14 @@ "\\addbibresource{bibfile}", {"type": "addbibresource", "file": "bibfile", "relative_to": None}, ), + ( + "\\glsxtrresourcefile{glsfile}", + {"type": "glsxtrresourcefile", "file": "glsfile", "relative_to": None}, + ), + ( + "\\GlsXtrLoadResources[src={glsfile}]", + {"type": "GlsXtrLoadResources", "file": "glsfile", "relative_to": None}, + ), ], ) def test_regex_tex(text, expected): From f7480698175e07b50d3b07ef232e49912dcd2f0c Mon Sep 17 00:00:00 2001 From: Max Weiner Date: Fri, 19 Nov 2021 15:38:07 +0100 Subject: [PATCH 2/5] extended scan for glossaries --- src/latex_dependency_scanner/scanner.py | 29 ++++++----- tests/resources/acronyms.glstex | 1 + tests/resources/symbols.bib | 5 ++ tests/test_scan.py | 65 +++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 tests/resources/acronyms.glstex create mode 100644 tests/resources/symbols.bib diff --git a/src/latex_dependency_scanner/scanner.py b/src/latex_dependency_scanner/scanner.py index ff6039d..1c5b686 100644 --- a/src/latex_dependency_scanner/scanner.py +++ b/src/latex_dependency_scanner/scanner.py @@ -9,7 +9,6 @@ COMMON_TEX_EXTENSIONS = [".ltx", ".tex"] """List[str]: List of typical file extensions that contain latex""" - COMMON_GRAPHICS_EXTENSIONS = [ # Image formats. ".eps", @@ -20,21 +19,19 @@ ] """List[str]: List of typical image extensions contained in LaTeX files.""" - COMMON_EXTENSIONS_IN_TEX = ( - [ - # No extension if the extension is provided. - "", - # TeX formats. - ".bib", - ".sty", - ] - + COMMON_GRAPHICS_EXTENSIONS - + COMMON_TEX_EXTENSIONS + [ + # No extension if the extension is provided. + "", + # TeX formats. + ".bib", + ".sty", + ] + + COMMON_GRAPHICS_EXTENSIONS + + COMMON_TEX_EXTENSIONS ) """List[str]: List of typical file extensions included in latex files""" - REGEX_TEX = re.compile( r"\\(?Pusepackage|RequirePackage|include|addbibresource|bibliography|putbib|" r"includegraphics|input|(sub)?import|lstinputlisting|glsxtrresourcefile|GlsXtrLoadResources)" @@ -69,9 +66,9 @@ def scan(paths: Union[Path, List[Path]]): def yield_nodes_from_node( - node: Path, - nodes: List[Path], - relative_to: Optional[Path] = None, + node: Path, + nodes: List[Path], + relative_to: Optional[Path] = None, ): r"""Yield nodes from node. @@ -137,6 +134,8 @@ 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: diff --git a/tests/resources/acronyms.glstex b/tests/resources/acronyms.glstex new file mode 100644 index 0000000..e197a5a --- /dev/null +++ b/tests/resources/acronyms.glstex @@ -0,0 +1 @@ +\newabbreviation{abc}{ABC}{AlphaBetaGamma} \ No newline at end of file diff --git a/tests/resources/symbols.bib b/tests/resources/symbols.bib new file mode 100644 index 0000000..fd40288 --- /dev/null +++ b/tests/resources/symbols.bib @@ -0,0 +1,5 @@ +@symbol{Ab, + name = {\ensuremath{A_b}}, + description = {Symbol Ab}, +} + diff --git a/tests/test_scan.py b/tests/test_scan.py index 0706f05..54cceac 100644 --- a/tests/test_scan.py +++ b/tests/test_scan.py @@ -390,3 +390,68 @@ 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") + print(nodes) + + 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"] From 894d66e8f18bad209a26f6b4942361f924a74f52 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 19 Nov 2021 14:51:02 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/latex_dependency_scanner/scanner.py | 29 ++++++++++++++----------- tests/resources/acronyms.glstex | 2 +- tests/resources/symbols.bib | 1 - tests/test_scan.py | 15 ++++++++++--- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/latex_dependency_scanner/scanner.py b/src/latex_dependency_scanner/scanner.py index 1c5b686..ea12d23 100644 --- a/src/latex_dependency_scanner/scanner.py +++ b/src/latex_dependency_scanner/scanner.py @@ -20,15 +20,15 @@ """List[str]: List of typical image extensions contained in LaTeX files.""" COMMON_EXTENSIONS_IN_TEX = ( - [ - # No extension if the extension is provided. - "", - # TeX formats. - ".bib", - ".sty", - ] - + COMMON_GRAPHICS_EXTENSIONS - + COMMON_TEX_EXTENSIONS + [ + # No extension if the extension is provided. + "", + # TeX formats. + ".bib", + ".sty", + ] + + COMMON_GRAPHICS_EXTENSIONS + + COMMON_TEX_EXTENSIONS ) """List[str]: List of typical file extensions included in latex files""" @@ -66,9 +66,9 @@ def scan(paths: Union[Path, List[Path]]): def yield_nodes_from_node( - node: Path, - nodes: List[Path], - relative_to: Optional[Path] = None, + node: Path, + nodes: List[Path], + relative_to: Optional[Path] = None, ): r"""Yield nodes from node. @@ -134,7 +134,10 @@ def yield_nodes_from_node( common_extensions = [ext] else: common_extensions = COMMON_GRAPHICS_EXTENSIONS - elif match.group("type") in ["glsxtrresourcefile", "GlsXtrLoadResources"]: + elif match.group("type") in [ + "glsxtrresourcefile", + "GlsXtrLoadResources", + ]: common_extensions = [".glstex", ".bib"] # .bib for bib2gls elif match.group("type") == "lstinputlistings": common_extensions = [""] diff --git a/tests/resources/acronyms.glstex b/tests/resources/acronyms.glstex index e197a5a..d121d4b 100644 --- a/tests/resources/acronyms.glstex +++ b/tests/resources/acronyms.glstex @@ -1 +1 @@ -\newabbreviation{abc}{ABC}{AlphaBetaGamma} \ No newline at end of file +\newabbreviation{abc}{ABC}{AlphaBetaGamma} diff --git a/tests/resources/symbols.bib b/tests/resources/symbols.bib index fd40288..7a0a217 100644 --- a/tests/resources/symbols.bib +++ b/tests/resources/symbols.bib @@ -2,4 +2,3 @@ @symbol{Ab name = {\ensuremath{A_b}}, description = {Symbol Ab}, } - diff --git a/tests/test_scan.py b/tests/test_scan.py index 54cceac..560d996 100644 --- a/tests/test_scan.py +++ b/tests/test_scan.py @@ -411,7 +411,11 @@ def test_glossaries(tmp_path): nodes = scan(tmp_path / "document.tex") - assert nodes == [tmp_path / "document.tex", tmp_path / "symbols.bib", tmp_path / "acronyms.glstex"] + assert nodes == [ + tmp_path / "document.tex", + tmp_path / "symbols.bib", + tmp_path / "acronyms.glstex", + ] @pytest.mark.end_to_end @@ -453,5 +457,10 @@ def test_glossaries_without_files(tmp_path): 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"] + assert nodes == [ + tmp_path / "document.tex", + tmp_path / "symbols.glstex", + tmp_path / "symbols.bib", + tmp_path / "acronyms.glstex", + tmp_path / "acronyms.bib", + ] From d0e4990214e80d851f3d270b5727205181d43466 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Sat, 20 Nov 2021 10:12:02 +0100 Subject: [PATCH 4/5] Final touches to PR. --- CHANGES.rst | 7 +++++++ README.rst | 4 ++-- src/latex_dependency_scanner/scanner.py | 9 +++++++-- tests/test_regex.py | 2 +- tests/test_scan.py | 1 - 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 8e2e5e2..fc1df44 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,13 @@ in reverse chronological order. Releases follow `semantic versioning `_. +0.0.5 - 2021-11-20 +------------------ + +- :gh:`12` adds support for glossaries and glossaries-extra. Thanks to + :ghuser:`axtimhaus`! + + 0.0.4 - 2021-08-08 ------------------ diff --git a/README.rst b/README.rst index afeda43..f66bfe7 100644 --- a/README.rst +++ b/README.rst @@ -79,7 +79,7 @@ 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 @@ -87,7 +87,7 @@ mainly used for validating test cases. import latex_dependency_scanner as lds - lds.generate_pdf("document.tex", "document.pdf") + lds.compile_pdf("document.tex", "document.pdf") Changes diff --git a/src/latex_dependency_scanner/scanner.py b/src/latex_dependency_scanner/scanner.py index ea12d23..45afdac 100644 --- a/src/latex_dependency_scanner/scanner.py +++ b/src/latex_dependency_scanner/scanner.py @@ -6,9 +6,11 @@ from typing import Optional from typing import Union + COMMON_TEX_EXTENSIONS = [".ltx", ".tex"] """List[str]: List of typical file extensions that contain latex""" + COMMON_GRAPHICS_EXTENSIONS = [ # Image formats. ".eps", @@ -19,6 +21,7 @@ ] """List[str]: List of typical image extensions contained in LaTeX files.""" + COMMON_EXTENSIONS_IN_TEX = ( [ # No extension if the extension is provided. @@ -32,9 +35,11 @@ ) """List[str]: List of typical file extensions included in latex files""" + REGEX_TEX = re.compile( - r"\\(?Pusepackage|RequirePackage|include|addbibresource|bibliography|putbib|" - r"includegraphics|input|(sub)?import|lstinputlisting|glsxtrresourcefile|GlsXtrLoadResources)" + r"\\(?Pusepackage|RequirePackage|include|addbibresource|bibliography|putbib" + r"|includegraphics|input|(sub)?import|lstinputlisting|glsxtrresourcefile" + r"|GlsXtrLoadResources)" r"(<[^<>]*>)?" r"(\[[^\[\]]*\])?" r"({(?P[^{}]*)})?(\[[^\[\]]*src=)?{(?P[^{}]*)}", diff --git a/tests/test_regex.py b/tests/test_regex.py index d88329d..b8070e8 100644 --- a/tests/test_regex.py +++ b/tests/test_regex.py @@ -62,7 +62,7 @@ {"type": "glsxtrresourcefile", "file": "glsfile", "relative_to": None}, ), ( - "\\GlsXtrLoadResources[src={glsfile}]", + "\\GlsXtrLoadResources[src={glsfile}, selection={all}]", {"type": "GlsXtrLoadResources", "file": "glsfile", "relative_to": None}, ), ], diff --git a/tests/test_scan.py b/tests/test_scan.py index 560d996..b126bed 100644 --- a/tests/test_scan.py +++ b/tests/test_scan.py @@ -435,7 +435,6 @@ def test_glossaries_both_extensions_present(tmp_path): shutil.copy(TEST_RESOURCES / "acronyms.glstex", tmp_path / "symbols.glstex") nodes = scan(tmp_path / "document.tex") - print(nodes) assert nodes == [tmp_path / "document.tex", tmp_path / "symbols.glstex"] From b6f5aedc612f6aa5a9fa29e9bea3d74d60c45df7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 May 2022 09:09:11 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/latex_dependency_scanner/scanner.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/latex_dependency_scanner/scanner.py b/src/latex_dependency_scanner/scanner.py index e4d1c2d..393ddcc 100644 --- a/src/latex_dependency_scanner/scanner.py +++ b/src/latex_dependency_scanner/scanner.py @@ -7,7 +7,6 @@ from typing import Generator - COMMON_TEX_EXTENSIONS = [".ltx", ".tex"] """List[str]: List of typical file extensions that contain latex"""