Commit 2fcd1756 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Fix for text properties being applied to clause titles

parent d3f8fd2f
Loading
Loading
Loading
Loading
Loading
+90 −26
Changes for generateBaseline/postprocessing.py: 90 added lines, 26 removed lines.
Original line number Diff line number Diff line
@@ -1090,16 +1090,89 @@ def update_heading_styles(docx_input, docx_output):
            sep_t = OxmlElement('w:t')
            sep_t.text = " "
            sep_run.append(sep_t)
        para.append(num_run)
        para.append(sep_run)
        # The title is appended separately so its superscript and subscript runs stay intact.
        if rest_text is not None:
            rest_run = OxmlElement('w:r')
            rest_t = OxmlElement('w:t')
            rest_t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
            rest_t.text = rest_text
            rest_run.append(rest_t)
        para.append(num_run)
        para.append(sep_run)
            para.append(rest_run)
        return num_run

    def _plain_run_text(run):
        parts = []
        for child in run:
            local = etree.QName(child).localname
            if local == 't' and child.text:
                parts.append(child.text)
            elif local == 'tab':
                parts.append(' ')
        return ''.join(parts)

    def _drop_plain_prefix(run, count):
        """Remove the first count characters. A tab counts as one, matching the heading text."""
        remaining = count
        for child in list(run):
            if remaining <= 0:
                break
            local = etree.QName(child).localname
            if local == 'tab':
                run.remove(child)
                remaining -= 1
                continue
            if local != 't' or not child.text:
                continue
            if remaining >= len(child.text):
                remaining -= len(child.text)
                run.remove(child)
            else:
                child.text = child.text[remaining:]
                if child.text[:1].isspace() or child.text[-1:].isspace():
                    child.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
                remaining = 0
        return remaining

    def _run_has_content(run):
        for child in run:
            local = etree.QName(child).localname
            if local == 't' and child.text:
                return True
            if local in ('tab', 'br', 'drawing', 'object', 'pict', 'sym'):
                return True
        return False

    def _take_title_runs(para, runs, prefix_len):
        """Detach the heading runs and return the title, including superscript and subscript runs."""
        title_runs = []
        remaining = prefix_len
        for run in runs:
            if remaining <= 0:
                para.remove(run)
                title_runs.append(run)
                continue
            run_len = len(_plain_run_text(run))
            if run_len <= remaining:
                remaining -= run_len
                para.remove(run)
                continue
            _drop_plain_prefix(run, remaining)
            remaining = 0
            para.remove(run)
            if _run_has_content(run):
                title_runs.append(run)
        return title_runs

    def _append_title(para, title_runs, fallback_text):
        if title_runs:
            for run in title_runs:
                para.append(run)
            return
        if fallback_text:
            _append_text_run(para, fallback_text)

    def _append_text_run(para, text):
        run = OxmlElement('w:r')
        text_elem = OxmlElement('w:t')
@@ -1166,12 +1239,10 @@ def update_heading_styles(docx_input, docx_output):
            number = match.group(1)
            rest_text = match.group(2)
            
            # Clear all existing runs from the paragraph
            for run in runs:
                para.remove(run)
            
            # Create new run with number + tab + rest
            label_run = create_new_run(para, number, rest_text, "tab")
            # Keep the title runs so a superscript or subscript in the title survives.
            title_runs = _take_title_runs(para, runs, match.start(2))
            label_run = create_new_run(para, number, None, "tab")
            _append_title(para, title_runs, rest_text)
            counter += 1
            _place_label_bookmark(para, label_run, number, rest_text)
            # Insert page break before "Scope" Heading1 and "History" Heading1
@@ -1185,10 +1256,8 @@ def update_heading_styles(docx_input, docx_output):
            annex_letter = match.group(2)
            annex_title = match.group(3)
            
            # Clear all existing runs from the paragraph
            for run in runs:
                para.remove(run)
            
            # Keep the title runs so a superscript or subscript in the title survives.
            title_runs = _take_title_runs(para, runs, match.start(3))
            # 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)
@@ -1199,9 +1268,10 @@ def update_heading_styles(docx_input, docx_output):
                sep_run = OxmlElement('w:r')
                sep_run.append(OxmlElement('w:br'))
                para.append(sep_run)
                _append_text_run(para, annex_title)
                _append_title(para, title_runs, annex_title)
            else:
                label_run = create_new_run(para, annex_label, annex_title, "break")
                label_run = create_new_run(para, annex_label, None, "break")
                _append_title(para, title_runs, annex_title)
            # Change style from Heading 1 to Heading 8
            pStyle_elems = para.xpath('./w:pPr/w:pStyle[@w:val="Heading1"]', namespaces=ns)
            if pStyle_elems:
@@ -1217,12 +1287,9 @@ def update_heading_styles(docx_input, docx_output):
            annex_letter = match.group(1)
            annex_title = match.group(2)
            
            # 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, "tab")
            title_runs = _take_title_runs(para, runs, match.start(2))
            label_run = create_new_run(para, annex_letter, None, "tab")
            _append_title(para, title_runs, annex_title)
            # Change style from Heading 2 to Heading 9
            pStyle_elems = para.xpath('./w:pPr/w:pStyle[@w:val="Heading2"]', namespaces=ns)
            if pStyle_elems:
@@ -1243,12 +1310,9 @@ def update_heading_styles(docx_input, docx_output):
                    pStyle_elems[0].set(f"{{{ns['w']}}}val", f"Heading{i-1}")# Heading9 for certain specs. editHelp is going to harmonise that    
                    counter_annex += 1

            # 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, "tab")
            title_runs = _take_title_runs(para, runs, match.start(2))
            label_run = create_new_run(para, annex_letter, None, "tab")
            _append_title(para, title_runs, annex_title)
            counter_annex += 1

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