fix: preserve per-property inheritance and semantic emphasis
This commit is contained in:
+25
-16
@@ -44,15 +44,26 @@ def _delta(current, base, authored=None):
|
||||
'keepNext': ('keep_with_next',), 'keepLines': ('keep_lines_together',), 'pageBreakBefore': ('page_break_before',),
|
||||
'widowControl': ('widow_control',), 'contextualSpacing': ('contextual_spacing',)}
|
||||
for node in list(current):
|
||||
if any(field in authored for field in fields.get(ET.QName(node).localname, ())): continue
|
||||
old = base.find(node.tag)
|
||||
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)
|
||||
elif old is not None and node.tag in {W + 'rFonts', W + 'spacing', W + 'ind'}:
|
||||
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') 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):
|
||||
if old.get(key) == node.get(key): del node.attrib[key]
|
||||
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:
|
||||
@@ -158,17 +169,6 @@ class WordStyles:
|
||||
_run_properties(props, merged)
|
||||
_run_properties(old, self.character_rule(keys[0]))
|
||||
_delta(props, old)
|
||||
# Word XORs style-level emphasis with the paragraph's emphasis. A named
|
||||
# context variant compensates for that (e.g. **text** in a bold heading),
|
||||
# keeping all formatting editable in the Styles panel, not on each run.
|
||||
compensate = [field for field in ('bold', 'italic', 'strike') if field in merged and self.effective[paragraph_target].get(field)]
|
||||
if compensate:
|
||||
context_id = identifier + 'In' + style_id(paragraph_target)[8:]
|
||||
if context_id not in self.created:
|
||||
style = self.new(context_id, 'character', 'MDHelper ' + '+'.join(style_name(k, self.rules) for k in keys) + '(' + style_name(paragraph_target, self.rules) + ')', identifier)
|
||||
for field in compensate:
|
||||
for tag in {'bold': ('b', 'bCs'), 'italic': ('i', 'iCs'), 'strike': ('strike',)}[field]: val(child(style, 'rPr'), tag, int(not merged[field]))
|
||||
identifier = context_id
|
||||
return identifier
|
||||
|
||||
def syntax_style(self, run_id, target):
|
||||
@@ -311,6 +311,15 @@ class WordStyles:
|
||||
remove_fields(rpr, RUN_FIELDS)
|
||||
if identifier: val(rpr, 'rStyle', identifier)
|
||||
else: 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 ('link', 'strong', 'emphasis', 'strike', 'inline_code'):
|
||||
if key in keys: 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'):
|
||||
|
||||
Reference in New Issue
Block a user