Files
MDHelper/mdhelper/docx_semantic.py
T

406 lines
23 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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, parent_style, style_name
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
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.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 = {}
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=''):
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:
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'])
_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)
_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)
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 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)
# Word cannot clear one inherited character-style property. Preserve
# the resolved remaining properties and inherit color from the text.
if rule.get('font_color') == 'inherit' and base and self.character_rule(base).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, self.character_rule(base))
_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 = {}
for key in keys:
merged.update({field: value for field, value in self.character_rule(key).items() if not (field == 'font_color' and value == 'inherit')})
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)
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 classify(self, paragraph, original_id):
names = self.names(original_id)
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 list_paragraph(self, target, base_id):
"""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 = {**base, **own}
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, own)
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.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')
borders = child(props, 'tblBorders')
for edge in ('top', 'left', 'bottom', 'right', 'insideH', 'insideV'):
element = child(borders, edge)
width = number(rule.get('border_width', .5))
color = str(rule.get('border_color', '#d0d7de'))
element.attrib.update({W + 'val': 'single' if width and color != 'transparent' else 'nil', W + 'sz': str(max(2, round(width * 8))), W + 'color': color.lstrip('#') if color != 'transparent' else 'auto'})
if 'cell_padding' in rule:
margins = child(props, 'tblCellMar')
for side in ('top', 'left', 'bottom', 'right'):
child(margins, side).attrib.update({W + 'w': str(round(number(rule['cell_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 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'):
ppr = properties(paragraph, 'pPr')
original_id = reference(ppr, 'pStyle') or 'Normal'
loose = any(reference(style, 'name') == LIST_PARAGRAPH_STYLE for style in self.chain(original_id))
target = self.classify(paragraph, original_id)
identifier = self.paragraph(target)
if target.startswith('list_'):
numpr = ppr.find(W + 'numPr')
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)
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, 'tblLook').attrib.update({W + 'val': '0020', W + 'firstRow': '1', W + 'noHBand': '0', W + 'noVBand': '1'})
for cell in table.iter(W + 'tc'):
tcpr = cell.find(W + 'tcPr')
if tcpr is not None: remove_fields(tcpr, ('shd', 'tcBorders', 'tcMar'))
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') == LIST_PARAGRAPH_STYLE 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') == LIST_PARAGRAPH_STYLE:
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])