fix: preserve scoped CSS and editable Word inheritance
This commit is contained in:
@@ -78,6 +78,7 @@ class WordStyles:
|
||||
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)
|
||||
@@ -167,8 +168,12 @@ class WordStyles:
|
||||
keys = ordered_styles(keys)
|
||||
if not keys: return ''
|
||||
merged = {}
|
||||
owners = {}
|
||||
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')})
|
||||
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]
|
||||
@@ -176,7 +181,7 @@ class WordStyles:
|
||||
props, old = child(style, 'rPr'), ET.Element(W + 'rPr')
|
||||
_run_properties(props, merged)
|
||||
_run_properties(old, self.character_rule(base))
|
||||
_delta(props, old)
|
||||
_delta(props, old, {field: value for field, value in merged.items() if owners[field] != base})
|
||||
return identifier
|
||||
|
||||
def syntax_style(self, run_id, target):
|
||||
@@ -207,6 +212,34 @@ class WordStyles:
|
||||
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)
|
||||
heading = re.search(r'heading\s*([1-6])', names)
|
||||
@@ -288,6 +321,7 @@ class WordStyles:
|
||||
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])
|
||||
@@ -359,6 +393,7 @@ class WordStyles:
|
||||
identifier = self.paragraph(target)
|
||||
if target.startswith('list_'):
|
||||
numpr = ppr.find(W + 'numPr')
|
||||
self.inherit_list_indents(numpr)
|
||||
level, _ = self.list_level(numpr)
|
||||
if level: identifier = self.nested_list(target, level)
|
||||
marker = self.effective[target].get('list_marker')
|
||||
|
||||
Reference in New Issue
Block a user