def section_tag_cleanup(debug_dir: str, tag_dict: dict, line: str): """ If the tag registry shows a closed section, insert an open section tag and an open paragraph tag. If it shows an open section, close it and open a new section and a new paragraph. """ tag_registry = os.path.join(debug_dir, "../../debugdir/tag_registry.json") with open(tag_registry) as tag_registry_pre: tag_registry = json.load(tag_registry_pre) tag_closed = "0" if tag_registry["section"] == tag_closed: content_update = tag_dict["section-beg"] output_file_update.content_append(debug_dir=debug_dir, content_update=content_update) try: if logger_debug.isEnabledFor(logging.DEBUG): msg = str(tag_dict["section-beg"] + f"{line}") logger_debug.error(msg) except AttributeError: logging.exception("Check setLevel for logger_debug.") else: # If a section tag is open, close it and open a new section. content_update = tag_dict["section-end"] + tag_dict["section-beg"] output_file_update.content_append(debug_dir=debug_dir, content_update=content_update) try: if logger_debug.isEnabledFor(logging.DEBUG): msg = str(tag_dict["section-end"] + tag_dict["section-beg"] + f"{line}") logger_debug.error(msg) except AttributeError: logging.exception("Check setLevel for logger_debug.")
def insert_opening_header_tag(debug_dir: str, tag_dict: dict, line_to_read: str) -> None: content_update = tag_dict["header-beg"] output_file_update.content_append(debug_dir=debug_dir, content_update=content_update) try: if logger_debug.isEnabledFor(logging.DEBUG): logger_debug.error(msg=str(tag_dict["header-beg"] + f"{line_to_read}")) except AttributeError: logging.exception("Check setLevel for logger_debug.")
def insert_closing_footnote_tag(debug_dir: str, tag_dict: dict, line: str) -> None: content_update = tag_dict["footnote-end"] output_file_update.content_append(debug_dir=debug_dir, content_update=content_update) try: if logger_debug.isEnabledFor(logging.DEBUG): msg = str(f"({line})" + tag_dict["footnote-end"]) logger_debug.error(msg) except AttributeError: logging.exception("Check setLevel for logger_debug.")
def insert_opening_cs_tag(cs_line_dict: dict, text: str, tag_dict: dict, debug_dir: str, line_to_read: str): list_of_tags = [] tag_tracker = 0 tag_closed = "0" tag_open = "1" for key in cs_line_dict: if cs_line_dict[key] == tag_closed: pass else: tag_tracker = 1 list_of_tags.append(key) if tag_tracker == 1: tag = "" tag_bag = [] # Create a string of all tags to open. for lot_item in list_of_tags: tag = tag + tag_dict[lot_item + "-beg"] tag_bag.append(lot_item) # Update the tag registry. content_update_dict = {lot_item: tag_open} tag_registry_update.processor(debug_dir=debug_dir, tag_update_dict=content_update_dict) try: if logger_debug.isEnabledFor(logging.DEBUG): logger_debug.error(msg=str(tag_dict[lot_item + "-beg"] + f" {line_to_read}")) except AttributeError: logging.exception("Check setLevel for logger_debug.") # To the string of tags, add the cs line text. Write the string to the # working_xml_file. content_update = tag + text output_file_update.content_append(debug_dir, content_update) else: # If no tags need to be added, just write the text to the file. tag_bag = [] content_update = text output_file_update.content_append(debug_dir, content_update) return tag_bag
def insert_closing_header_tag(debug_dir: str, tag_dict: dict, line: str) -> None: # TODO At least in TPRES, a header may be embedded in a paragraph or fall # at the end or beginning of a paragraph. See also footnote. """ Insert the header closing tag. Note that a header ending is different than a footnote ending. Headers are separate blocks and need a paragraph opening tag afterwards. """ content_update = tag_dict["header-end"] + tag_dict["paragraph-beg"] output_file_update.content_append(debug_dir=debug_dir, content_update=content_update) try: if logger_debug.isEnabledFor(logging.DEBUG): msg = str(tag_dict["header-end"] + tag_dict["paragraph-beg"] + f"{line}") logger_debug.error(msg) except AttributeError: logging.exception("Check setLevel for logger_debug.")
def insert_closing_cs_tags(debug_dir: str, tag_dict: dict, tag_bag: list, line: str): """ All tags opened by cs_opening_tags must be closed. This may happen immediately after we write the tags and text to the working_xml_file if the cs text line does not contain any nested tags (e.g., a footnote or paragraph). Or, it may happen after processing the nested lines if they exist. """ tag = "" tag_closed = "0" # If the tag_bag is empty, no tags need to be closed and control should # be returned to the line_parser module. if not tag_bag: pass else: # If there are tags in the tag_bag, reverse the order of the tags. tag_bag.reverse() # Add a closing tag for each tag in the reversed list. for tag_item in tag_bag: tag = tag + tag_dict[tag_item + "-end"] # Update the tag registry. content_update_dict = {tag_item: tag_closed} tag_registry_update.processor(debug_dir=debug_dir, tag_update_dict=content_update_dict) try: if logger_debug.isEnabledFor(logging.DEBUG): msg = str(f"{line} " + tag_dict[tag_item + "-end"]) logger_debug.error(msg) except AttributeError: logging.exception("Check setLevel for logger_debug.") # Add the closing tags to the working_xml_file. content_update = tag output_file_update.content_append(debug_dir, content_update) # Emptying the tag_bag prevents unwanted closing tags from being # added when there are nested tags in the cs text line. tag_bag.clear()