Commit 4103a627 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Fix bookmarks for annexes main clause

parent 6bcf7082
Loading
Loading
Loading
Loading
Loading
+39 −15
Changes for generateBaseline/postprocessing.py: 39 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -1100,6 +1100,16 @@ def update_heading_styles(docx_input, docx_output):
        para.append(rest_run)
        return num_run

    def _append_text_run(para, text):
        run = OxmlElement('w:r')
        text_elem = OxmlElement('w:t')
        if text[:1].isspace() or text[-1:].isspace():
            text_elem.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
        text_elem.text = text
        run.append(text_elem)
        para.append(run)
        return run

    def _place_label_bookmark(para, label_run, number, title):
        nonlocal next_bookmark_id
        if label_run is None:
@@ -1162,24 +1172,37 @@ def update_heading_styles(docx_input, docx_output):
                insert_page_break_before_heading(para, full_text)

        # Annex heading: "Annex A: Title" or "Annex A (informative): Title" or "Annex: Title"
        elif (match := re.match(r'^(Annex\s[A-Z]?(?:\s*\((?:informative|normative|informative or normative|normative or informative)\))?:|Annex:\s*)\s*(.+)$', full_text, re.IGNORECASE)) is not None:
            annex_letter = match.group(1)
            annex_title = match.group(2)
        # Group 1 is the full label, group 2 is the annex letter, group 3 is the title.
        elif (match := re.match(r'^(Annex\s([A-Z])?(?:\s*\((?:informative|normative|informative or normative|normative or informative)\))?:|Annex:\s*)\s*(.+)$', full_text, re.IGNORECASE)) is not None:
            annex_label = match.group(1)
            annex_letter = match.group(2)
            annex_title = match.group(3)
            
            # Clear all existing runs from the paragraph
            for run in runs:
                para.remove(run)
            
            # Create new run with number + break + rest
            label_run = create_new_run(para, annex_letter, annex_title, "break")
            # The bookmark covers the annex letter only, so a "Clause A" field shows "A".
            if annex_letter:
                parts = re.match(rf'^(Annex\s+)({re.escape(annex_letter)})(.*)$', annex_label.rstrip(), re.IGNORECASE)
                _append_text_run(para, parts.group(1))
                label_run = _append_text_run(para, parts.group(2))
                if parts.group(3):
                    _append_text_run(para, parts.group(3))
                sep_run = OxmlElement('w:r')
                sep_run.append(OxmlElement('w:br'))
                para.append(sep_run)
                _append_text_run(para, annex_title)
            else:
                label_run = create_new_run(para, annex_label, annex_title, "break")
            # Change style from Heading 1 to Heading 8
            pStyle_elems = para.xpath('./w:pPr/w:pStyle[@w:val="Heading1"]', namespaces=ns)
            if pStyle_elems:
                if re.search(r'(informative|normative|informative or normative|normative or informative)', annex_letter, re.IGNORECASE):
                if re.search(r'(informative|normative|informative or normative|normative or informative)', annex_label, re.IGNORECASE):
                    pStyle_elems[0].set(f"{{{ns['w']}}}val", "Heading8")# Heading8 for certain specs. editHelp is going to harmonise that 
                else:
                    pStyle_elems[0].set(f"{{{ns['w']}}}val", "Heading9")# Heading9 for certain specs. editHelp is going to harmonise that 
            _place_label_bookmark(para, label_run, annex_letter, annex_title)
            _place_label_bookmark(para, label_run, (annex_letter or 'annex').lower(), annex_title)
            insert_page_break_before_heading(para, full_text)  
            counter_annex += 1
        # Annex subheading Heading 2: "A.1 Title", not matching "A.1.1 Title"
@@ -1199,7 +1222,7 @@ def update_heading_styles(docx_input, docx_output):
                pStyle_elems[0].set(f"{{{ns['w']}}}val", "Heading1")# Heading9 for certain specs. editHelp is going to harmonise that    
            counter_annex += 1

            _place_label_bookmark(para, label_run, annex_letter, annex_title)
            _place_label_bookmark(para, label_run, annex_letter.lower(), annex_title)

        # Annex subheading Heading 3+: "A.1.1 Title", "A.1.1.1 Title"
        elif (match := re.match(r'^\s*([A-Z]\.\d+(?:\.\d+)+)\s+(.+)$', full_text)) is not None:
