fix: align reader and Word inheritance and nested formatting
This commit is contained in:
@@ -13,6 +13,7 @@ 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
|
||||
|
||||
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()
|
||||
@@ -158,16 +159,17 @@ class WordStyles:
|
||||
return identifier
|
||||
|
||||
def combined_character(self, keys, paragraph_target):
|
||||
keys = tuple(key for key in ('link', 'strong', 'emphasis', 'strike', 'inline_code') if key in keys)
|
||||
keys = tuple(dict.fromkeys(keys))
|
||||
if not keys: return ''
|
||||
merged = dict(self.character_rule(keys[0]))
|
||||
for key in keys[1:]: merged.update(self.character_rule(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:
|
||||
style = self.new(identifier, 'character', 'MDHelper ' + '+'.join(style_name(k, self.rules) for k in keys), self.character(keys[0]))
|
||||
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(keys[0]))
|
||||
_run_properties(old, self.character_rule(base))
|
||||
_delta(props, old)
|
||||
return identifier
|
||||
|
||||
@@ -298,6 +300,7 @@ class WordStyles:
|
||||
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)
|
||||
@@ -306,17 +309,18 @@ class WordStyles:
|
||||
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()
|
||||
identifier = self.combined_character(keys, target)
|
||||
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)
|
||||
else: remove_fields(rpr, ('rStyle',))
|
||||
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 ('link', 'strong', 'emphasis', 'strike', 'inline_code'):
|
||||
if key in keys: merged.update(self.character_rule(key))
|
||||
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]))
|
||||
@@ -350,8 +354,17 @@ def apply_semantic_styles(path, rules, **defaults):
|
||||
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') if name in payloads}
|
||||
WordStyles(parts['word/styles.xml'], parts.get('word/numbering.xml'), rules, **defaults).apply(parts['word/document.xml'])
|
||||
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 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')): 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)
|
||||
|
||||
Reference in New Issue
Block a user