Files
MDHelper/tests/test_style_revision_058.py
T

69 lines
3.9 KiB
Python

"""Nested list markers and quote containers remain shared, editable Word styles."""
import pytest
from mdhelper.docx_exporter import export_markdown_to_docx
from mdhelper.docx_style import W, NS
from test_style_regressions import parts
from docx_assertions import effective_document
ENGINES = ['builtin', 'python-docx', 'pandoc']
NESTED_TABLE = '1. Outer\n - | HeadOne | HeadTwo |\n |---|---|\n | CellOne | CellTwo |\n\n2. End'
QUOTE_TABLE = '1. > | HeadOne | HeadTwo |\n > |---|---|\n > | CellOne | CellTwo |\n\n2. End'
NESTED_FIRST = '1. - FirstChild\n - SecondChild\n\n2. End'
QUOTE_TEXT = '> OuterText\n>\n> > InnerText\n> >\n> > ### InnerHeading\n> >\n> > ```text\n> > InnerCode\n> > ```'
MIXED_QUOTE = '1. Outer\n\n > - Inner\n >\n > Continued\n >\n > ### Heading\n >\n > ```text\n > CodeLine\n > ```\n\n2. End'
@pytest.mark.parametrize('engine', ENGINES)
@pytest.mark.parametrize('source', [NESTED_TABLE, QUOTE_TABLE, NESTED_FIRST])
def test_nested_blocks_keep_parent_marker_and_content(tmp_path, engine, source):
doc, styles = parts(export_markdown_to_docx(source, tmp_path/'nested.docx', engine=engine))
paragraphs = doc.findall('w:body/w:p', NS)
first, last = paragraphs[0], paragraphs[-1]
assert first.find('w:pPr/w:numPr', NS) is not None
assert first.find('w:pPr/w:numPr/w:numId', NS).get(W+'val') == last.find('w:pPr/w:numPr/w:numId', NS).get(W+'val')
assert 'H2' not in first.find('w:pPr/w:pStyle', NS).get(W+'val')
if 'HeadOne' in source:
assert [''.join(c.itertext()) for c in doc.findall('.//w:tbl/w:tr/w:tc', NS)] == ['HeadOne','HeadTwo','CellOne','CellTwo']
assert '\u200b' not in ''.join(doc.itertext())
assert not any(a.startswith('{urn:mdhelper:source}') for el in doc.iter() for a in el.attrib)
@pytest.mark.parametrize('engine', ENGINES)
def test_parent_text_after_nested_list_is_a_continuation(tmp_path, engine):
source = NESTED_FIRST.replace('\n\n2. End', '\n\n Continued\n\n2. End')
doc, _ = parts(export_markdown_to_docx(source, tmp_path/'continued.docx', engine=engine))
p = next(p for p in doc.iter(W+'p') if ''.join(p.itertext()) == 'Continued')
assert p.find('w:pPr/w:numPr', NS) is None
@pytest.mark.parametrize('engine', ENGINES)
def test_nested_quote_text_keeps_depth_and_base_font_inheritance(tmp_path, engine):
doc, styles = parts(export_markdown_to_docx(QUOTE_TEXT, tmp_path/'quote.docx', engine=engine))
effective = effective_document(doc, styles)
def left(text):
p = next(p for p in effective.iter(W+'p') if ''.join(p.itertext()) == text)
return int(p.find('w:pPr/w:ind', NS).get(W+'left'))
assert left('InnerText') - left('OuterText') == 300
styles.find("w:style[@w:styleId='MDHelperBlockquote']/w:rPr/w:color", NS).set(W+'val','008000')
for p in effective_document(doc,styles).iter(W+'p'):
if ''.join(p.itertext()) in ('OuterText', 'InnerText'):
assert p.find('.//w:rPr/w:color',NS).get(W+'val') == '008000'
@pytest.mark.parametrize('engine', ENGINES)
def test_mixed_quote_containers_align_text_headings_and_code(tmp_path, engine):
doc, styles = parts(export_markdown_to_docx(MIXED_QUOTE, tmp_path/'mixed.docx', engine=engine))
positions = []
for p in effective_document(doc,styles).iter(W+'p'):
if ''.join(p.itertext()) in ('Inner','Continued','Heading','CodeLine'):
positions.append(int(p.find('w:pPr/w:ind',NS).get(W+'left')))
assert positions == [1500]*4
@pytest.mark.parametrize('engine', ENGINES)
@pytest.mark.parametrize('depth', [1, 2])
def test_quoted_tables_preserve_container_depth(tmp_path, engine, depth):
source = '\n'.join('> '*depth+line for line in ['| Head |','|---|','| Cell |'])
doc, _ = parts(export_markdown_to_docx(source, tmp_path/'table.docx', engine=engine))
assert doc.find('.//w:tblPr/w:tblInd', NS).get(W+'w') == str(round(depth*300 + 12*.76*20))