@@ -1221,7 +1244,7 @@ def update_heading_styles(docx_input, docx_output):
            label_run = create_new_run(para, annex_letter, annex_title, "tab")
            counter_annex += 1

            _place_label_bookmark(para, label_run, annex_letter, annex_title)
            _place_label_bookmark(para, label_run, annex_letter.lower(), annex_title)

        elif re.match(r'^\s*History\s*', full_text):
            insert_page_break_before_heading(para, full_text)
@@ -3351,8 +3374,8 @@ def update_references_style(docx_input, docx_output):

    # Pattern to match internal references: [i.1] or [1] or [i.12] or [12]
    reference_pattern = re.compile(r'^\[(i\.)?\d+\]$', re.IGNORECASE)
    # Clause 6.2.3, Figure 6-1 or Figure 6.2-1, Table 7.1-1.
    clause_pattern = re.compile(r'^clauses?\s+(\d+(?:\.\d+)*)$', re.IGNORECASE)
    # Clause 6.2.3, Clause A, or Clause A.1. Figure 6-1 or Figure 6.2-1, Table 7.1-1.
    clause_pattern = re.compile(r'^(?:clauses?|annex(?:es)?)\s+((?:[A-Z]|\d+)(?:\.\d+)*)$', re.IGNORECASE)
    figure_pattern = re.compile(r'^figures?\s+\d+(?:\.\d+)*-\d+$', re.IGNORECASE)
    table_pattern = re.compile(r'^tables?\s+\d+(?:\.\d+)*-\d+$', re.IGNORECASE)

@@ -3375,8 +3398,8 @@ def update_references_style(docx_input, docx_output):
    for start in root.xpath('.//w:bookmarkStart', namespaces=ns):
        name = start.get(f"{{{ns['w']}}}name")
        number = _bookmark_inner_text(start).replace('\u00A0', ' ').strip()
        if name and re.fullmatch(r'\d+(?:\.\d+)*', number) and name.startswith(number.replace('.', '') + '-'):
            clause_targets[number] = name
        if name and re.fullmatch(r'(?:[A-Z]|\d+)(?:\.\d+)*', number, re.IGNORECASE) and name.lower().startswith(number.replace('.', '').lower() + '-'):
            clause_targets[number.lower()] = name

    # Old declaration bookmark name -> REF_ bookmark added by update_references.
    # The old bookmarks stay in the document.
@@ -3422,7 +3445,8 @@ def update_references_style(docx_input, docx_output):
        clause_match = clause_pattern.match(full_text)
        is_reference = reference_pattern.match(full_text) is not None
        caption_name = _caption_bookmark_name(full_text)
        clause_name = clause_targets.get(clause_match.group(1)) if clause_match else None
        clause_number = clause_match.group(1) if clause_match else None
        clause_name = clause_targets.get(clause_number.lower()) if clause_number else None
        is_figure = bool(figure_pattern.match(full_text) and _anchor_matches_bookmark(anchor, caption_name))
        is_table = bool(table_pattern.match(full_text) and _anchor_matches_bookmark(anchor, caption_name))
        is_clause = bool(clause_match and _anchor_matches_bookmark(anchor, clause_name))
@@ -3587,7 +3611,7 @@ def add_non_breaking_spaces(docx_input, docx_output):
    # RFC 1234
    # 3GPP TS 29.122 
    ref_phrase = re.compile(
        r'\b((?:clauses?|tables?|figures?|sections?|appendices?|annexes?|RFCs?|3GPP TS)\s+(?:[A-Z](?:\.\d+)+|\d+(?:[.\-]\d+)*))',
        r'\b((?:clauses?|tables?|figures?|sections?|appendi(?:x|ces)|annex(?:es)?|RFCs?|3GPP TS)\s+(?:[A-Z](?:\.\d+)+|\d+(?:[.\-]\d+)*))',
        re.IGNORECASE,
    )