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

Keep uppercase lettered list properly

parent bc141bca
Loading
Loading
Loading
Loading
Loading
+73
Changes for generateBaseline/postprocessing.py: 73 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1666,6 +1666,68 @@ def update_lists(docx_input, docx_output):
                abstract.set(qn('w:val'), target_abstract_num_id)
                numbering_modified = True

    def uppercase_marker_formats(numbering_root, num_id):
        """numFmt of each level Pandoc stored as an uppercase letter or roman marker."""
        abstract_id = get_abstract_id_for_num(numbering_root, num_id)
        if abstract_id is None:
            return {}
        formats = {}
        for lvl in numbering_root.xpath(
            f'.//w:abstractNum[@w:abstractNumId="{abstract_id}"]/w:lvl',
            namespaces=ns,
        ):
            ilvl_val = lvl.get(qn('w:ilvl'))
            num_fmt_el = lvl.find(qn('w:numFmt'))
            if ilvl_val is None or num_fmt_el is None:
                continue
            fmt = num_fmt_el.get(qn('w:val'))
            if fmt in ('upperLetter', 'upperRoman'):
                formats[ilvl_val] = fmt
        return formats

    def keep_uppercase_markers(numbering_root, num_id, bl_abstract_id, formats):
        """Pointing the list at BL replaces every level. Put BL's level back with Pandoc's marker."""
        nonlocal numbering_modified
        if not formats or num_id is None or bl_abstract_id is None:
            return
        nums = numbering_root.xpath(f'//w:num[@w:numId="{num_id}"]', namespaces=ns)
        abstracts = numbering_root.xpath(
            f'.//w:abstractNum[@w:abstractNumId="{bl_abstract_id}"]',
            namespaces=ns,
        )
        if not nums or not abstracts:
            print(f'Uppercase list numId {num_id}: numbering instance or BL abstract {bl_abstract_id} was not found')
            return
        num = nums[0]
        # Pandoc writes the same format on every level. Only level 0 is the A. B. C. marker.
        # Deeper levels stay as BL defines them, so a roman sublist is left alone.
        fmt = formats.get('0')
        if fmt is None:
            print(f'Uppercase list numId {num_id}: no uppercase format at level 0')
            return
        bl_lvls = abstracts[0].xpath('./w:lvl[@w:ilvl="0"]', namespaces=ns)
        if not bl_lvls or bl_lvls[0].find(qn('w:numFmt')) is None:
            print(f'Uppercase list numId {num_id} level 0: BL abstract {bl_abstract_id} has no marker format')
            return
        lvl_copy = etree.fromstring(etree.tostring(bl_lvls[0]))
        lvl_copy.find(qn('w:numFmt')).set(qn('w:val'), fmt)
        existing = num.xpath('./w:lvlOverride[@w:ilvl="0"]', namespaces=ns)
        if existing:
            # Pandoc's override restarts the list. It does not set the letter case.
            override = existing[0]
            current = override.find(qn('w:lvl'))
            if current is not None:
                override.remove(current)
            override.append(lvl_copy)
            print(f'Uppercase list numId {num_id} level 0: set {fmt} on the existing override')
        else:
            override = OxmlElement('w:lvlOverride')
            override.set(qn('w:ilvl'), '0')
            override.append(lvl_copy)
            num.append(override)
            print(f'Uppercase list numId {num_id} level 0: kept {fmt} on the BL numbering')
        numbering_modified = True

    def get_abstract_id_for_num(numbering_root, num_id):
        if numbering_root is None or num_id is None:
            return None
@@ -1767,7 +1829,18 @@ def update_lists(docx_input, docx_output):
                    if ilvl == "0":
                        abstract_id_bl = get_style_abstract_id(styles_root, numbering_root, LIST_STYLES[ilvl]["numbered_style"]["letter"])
                        if abstract_id_bl is not None:
                            # Read every uppercase level before the list is pointed at BL.
                            if num_format in ('upperLetter', 'upperRoman'):
                                uppercase_formats = uppercase_marker_formats(numbering_root, num_id)
                                print(f'Uppercase list numId {num_id} level {ilvl} format {num_format}, levels {uppercase_formats or "none"}')
                            else:
                                uppercase_formats = {}
                            update_list_instance_to_style(numbering_root, num_id, abstract_id_bl)
                            keep_uppercase_markers(numbering_root, num_id, abstract_id_bl, uppercase_formats)
                        elif num_format in ('upperLetter', 'upperRoman'):
                            print(f'Uppercase list numId {num_id} level {ilvl} format {num_format}: BL style has no numbering')
                    elif num_format in ('upperLetter', 'upperRoman'):
                        print(f'Uppercase list numId {num_id} level {ilvl} format {num_format}: not level 0, marker left unchanged')
                    compact_style.set(f"{{{ns['w']}}}val", LIST_STYLES[ilvl]["numbered_style"]["letter"])
                apply_list_level_indent(
                    pPr,