From fa14b42354f8e4657683a1a649bf7ff87ff0c15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Tue, 28 May 2024 18:26:18 +0200 Subject: [PATCH 1/3] Update add keyword script with new option Added a new option --no-appendix to the add-keyword script. This can be used to avoid adding the keyword to the appendix --- scripts/python/README.md | 2 ++ scripts/python/src/fodt/add_keyword.py | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/scripts/python/README.md b/scripts/python/README.md index 3f74edbc..2519fdf9 100644 --- a/scripts/python/README.md +++ b/scripts/python/README.md @@ -46,6 +46,8 @@ to include the new file `parts/chapters/subsections/4.3/HELLO.fodt`. The generated file `HELLO.fodt` is created from a template such that it initially contains just the heading with the keyword name. +Note: In the rare case you want to add the same keyword name to different sections, you can avoid adding the keyword twice to Appendix A by giving option `--no-appendix`. + ## Changing the status of a keyword in Appendix A To change the status of a keyword in the status column in the alphabetical listing diff --git a/scripts/python/src/fodt/add_keyword.py b/scripts/python/src/fodt/add_keyword.py index ebce5ae1..9e37ed2d 100644 --- a/scripts/python/src/fodt/add_keyword.py +++ b/scripts/python/src/fodt/add_keyword.py @@ -225,7 +225,8 @@ def __init__( chapter: int, section: int, title: str, - status: KeywordStatus + status: KeywordStatus, + appendix: bool ) -> None: self.maindir = Helpers.get_maindir(maindir) self.keyword_dir = Helpers.get_keyword_dir(keyword_dir, self.maindir) @@ -234,10 +235,14 @@ def __init__( self.section = section self.title = title self.status = status + self.appendix = appendix self.add_keyword() self.update_subdocument() self.update_chapter_document() - self.update_appendixA() + if self.appendix: + self.update_appendixA() + else: + logging.info("Not updating appendix A since requested not to.") def add_keyword(self) -> None: self.documentdir = Path(self.maindir) / Directories.chapters @@ -338,13 +343,15 @@ def update_subdocument(self) -> None: '--title', type=str, required=True, help='The link text displayed in the appendix.' ) @click.option('--status', type=str, required=True, help='The status of the keyword.') +@click.option('--appendix/--no-appendix', type=bool, default=True, help="Include keyword in appendix or not.") def add_keyword( maindir: str, keyword_dir: str, keyword: str, section: str, title: str, - status: str + status: str, + appendix: bool ) -> None: logging.basicConfig(level=logging.INFO) (chapter, section) = Helpers.split_section(section) @@ -352,7 +359,7 @@ def add_keyword( status = KeywordStatus[status.upper()] except ValueError: raise ValueError(f"Invalid status value: {status}.") - add_keyword = AddKeyword(maindir, keyword_dir, keyword, chapter, section, title, status) + add_keyword = AddKeyword(maindir, keyword_dir, keyword, chapter, section, title, status, appendix) if __name__ == "__main__": add_keyword() \ No newline at end of file From 721fe6cd62cf5da07c225fcb079e3b9c2c3b7efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Tue, 28 May 2024 21:03:34 +0200 Subject: [PATCH 2/3] Made bookmark heading name more specific --- scripts/python/src/fodt/add_keyword.py | 17 +++++++++++++-- scripts/python/src/fodt/create_subdocument.py | 21 ++++++++++++------- .../python/src/fodt/data/keyword_template.xml | 2 +- scripts/python/src/fodt/templates.py | 2 +- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/scripts/python/src/fodt/add_keyword.py b/scripts/python/src/fodt/add_keyword.py index 9e37ed2d..24f7d5d7 100644 --- a/scripts/python/src/fodt/add_keyword.py +++ b/scripts/python/src/fodt/add_keyword.py @@ -19,10 +19,19 @@ from fodt.xml_helpers import XMLHelper class AppendixHandler(xml.sax.handler.ContentHandler): - def __init__(self, keyword: str, status: KeywordStatus, title: str) -> None: + def __init__( + self, + keyword: str, + status: KeywordStatus, + title: str, + chapter: int, + section: int + ) -> None: self.keyword_name = keyword self.keyword_status = status self.keyword_title = title + self.keyword_chapter = chapter + self.keyword_section = section self.in_styles = False self.content = io.StringIO() self.current_row = io.StringIO() @@ -124,7 +133,9 @@ def get_keyword_table_number(self) -> int: def get_new_appendix_row(self) -> str: new_row = Templates.AppendixA.Content.table_row_template + bookmark_name = f"{self.keyword_name}_{self.keyword_chapter}_{self.keyword_section}" new_row = re.sub(r'###KEYWORD_NAME###', self.keyword_name, new_row) + new_row = re.sub(r'###KEYWORD_NAME_BOOKMARK###', bookmark_name, new_row) new_row = re.sub(r'###KEYWORD_DESCRIPTION###', self.keyword_title, new_row) if self.keyword_status == KeywordStatus.ORANGE: color = "Orange" @@ -266,7 +277,9 @@ def update_appendixA(self) -> None: raise FileNotFoundError(f"File {self.filename} not found.") # parse the xml file parser = xml.sax.make_parser() - handler = AppendixHandler(self.keyword, self.status, self.title) + handler = AppendixHandler( + self.keyword, self.status, self.title, self.chapter, self.section + ) parser.setContentHandler(handler) parser.parse(self.filename) # Take a backup of the file diff --git a/scripts/python/src/fodt/create_subdocument.py b/scripts/python/src/fodt/create_subdocument.py index e974f580..91c0cbf1 100644 --- a/scripts/python/src/fodt/create_subdocument.py +++ b/scripts/python/src/fodt/create_subdocument.py @@ -11,12 +11,16 @@ from fodt.styles_filter import StylesFilter class CreateSubDocument(): - def create_documents(self, parts: list[str]) -> None: + def create_documents(self, parts: list[str], bookmark_info: list[str]|None = None) -> None: + # NOTE: The bookmark_info parameter is used by CreateSubDocument3 to differentiate + # between keywords with the same name in different chapters. + # See: https://github.com/OPM/opm-reference-manual/pull/259 outputdir = self.outputdir if not self.is_chapter: outputdir = outputdir / f"{self.chapter}.{self.section}" outputdir.mkdir(parents=True, exist_ok=True) - for part in parts: + for (i, part) in enumerate(parts): + part_extra = bookmark_info[i] if bookmark_info is not None else None outputfile: Path = outputdir / f"{part}.{FileExtensions.fodt}" if outputfile.exists(): logging.info(f"Skipping {outputfile} because it already exists.") @@ -27,12 +31,14 @@ def create_documents(self, parts: list[str]) -> None: self.write_office_document_start_tag() self.write_meta(part) self.write_office_body_start_tag(part) - self.write_section(part) + self.write_section(part, part_extra) self.write_xml_footer() logging.info(f"Created FODT subdocument {outputfile}") - def create_subsection_template(self, part: str) -> str: + def create_subsection_template(self, part: str, part_extra: str|None) -> str: template = Helpers.read_keyword_template() + part_bookmark = f"{part}_{part_extra}" if part_extra is not None else part + template = re.sub(r"###KEYWORD_NAME_BOOKMARK###", part_bookmark, template) template = re.sub(r"###KEYWORD_NAME###", part, template) if self.add_keyword: description = self.title @@ -101,7 +107,7 @@ def write_meta_section(self, section_name: str, part: str) -> None: content = filter.get_filtered_content() self.outputfile.write(content) - def write_section(self, part: str) -> None: + def write_section(self, part: str, part_extra: str|None) -> None: if self.is_chapter: dir_ = self.extracted_sections_dir else: @@ -113,7 +119,7 @@ def write_section(self, part: str) -> None: content = f.read() else: if not self.is_chapter: - content = self.create_subsection_template(part) + content = self.create_subsection_template(part, part_extra) if content is None: raise InputException(f"Could not find section file {section_file}") self.outputfile.write("\n") @@ -195,7 +201,8 @@ def __init__( self.outputdir = self.documentdir / Directories.subsections self.is_chapter = False parts = [self.keyword] + parts_extra = [f"{self.chapter}_{self.section}"] keyw_list = Helpers.read_keyword_order_v2(self.keyword_dir, chapter, section) self.keywords = Helpers.keywords_inverse_map(keyw_list) self.add_keyword = True - self.create_documents(parts) + self.create_documents(parts, parts_extra) diff --git a/scripts/python/src/fodt/data/keyword_template.xml b/scripts/python/src/fodt/data/keyword_template.xml index 365bc119..5e06dbeb 100644 --- a/scripts/python/src/fodt/data/keyword_template.xml +++ b/scripts/python/src/fodt/data/keyword_template.xml @@ -1,4 +1,4 @@ - ###KEYWORD_NAME### – ###SHORT_DESCRIPTION### + ###KEYWORD_NAME### – ###SHORT_DESCRIPTION### diff --git a/scripts/python/src/fodt/templates.py b/scripts/python/src/fodt/templates.py index cdd1b7ba..459f6d98 100644 --- a/scripts/python/src/fodt/templates.py +++ b/scripts/python/src/fodt/templates.py @@ -35,7 +35,7 @@ class Content: """text:style-name="Internet_20_link" """ """text:visited-style-name="Visited_20_Internet_20_Link">""" """""" + """text:ref-name="REF_HEADING_KEYWORD_###KEYWORD_NAME_BOOKMARK###">""" """###KEYWORD_NAME### – ###KEYWORD_DESCRIPTION###""" """\n""" """ From 559acc513be253bc5ea9ecbc84f72bbbe6be7821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Wed, 29 May 2024 11:31:56 +0200 Subject: [PATCH 3/3] Also change bookmark end tag name --- scripts/python/src/fodt/data/keyword_template.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/python/src/fodt/data/keyword_template.xml b/scripts/python/src/fodt/data/keyword_template.xml index 5e06dbeb..4013c2b1 100644 --- a/scripts/python/src/fodt/data/keyword_template.xml +++ b/scripts/python/src/fodt/data/keyword_template.xml @@ -1,4 +1,4 @@ - ###KEYWORD_NAME### – ###SHORT_DESCRIPTION### + ###KEYWORD_NAME### – ###SHORT_DESCRIPTION###