88 lines
4.2 KiB
PowerShell
88 lines
4.2 KiB
PowerShell
param([string]$Python = (Join-Path $PSScriptRoot '..\mdhelper_env\mdhelper_env\Scripts\python.exe'))
|
|
$ErrorActionPreference='Stop'
|
|
Push-Location (Join-Path $PSScriptRoot '..')
|
|
$word=$null
|
|
try {
|
|
$folder = (@'
|
|
import sys,tempfile
|
|
from pathlib import Path
|
|
sys.path.insert(0,str(Path('tests').resolve()))
|
|
from test_style_revision_058 import *
|
|
from mdhelper.style_sync import normalize_payload
|
|
folder=Path(tempfile.mkdtemp(prefix='MDHelper-058-Word-'))
|
|
for engine in ENGINES:
|
|
for name,source in [('nested-table',NESTED_TABLE),('quote-table',QUOTE_TABLE),('nested-first',NESTED_FIRST),('quote-text',QUOTE_TEXT),('mixed',MIXED_QUOTE)]:
|
|
export_markdown_to_docx(source,folder/f'{engine}-{name}.docx',engine=engine)
|
|
for depth in range(3):
|
|
source='\n'.join('> '*depth+line for line in ['| Head |','|---|','| Cell |'])
|
|
export_markdown_to_docx(source,folder/f'{engine}-table-{depth}.docx',engine=engine)
|
|
rules=normalize_payload({'css':'p{font:italic 20pt Initial Font}'})['rules']
|
|
export_markdown_to_docx('Text',folder/f'{engine}-font.docx',engine=engine,style_rules=rules)
|
|
print(folder)
|
|
'@ | & $Python -B -).Trim()
|
|
if($LASTEXITCODE -ne 0){throw 'Fixture generation failed'}
|
|
$word=New-Object -ComObject Word.Application
|
|
$word.Visible=$false;$word.DisplayAlerts=0
|
|
foreach($engine in @('builtin','python-docx','pandoc')) {
|
|
foreach($case in @('nested-table','quote-table','nested-first','quote-text','mixed','table-0','table-1','table-2','font')) {
|
|
$doc=$word.Documents.Open((Join-Path $folder "$engine-$case.docx"),$false,$true,$false)
|
|
try {
|
|
if($case -in @('nested-table','quote-table','nested-first')) {
|
|
if($doc.Paragraphs.Item(1).Range.ListFormat.ListString.Trim() -ne '1.'){throw "$engine $case parent number missing"}
|
|
$r=$doc.Content.Duplicate;[void]$r.Find.Execute('End')
|
|
if($r.ListFormat.ListString.Trim() -ne '2.'){throw "$engine $case next number wrong"}
|
|
if($case -eq 'nested-table') {
|
|
$r=$doc.Content.Duplicate;[void]$r.Find.Execute('Outer')
|
|
if($r.Style.NameLocal -match '标题|Heading'){throw "$engine parent became a heading"}
|
|
}
|
|
} elseif($case -eq 'font') {
|
|
$r=$doc.Content.Duplicate;[void]$r.Find.Execute('Text')
|
|
if($r.Font.Size -ne 20 -or $r.Font.Italic -ne -1){throw "$engine font CSS lost"}
|
|
$doc.Styles.Item('MDHelper 正文').Font.Size=24
|
|
if($r.Font.Size -ne 24){throw "$engine font no longer follows paragraph style"}
|
|
} else {
|
|
$doc.ExportAsFixedFormat((Join-Path $folder "$engine-$case.pdf"),17)
|
|
if($case -eq 'quote-text') {
|
|
$doc.Styles.Item('MDHelper 引用').Font.Color=255
|
|
foreach($text in @('OuterText','InnerText')) {
|
|
$r=$doc.Content.Duplicate;[void]$r.Find.Execute($text)
|
|
if($r.Font.Color -ne 255){throw "$engine nested quote lost base inheritance"}
|
|
}
|
|
} elseif($case -eq 'mixed') {
|
|
$doc.Styles.Item('MDHelper 标题 3').Font.Size=20
|
|
$r=$doc.Content.Duplicate;[void]$r.Find.Execute('Heading')
|
|
if($r.Font.Size -ne 20){throw "$engine nested heading lost base inheritance"}
|
|
}
|
|
}
|
|
Write-Output "$engine $case PASS"
|
|
} finally {$doc.Close(0)}
|
|
}
|
|
}
|
|
@'
|
|
import sys
|
|
from pathlib import Path
|
|
from pypdf import PdfReader
|
|
folder=Path(sys.argv[1])
|
|
def positions(name):
|
|
result={}
|
|
def visit(text,cm,tm,font,size):
|
|
if text.strip():result[text.strip()]=tm[4]*cm[0]+tm[5]*cm[2]+cm[4]
|
|
for page in PdfReader(folder/name).pages:page.extract_text(visitor_text=visit)
|
|
return result
|
|
for engine in ('builtin','python-docx','pandoc'):
|
|
quote=positions(f'{engine}-quote-text.pdf')
|
|
assert abs(quote['InnerText']-quote['OuterText']-15)<.3,(engine,quote)
|
|
mixed=positions(f'{engine}-mixed.pdf')
|
|
values=[mixed[x] for x in ('Inner','Continued','Heading','CodeLine')]
|
|
assert max(values)-min(values)<.3,(engine,mixed)
|
|
tables=[positions(f'{engine}-table-{d}.pdf')['Head'] for d in range(3)]
|
|
assert all(abs(tables[d]-tables[d-1]-15)<.3 for d in (1,2)),(engine,tables)
|
|
print(engine,'actual Word quote/table layout PASS')
|
|
'@ | & $Python -B - $folder
|
|
if($LASTEXITCODE -ne 0){throw 'Word PDF layout failed'}
|
|
Write-Output "Fixtures: $folder"
|
|
} finally {
|
|
if($word){$word.Quit();[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($word)}
|
|
Pop-Location
|
|
}
|