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 $tag = '<one:Tag index="0" completed="true" disabled="false" creationDate="2020-01-02T03:04:05.000Z" completionDate="2020-01-02T04:05:06.000Z"/>'
15 $rightTag = $tag.Replace('index="0"', 'index="1"')
16 $cases = @(
17 @{ name = 'Join empty left right tag'; body = '<one:OE><one:T><![CDATA[]]></one:T></one:OE><one:OE>' + $rightTag + $text + '</one:OE>' },
18 @{ name = 'Join empty left two tags'; body = '<one:OE>' + $tag + '<one:T><![CDATA[]]></one:T></one:OE><one:OE>' + $rightTag + $text + '</one:OE>' },
19 @{ name = 'Join different tags'; body = '<one:OE>' + $tag + '<one:T>Left</one:T></one:OE><one:OE>' + $rightTag + $text + '</one:OE>' }
20 )
21 $inputs = Join-Path $Root 'inputs'
22 New-Item -ItemType Directory -Path $inputs | Out-Null
23 foreach ($case in $cases) {
24 $page = ''
25 $app.CreateNewPage($section, [ref]$page, 0)
26 $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:TagDef index="1" type="0" symbol="3" fontColor="automatic" highlightColor="none" name="Other task"/><one:QuickStyleDef index="0" name="p" font="Calibri" fontSize="11" fontColor="automatic" highlightColor="automatic"/><one:QuickStyleDef index="1" name="h1" font="Georgia" fontSize="18" bold="true" fontColor="#123456" highlightColor="automatic" spaceBefore="8" spaceAfter="4"/><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>'
27 [IO.File]::WriteAllText((Join-Path $inputs ($case.name + '.xml')), $xml, [Text.Encoding]::UTF8)
28 $app.UpdatePageContent($xml, [DateTime]::MinValue, 1, $true)
29 $case.page = $page
30 }
31 $cases | ConvertTo-Json -Depth 6 | Set-Content (Join-Path $Root 'cases.json') -Encoding UTF8
32 $app.SyncHierarchy($notebook)
33} finally {
34 if ($notebook) { $app.CloseNotebook($notebook, $false) }
35 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
36 $app = $null
37 [GC]::Collect()
38 [GC]::WaitForPendingFinalizers()
39}