| 1 | param([Parameter(Mandatory=$true)][string]$Root, [Parameter(Mandatory=$true)][string]$CloneHost) |
| 2 | Set-StrictMode -Version Latest |
| 3 | $ErrorActionPreference = 'Stop' |
| 4 | & "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost |
| 5 | $namespace = 'http://schemas.microsoft.com/office/onenote/2010/onenote' |
| 6 | $app = New-Object -ComObject OneNote.Application |
| 7 | $notebookId = '' |
| 8 | try { |
| 9 | $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebookId, 0) |
| 10 | $sectionId = '' |
| 11 | $app.OpenHierarchy('links.one', $notebookId, [ref]$sectionId, 0) |
| 12 | $deadline = [DateTime]::UtcNow.AddSeconds(30) |
| 13 | $page = $null |
| 14 | $pages = @() |
| 15 | do { |
| 16 | $hierarchy = '' |
| 17 | $app.GetHierarchy($sectionId, 4, [ref]$hierarchy, 1) |
| 18 | [xml]$tree = $hierarchy |
| 19 | $pages = @($tree.SelectNodes('//*[local-name()="Page"]')) |
| 20 | if ($pages.Count -eq 1) { |
| 21 | $content = '' |
| 22 | $app.GetPageContent($pages[0].GetAttribute('ID'), [ref]$content, 3, 1) |
| 23 | [xml]$page = $content |
| 24 | if (@($page.SelectNodes('//*[local-name()="OE"]')).Count -ge 1) { break } |
| 25 | } |
| 26 | Start-Sleep -Milliseconds 250 |
| 27 | } while ([DateTime]::UtcNow -lt $deadline) |
| 28 | if ($pages.Count -ne 1) { throw 'Expected one page in the section.' } |
| 29 | [IO.File]::WriteAllText((Join-Path $Root 'before.xml'), $content, [Text.Encoding]::UTF8) |
| 30 | $targetId = '' |
| 31 | $app.CreateNewPage($sectionId, [ref]$targetId, 0) |
| 32 | $target = '<one:Page xmlns:one="' + $namespace + '" ID="' + $targetId + '"><one:Title><one:OE><one:T><![CDATA[Link target]]></one:T></one:OE></one:Title><one:Outline><one:Position x="36" y="86"/><one:Size width="300" height="30"/><one:OEChildren><one:OE><one:T><![CDATA[Target paragraph]]></one:T></one:OE></one:OEChildren></one:Outline></one:Page>' |
| 33 | $app.UpdatePageContent($target, [DateTime]::MinValue, 1, $true) |
| 34 | $targetContent = '' |
| 35 | $app.GetPageContent($targetId, [ref]$targetContent, 3, 1) |
| 36 | [xml]$targetPage = $targetContent |
| 37 | $targetParagraph = $targetPage.SelectSingleNode('//*[local-name()="Outline"]/*[local-name()="OEChildren"]/*[local-name()="OE"]').GetAttribute('objectID') |
| 38 | $pageLink = '' |
| 39 | $app.GetHyperlinkToObject($targetId, '', [ref]$pageLink) |
| 40 | $paragraphLink = '' |
| 41 | $app.GetHyperlinkToObject($targetId, $targetParagraph, [ref]$paragraphLink) |
| 42 | $sectionLink = '' |
| 43 | $app.GetHyperlinkToObject($sectionId, '', [ref]$sectionLink) |
| 44 | @{ page = $pageLink; paragraph = $paragraphLink; section = $sectionLink; target = $targetId; target_paragraph = $targetParagraph } | ConvertTo-Json | Set-Content (Join-Path $Root 'links.json') -Encoding UTF8 |
| 45 | $children = $page.SelectSingleNode('//*[local-name()="Outline"]/*[local-name()="OEChildren"]') |
| 46 | foreach ($entry in @(@('Page link', $pageLink), @('Paragraph link', $paragraphLink), @('Section link', $sectionLink))) { |
| 47 | $oe = $page.CreateElement('one', 'OE', $namespace) |
| 48 | $t = $page.CreateElement('one', 'T', $namespace) |
| 49 | [void]$t.AppendChild($page.CreateCDataSection('<a href="' + [Security.SecurityElement]::Escape($entry[1]) + '">' + $entry[0] + '</a>')) |
| 50 | [void]$oe.AppendChild($t) |
| 51 | [void]$children.AppendChild($oe) |
| 52 | } |
| 53 | [IO.File]::WriteAllText((Join-Path $Root 'update.xml'), $page.OuterXml, [Text.Encoding]::UTF8) |
| 54 | $app.UpdatePageContent($page.OuterXml, [DateTime]::MinValue, 1, $false) |
| 55 | $app.SyncHierarchy($notebookId) |
| 56 | $app.CloseNotebook($notebookId, $false) |
| 57 | $notebookId = '' |
| 58 | } finally { |
| 59 | if ($notebookId) { $app.CloseNotebook($notebookId, $false) } |
| 60 | [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app) |
| 61 | [GC]::Collect() |
| 62 | [GC]::WaitForPendingFinalizers() |
| 63 | } |