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

Some fixes:

- Headings can contain "." in the title
- Removed leading space from note and examples text
- Add box with shading to PL blocks
parent a8756a02
Loading
Loading
Loading
Loading
Loading
+72 −21
Original line number Diff line number Diff line
@@ -733,7 +733,7 @@ def update_heading_styles(docx_input, docx_output):
                pStyle_elems[0].set(f"{{{ns['w']}}}val", "Heading8")    
            counter_annex += 1
        # Annex subheading Heading 2: "A.1 Title", not matching "A.1.1 Title"
        elif (match := re.match(r'^\s*([A-Z]\.\d+)\s+([^\.]+)$', full_text)) is not None:
        elif (match := re.match(r'^\s*([A-Z]\.\d+)\s+(.+)$', full_text)) is not None:
            annex_letter = match.group(1)
            annex_title = match.group(2)
            
@@ -965,11 +965,19 @@ def update_unnumbered_lists(docx_input, docx_output):
            continue
        
        if (para.xpath('./w:pPr/w:pStyle[@w:val="Compact"]', namespaces=ns) or not para.xpath('./w:pPr/w:pStyle', namespaces=ns)) and para.xpath('./w:pPr/w:numPr', namespaces=ns):
            # Change Compact style if exists to B1, B2 or B3 style
            # Get pPr element to work with styles/numPr
            pPr = para.xpath('./w:pPr', namespaces=ns)[0]
            # Try to get existing Compact style; if not present, create pStyle
            compact_style_elems = pPr.xpath('./w:pStyle[@w:val="Compact"]', namespaces=ns)
            if compact_style_elems:
                compact_style = compact_style_elems[0]
            else:
                compact_style = OxmlElement('w:pStyle')
                pPr.insert(0, compact_style)
           # Check if it is a numbered list and get the format
            is_numbered, num_format = is_numbered_list(para, numbering_root)
            if is_numbered:  # is_numbered is True if numbered, False if unnumbered, None if cannot determine
                # Get pPr element to add pStyle to it
                pPr = para.xpath('./w:pPr', namespaces=ns)[0]
                pStyle = OxmlElement('w:pStyle')
                # If format is decimal (numbers), use BN; otherwise use BL
                if num_format == 'decimal':
@@ -979,17 +987,6 @@ def update_unnumbered_lists(docx_input, docx_output):
                pPr.append(pStyle)
                counter_numbered += 1
            else:                
                # Change Compact style if exists to B1, B2 or B3 style
                # Get pPr element to work with styles/numPr
                pPr = para.xpath('./w:pPr', namespaces=ns)[0]
                # Try to get existing Compact style; if not present, create pStyle
                compact_style_elems = pPr.xpath('./w:pStyle[@w:val="Compact"]', namespaces=ns)
                if compact_style_elems:
                    compact_style = compact_style_elems[0]
                else:
                    compact_style = OxmlElement('w:pStyle')
                    pPr.insert(0, compact_style)
                
                if para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="0"]', namespaces=ns):
                    if para.xpath('ancestor::w:tbl', namespaces=ns):
                        compact_style.set(f"{{{ns['w']}}}val", "TB1")
@@ -1453,10 +1450,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*:')
        note_pattern = re.compile(r'NOTE\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*:')
        example_pattern = re.compile(r'EXAMPLE\s*\d*:\s+')
        example_match = example_pattern.search(full_text)
        if note_match:
            # Change style to NO
@@ -2119,13 +2116,67 @@ def update_source_code_style(docx_input, docx_output):
    new_style = "PL"
    counter = 0

    # Loop over all elements to find "SourceCode" and change to "PL"
    def add_borders_to_paragraph(pPr):
        """Helper function to add borders and background color to a paragraph"""
        if pPr is None:
            return
        
        # Check if pBdr already exists
        pBdr = pPr.xpath('./w:pBdr', namespaces=ns)
        if not pBdr:
            # Create pBdr element
            pBdr_elem = etree.Element(f"{{{ns['w']}}}pBdr")
            pPr.append(pBdr_elem)
        else:
            pBdr_elem = pBdr[0]
        
        # Define border properties (single line, black, 0.5pt)
        border_attrs = {
            f"{{{ns['w']}}}val": "single",
            f"{{{ns['w']}}}sz": "4",  # 0.5pt (4 = 0.5pt in eighths of a point)
            f"{{{ns['w']}}}space": "0",
            f"{{{ns['w']}}}color": "000000"  # Black
        }
        
        # Add borders for all four sides
        for side in ['top', 'left', 'bottom', 'right']:
            border_elem = pBdr_elem.xpath(f'./w:{side}', namespaces=ns)
            if not border_elem:
                border_elem = etree.Element(f"{{{ns['w']}}}{side}")
                for attr, value in border_attrs.items():
                    border_elem.set(attr, value)
                pBdr_elem.append(border_elem)
            else:
                # Update existing border
                for attr, value in border_attrs.items():
                    border_elem[0].set(attr, value)
        
        # Add grey background color (shading)
        shd = pPr.xpath('./w:shd', namespaces=ns)
        if not shd:
            shd_elem = etree.Element(f"{{{ns['w']}}}shd")
            shd_elem.set(f"{{{ns['w']}}}val", "clear")  # Clear pattern
            shd_elem.set(f"{{{ns['w']}}}color", "auto")  # Auto text color
            shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2")  # Light grey background
            pPr.append(shd_elem)
        else:
            # Update existing shading
            shd_elem = shd[0]
            shd_elem.set(f"{{{ns['w']}}}val", "clear")
            shd_elem.set(f"{{{ns['w']}}}color", "auto")
            shd_elem.set(f"{{{ns['w']}}}fill", "F2F2F2")
    
    # Loop over all elements to find "SourceCode" and change to "PL", and add borders
    for elem in root.xpath('.//w:pStyle[@w:val="SourceCode"]', namespaces=ns):
        old_val = elem.get(f"{{{ns['w']}}}val")
        elem.set(f"{{{ns['w']}}}val", new_style)
        counter += 1
        
    print(f'Changed style "SourceCode" to "PL" {counter} times')
        # Get the paragraph element to add borders
        pPr = elem.getparent()
        add_borders_to_paragraph(pPr)
        
    print(f'Changed style "SourceCode" to "PL" and added borders to {counter} paragraphs')

    xml_data = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone="yes")