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

Fix code blocks in list elements

parent 7ae0a0ea
Loading
Loading
Loading
Loading
Loading
+57 −36
Original line number Diff line number Diff line
@@ -1101,6 +1101,10 @@ def update_lists(docx_input, docx_output):
                            # Take all elements except pPr (usually only runs and hyperlinks) in this paragraph and append them to the previous paragraph. New run starting by <w:br/>
                            previous_para = para.getprevious()
                            if previous_para is not None:
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", "B1")
                                    #continue
                                else:
                                    break_run = OxmlElement('w:r')
                                    break_run.append(OxmlElement('w:br'))
                                    previous_para.append(break_run)
@@ -1113,9 +1117,9 @@ def update_lists(docx_input, docx_output):
                            #print(f'Removed the original paragraph')
                            #compact_style.set(f"{{{ns['w']}}}val", "FP")
                    #Remove numPr from pPr
                    if not para_removed:
                        numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                        pPr.remove(numPr)
                    #if not para_removed:
                    #    numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                    #    pPr.remove(numPr)
                    counter_b1 += 1
                elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="1"]', namespaces=ns):
                    if para.xpath('ancestor::w:tbl', namespaces=ns):
@@ -1135,6 +1139,11 @@ def update_lists(docx_input, docx_output):
                        else:
                           # Take all elements except pPr (usually only runs and hyperlinks) in this paragraph and append them to the previous paragraph. New run starting by <w:br/>
                            previous_para = para.getprevious()
                            if previous_para is not None:
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", "B2")
                                    #ontinue
                                else:
                                    if previous_para is not None:
                                        break_run = OxmlElement('w:r')
                                        break_run.append(OxmlElement('w:br'))
@@ -1148,9 +1157,9 @@ def update_lists(docx_input, docx_output):
                            #print(f'Removed the original paragraph')
                            #compact_style.set(f"{{{ns['w']}}}val", "FP")
                    #Remove numPr from pPr
                    if not para_removed:
                        numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                        pPr.remove(numPr)
                    #if not para_removed:
                    #    numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                    #    pPr.remove(numPr)
                    counter_b2 += 1 
                elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="2"]', namespaces=ns):
                    if para.xpath('ancestor::w:tbl', namespaces=ns):
@@ -1170,6 +1179,11 @@ def update_lists(docx_input, docx_output):
                        else:
                            # Take all elements except pPr (usually only runs and hyperlinks) in this paragraph and append them to the previous paragraph. New run starting by <w:br/>
                            previous_para = para.getprevious()
                            if previous_para is not None:
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", "B3")
                                    #continue
                                else:
                                    if previous_para is not None:
                                        break_run = OxmlElement('w:r')
                                        break_run.append(OxmlElement('w:br'))
@@ -1183,9 +1197,9 @@ def update_lists(docx_input, docx_output):
                            #print(f'Removed the original paragraph')
                            #compact_style.set(f"{{{ns['w']}}}val", "FP")
                    #Remove numPr from pPr
                    if not para_removed:
                        numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                        pPr.remove(numPr)
                    #if not para_removed:
                    #   numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                    #   pPr.remove(numPr)
                    counter_b3 += 1
                
                pPr.insert(0, compact_style)
@@ -2372,6 +2386,13 @@ def update_source_code_style(docx_input, docx_output):
        pPr = elem.getparent()
        add_borders_to_paragraph(pPr)

        # Remove other rPr:rStyle elements
        para = pPr.getparent()
        for rStyle in para.xpath('./w:r/w:rPr/w:rStyle', namespaces=ns):
            rPr = rStyle.getparent()
            if rPr is not None:
                rPr.remove(rStyle)
        
    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")