fix: align reader and Word inheritance and nested formatting

This commit is contained in:
2026-10-30 12:04:51 +08:00
parent 88e3fe9e2b
commit df1fb2140f
10 changed files with 254 additions and 24 deletions
+21 -5
View File
@@ -278,8 +278,9 @@ class _CoreRuleReader:
self.choice('text-align', 'alignment', {name: name for name in ('left', 'center', 'right', 'justify')})
self.read('color', self.color)
self.read('letter-spacing', self.tracking)
self.read('text-decoration', self.decoration)
self.read('text-decoration-line', self.decoration)
decorations = [prop for prop in ('text-decoration', 'text-decoration-line') if prop in self.entries]
if decorations:
self.read(max(decorations, key=lambda prop: self.entries[prop][0]), self.decoration)
for prop, field in (('background-color', 'background_color'), ('border-color', 'border_color')):
self.read(prop, lambda value, field=field: self.effect_color(value, field))
for prop, field in (('border-width', 'border_width'), ('padding', 'cell_padding'), ('--mdhelper-list-level-indent', 'level_indent')):
@@ -543,12 +544,27 @@ def render_css(source: str, rules: dict, font_family: str = '', font_size_px: fl
from .document_style import resolved_rules
effective = resolved_rules(rules, font_family, font_size_px, preset)
for key, metadata in normalize_metadata(rules.get('_metadata')).items():
if metadata.get('based_on') and CATALOG[key].kind != 'character':
inherited[key] = effective[key]
if 'based_on' in metadata and CATALOG[key].kind != 'character':
inherited[key] = dict(effective[key])
if not metadata['based_on'] and CATALOG[key].base:
legacy = rules.get(CATALOG[key].base, {})
defaults = {'underline': False, 'strike': False, 'background_color': 'transparent',
'list_marker': 'decimal' if key == 'list_ordered' else 'none' if key == 'list_task' else 'disc',
'level_indent': 36}
inherited[key].update({field: value for field, value in defaults.items() if field in legacy and field not in rules.get(key, {})})
inherited = {key: {field: value for field, value in rule.items() if field not in rules.get(key, {})} for key, rule in inherited.items()}
contextual = {key: {"contextual_spacing": True} for key, rule in rules.items() if isinstance(rule, dict) and rule.get("contextual_spacing")}
helpers = build_document_style_css(contextual)
helpers += '\n' + build_document_style_css(inherited)
# Emit the same font variables as the authored rules; a child's Chinese
# font declaration must not shadow an inherited English font declaration.
for key, rule in inherited.items():
declarations = _core_declarations(rule)
if declarations: helpers += '\n' + SELECTORS[key] + ' { ' + ' '.join(declarations) + ' }'
layout_helpers = {}
for key, rule in explicit_rules(rules).items():
materialized = {**inherited.get(key, {}), **rule}
layout_helpers[key] = {field: materialized[field] for field in ('level_indent', 'contextual_spacing') if field in materialized}
helpers += '\n' + build_document_style_css(layout_helpers)
# Independent flags retain their inherited counterpart. Literal CSS such as
# text-decoration-line:none still wins over this opt-in variable expression.
for key, rule in effective.items():