feat: refine table presentation and editable Word export

This commit is contained in:
2026-11-04 15:40:52 +08:00
parent 31d0ccfde5
commit 3bb2cc5438
15 changed files with 337 additions and 58 deletions
+10 -2
View File
@@ -14,6 +14,7 @@ from tinycss2.color3 import parse_color
from .document_style import (STYLE_TARGETS, normalize_rule, css_declarations,
build_document_style_css, fmt, font_points, resolved_rules)
from .table_style import TABLE_OPTIONS, TABLE_BOOL_FIELDS, TABLE_ONLY_FIELDS, table_helper_css
SELECTORS = {key: selector for key, _, selector in STYLE_TARGETS}
LABELS = {key: label for key, label, _ in STYLE_TARGETS}
@@ -28,6 +29,7 @@ GROUPS = {
**{side: (f"{side}_indent_value", f"{side}_indent_unit") for side in ("left", "right")},
**{side: (f"space_{side}_value", f"space_{side}_unit") for side in ("before", "after")},
**{name: (name,) for name in ("contextual_spacing", "keep_with_next", "keep_lines_together", "page_break_before", "widow_control")},
**{name: (name,) for name in TABLE_OPTIONS},
}
PROPERTIES = {
"fonts": {"font-family", "--mdhelper-font-chinese", "--mdhelper-font-english"},
@@ -43,6 +45,7 @@ PROPERTIES = {
"keep_with_next": {"break-after", "page-break-after"},
"keep_lines_together": {"break-inside", "page-break-inside"},
"page_break_before": {"break-before", "page-break-before"}, "widow_control": {"widows", "orphans"},
**{name: {prop} for name, prop in TABLE_OPTIONS.items()},
}
CORE_PROPERTIES = set().union(*PROPERTIES.values())
@@ -346,7 +349,7 @@ def _collect_cascade(source: str, result: Analysis):
for selector, origin in aliases.items():
child = {'th': 'table_head', 'td': 'table_cell', 'code': 'code_block', 'pre': 'code_block'}.get(selector.removeprefix('.reader '))
if child and origin in ('table', 'code'):
if origin == 'table' and prop in PROPERTIES['border_color'] | PROPERTIES['border_width'] | PROPERTIES['border_style'] | PROPERTIES['padding']: continue
if origin == 'table' and prop in PROPERTIES['border_color'] | PROPERTIES['border_width'] | PROPERTIES['border_style'] | PROPERTIES['padding'] | set(TABLE_OPTIONS.values()): continue
bindings.append((child, origin, 1, _specificity(selector)))
character_props = set().union(*(PROPERTIES[g] for g in ('fonts','size','bold','italic','color','tracking','decoration')))
for target, origin, direct, specificity in bindings:
@@ -383,6 +386,10 @@ class _CoreRuleReader:
for prop, field in (('border-width', 'border_width'), ('padding', 'cell_padding'), ('--mdhelper-list-level-indent', 'level_indent')):
self.read(prop, lambda value, field=field: self.effect_length(value, field))
self.choice('border-style','border_style',{name:name for name in ('none','hidden','solid','dotted','dashed','double','groove','ridge','inset','outset')})
self.choice(TABLE_OPTIONS['table_border_mode'], 'table_border_mode', {name: name for name in ('grid', 'horizontal', 'none')})
for field in TABLE_BOOL_FIELDS:
self.choice(TABLE_OPTIONS[field], field, {'true': True, 'false': False})
self.read(TABLE_OPTIONS['table_stripe_color'], lambda value: self.effect_color(value, 'table_stripe_color'))
self.choice('list-style-type', 'list_marker', {x: x for x in ('none', 'disc', 'circle', 'square', 'decimal', 'lower-alpha', 'upper-alpha', 'lower-roman', 'upper-roman')})
self.read('line-height', self.line)
for side, prefix in (('left', 'left_indent'), ('right', 'right_indent'), ('top', 'space_before'), ('bottom', 'space_after')):
@@ -392,7 +399,7 @@ class _CoreRuleReader:
self.choice('--mdhelper-contextual-spacing', 'contextual_spacing', {'true': True, 'false': False})
from .style_catalog import CATALOG, CHARACTER_FIELDS
unsupported = set()
if self.target != 'table': unsupported.update(('border_color', 'border_width', 'border_style', 'cell_padding'))
if self.target != 'table': unsupported.update(TABLE_ONLY_FIELDS)
if not self.target.startswith('list'): unsupported.update(('list_marker', 'level_indent'))
if CATALOG[self.target].kind == 'character': unsupported.update(set(self.rule) - CHARACTER_FIELDS)
for field in sorted(unsupported & self.rule.keys()):
@@ -667,6 +674,7 @@ def render_css(source: str, rules: dict, font_family: str = '', font_size_px: fl
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, reset_list_paragraphs=False)
helpers += '\n' + table_helper_css(rules.get('table', {}))
# 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():