| 1 | param([Parameter(Mandatory=$true)][string]$Root, [string]$CloneHost = '') |
| 2 | Set-StrictMode -Version Latest |
| 3 | $ErrorActionPreference = 'Stop' |
| 4 | & "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost |
| 5 | $app = New-Object -ComObject OneNote.Application |
| 6 | $notebook = '' |
| 7 | try { |
| 8 | $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebook, 0) |
| 9 | $section = '' |
| 10 | $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0) |
| 11 | $deadline = [DateTime]::UtcNow.AddSeconds(60) |
| 12 | do { |
| 13 | $hierarchy = '' |
| 14 | $app.GetHierarchy($section, 4, [ref]$hierarchy, 1) |
| 15 | [xml]$tree = $hierarchy |
| 16 | $pages = @($tree.SelectNodes('//*[local-name()="Page"]')) |
| 17 | if ($pages.Count -eq 1) { break } |
| 18 | Start-Sleep -Milliseconds 250 |
| 19 | } while ([DateTime]::UtcNow -lt $deadline) |
| 20 | if ($pages.Count -ne 1) { throw 'Expected one fixture page.' } |
| 21 | $pageId = $pages[0].GetAttribute('ID') |
| 22 | foreach ($phase in @('control', 'edit')) { |
| 23 | $content = '' |
| 24 | $app.GetPageContent($pageId, [ref]$content, 1, 1) |
| 25 | [IO.File]::WriteAllText((Join-Path $Root ($phase + '-before.xml')), $content, [Text.Encoding]::UTF8) |
| 26 | [xml]$page = $content |
| 27 | $matches = @($page.SelectNodes('//*[local-name()="T"]') | Where-Object { |
| 28 | $plain = [regex]::Replace($_.InnerText, '<[^>]*>', '') |
| 29 | $plain.StartsWith('Bold ') -or $plain.StartsWith('Typed ') |
| 30 | }) |
| 31 | if ($matches.Count -ne 1) { throw 'Expected one inserted rich paragraph.' } |
| 32 | $target = $matches[0] |
| 33 | if ($phase -eq 'edit') { $target.InnerText = $target.InnerText.Replace('Bold', 'Native Bold').Replace('Typed', 'Native Typed') } |
| 34 | $outline = $target |
| 35 | while ($outline.LocalName -ne 'Outline') { $outline = $outline.ParentNode } |
| 36 | $update = '<one:Page xmlns:one="http://schemas.microsoft.com/office/onenote/2010/onenote" ID="' + $pageId + '">' |
| 37 | foreach ($style in $page.SelectNodes('/*/*[local-name()="QuickStyleDef"]')) { $update += $style.OuterXml } |
| 38 | if ($outline.SelectSingleNode('.//*[local-name()="Table"]')) { |
| 39 | # Resubmitting measured outline size constrains native table auto-fit. |
| 40 | foreach ($size in @($outline.SelectNodes('*[local-name()="Size"]'))) { [void]$outline.RemoveChild($size) } |
| 41 | } |
| 42 | $update += $outline.OuterXml + '</one:Page>' |
| 43 | [IO.File]::WriteAllText((Join-Path $Root ($phase + '-update.xml')), $update, [Text.Encoding]::UTF8) |
| 44 | $app.UpdatePageContent($update, [DateTime]::MinValue, 1, $false) |
| 45 | } |
| 46 | $app.SyncHierarchy($notebook) |
| 47 | } finally { |
| 48 | if ($notebook) { $app.CloseNotebook($notebook, $false) } |
| 49 | [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app) |
| 50 | $app = $null |
| 51 | [GC]::Collect() |
| 52 | [GC]::WaitForPendingFinalizers() |
| 53 | } |