fix: preserve per-property inheritance and semantic emphasis

This commit is contained in:
2026-10-30 11:42:18 +08:00
parent 151b176c1a
commit 88e3fe9e2b
7 changed files with 163 additions and 21 deletions
+16 -2
View File
@@ -33,7 +33,7 @@ PROPERTIES = {
"fonts": {"font-family", "--mdhelper-font-chinese", "--mdhelper-font-english"},
"size": {"font-size"}, "bold": {"font-weight"}, "italic": {"font-style"}, "color": {"color"},
"tracking": {"letter-spacing"}, "align": {"text-align"},
'decoration': {'text-decoration', 'text-decoration-line'}, 'background': {'background-color'},
'decoration': {'text-decoration', 'text-decoration-line', '--mdhelper-underline', '--mdhelper-strike'}, 'background': {'background-color'},
'border_color': {'border-color'}, 'border_width': {'border-width', 'border-style'}, 'padding': {'padding'},
'marker': {'list-style-type'}, 'level_indent': {'--mdhelper-list-level-indent'},
"line": {"line-height", "--mdhelper-line-spacing-mode"}, "indent": {"text-indent"},
@@ -62,6 +62,7 @@ ALIASES.update({'strong': 'strong', 'em': 'emphasis', 's': 'strike', 'del': 'str
ALIASES[_normalize_selector_text(SELECTORS["paragraph"])] = "paragraph"
FONT_VARIABLES = {"chinese_font_family": "--mdhelper-font-chinese", "english_font_family": "--mdhelper-font-english"}
FONT_EXPRESSION = "var(--mdhelper-font-english, var(--mdhelper-font-chinese, var(--reader-font-family))), var(--mdhelper-font-chinese, sans-serif)"
DECORATION_EXPRESSION = 'var(--mdhelper-text-decoration, none)'
@dataclass
@@ -301,6 +302,10 @@ class _CoreRuleReader:
return self.rule
def decoration(self, value):
if re.sub(r'\s+', '', value) == re.sub(r'\s+', '', DECORATION_EXPRESSION):
for key in ('underline', 'strike'):
self.choice('--mdhelper-' + key, key, {'true': True, 'false': False})
return
parts = value.split()
if not parts or not set(parts) <= {'none', 'underline', 'line-through'}: raise ValueError
self.rule.update(underline='underline' in parts, strike='line-through' in parts)
@@ -426,7 +431,10 @@ def analyze_css(source: str, font_size_px: float = 16) -> Analysis:
def _core_declarations(rule):
declarations = [d for d in css_declarations(rule) if not d.startswith(("font-family:", "padding-left:"))]
declarations = [d for d in css_declarations(rule) if not d.startswith(("font-family:", "padding-left:", 'text-decoration-line:'))]
if 'underline' in rule or 'strike' in rule:
declarations.extend(f'--mdhelper-{key}: {str(rule[key]).lower()};' for key in ('underline', 'strike') if key in rule)
declarations.append(f'text-decoration-line: {DECORATION_EXPRESSION};')
if any(rule.get(key) for key in FONT_VARIABLES):
for key, variable in FONT_VARIABLES.items():
if rule.get(key):
@@ -541,6 +549,12 @@ def render_css(source: str, rules: dict, font_family: str = '', font_size_px: fl
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)
# 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():
if any(field in rules.get(key, {}) or field in inherited.get(key, {}) for field in ('underline', 'strike')):
decoration = ' '.join(token for field, token in (('underline', 'underline'), ('strike', 'line-through')) if rule.get(field)) or 'none'
helpers += '\n' + SELECTORS[key] + ' { --mdhelper-text-decoration: ' + decoration + '; }'
paragraph = rules.get("paragraph", {})
if paragraph.get("font_size"):
helpers += f"\n.reader {{ --reader-math-font-size: {fmt(paragraph['font_size'])}{paragraph.get('font_size_unit', 'px')}; }}"