From d79b9e929af222fe14b15c4f2990dbfebbe59abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Tue, 7 Jan 2025 18:21:22 +0100 Subject: [PATCH 1/3] Update keyword linker script Update keyword linker script to handle appendices. --- scripts/python/src/fodt/keyword_linker.py | 33 +++++++++++++++++------ 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/scripts/python/src/fodt/keyword_linker.py b/scripts/python/src/fodt/keyword_linker.py index 782e255c..924a0728 100644 --- a/scripts/python/src/fodt/keyword_linker.py +++ b/scripts/python/src/fodt/keyword_linker.py @@ -20,6 +20,7 @@ class FileType(enum.Enum): CHAPTER = 1 SUBSECTION = 2 + APPENDIX = 3 @dataclass class MonoParagraphStyle: @@ -226,7 +227,8 @@ def maybe_write_characters(self) -> None: and (not self.in_mono_paragraph) ): if not self.is_example_p[-1]: - if (self.file_type == FileType.CHAPTER or + if (( self.file_type == FileType.CHAPTER + or self.file_type == FileType.APPENDIX) or (self.file_type == FileType.SUBSECTION and (not self.is_table_caption(characters)))): characters = self.regex.sub(self.replace_match_function, characters) @@ -301,6 +303,7 @@ def __init__( maindir: Path, subsection: str|None, chapter: str|None, + appendix: str|None, filename: str|None, start_dir: Path, kw_uri_map: dict[str, str] @@ -311,18 +314,26 @@ def __init__( self.subsection = subsection self.chapter = chapter self.filename = filename + self.appendix = appendix def insert_links(self) -> None: if self.chapter: self.insert_links_in_chapter() - else: + elif self.subsection: self.insert_links_in_subsections() + else: + self.insert_links_in_appendix() def insert_links_in_chapter(self) -> None: filename = f"{self.chapter}.{FileExtensions.fodt}" path = self.start_dir / filename self.insert_links_in_file(path, filename, FileType.CHAPTER) + def insert_links_in_appendix(self) -> None: + filename = f"{self.appendix}.{FileExtensions.fodt}" + path = self.start_dir / filename + self.insert_links_in_file(path, filename, FileType.APPENDIX) + def insert_links_in_subsections(self) -> None: for item in self.start_dir.iterdir(): if not item.is_dir(): @@ -387,6 +398,7 @@ def load_kw_uri_map(maindir: Path) -> dict[str, str]: # --keyword_dir= \ # --subsection= \ # --chapter= \ +# --appendix= \ # --filename= \ # --use-map-file # @@ -403,9 +415,10 @@ def load_kw_uri_map(maindir: Path) -> dict[str, str]: # # If --subsection is not given, the script will process all subsections. If --subsection # is given, the script will only process the specified subsection, or if --chapter is -# given, the script will only process the specified chapter, or if --filename is -# given, the script will only process the specified file within the specified subsection. -# The options --chapter and --subsection are mutually exclusive. +# given, the script will only process the specified chapter, or if --appendix is given, +# the script will only process the specified appendix. If --filename is given, the script +# will only process the specified file within the specified subsection. +# The options --appendix, --chapter and --subsection are mutually exclusive. # # EXAMPLES: # @@ -427,6 +440,7 @@ def load_kw_uri_map(maindir: Path) -> dict[str, str]: @ClickOptions.keyword_dir @click.option('--subsection', help='The subsection to process') @click.option('--chapter', help='The chapter to process') +@click.option('--appendix', help='The appendix to process') @click.option('--use-map-file', is_flag=True, help='Use the mapping file "meta/kw_uri_map.txt"') @click.option('--filename', help='The filename to process') def link_keywords( @@ -434,6 +448,7 @@ def link_keywords( keyword_dir: str|None, subsection: str|None, chapter: str|None, + appendix: str|None, filename: str|None, use_map_file: bool ) -> None: @@ -444,13 +459,15 @@ def link_keywords( kw_uri_map = load_kw_uri_map(maindir) else: kw_uri_map = keyword_uri_map_generator.get_kw_uri_map(maindir, keyword_dir) - if chapter and subsection: - raise ValueError("Options --chapter and --subsection are mutually exclusive.") + if sum(x is not None for x in [chapter, appendix, filename]) != 1: + raise ValueError("Options --appendix, --chapter and --subsection are mutually exclusive.") if chapter: file_dir = maindir / Directories.chapters + elif appendix: + file_dir = maindir / Directories.appendices else: file_dir = maindir / Directories.chapters / Directories.subsections - InsertLinks(maindir, subsection, chapter, filename, file_dir, kw_uri_map).insert_links() + InsertLinks(maindir, subsection, chapter, appendix, filename, file_dir, kw_uri_map).insert_links() if __name__ == "__main__": link_keywords() From 5189250fec49ad73d8194018bc0fb405f606dc79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Fri, 10 Jan 2025 11:40:51 +0100 Subject: [PATCH 2/3] Fix bug with command line arguments Fixed typo that lead to unexpected behavior that allowed both --subsection and --appendix to be given, even though they should be mutually exclusive. --- scripts/python/src/fodt/keyword_linker.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/python/src/fodt/keyword_linker.py b/scripts/python/src/fodt/keyword_linker.py index 924a0728..673a8458 100644 --- a/scripts/python/src/fodt/keyword_linker.py +++ b/scripts/python/src/fodt/keyword_linker.py @@ -335,6 +335,7 @@ def insert_links_in_appendix(self) -> None: self.insert_links_in_file(path, filename, FileType.APPENDIX) def insert_links_in_subsections(self) -> None: + files_processed = 0 for item in self.start_dir.iterdir(): if not item.is_dir(): continue @@ -350,7 +351,12 @@ def insert_links_in_subsections(self) -> None: logging.info(f"Skipping file: {item2.name}") continue keyword_name = item2.name.removesuffix(f".{FileExtensions.fodt}") + files_processed += 1 self.insert_links_in_file(item2, keyword_name, FileType.SUBSECTION) + if files_processed == 0: + logging.info("No files processed.") + else: + logging.info(f"Processed {files_processed} files.") def insert_links_in_file(self, filename: Path, file_info: str, file_type: FileType) -> None: parser = xml.sax.make_parser() @@ -441,7 +447,7 @@ def load_kw_uri_map(maindir: Path) -> dict[str, str]: @click.option('--subsection', help='The subsection to process') @click.option('--chapter', help='The chapter to process') @click.option('--appendix', help='The appendix to process') -@click.option('--use-map-file', is_flag=True, help='Use the mapping file "meta/kw_uri_map.txt"') +@click.option('--use-map-file', is_flag=True, default=True, help='Use the mapping file "meta/kw_uri_map.txt". This is generally recommended to speed up the processing. Only if we suspect that libreoffice might have changed the references to the keywords, we should generate the map on the fly. In this case, the map file should also be regenerated and committed to the repository.') @click.option('--filename', help='The filename to process') def link_keywords( maindir: str|None, @@ -459,7 +465,7 @@ def link_keywords( kw_uri_map = load_kw_uri_map(maindir) else: kw_uri_map = keyword_uri_map_generator.get_kw_uri_map(maindir, keyword_dir) - if sum(x is not None for x in [chapter, appendix, filename]) != 1: + if sum(x is not None for x in [chapter, appendix, subsection]) != 1: raise ValueError("Options --appendix, --chapter and --subsection are mutually exclusive.") if chapter: file_dir = maindir / Directories.chapters From 097b32cddb43fc0db7168b45c3a79aeaea448748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Fri, 10 Jan 2025 11:45:10 +0100 Subject: [PATCH 3/3] Fixed typo in the comments --- scripts/python/src/fodt/keyword_linker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/python/src/fodt/keyword_linker.py b/scripts/python/src/fodt/keyword_linker.py index 673a8458..d3accef8 100644 --- a/scripts/python/src/fodt/keyword_linker.py +++ b/scripts/python/src/fodt/keyword_linker.py @@ -399,7 +399,7 @@ def load_kw_uri_map(maindir: Path) -> dict[str, str]: # # SHELL USAGE: # -# fodt-link-keyword \ +# fodt-link-keywords \ # --maindir= \ # --keyword_dir= \ # --subsection= \