Skip to content

Commit

Permalink
Merge pull request #465 from hakonhagland/upd_script_app
Browse files Browse the repository at this point in the history
Update keyword linker script
  • Loading branch information
lisajulia authored Jan 10, 2025
2 parents 187a8db + 097b32c commit 0757784
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions scripts/python/src/fodt/keyword_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
class FileType(enum.Enum):
CHAPTER = 1
SUBSECTION = 2
APPENDIX = 3

@dataclass
class MonoParagraphStyle:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -311,19 +314,28 @@ 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:
files_processed = 0
for item in self.start_dir.iterdir():
if not item.is_dir():
continue
Expand All @@ -339,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()
Expand Down Expand Up @@ -382,11 +399,12 @@ def load_kw_uri_map(maindir: Path) -> dict[str, str]:
#
# SHELL USAGE:
#
# fodt-link-keyword \
# fodt-link-keywords \
# --maindir=<main_dir> \
# --keyword_dir=<keyword_dir> \
# --subsection=<subsection> \
# --chapter=<chapter> \
# --appendix=<appendix> \
# --filename=<filename> \
# --use-map-file
#
Expand All @@ -403,9 +421,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:
#
Expand All @@ -427,13 +446,15 @@ 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('--use-map-file', is_flag=True, help='Use the mapping file "meta/kw_uri_map.txt"')
@click.option('--appendix', help='The appendix to process')
@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,
keyword_dir: str|None,
subsection: str|None,
chapter: str|None,
appendix: str|None,
filename: str|None,
use_map_file: bool
) -> None:
Expand All @@ -444,13 +465,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, subsection]) != 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()

0 comments on commit 0757784

Please sign in to comment.