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('synthetic.one', $notebook, [ref]$section, 0)
11 $mixed = '<b>Bold</b> <i>italic</i> ' + [char]::ConvertFromUtf32(0x1F980) + ' e' + [char]0x0301 + ' <span style="color:#123456">tail</span>'
12 $text = '<one:T><![CDATA[' + $mixed + ']]></one:T>'
13 $child = '<one:OEChildren><one:OE><one:T>Preserved child</one:T></one:OE></one:OEChildren>'
14 $cases = @(
15 @{ name = 'Split middle'; keys = '{Left}{Right 2}{Enter}'; body = '<one:OE>' + $text + '</one:OE>' },
16 @{ name = 'Split style boundary'; keys = '{Left}{Right 4}{Enter}'; body = '<one:OE>' + $text + '</one:OE>' },
17 @{ name = 'Split start'; keys = '{Left}{Enter}'; body = '<one:OE>' + $text + '</one:OE>' },
18 @{ name = 'Split end'; keys = '{Right}{Enter}'; body = '<one:OE>' + $text + '</one:OE>' },
19 @{ name = 'Split empty'; keys = '{Left}{Enter}'; body = '<one:OE><one:T><![CDATA[]]></one:T></one:OE>' },
20 @{ name = 'Split parent'; keys = '{Left}{Right 2}{Enter}'; body = '<one:OE>' + $text + $child + '</one:OE>' },
21 @{ name = 'Split child'; keys = '{Left}{Right 2}{Enter}'; nested = $true; body = '<one:OE><one:T>Preserved parent</one:T><one:OEChildren><one:OE>' + $text + '</one:OE></one:OEChildren></one:OE>' },
22 @{ name = 'Split bullet'; keys = '{Left}{Right 2}{Enter}'; body = '<one:OE><one:List><one:Bullet bullet="3" fontSize="11"/></one:List>' + $text + $child + '</one:OE>' },
23 @{ name = 'Split number'; keys = '{Left}{Right 2}{Enter}'; body = '<one:OE><one:List><one:Number numberSequence="1" numberFormat="##." restartNumberingAt="3" font="Calibri" fontSize="11"/></one:List>' + $text + '</one:OE>' },
24 @{ name = 'Split tag'; keys = '{Left}{Right 2}{Enter}'; body = '<one:OE><one:Tag index="0" completed="true" disabled="false" creationDate="2020-01-02T03:04:05.000Z" completionDate="2020-01-02T04:05:06.000Z"/>' + $text + '</one:OE>' },
25 @{ name = 'Split cell'; keys = '{Left}{Right 2}{Enter}'; body = '<one:OE><one:Table bordersVisible="true"><one:Columns><one:Column index="0" width="180" isLocked="true"/><one:Column index="1" width="180"/></one:Columns><one:Row><one:Cell><one:OEChildren><one:OE>' + $text + '</one:OE></one:OEChildren></one:Cell><one:Cell><one:OEChildren><one:OE><one:T>Preserved cell</one:T></one:OE></one:OEChildren></one:Cell></one:Row></one:Table></one:OE>' },
26 @{ name = 'Split soft break'; keys = '{Left}{Right 2}{Enter}'; body = '<one:OE><one:T><![CDATA[Bold<br>' + "`n" + 'soft break]]></one:T></one:OE>' },
27 @{ name = 'Split before hyperlink'; keys = '{Left}{Right 2}{Enter}'; body = '<one:OE><one:T><![CDATA[Before <a href="https://example.invalid/paragraph">Link label</a> tail]]></one:T></one:OE>' }
28 )
29 $inputs = Join-Path $Root 'inputs'
30 New-Item -ItemType Directory -Path $inputs | Out-Null
31 foreach ($case in $cases) {
32 $page = ''
33 $app.CreateNewPage($section, [ref]$page, 0)
34 $xml = '<one:Page xmlns:one="http://schemas.microsoft.com/office/onenote/2010/onenote" ID="' + $page + '"><one:TagDef index="0" type="0" symbol="3" fontColor="automatic" highlightColor="none" name="Fixture task"/><one:Title><one:OE><one:T>' + $case.name + '</one:T></one:OE></one:Title><one:Outline><one:Position x="72" y="108"/><one:OEChildren>' + $case.body + '<one:OE><one:T>Preserved sibling</one:T></one:OE></one:OEChildren></one:Outline></one:Page>'
35 [IO.File]::WriteAllText((Join-Path $inputs ($case.name + '.xml')), $xml, [Text.Encoding]::UTF8)
36 $app.UpdatePageContent($xml, [DateTime]::MinValue, 1, $true)
37 $case.page = $page
38 }
39 $cases | ConvertTo-Json -Depth 6 | Set-Content (Join-Path $Root 'cases.json') -Encoding UTF8
40 $app.SyncHierarchy($notebook)
41} finally {
42 if ($notebook) { $app.CloseNotebook($notebook, $false) }
43 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
44 $app = $null
45 [GC]::Collect()
46 [GC]::WaitForPendingFinalizers()
47}