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

Adding keepnext to list lead-in and parent element list (with a nested sublist)

parent 83c24db6
Loading
Loading
Loading
Loading
Loading
+30
Changes for generateBaseline/postprocessing.py: 30 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1698,6 +1698,34 @@ def update_lists(docx_input, docx_output):
            if src is not None:
                pPr.append(etree.fromstring(etree.tostring(src)))

    def list_ilvl(para):
        ilvl_elems = para.xpath('./w:pPr/w:numPr/w:ilvl', namespaces=ns)
        if not ilvl_elems:
            return 0
        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:
            pPr = OxmlElement('w:pPr')
            para.insert(0, pPr)
        if not pPr.xpath('./w:keepNext', namespaces=ns):
            pPr.append(OxmlElement('w:keepNext'))

    def keep_list_with_following(para):
        """Keep a lead-in ending in ':' with the first item, and a parent item with its sublist."""
        following = para.getnext()
        if (following is not None and following.tag == f'{{{ns["w"]}}}p'
                and following.xpath('./w:pPr/w:numPr', namespaces=ns)
                and list_ilvl(following) > list_ilvl(para)):
            ensure_keep_next(para)
        previous = para.getprevious()
        if (previous is not None and previous.tag == f'{{{ns["w"]}}}p'
                and not previous.xpath('./w:pPr/w:numPr', namespaces=ns)
                and _paragraph_text(previous).endswith(':')):
            ensure_keep_next(previous)

    for para in paragraphs:
        if not after_toc(para):
            continue
@@ -1748,6 +1776,7 @@ def update_lists(docx_input, docx_output):
                # Remove numId from numPr
                #numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                #numPr.remove(numPr.xpath('./w:numId', namespaces=ns)[0])
                keep_list_with_following(para)
                counter_numbered += 1
            else:
                #print(f'The lvl_text is {lvl_text} and the abstract_num_id is {abstract_num_id}')
@@ -1808,6 +1837,7 @@ def update_lists(docx_input, docx_output):
                    apply_list_level_indent(
                        pPr, numbering_root, num_id, ilvl, fallback_abstract
                    )
                    keep_list_with_following(para)
                    pPr.insert(0, compact_style)
                counter_compact += 1
                continue