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

Fix for elements belonging to a list element

parent 3a3d77c9
Loading
Loading
Loading
Loading
Loading
+29 −18
Changes for generateBaseline/postprocessing.py: 29 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -1773,6 +1773,16 @@ def update_lists(docx_input, docx_output):
        raw = ilvl_elems[0].get(qn('w:val'))
        return int(raw) if raw and raw.isdigit() else 0

    def _list_item_ilvl(para):
        """ilvl of a list paragraph, or None when the paragraph is not a list item."""
        if para is None or para.tag != f"{{{ns['w']}}}p":
            return None
        ilvl_elems = para.xpath('./w:pPr/w:numPr/w:ilvl', namespaces=ns)
        if not ilvl_elems:
            return None
        raw = ilvl_elems[0].get(qn('w:val'))
        return int(raw) if raw and raw.isdigit() else 0

    def ensure_keep_next(para):
        pPr = para.find(f'{{{ns["w"]}}}pPr')
        if pPr is None:
@@ -1877,22 +1887,13 @@ def update_lists(docx_input, docx_output):

                            compact_style.set(f"{{{ns['w']}}}val", LIST_STYLES[ilvl]["bullet_style"]["normal"])
                        else:
                            para_processed = False
                            # 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/>
                            # A blank marker continues a list item. Join it to the previous
                            # paragraph only when that paragraph is a list item at this level.
                            # Otherwise keep this paragraph and copy the style of the nearest
                            # earlier item at this level.
                            previous_para = para.getprevious()
                            if previous_para is not None and previous_para.tag == f"{{{ns['w']}}}p":
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", LIST_STYLES[ilvl]["bullet_style"]["normal"])
                                    para_processed = True
                                # If the previous paragraph is a list item, and is one level higher than the current, then
                                # leave it as is (probably it belongs to a previous list item)
                                elif previous_para.xpath('./w:pPr/w:numPr/w:ilvl', namespaces=ns):
                                    previous_ilvl = previous_para.xpath('./w:pPr/w:numPr/w:ilvl', namespaces=ns)[0].get(f"{{{ns['w']}}}val")
                                    if int(previous_ilvl) > int(ilvl):
                                        compact_style.set(f"{{{ns['w']}}}val", LIST_STYLES[ilvl]["bullet_style"]["normal"])
                                        para_processed = True
                                
                                if not para_processed:
                            previous_ilvl = _list_item_ilvl(previous_para)
                            if previous_ilvl is not None and previous_ilvl == int(ilvl):
                                break_run = OxmlElement('w:r')
                                break_run.append(OxmlElement('w:br'))
                                previous_para.append(break_run)
@@ -1901,10 +1902,20 @@ def update_lists(docx_input, docx_output):
                                        previous_para.append(element)
                                para.getparent().remove(para)
                                para_removed = True
                                    #print(f'Removed the original paragraph')
                                    #compact_style.set(f"{{{ns['w']}}}val", "FP")
                            else:
                                compact_style.set(f"{{{ns['w']}}}val", LIST_STYLES[ilvl]["bullet_style"]["normal"])
                                style_name = None
                                ancestor = previous_para
                                while ancestor is not None:
                                    if _list_item_ilvl(ancestor) == int(ilvl):
                                        style_elems = ancestor.xpath('./w:pPr/w:pStyle', namespaces=ns)
                                        if style_elems:
                                            style_name = style_elems[0].get(qn('w:val'))
                                        break
                                    ancestor = ancestor.getprevious()
                                compact_style.set(
                                    qn('w:val'),
                                    style_name or LIST_STYLES[ilvl]["bullet_style"]["normal"],
                                )
                    #Remove numPr from pPr
                    #if not para_removed:
                    #    numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]