1param([Parameter(Mandatory=$true)][string]$Root, [Parameter(Mandatory=$true)][string]$CloneHost)
2Set-StrictMode -Version Latest
3$ErrorActionPreference = 'Stop'
4& "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost
5$app = New-Object -ComObject OneNote.Application
6$notebook = ''
7try {
8 $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebook, 0)
9 $section = ''
10 $app.OpenHierarchy('TitleControl.one', $notebook, [ref]$section, 0)
11 $deadline = [DateTime]::UtcNow.AddSeconds(30)
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 3) { break }
18 Start-Sleep -Milliseconds 200
19 } while ([DateTime]::UtcNow -lt $deadline)
20 if ($pages.Count -ne 3) { throw 'Expected three outline title controls.' }
21 $inputs = Join-Path $Root 'inputs'
22 [void](New-Item -ItemType Directory -Path $inputs)
23 foreach ($page in $pages) {
24 $name = $page.GetAttribute('name')
25 if ($name -notin @('Vertical first', 'Horizontal first', 'Explicit title')) { throw 'Unexpected control title.' }
26 $content = ''
27 $app.GetPageContent($page.GetAttribute('ID'), [ref]$content, 1, 1)
28 [xml]$xml = $content
29 $outlines = @($xml.SelectNodes('/*/*[local-name()="Outline"]') | Where-Object { $_.InnerText -match 'Second' })
30 if ($outlines.Count -ne 1) { throw 'Expected one movable outline.' }
31 $outline = $outlines[0]
32 $position = $outline.SelectSingleNode('./*[local-name()="Position"]')
33 $position.SetAttribute('x', '36')
34 $position.SetAttribute('y', $(if ($name -eq 'Horizontal first') { '108' } else { '72' }))
35 foreach ($child in @($xml.DocumentElement.ChildNodes)) {
36 if ($child -ne $outline -and $child.LocalName -notin @('QuickStyleDef', 'TagDef')) {
37 [void]$xml.DocumentElement.RemoveChild($child)
38 }
39 }
40 [IO.File]::WriteAllText((Join-Path $inputs ($name + '.xml')), $xml.OuterXml, [Text.Encoding]::UTF8)
41 $app.UpdatePageContent($xml.OuterXml, [DateTime]::MinValue, 1, $false)
42 }
43 $app.SyncHierarchy($notebook)
44} finally {
45 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
46 [GC]::Collect()
47 [GC]::WaitForPendingFinalizers()
48}