146 lines
11 KiB
PowerShell
146 lines
11 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
|
|
from test_style_revision_052 import REPEATED_SOURCE, REPEATED_RULES, ABSOLUTE_PAYLOAD, SCOPED_SOURCE, SCOPED_CSS
|
|
from mdhelper.style_sync import normalize_payload
|
|
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)
|
|
export_markdown_to_docx(REPEATED_SOURCE, folder / (engine + '-repeated.docx'), engine=engine, style_rules=REPEATED_RULES)
|
|
export_markdown_to_docx('- OuterList\n - InnerList', folder / (engine + '-list.docx'), engine=engine)
|
|
export_markdown_to_docx('## Indented', folder / (engine + '-absolute.docx'), engine=engine, style_rules=normalize_payload(ABSOLUTE_PAYLOAD)['rules'])
|
|
export_markdown_to_docx('BookText', folder / (engine + '-preset.docx'), engine=engine, style_rules={'_preset': 'serif'}, preset='custom:copy')
|
|
export_markdown_to_docx(SCOPED_SOURCE, folder / (engine + '-scope.docx'), engine=engine, style_rules=normalize_payload({'css': SCOPED_CSS})['rules'])
|
|
export_markdown_to_docx('Body **ContextColor** [ContextLink](https://example.com)', folder / (engine + '-context.docx'), engine=engine,
|
|
style_rules=normalize_payload({'css': 'p {color:red;} strong,a {color:currentColor;}'})['rules'])
|
|
export_markdown_to_docx('ValidValue', folder / (engine + '-fallback.docx'), engine=engine,
|
|
style_rules=normalize_payload({'css': 'p {font-size:20pt;font-size:nope;color:red;color:invalid;}'})['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) }
|
|
foreach ($case in @('repeated', 'list', 'absolute', 'preset')) {
|
|
$doc = $word.Documents.Open((Join-Path $testDirectory "$engine-$case.docx"), $false, $true)
|
|
try {
|
|
$text = switch ($case) { 'repeated' { 'Deep' }; 'list' { 'OuterList' }; 'absolute' { 'Indented' }; 'preset' { 'BookText' } }
|
|
$range = $doc.Content.Duplicate
|
|
if (-not $range.Find.Execute($text)) { throw "Missing $case text" }
|
|
switch ($case) {
|
|
'repeated' { if ($range.Font.Color -ne 255) { throw "$engine lost innermost repeated style." } }
|
|
'absolute' { if ($range.ParagraphFormat.FirstLineIndent -ne 24) { throw "$engine changed absolute indentation." } }
|
|
'preset' { if ($range.Font.NameAscii -ne 'Georgia') { throw "$engine lost copied preset." } }
|
|
'list' {
|
|
$base = $doc.Styles.Item($range.Style.NameLocal)
|
|
$base.ParagraphFormat.CharacterUnitRightIndent = 0
|
|
$base.ParagraphFormat.RightIndent = 20
|
|
$base.ParagraphFormat.CharacterUnitFirstLineIndent = -2
|
|
$range = $doc.Content.Duplicate; [void]$range.Find.Execute('InnerList')
|
|
if ($range.ParagraphFormat.RightIndent -ne 20 -or $range.ParagraphFormat.CharacterUnitFirstLineIndent -ne -2) { throw "$engine froze nested list inheritance." }
|
|
}
|
|
}
|
|
} finally { $doc.Close(0); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($doc) }
|
|
}
|
|
Write-Output "$engine passed: repeated inline styles, nested list edits, absolute indents and copied presets."
|
|
foreach ($case in @('scope', 'context', 'fallback')) {
|
|
$doc = $word.Documents.Open((Join-Path $testDirectory "$engine-$case.docx"), $false, $true)
|
|
try {
|
|
$texts = switch ($case) { 'scope' { @('BodyText', 'QuoteText', 'LooseItem', 'LooseSecond') }; 'context' { @('ContextColor', 'ContextLink') }; 'fallback' { @('ValidValue') } }
|
|
foreach ($text in $texts) {
|
|
$range = $doc.Content.Duplicate
|
|
if (-not $range.Find.Execute($text)) { throw "Missing $text" }
|
|
if ($range.Font.Color -ne 255) { throw "$engine lost $case color for $text." }
|
|
if ($case -ne 'context' -and $range.Font.Size -ne 20) { throw "$engine lost $case font size." }
|
|
}
|
|
if ($case -eq 'scope') {
|
|
$range = $doc.Content.Duplicate; [void]$range.Find.Execute('TightItem')
|
|
if ($range.Font.Color -eq 255) { throw "$engine applied paragraph CSS to tight list." }
|
|
}
|
|
} finally { $doc.Close(0); [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($doc) }
|
|
}
|
|
Write-Output "$engine passed: paragraph selector scope, contextual colors and invalid-value fallback."
|
|
}
|
|
} 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
|
|
}
|