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

Support for code block inside examples

parent 8fd18fad
Loading
Loading
Loading
Loading
Loading
+32 −4
Original line number Diff line number Diff line
@@ -1933,10 +1933,10 @@ def update_notes(docx_input, docx_output):
        all_children = [child for child in para if child.tag != f"{{{ns['w']}}}pPr"]
        
        # Check if text matches NOTE pattern: NOTE\s*\d*:
        note_pattern = re.compile(r'NOTE\s*\d*:\s+')
        note_pattern = re.compile(r'NOTES?\s*\d*:?\s+')
        note_match = note_pattern.search(full_text)
        # Check if text matches EXAMPLE pattern: EXAMPLE\s*\d*:
        example_pattern = re.compile(r'EXAMPLE\s*\d*:\s+')
        example_pattern = re.compile(r'EXAMPLES?\s*\d*:?\s+')
        example_match = example_pattern.search(full_text)
        if note_match:
            # Change style to NO
@@ -2028,6 +2028,34 @@ def update_notes(docx_input, docx_output):
            # Re-append all original children (runs and hyperlinks) in their original order
            for child in all_children:
                para.append(child)
        else:
            # Check whether BlockText is preceded by paragraph style "EW".
            # Rules:
            # - if preceding paragraph style is EW -> change to PL and stop
            # - if preceding paragraph has a style and it is not EW -> stop (no change)
            # - if no pPr or no pStyle -> continue searching backwards
            # - if not a paragraph -> continue searching backwards
            prev_para = para.getprevious()
            while prev_para is not None:
                if prev_para.tag != f"{{{ns['w']}}}p":
                    prev_para = prev_para.getprevious()
                    continue

                prev_pPr = prev_para.xpath('./w:pPr', namespaces=ns)
                if not prev_pPr:
                    prev_para = prev_para.getprevious()
                    continue

                prev_pStyle = prev_pPr[0].xpath('./w:pStyle', namespaces=ns)
                if not prev_pStyle:
                    prev_para = prev_para.getprevious()
                    continue

                prev_pStyle_val = prev_pStyle[0].get(f"{{{ns['w']}}}val")
                if prev_pStyle_val == example_style:
                    pStyle_elem.set(f"{{{ns['w']}}}val", "PL")
                    print(f'Changed style "BlockText" to "PL" because it is preceded by a paragraph with style "EW" (line {prev_para.sourceline})')
                break
            
    print(f'Changed style "BlockText/Notes" to "NO" {counter_no} times and "BlockText/Examples" to "EW" {counter_ew} times')

@@ -2225,7 +2253,7 @@ def update_references(docx_input, docx_output):
        # Keep Editor's Notes untouched here; they are formatted later in
        # update_body_text_style (including highlighting).
        para_text = _paragraph_text_from_runs(para)
        if re.search(r'Editor[\'’]s\s+Note:', para_text, re.IGNORECASE):
        if re.search(r'Editor(?:[\'’]?s)?\s+Note:', para_text, re.IGNORECASE):
            continue

        # A reference is a paragraph that contains: