"""Attach editable, shared Word styles without rewriting document content. Only the formatting owned by MDHelper is removed from document.xml. Numbering, relationships, equations, bookmarks and other engine content remain in place. """ from __future__ import annotations from copy import deepcopy import re import zipfile from lxml import etree as ET from .document_style import resolved_rules, normalize_rule, font_points, length_points, number from .style_catalog import CATALOG, CHARACTER_DEFAULTS, explicit_rules, character_declarations, parent_style, style_name, legacy_base from .docx_style import W, NS, child, val, properties, _run_properties, _paragraph_properties, _order_properties from .docx_inline import source_style_order, ordered_styles, LIST_PARAGRAPH_STYLE, QUOTED_LIST_PARAGRAPH_STYLE, SOURCE_PARAGRAPH_STYLES, CONTINUATION, INNER_QUOTE_STYLE, LIST_TARGET, LIST_TARGETS, SOURCE_LIST_STYLES from .table_style import HEADER_COLOR, STRIPE_COLOR, BORDER_COLOR, VERTICAL_BORDER_COLOR RUN_FIELDS = 'rFonts sz szCs b bCs i iCs color spacing u strike shd'.split() PARAGRAPH_FIELDS = 'jc spacing ind shd snapToGrid contextualSpacing keepNext keepLines pageBreakBefore widowControl rPr outlineLvl'.split() def style_id(key): return 'MDHelper' + ''.join(word.capitalize() for word in key.split('_')) def reference(parent, name): node = parent.find(W + name) if parent is not None else None return node.get(W + 'val', '') if node is not None else '' def remove_fields(parent, names): for name in names: for element in parent.findall(W + name): parent.remove(element) def _delta(current, base, authored=None): """Keep just changed properties, allowing subsequent Word base-style edits.""" if base is None: return authored = authored or {} fields = {'rFonts': ('english_font_family', 'chinese_font_family'), 'sz': ('font_size',), 'szCs': ('font_size',), 'b': ('bold',), 'bCs': ('bold',), 'i': ('italic',), 'iCs': ('italic',), 'strike': ('strike',), 'u': ('underline',), 'shd': ('background_color',), 'color': ('font_color',), 'jc': ('alignment',), 'ind': ('indent_mode', 'indent_value', 'indent_unit', 'left_indent_value', 'right_indent_value'), 'spacing': ('letter_spacing', 'space_before_value', 'space_after_value', 'line_height_value'), 'keepNext': ('keep_with_next',), 'keepLines': ('keep_lines_together',), 'pageBreakBefore': ('page_break_before',), 'widowControl': ('widow_control',), 'contextualSpacing': ('contextual_spacing',)} for node in list(current): old = base.find(node.tag) compound = { 'rFonts': {'ascii': ('english_font_family',), 'hAnsi': ('english_font_family',), 'cs': ('english_font_family',), 'eastAsia': ('chinese_font_family',)}, 'spacing': {**{attr: (f'space_{side}_value', f'space_{side}_unit') for side in ('before', 'after') for attr in (side, side + 'Lines', side + 'Autospacing')}, 'line': ('line_height_value', 'line_spacing_mode'), 'lineRule': ('line_height_value', 'line_spacing_mode')}, 'ind': {**{attr: (f'{side}_indent_value', f'{side}_indent_unit') for side in ('left', 'right') for attr in (side, side + 'Chars')}, **{attr: ('indent_mode', 'indent_value', 'indent_unit') for attr in ('firstLine', 'firstLineChars', 'hanging', 'hangingChars')}}, }.get(ET.QName(node).localname) if old is not None and compound and not (node.tag == W + 'spacing' and current.tag == W + 'rPr'): # These properties merge attribute by attribute in Word. for key in list(node.attrib): explicit = any(field in authored for field in compound.get(ET.QName(key).localname, ())) if not explicit and old.get(key) == node.get(key): del node.attrib[key] if not node.attrib and not len(node): current.remove(node) elif not any(field in authored for field in fields.get(ET.QName(node).localname, ())): if old is not None and node.attrib == old.attrib and node.text == old.text and not len(node) and not len(old): current.remove(node) class WordStyles: def __init__(self, styles, numbering, rules, **defaults): self.styles, self.numbering = styles, numbering self.rules = rules if isinstance(rules, dict) else {} self.effective = resolved_rules(self.rules, **defaults) self.explicit = explicit_rules(self.rules) self.explicit.update(character_declarations(self.rules, self.effective)) self.original = {s.get(W + 'styleId'): s for s in styles.findall(W + 'style')} self.used_names = {reference(s, 'name') for s in self.original.values()} self.created = {} self.characters = {} self.number_overrides = {} self.style_numbering = {} for key in CATALOG: if CATALOG[key].kind == 'character': self.character(key) elif CATALOG[key].kind == 'paragraph': self.paragraph(key) self.table() def chain(self, identifier): seen = set() while identifier in self.original and identifier not in seen: seen.add(identifier) style = self.original[identifier] yield style identifier = reference(style, 'basedOn') def names(self, identifier): return ' '.join([identifier] + [reference(s, 'name') for s in self.chain(identifier)]).lower() def new(self, identifier, kind, name, base=''): # Some converters use our reserved IDs for temporary code styles. # Retain their detached nodes in original for classification, but emit # exactly one definition for each final Word style ID. original = self.original.get(identifier) if original is not None and original.getparent() is self.styles: self.styles.remove(original) if name in self.used_names: name += ' [' + identifier + ']' self.used_names.add(name) style = ET.SubElement(self.styles, W + 'style', {W + 'type': kind, W + 'customStyle': '1', W + 'styleId': identifier}) val(style, 'name', name) if base: val(style, 'basedOn', base) val(style, 'uiPriority', 10 + len(self.created)) child(style, 'qFormat') self.created[identifier] = style return style def paragraph(self, key): identifier = style_id(key) if identifier in self.created: return identifier base = parent_style(key, self.rules) base_id = self.paragraph(base) if base else '' # Table styles cannot be a paragraph's basedOn. A shared paragraph base # carries the legacy table text settings and remains editable in Word. if key in ('table_head', 'table_cell') and not base and legacy_base(key, self.rules): base_id = 'MDHelperTableText' if base_id not in self.created: table_text = self.new(base_id, 'paragraph', 'MDHelper 表格文字基准') _paragraph_properties(child(table_text, 'pPr'), self.effective['table']) remove_fields(child(table_text, 'pPr'), ('shd',)) _run_properties(child(table_text, 'rPr'), self.effective['table']) remove_fields(child(table_text, 'rPr'), ('shd',)) base = 'table' style = self.new(identifier, 'paragraph', 'MDHelper ' + style_name(key, self.rules), base_id) rule = dict(self.effective[key]) if rule.get('font_color') == 'inherit': rule['font_color'] = 'auto' _paragraph_properties(child(style, 'pPr'), rule) if key in ('table_head', 'table_cell'): remove_fields(child(style, 'pPr'), ('shd',)) _run_properties(child(style, 'rPr'), rule) remove_fields(child(style, 'rPr'), ('shd',)) if base: for tag, writer in (('pPr', _paragraph_properties), ('rPr', _run_properties)): base_props = ET.Element(W + tag) writer(base_props, self.effective[base]) if tag == 'rPr': remove_fields(base_props, ('shd',)) _delta(child(style, tag), base_props, normalize_rule(self.rules.get(key))) if re.fullmatch('h[1-6]', key): val(child(style, 'pPr'), 'outlineLvl', int(key[1]) - 1) if key in ('table_head', 'table_cell') and base and base != 'table': # Cell shading belongs to the table style. Explicitly cancel any # paragraph shading inherited now or added to its parent in Word. child(child(style, 'pPr'), 'shd').attrib.update({W+'val': 'clear', W+'fill': 'auto'}) val(style, 'next', style_id('paragraph')) return identifier def character_rule(self, key): if key not in self.characters: base = parent_style(key, self.rules) rule = dict(CHARACTER_DEFAULTS.get(key, {})) if base: rule.update(self.character_rule(base)) rule.update(normalize_rule(self.explicit[key])) self.characters[key] = rule return self.characters[key] def paragraph_character(self, key): """Link a paragraph's character counterpart and its entire base chain. A flattened copy would stop following edits to ancestors in Word. Keep authored overrides only, just as for paragraph basedOn styles. """ identifier = style_id(key) + 'Char' if identifier not in self.created: base = parent_style(key, self.rules) base_id = self.paragraph_character(base) if base else '' paragraph_id = self.paragraph(key) style = self.new(identifier, 'character', 'MDHelper ' + style_name(key, self.rules) + '字符', base_id) val(style, 'link', paragraph_id) val(self.created[paragraph_id], 'link', identifier) props = child(style, 'rPr') _run_properties(props, self.code_character_rule(key)) if base: old = ET.Element(W + 'rPr') _run_properties(old, self.code_character_rule(base)) _delta(props, old, normalize_rule(self.rules.get(key))) return identifier def code_character_rule(self, key='code'): return self.effective[key] if parent_style('code', self.rules) else self.explicit[key] def character(self, key): identifier = style_id(key) if identifier in self.created: return identifier base = parent_style(key, self.rules) base_id = self.character(base) if base else '' rule = self.character_rule(key) legacy = key == 'inline_code' and not base and legacy_base(key, self.rules) if legacy: base_id = self.paragraph_character('code') # Word cannot clear one inherited character-style property. Preserve # the resolved remaining properties and inherit color from the text. base_rule = self.code_character_rule() if legacy else self.character_rule(base) if base else {} if rule.get('font_color') == 'inherit' and base_rule.get('font_color') not in (None, 'inherit'): base_id = '' style = self.new(identifier, 'character', 'MDHelper ' + style_name(key, self.rules), base_id) _run_properties(child(style, 'rPr'), rule) if base_id: old = ET.Element(W + 'rPr') _run_properties(old, base_rule) _delta(child(style, 'rPr'), old, normalize_rule(self.rules.get(key))) return identifier def combined_character(self, keys, paragraph_target): keys = ordered_styles(keys) if not keys: return '' merged = {} owners = {} for key in keys: for field, value in self.character_rule(key).items(): if field == 'font_color' and value == 'inherit': continue merged[field] = value owners[field] = key identifier = self.character(keys[0]) if len(keys) == 1 else 'MDHelperMix' + ''.join(style_id(key)[8:] for key in keys) if identifier not in self.created: base = 'link' if 'link' in keys else keys[0] style = self.new(identifier, 'character', 'MDHelper ' + '+'.join(style_name(k, self.rules) for k in keys), self.character(base)) props, old = child(style, 'rPr'), ET.Element(W + 'rPr') _run_properties(props, merged) _run_properties(old, self.character_rule(base)) _delta(props, old, {field: value for field, value in merged.items() if owners[field] != base}) return identifier def syntax_style(self, run_id, target): """Preserve token colors, while letting the code paragraph own its font.""" identifier = 'MDHelperSyntax' + run_id if identifier in self.created: return identifier props = ET.Element(W + 'rPr') for original in reversed(list(self.chain(run_id))): source = original.find(W + 'rPr') if source is None: continue for item in source: remove_fields(props, (ET.QName(item).localname,)) props.append(deepcopy(item)) remove_fields(props, ('rFonts', 'sz', 'szCs', 'rStyle')) for field, tags in (('font_color', ('color',)), ('bold', ('b', 'bCs')), ('italic', ('i', 'iCs')), ('letter_spacing', ('spacing',))): if field in self.explicit[target]: remove_fields(props, tags) if not len(props): return '' style = self.new(identifier, 'character', 'MDHelper 代码着色 ' + (reference(self.original.get(run_id), 'name') or run_id)) style.append(props) return identifier def list_level(self, numpr): level = int(number(reference(numpr, 'ilvl'))) num_id = reference(numpr, 'numId') if self.numbering is None or not num_id: return level, None num = self.numbering.find(f"w:num[@w:numId='{num_id}']", NS) abstract_id = reference(num, 'abstractNumId') abstract = self.numbering.find(f"w:abstractNum[@w:abstractNumId='{abstract_id}']", NS) return level, abstract.find(f"w:lvl[@w:ilvl='{level}']", NS) if abstract is not None else None def inherit_list_indents(self, numpr): """Numbering owns the marker; named paragraph styles own indentation.""" original = reference(numpr, 'numId') if not original: return if original not in self.style_numbering: _, level = self.list_level(numpr) if level is None: return num = deepcopy(self.numbering.find(f"w:num[@w:numId='{original}']", NS)) abstract = deepcopy(level.getparent()) num_id = 1 + max(int(n.get(W + 'numId')) for n in self.numbering.findall(W + 'num')) abstract_id = 1 + max(int(n.get(W + 'abstractNumId')) for n in self.numbering.findall(W + 'abstractNum')) num.set(W + 'numId', str(num_id)); val(num, 'abstractNumId', abstract_id) abstract.set(W + 'abstractNumId', str(abstract_id)) self.identify_numbering(abstract, abstract_id) for root in (num, abstract): for props in root.iter(W + 'pPr'): remove_fields(props, ('ind', 'tabs')) self.numbering.extend((abstract, num)) self.style_numbering[original] = str(num_id) val(numpr, 'numId', self.style_numbering[original]) def identify_numbering(self, abstract, identifier): # Word coalesces definitions with the same nsid, even when their XML # ids differ. A modified clone must have its own list identity. used = {reference(node, 'nsid') for node in self.numbering.findall(W + 'abstractNum')} candidate = (0x4d440000 + identifier) & 0xffffffff while f'{candidate:08X}' in used: candidate = (candidate + 1) & 0xffffffff val(abstract, 'nsid', f'{candidate:08X}') def classify(self, paragraph, original_id): names = self.names(original_id) if INNER_QUOTE_STYLE.lower() in names: return 'blockquote' heading = re.search(r'heading\s*([1-6])', names) if heading: return 'h' + heading[1] if any(word in names for word in ('sourcecode', 'source code', 'code', 'verbatim')): return 'code_block' for ancestor in paragraph.iterancestors(W + 'tc'): row = ancestor.getparent() table = row.getparent() is_head = row is table.find(W + 'tr') or row.find('w:trPr/w:tblHeader', NS) is not None return 'table_head' if is_head else 'table_cell' ppr = properties(paragraph, 'pPr') numpr = ppr.find(W + 'numPr') if numpr is None: inherited = next((s.find('w:pPr/w:numPr', NS) for s in self.chain(original_id) if s.find('w:pPr/w:numPr', NS) is not None), None) if inherited is not None: numpr = deepcopy(inherited) ppr.append(numpr) if 'list' in names or numpr is not None: text = ''.join(paragraph.itertext()).lstrip('• ') if re.match(r'^(?:\[[ xX]\]|[☐☒☑])', text): return 'list_task' _, level = self.list_level(numpr) if reference(level, 'lvlText') in ('☐', '☒', '☑'): return 'list_task' fmt = reference(level, 'numFmt') ordered = (fmt and fmt != 'bullet') or 'number' in names or re.match(r'^\d+[.)]\s', text) return 'list_ordered' if ordered else 'list_bullet' if any(word in names for word in ('quote', 'quotation', 'blockquote', 'blocktext', 'block text')): return 'blockquote' return 'paragraph' def nested_list(self, target, level): identifier = style_id(target) + 'Level' + str(level + 1) if identifier not in self.created: style = self.new(identifier, 'paragraph', 'MDHelper ' + style_name(target, self.rules) + f'(第 {level + 1} 级)', self.paragraph(target)) rule = dict(self.effective[target]) points = font_points(rule) offset = level * number(rule.get('level_indent', 36)) if rule['left_indent_unit'] == 'em': rule['left_indent_value'] += offset / points else: rule['left_indent_value'] = length_points(rule['left_indent_value'], rule['left_indent_unit'], points) + offset rule['left_indent_unit'] = 'pt' temp = ET.Element(W + 'pPr') _paragraph_properties(temp, rule) ind = temp.find(W + 'ind') for attr in list(ind.attrib): if attr not in (W + 'left', W + 'leftChars'): del ind.attrib[attr] child(style, 'pPr').append(ind) return identifier def contextual_list_rule(self, target, own, level): """Compose a real inner paragraph with its list container geometry.""" base = self.effective[target] rule = {**base, **own} for side in ('left', 'right'): field, unit = f'{side}_indent_value', f'{side}_indent_unit' if field not in own and base[unit] == 'em' and font_points(base) == font_points(rule): if side == 'left': rule[field] += level * number(base.get('level_indent', 36)) / font_points(base) continue rule[field] = length_points(base[field], base[unit], font_points(base)) if field in own: rule[field] += length_points(own[field], own.get(unit, 'pt'), font_points(rule)) if side == 'left': rule[field] += level * number(base.get('level_indent', 36)) rule[unit] = 'pt' # text-indent is inherited as a computed length. An inner p's font # must not resize an em indent declared on li. Absolute hanging units # also stop Word's character indent from flattening nested levels. if rule['indent_mode'] in ('hanging', 'first') and rule.get('indent_unit', 'em') == 'em' and (rule['left_indent_unit'] != 'em' or font_points(rule) != font_points(base)): origin = rule if 'indent_mode' in own else base rule['indent_value'] = length_points(rule['indent_value'], 'em', font_points(origin)) rule['indent_unit'] = 'pt' return rule def list_paragraph(self, target, base_id, level=0): """A loose Markdown list contains a real p element; a tight one does not.""" own = normalize_rule(self.rules.get('_list_paragraph')) if not own: return base_id identifier = base_id + 'Paragraph' if identifier not in self.created: style = self.new(identifier, 'paragraph', 'MDHelper ' + style_name(target, self.rules) + '(段落)' + base_id.removeprefix(style_id(target)), base_id) base = self.effective[target] rule = self.contextual_list_rule(target, own, level) authored = {**own, **{field: rule[field] for field in ('left_indent_unit', 'right_indent_unit', 'indent_unit') if rule.get(field) != base.get(field)}} for tag, writer in (('pPr', _paragraph_properties), ('rPr', _run_properties)): props, old = child(style, tag), ET.Element(W + tag) writer(props, rule); writer(old, base) if tag == 'rPr': remove_fields(props, ('shd',)); remove_fields(old, ('shd',)) _delta(props, old, authored) return identifier def list_block(self, target, list_target, level): """Keep a heading/code style as the editable base inside a numbered item.""" identifier = style_id(target) + style_id(list_target) + 'Level' + str(level + 1) if identifier not in self.created: style = self.new(identifier, 'paragraph', 'MDHelper ' + style_name(target, self.rules) + '(' + style_name(list_target, self.rules) + f'第 {level + 1} 级)', self.paragraph(target)) own = {k: v for k, v in self.effective[target].items() if 'indent' not in k} own.update({k: v for k, v in self.explicit[target].items() if 'indent' in k}) rule = self.contextual_list_rule(list_target, own, level) props = ET.Element(W + 'pPr'); _paragraph_properties(props, rule) child(style, 'pPr').append(props.find(W + 'ind')) return identifier def quoted_list(self, target, base_id, level): """A real p inside a quoted list receives the quote's explicit format.""" identifier = base_id + 'Quote' if identifier not in self.created: quote_id = self.paragraph('blockquote') style = self.new(identifier, 'paragraph', 'MDHelper ' + style_name(target, self.rules) + '(引用内段落)' + (str(level + 1) if level else ''), quote_id) own = self.explicit['blockquote'] # Container typography is inherited by li; a declaration on li # wins over that inherited value. A direct blockquote p rule wins # again. Preserve this distinction from the original CSS cascade. overrides = {field: value for field, value in self.explicit[target].items() if field in self.rules.get('_quote_container', {})} rule = self.contextual_list_rule(target, {**normalize_rule(self.rules.get('_list_paragraph')), **own, **overrides}, level) for tag, writer in (('pPr', _paragraph_properties), ('rPr', _run_properties)): props, old = child(style, tag), ET.Element(W + tag) writer(props, rule); writer(old, self.effective['blockquote']) if tag == 'rPr': remove_fields(props, ('shd',)); remove_fields(old, ('shd',)) _delta(props, old, overrides) return identifier def table_column(self, target, alignment): identifier = style_id(target) + alignment.capitalize() if identifier not in self.created: label = {'left':'左对齐','center':'居中','right':'右对齐','both':'两端对齐'}[alignment] style = self.new(identifier, 'paragraph', 'MDHelper ' + style_name(target, self.rules) + f'({label}列)', self.paragraph(target)) val(child(style, 'pPr'), 'jc', alignment) return identifier def apply_list_marker(self, numpr, target, marker): level, original_level = self.list_level(numpr) if original_level is None: return formats = {'decimal': ('decimal', f'%{level + 1}.'), 'lower-alpha': ('lowerLetter', f'%{level + 1}.'), 'upper-alpha': ('upperLetter', f'%{level + 1}.'), 'lower-roman': ('lowerRoman', f'%{level + 1}.'), 'upper-roman': ('upperRoman', f'%{level + 1}.'), 'disc': ('bullet', '•'), 'circle': ('bullet', '○'), 'square': ('bullet', '▪'), 'none': ('none', '')} if marker not in formats: return key = (reference(numpr, 'numId'), target, marker) if key not in self.number_overrides: # Numbering definitions may be shared by unrelated semantic types. # Clone before modifying, retaining starts and per-instance overrides. num = deepcopy(self.numbering.find(f"w:num[@w:numId='{key[0]}']", NS)) abstract = deepcopy(original_level.getparent()) num_id = 1 + max(int(n.get(W + 'numId')) for n in self.numbering.findall(W + 'num')) abstract_id = 1 + max(int(n.get(W + 'abstractNumId')) for n in self.numbering.findall(W + 'abstractNum')) num.set(W + 'numId', str(num_id)); val(num, 'abstractNumId', abstract_id) abstract.set(W + 'abstractNumId', str(abstract_id)) self.identify_numbering(abstract, abstract_id) self.numbering.extend((abstract, num)) self.number_overrides[key] = str(num_id) val(numpr, 'numId', self.number_overrides[key]) _, numbering_level = self.list_level(numpr) fmt, text = formats[marker] val(numbering_level, 'numFmt', fmt); val(numbering_level, 'lvlText', text) rpr = numbering_level.find(W + 'rPr') if rpr is not None: remove_fields(rpr, ('rFonts',)) def table(self): identifier = style_id('table') style = self.new(identifier, 'table', 'MDHelper ' + style_name('table', self.rules)) rule = self.effective['table'] props = child(style, 'tblPr') val(props, 'tblStyleRowBandSize', 1) borders = child(props, 'tblBorders') mode = rule.get('table_border_mode', 'grid') for edge in ('top', 'left', 'bottom', 'right', 'insideH', 'insideV'): element = child(borders, edge) width = number(rule.get('border_width', .5)) vertical = edge in ('left', 'right', 'insideV') color = str(rule.get('border_color', VERTICAL_BORDER_COLOR if vertical else BORDER_COLOR)) border = {'none':'none','hidden':'none','solid':'single','dotted':'dotted','dashed':'dashed','double':'double','groove':'threeDEngrave','ridge':'threeDEmboss','inset':'inset','outset':'outset'}.get(rule.get('border_style'), 'single') visible = width and color != 'transparent' and mode != 'none' and not (mode == 'horizontal' and vertical) element.attrib.update({W + 'val': border if visible else 'none', W + 'sz': str(max(2, round(width * 8))), W + 'color': color.lstrip('#') if color != 'transparent' else 'auto'}) margins = child(props, 'tblCellMar') for side in ('top', 'left', 'bottom', 'right'): padding = number(rule.get('cell_padding', font_points(rule) * (.76 if side in ('left', 'right') else .62))) child(margins, side).attrib.update({W + 'w': str(round(padding * 20)), W + 'type': 'dxa'}) if 'background_color' in rule: child(child(style, 'tcPr'), 'shd').attrib.update({W + 'val': 'clear', W + 'fill': str(rule['background_color']).lstrip('#') if rule['background_color'] != 'transparent' else 'auto'}) for target, condition in (('table_head', 'firstRow'), ('table_cell', 'band1Horz'), ('table_cell', 'band2Horz')): color = self.explicit[target].get('background_color') if not color and target == 'table_head': color = HEADER_COLOR if not color and condition == 'band2Horz' and rule.get('table_banded_rows'): color = rule.get('table_stripe_color', STRIPE_COLOR) if color: conditional = ET.SubElement(style, W + 'tblStylePr', {W + 'type': condition}) child(child(conditional, 'tcPr'), 'shd').attrib.update({W + 'val': 'clear', W + 'fill': color.lstrip('#') if color != 'transparent' else 'auto'}) return identifier def apply_run(self, run, target): rpr = properties(run, 'rPr') run_id = reference(rpr, 'rStyle') names = self.names(run_id) source_order = next((order for style in self.chain(run_id) if (order := source_style_order(reference(style, 'name')))), ()) keys = set() for tag, key, words in (('b', 'strong', ('strong',)), ('i', 'emphasis', ('emphasis',)), ('strike', 'strike', ('strike',))): node = rpr.find(W + tag) if node is not None and node.get(W + 'val', '1') not in ('0', 'false', 'off') or any(word in names for word in words): keys.add(key) if any(a.tag == W + 'hyperlink' for a in run.iterancestors()) or 'hyperlink' in names: keys.add('link') if target != 'code_block' and any(word in names for word in ('code', 'verbatim')): keys.add('inline_code') # Preserve syntax-highlighting styles supplied by Pandoc for code blocks. if target == 'code_block': keys.clear() ordered = source_order or tuple(key for key in ('link', 'strong', 'emphasis', 'strike', 'inline_code') if key in keys) if target == 'code_block': ordered = () identifier = self.combined_character(ordered, target) if target == 'code_block' and run_id: identifier = self.syntax_style(run_id, target) remove_fields(rpr, RUN_FIELDS) if identifier: val(rpr, 'rStyle', identifier) elif target == 'code_block' or not run_id: remove_fields(rpr, ('rStyle',)) # Word character-style emphasis XORs with paragraph emphasis. Protect # only explicit semantic emphasis; fonts, sizes, colors and spacing stay # style-driven. This is an intentional export contract, not a style copy. merged = {} for key in ordered: merged.update(self.character_rule(key)) for field, tags in (('bold', ('b', 'bCs')), ('italic', ('i', 'iCs')), ('strike', ('strike',))): if field in merged: for tag in tags: val(rpr, tag, int(merged[field])) def apply(self, document): for paragraph in document.iter(W + 'p'): continuation = paragraph.attrib.pop(CONTINUATION, '') == '1' list_target = paragraph.attrib.pop(LIST_TARGET, '') ppr = properties(paragraph, 'pPr') original_id = reference(ppr, 'pStyle') or 'Normal' names = {reference(style, 'name') for style in self.chain(original_id)} if 'MDHelperSourceTableMarker' in names: for text in paragraph.iter(W + 't'): if text.text == '\u200b': text.text = '' for name in tuple(names): if name in SOURCE_LIST_STYLES: base, list_target = SOURCE_LIST_STYLES[name] names.add(base) loose = bool(names & set(SOURCE_PARAGRAPH_STYLES)) target = self.classify(paragraph, original_id) if target.startswith('list_') and list_target in LIST_TARGETS: target = list_target identifier = self.paragraph(target) numpr = ppr.find(W + 'numPr') # Every numbered paragraph in a list must share the same rewritten # numbering instance, including headings and code. Otherwise Word # restarts numbering when the next ordinary paragraph is reached. if numpr is not None: if not list_target: _, original_level = self.list_level(numpr) list_target = 'list_bullet' if reference(original_level, 'numFmt') == 'bullet' else 'list_ordered' self.inherit_list_indents(numpr) if target == 'code_block' or re.fullmatch('h[1-6]', target): level, _ = self.list_level(numpr) identifier = self.list_block(target, list_target, level) marker = self.effective[list_target].get('list_marker') if marker: self.apply_list_marker(numpr, list_target, marker) if continuation and target == 'blockquote': remove_fields(ppr, ('numPr',)) if target.startswith('list_'): level, _ = self.list_level(numpr) if level: identifier = self.nested_list(target, level) marker = self.effective[target].get('list_marker') if marker: self.apply_list_marker(numpr, target, marker) if loose: identifier = self.list_paragraph(target, identifier, level) if QUOTED_LIST_PARAGRAPH_STYLE in names: identifier = self.quoted_list(target, identifier, level) if continuation: remove_fields(ppr, ('numPr',)) alignment = reference(ppr, 'jc') if target in ('table_head','table_cell') and alignment in ('left','center','right','both'): identifier = self.table_column(target, alignment) remove_fields(ppr, PARAGRAPH_FIELDS) val(ppr, 'pStyle', identifier) for run in paragraph.iter(W + 'r'): self.apply_run(run, target) for table in document.iter(W + 'tbl'): props = properties(table, 'tblPr') remove_fields(props, ('tblBorders', 'tblCellMar', 'shd')) val(props, 'tblStyle', style_id('table')) child(props, 'tblLayout').set(W + 'type', 'autofit') child(props, 'tblW').attrib.update({W + 'type': 'pct', W + 'w': '5000'}) child(props, 'tblLook').attrib.update({W + 'val': '0020', W + 'firstRow': '1', W + 'noHBand': '0', W + 'noVBand': '1'}) rule = self.effective['table'] for index, row in enumerate(table.findall(W + 'tr')): row_props = properties(row, 'trPr') val(row_props, 'tblHeader', int(index == 0 and rule.get('table_repeat_header', True))) val(row_props, 'cantSplit', int(not rule.get('table_allow_row_split', False))) for cell in table.iter(W + 'tc'): tcpr = cell.find(W + 'tcPr') if tcpr is not None: remove_fields(tcpr, ('shd', 'tcBorders', 'tcMar')) child(tcpr, 'tcW').attrib.update({W + 'type': 'auto', W + 'w': '0'}) def apply_semantic_styles(path, rules, **defaults): with zipfile.ZipFile(path) as package: entries = [(info, package.read(info.filename)) for info in package.infolist()] payloads = {info.filename: data for info, data in entries} parser = ET.XMLParser(resolve_entities=False, no_network=True) parts = {name: ET.fromstring(payloads[name], parser) for name in ('word/document.xml', 'word/styles.xml', 'word/numbering.xml', 'word/footnotes.xml', 'word/endnotes.xml') if name in payloads} adapter = WordStyles(parts['word/styles.xml'], parts.get('word/numbering.xml'), rules, **defaults) adapter.apply(parts['word/document.xml']) for name in ('word/footnotes.xml', 'word/endnotes.xml'): if name in parts: for paragraph in parts[name].iter(W + 'p'): identifier = reference(paragraph.find(W + 'pPr'), 'pStyle') if any(reference(s, 'name') in SOURCE_PARAGRAPH_STYLES for s in adapter.chain(identifier)): adapter.apply(paragraph) for run in parts[name].iter(W + 'r'): identifier = reference(run.find(W + 'rPr'), 'rStyle') if any(source_style_order(reference(s, 'name')) for s in adapter.chain(identifier)): adapter.apply_run(run, 'paragraph') for style in list(parts['word/styles.xml']): if source_style_order(reference(style, 'name')) or reference(style, 'name') in (*SOURCE_PARAGRAPH_STYLES, 'MDHelperSourceTableMarker'): parts['word/styles.xml'].remove(style) for filename, root in parts.items(): _order_properties(root) payloads[filename] = ET.tostring(root, encoding='UTF-8', xml_declaration=True, standalone=True) with zipfile.ZipFile(path, 'w', compression=zipfile.ZIP_DEFLATED) as package: for info, _ in entries: package.writestr(info, payloads[info.filename])