89 lines
5.3 KiB
Python
89 lines
5.3 KiB
Python
"""Code ancestor defaults and non-paragraph blocks inside Markdown lists."""
|
|
import pytest
|
|
from mdhelper.style_sync import normalize_payload
|
|
from mdhelper.document_style import resolved_rules
|
|
from mdhelper.docx_exporter import export_markdown_to_docx
|
|
from mdhelper.docx_style import W, NS
|
|
from test_style_regressions import browser, parts
|
|
from docx_assertions import effective_document
|
|
|
|
ENGINES = ('builtin', 'python-docx', 'pandoc')
|
|
CODE_RULES = {'h1': {'font_color': '#ff0000', 'font_size': 20, 'font_size_unit': 'pt', 'underline': False},
|
|
'strong': {'underline': True}, '_metadata': {'code': {'based_on': 'h1'}}}
|
|
CODE_SOURCE = '# Parent\n\n`PlainCode`\n\n**Outer `InnerCode` outer**\n\n**Other `OtherCode`**'
|
|
BOLD_RULES = {'paragraph': {'bold': True}, 'h1': {'bold': True}, '_metadata': {'code': {'based_on': 'h1'}}}
|
|
HEADING_LIST = '1. ### FirstHeading\n\n Continuation\n\n2. ### SecondHeading'
|
|
CODE_LIST = '1. ```text\n CodeFirst\n CodeLine\n ```\n\n Continuation\n\n2. Second'
|
|
NESTED_LIST = '1. Outer\n\n 1. ### NestedHeading\n\n 2. ```text\n NestedCode\n ```\n\n 3. LastNested\n\n2. LastOuter'
|
|
|
|
|
|
def test_code_ancestor_is_shared_by_reader_and_graphical_settings(qt_application, tmp_path):
|
|
payload = normalize_payload({'rules': CODE_RULES})
|
|
rule = resolved_rules(payload['rules'])['inline_code']
|
|
assert (rule['font_color'], rule['font_size'], rule['bold'], rule['underline']) == ('#ff0000', 20, True, False)
|
|
assert normalize_payload(payload) == payload
|
|
result = browser(qt_application, tmp_path, payload, CODE_SOURCE,
|
|
"[...document.querySelectorAll('code')].map(e=>{const s=getComputedStyle(e);return [s.color,s.fontSize,s.fontWeight,getComputedStyle(e.querySelector('[data-mdh-text]')).textDecorationLine]})")
|
|
assert len(result) == 3
|
|
for color, size, weight, line in result:
|
|
assert color == 'rgb(255, 0, 0)' and abs(float(size[:-2])-20*96/72) < .01
|
|
assert weight == '700' and line == 'none'
|
|
|
|
|
|
def test_character_descendants_keep_materialized_code_ancestor():
|
|
rules = {'h1': {'english_font_family': 'Arial'}, '_metadata': {
|
|
'code': {'based_on': 'h1'}, 'emphasis': {'based_on': 'inline_code'}}}
|
|
effective = resolved_rules(rules)
|
|
assert effective['emphasis']['english_font_family'] == effective['inline_code']['english_font_family'] == 'Arial'
|
|
assert effective['emphasis']['font_size'] == effective['h1']['font_size']
|
|
|
|
|
|
@pytest.mark.parametrize('engine', ENGINES)
|
|
def test_word_nested_code_keeps_ancestor_attributes_and_named_style(tmp_path, engine):
|
|
doc, styles = parts(export_markdown_to_docx(CODE_SOURCE, tmp_path/'code.docx', engine=engine, style_rules=CODE_RULES))
|
|
effective = effective_document(doc, styles)
|
|
for word in ('PlainCode', 'InnerCode', 'OtherCode'):
|
|
run = next(r for r in effective.iter(W+'r') if ''.join(r.itertext()) == word)
|
|
assert run.find('w:rPr/w:color', NS).get(W+'val') == 'ff0000'
|
|
assert run.find('w:rPr/w:sz', NS).get(W+'val') == '40'
|
|
assert run.find('w:rPr/w:b', NS).get(W+'val', '1') == '1'
|
|
assert run.find('w:rPr/w:u', NS).get(W+'val') == 'none'
|
|
mixed = styles.find("w:style[@w:styleId='MDHelperMixStrongInlineCode']", NS)
|
|
mixed.find('w:rPr/w:color', NS).set(W+'val', '0000ff')
|
|
for run in effective_document(doc, styles).iter(W+'r'):
|
|
if ''.join(run.itertext()) in ('InnerCode', 'OtherCode'):
|
|
assert run.find('w:rPr/w:color', NS).get(W+'val') == '0000ff'
|
|
|
|
|
|
@pytest.mark.parametrize('engine', ENGINES)
|
|
def test_code_ancestor_emphasis_is_protected_in_bold_paragraph(tmp_path, engine):
|
|
doc, styles = parts(export_markdown_to_docx('# Parent\n\nBody `Inline`', tmp_path/'bold.docx', engine=engine, style_rules=BOLD_RULES))
|
|
run = next(r for r in effective_document(doc, styles).iter(W+'r') if ''.join(r.itertext()) == 'Inline')
|
|
assert run.find('w:rPr/w:b', NS).get(W+'val', '1') == '1'
|
|
assert run.find('w:rPr/w:sz', NS).get(W+'val') == '49' # Default h1, rounded to Word half-points.
|
|
|
|
|
|
@pytest.mark.parametrize('engine', ENGINES)
|
|
@pytest.mark.parametrize('source,words', [(HEADING_LIST, ('FirstHeading', 'SecondHeading')),
|
|
(CODE_LIST, ('CodeFirst', 'Second')),
|
|
(NESTED_LIST, ('NestedHeading', 'NestedCode', 'LastNested'))])
|
|
def test_list_blocks_share_numbering_and_keep_editable_block_styles(tmp_path, engine, source, words):
|
|
path = export_markdown_to_docx(source, tmp_path/'list.docx', engine=engine)
|
|
doc, styles = parts(path)
|
|
paragraphs = {''.join(p.itertext()): p for p in doc.iter(W+'p')}
|
|
numbers = []
|
|
for word in words:
|
|
p = next(p for text,p in paragraphs.items() if text.startswith(word))
|
|
numbers.append(p.find('w:pPr/w:numPr/w:numId', NS).get(W+'val'))
|
|
style_id = p.find('w:pPr/w:pStyle', NS).get(W+'val')
|
|
style = styles.find(f"w:style[@w:styleId='{style_id}']", NS)
|
|
if 'Heading' in word:
|
|
assert style.find('w:basedOn', NS).get(W+'val') == 'MDHelperH3'
|
|
if 'Code' in word:
|
|
assert style.find('w:basedOn', NS).get(W+'val') == 'MDHelperCodeBlock'
|
|
assert len(set(numbers)) == 1
|
|
for text,p in paragraphs.items():
|
|
if text in ('Continuation', 'CodeLine'):
|
|
assert p.find('w:pPr/w:numPr', NS) is None or engine == 'pandoc'
|
|
assert not any(a.startswith('{urn:mdhelper:source}') for p in doc.iter(W+'p') for a in p.attrib)
|