96 lines
6.8 KiB
PowerShell
96 lines
6.8 KiB
PowerShell
param([string]$Python = (Join-Path $PSScriptRoot '..\mdhelper_env\mdhelper_env\Scripts\python.exe'))
|
|
$ErrorActionPreference = 'Stop'
|
|
Push-Location (Join-Path $PSScriptRoot '..')
|
|
$testDirectory = $null
|
|
$word = $null
|
|
try {
|
|
$testDirectory = (@'
|
|
import json, tempfile, sys
|
|
from pathlib import Path
|
|
sys.path.insert(0, str(Path('tests').resolve()))
|
|
from test_word_named_styles import SOURCE
|
|
from mdhelper.docx_exporter import export_markdown_to_docx
|
|
from mdhelper.style_catalog import style_name
|
|
from test_style_regressions import PARTIAL_RULES, UNIT_RULES, NESTED_SOURCE, NESTED_RULES
|
|
folder = Path(tempfile.mkdtemp(prefix='MDHelper-Word-Styles-'))
|
|
rules = {'h2': {'bold': False}, '_metadata': {'h2': {'based_on': 'h1'}}}
|
|
for engine in ('builtin', 'python-docx', 'pandoc'):
|
|
export_markdown_to_docx(SOURCE + '\n\n## Child title', folder / (engine + '.docx'), engine=engine, style_rules=rules)
|
|
export_markdown_to_docx('# Parent\n\n## Child', folder / (engine + '-partial.docx'), engine=engine, style_rules=PARTIAL_RULES)
|
|
export_markdown_to_docx('# Parent\n\n## ChildUnits', folder / (engine + '-units.docx'), engine=engine, style_rules=UNIT_RULES)
|
|
export_markdown_to_docx(NESTED_SOURCE, folder / (engine + '-nested.docx'), engine=engine, style_rules=NESTED_RULES)
|
|
(folder / 'names.json').write_text(json.dumps({key: 'MDHelper ' + style_name(key) for key in ('paragraph', 'strong', 'link', 'h1')}), encoding='utf-8')
|
|
print(folder)
|
|
'@ | & $Python -B -).Trim()
|
|
if ($LASTEXITCODE -ne 0) { throw 'Fixture generation failed.' }
|
|
$names = Get-Content -LiteralPath (Join-Path $testDirectory 'names.json') -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
$word = New-Object -ComObject Word.Application
|
|
$word.Visible = $false
|
|
$word.DisplayAlerts = 0
|
|
foreach ($engine in @('builtin', 'python-docx', 'pandoc')) {
|
|
$doc = $word.Documents.Open((Join-Path $testDirectory "$engine.docx"), $false, $true)
|
|
try {
|
|
$range = $doc.Content.Duplicate
|
|
if (-not $range.Find.Execute('bold title')) { throw 'Missing heading text.' }
|
|
if ($range.Font.Bold -ne -1) { throw "$engine lost emphasis inside bold heading." }
|
|
$range = $doc.Content.Duplicate
|
|
if (-not $range.Find.Execute('strong second')) { throw 'Missing body text.' }
|
|
$doc.Styles.Item($names.paragraph).Font.Size = 23
|
|
if ($range.Font.Size -ne 23) { throw "$engine body style does not reach emphasized text." }
|
|
$doc.Styles.Item($names.strong).Font.Color = 255
|
|
if ($range.Font.Color -ne 255) { throw "$engine character style edit did not propagate." }
|
|
$range = $doc.Content.Duplicate
|
|
if (-not $range.Find.Execute('bold link')) { throw 'Missing mixed link text.' }
|
|
$doc.Styles.Item($names.link).Font.Color = 65280
|
|
if ($range.Font.Color -ne 65280) { throw "$engine link color does not reach combined styles." }
|
|
$range = $doc.Content.Duplicate
|
|
if (-not $range.Find.Execute('Child title')) { throw 'Missing derived heading text.' }
|
|
if ($range.Font.Bold -ne 0) { throw "$engine explicit child-style bold=False did not override its base." }
|
|
$doc.Styles.Item($names.h1).Font.Size = 30
|
|
if ($range.Font.Size -ne 30) { throw "$engine base-style change does not reach child heading." }
|
|
$range = $doc.Content.Duplicate; [void]$range.Find.Execute('strong second')
|
|
$doc.Styles.Item($names.paragraph).Font.Bold = -1
|
|
if ($range.Font.Bold -ne -1) { throw "$engine paragraph bold inverted semantic emphasis." }
|
|
$range = $doc.Content.Duplicate; [void]$range.Find.Execute('bold title')
|
|
$doc.Styles.Item($names.h1).Font.Bold = 0
|
|
if ($range.Font.Bold -ne -1) { throw "$engine heading edit removed semantic emphasis." }
|
|
Write-Output "$engine passed: Word opened document, heading emphasis, body and character edits, mixed links, base-style inheritance."
|
|
} finally { $doc.Close(0); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($doc) }
|
|
$doc = $word.Documents.Open((Join-Path $testDirectory "$engine-partial.docx"), $false, $true)
|
|
try {
|
|
$range = $doc.Content.Duplicate; [void]$range.Find.Execute('Child')
|
|
$base = $doc.Styles.Item($names.h1)
|
|
$base.Font.NameFarEast = 'KaiTi'; $base.ParagraphFormat.SpaceAfter = 24
|
|
$base.ParagraphFormat.LineSpacingRule = 0; $base.ParagraphFormat.LeftIndent = 40
|
|
if ($range.Font.NameFarEast -ne $base.Font.NameFarEast -or $range.ParagraphFormat.SpaceAfter -ne 24 -or $range.ParagraphFormat.LineSpacingRule -ne 0 -or $range.ParagraphFormat.LeftIndent -ne 40) {
|
|
throw "$engine partial overrides froze unrelated inherited properties."
|
|
}
|
|
Write-Output "$engine passed: partial overrides preserve inheritance of fonts, spacing and indents."
|
|
} finally { $doc.Close(0); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($doc) }
|
|
$doc = $word.Documents.Open((Join-Path $testDirectory "$engine-units.docx"), $false, $true)
|
|
try {
|
|
$range = $doc.Content.Duplicate; [void]$range.Find.Execute('ChildUnits')
|
|
if ($range.ParagraphFormat.LeftIndent -ne 20 -or $range.ParagraphFormat.SpaceBefore -ne 0) { throw "$engine child units did not override parent units." }
|
|
} finally { $doc.Close(0); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($doc) }
|
|
$doc = $word.Documents.Open((Join-Path $testDirectory "$engine-nested.docx"), $false, $true)
|
|
try {
|
|
$link = $doc.Content.Duplicate; [void]$link.Find.Execute('InnerLink')
|
|
$bold = $doc.Content.Duplicate; [void]$bold.Find.Execute('InnerBold')
|
|
if ($link.Font.Color -ne 16711680 -or $bold.Font.Color -ne 255) { throw "$engine lost inline nesting order." }
|
|
$doc.Styles.Item($names.link).Font.Color = 65280
|
|
if ($link.Font.Color -ne 65280 -or $bold.Font.Color -ne 255) { throw "$engine lost shared link style or inner override." }
|
|
Write-Output "$engine passed: child units, nested colors and shared style updates."
|
|
} finally { $doc.Close(0); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($doc) }
|
|
}
|
|
} finally {
|
|
if ($word) { $word.Quit(); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($word) }
|
|
if ($testDirectory -and (Test-Path -LiteralPath $testDirectory)) {
|
|
$resolved = [IO.Path]::GetFullPath($testDirectory)
|
|
$tempRoot = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()).TrimEnd('\') + '\'
|
|
if ($resolved.StartsWith($tempRoot, [StringComparison]::OrdinalIgnoreCase) -and ([IO.Path]::GetFileName($resolved)).StartsWith('MDHelper-Word-Styles-')) {
|
|
Remove-Item -LiteralPath $resolved -Recurse -Force
|
|
}
|
|
}
|
|
Pop-Location
|
|
}
|