Constant WRITE_SCRIPT
Source pub const WRITE_SCRIPT: &str = "# Windows PowerShell 5.1; fixed code with a JSON request on stdin.\n$ErrorActionPreference = \'Stop\'\n$ProgressPreference = \'SilentlyContinue\'\n[Console]::InputEncoding = New-Object Text.UTF8Encoding($false)\n[Console]::OutputEncoding = New-Object Text.UTF8Encoding($false)\n$ns = \'http://schemas.microsoft.com/office/onenote/2013/onenote\'\n$app = $null\n$pageId = \'\'\n$attempted = $false\n$created = $false\n$failed = $false\nfunction Parse-Page([string]$xml, [string]$id) {\n $doc = New-Object Xml.XmlDocument\n $doc.XmlResolver = $null\n $settings = New-Object Xml.XmlReaderSettings\n $settings.DtdProcessing = [Xml.DtdProcessing]::Prohibit\n $reader = [Xml.XmlReader]::Create((New-Object IO.StringReader($xml)), $settings)\n try { $doc.Load($reader) } finally { $reader.Dispose() }\n if ($doc.DocumentElement.LocalName -ne \'Page\' -or $doc.DocumentElement.NamespaceURI -ne $ns -or $doc.DocumentElement.GetAttribute(\'ID\') -cne $id) {\n throw \'OneNote returned a different page than requested.\'\n }\n return ,$doc\n}\nfunction Nodes($node, [string]$name) {\n return $node.SelectNodes(\"*[local-name()=\'$name\' and namespace-uri()=\'$ns\']\")\n}\nfunction Normalize([string]$text) { return [regex]::Replace($text, \'\\s+\', \' \').Trim() }\nfunction Plain($node) {\n $parts = @($node.SelectNodes(\".//*[local-name()=\'T\' and namespace-uri()=\'$ns\']\") | ForEach-Object {\n $html = [regex]::Replace($_.InnerText, \'(?i)<br\\s*/?>\', \"`n\")\n [Net.WebUtility]::HtmlDecode([regex]::Replace($html, \'<[^>]*>\', \'\'))\n })\n return Normalize ($parts -join \"`n\")\n}\nfunction Text-Node($doc, $parent, [string]$text) {\n foreach ($line in $text.Replace(\"`r`n\", \"`n\").Replace(\"`r\", \"`n\").Split(\"`n\")) {\n $oe = $doc.CreateElement(\'one\', \'OE\', $ns)\n $t = $doc.CreateElement(\'one\', \'T\', $ns)\n # OneNote drops HTML <br> during import; use a separate OE per line.\n $t.InnerText = if ($line.Length -eq 0) { \' \' } else { [Net.WebUtility]::HtmlEncode($line) }\n [void]$oe.AppendChild($t)\n [void]$parent.AppendChild($oe)\n }\n}\ntry {\n $request = [Console]::In.ReadToEnd() | ConvertFrom-Json\n if ($request.operation -notin @(\'create\', \'append\')) { throw [ArgumentException]::new(\'Unknown write operation.\') }\n $target = [string]$request.target\n $text = [string]$request.text\n if ([string]::IsNullOrWhiteSpace($target) -or $target.Length -gt 4096 -or $target -match \'[\\x00-\\x1f\\x7f]\') { throw [ArgumentException]::new(\'Invalid target ID.\') }\n if ([string]::IsNullOrWhiteSpace($text) -or [Text.Encoding]::UTF8.GetByteCount($text) -gt 1048576) { throw [ArgumentException]::new(\'Content must contain 1 byte to 1 MiB of text.\') }\n [void][Xml.XmlConvert]::VerifyXmlChars($text)\n if ($request.operation -eq \'create\') {\n if ([string]::IsNullOrWhiteSpace($request.title) -or [Text.Encoding]::UTF8.GetByteCount([string]$request.title) -gt 512 -or $request.title -match \'[\\x00-\\x1f\\x7f]\') { throw [ArgumentException]::new(\'Invalid title.\') }\n [void][Xml.XmlConvert]::VerifyXmlChars([string]$request.title)\n }\n $app = New-Object -ComObject OneNote.Application\n if ($request.operation -eq \'create\') {\n # Validate the section before the first mutating call.\n [string]$hierarchy = \'\'\n $app.GetHierarchy($target, 3, [ref]$hierarchy, 2)\n $sectionDoc = New-Object Xml.XmlDocument\n $sectionDoc.XmlResolver = $null\n $sectionDoc.LoadXml($hierarchy)\n $section = $sectionDoc.DocumentElement\n if ($section.LocalName -ne \'Section\' -or $section.NamespaceURI -ne $ns -or $section.GetAttribute(\'ID\') -cne $target) { throw [ArgumentException]::new(\'Target must be a section ID.\') }\n if ($section.GetAttribute(\'locked\') -eq \'true\') { throw \'Unlock this section in OneNote desktop before creating a page.\' }\n $attempted = $true\n $app.CreateNewPage($target, [ref]$pageId, 1)\n $created = $true\n } else { $pageId = $target }\n [string]$beforeXml = \'\'\n $app.GetPageContent($pageId, [ref]$beforeXml, 0, 2)\n $before = Parse-Page $beforeXml $pageId\n $modified = $before.DocumentElement.GetAttribute(\'lastModifiedTime\')\n if ([string]::IsNullOrWhiteSpace($modified)) { throw \'OneNote returned no modification timestamp; refusing an unchecked update.\' }\n $expected = [Xml.XmlConvert]::ToDateTime($modified, [Xml.XmlDateTimeSerializationMode]::Utc)\n $oldIds = @($before.DocumentElement.ChildNodes | Where-Object { $_ -is [Xml.XmlElement] -and $_.HasAttribute(\'objectID\') } | ForEach-Object { $_.GetAttribute(\'objectID\') })\n $bottom = 100.0\n foreach ($node in $before.DocumentElement.ChildNodes) {\n if ($node -isnot [Xml.XmlElement]) { continue }\n $pos = @(Nodes $node \'Position\')\n $size = @(Nodes $node \'Size\')\n if ($pos.Count -gt 0 -and $size.Count -gt 0) {\n $y = [double]::Parse($pos[0].GetAttribute(\'y\'), [Globalization.CultureInfo]::InvariantCulture)\n $height = [double]::Parse($size[0].GetAttribute(\'height\'), [Globalization.CultureInfo]::InvariantCulture)\n if ([double]::IsNaN($y + $height) -or [double]::IsInfinity($y + $height)) { throw \'Invalid page geometry.\' }\n $bottom = [Math]::Max($bottom, $y + $height + 24)\n }\n }\n $changes = New-Object Xml.XmlDocument\n $page = $changes.CreateElement(\'one\', \'Page\', $ns)\n $page.SetAttribute(\'ID\', $pageId)\n [void]$changes.AppendChild($page)\n if ($created) {\n $title = $changes.CreateElement(\'one\', \'Title\', $ns)\n Text-Node $changes $title ([string]$request.title)\n [void]$page.AppendChild($title)\n }\n # New outline only: never serialize existing objects back into UpdatePageContent.\n $outline = $changes.CreateElement(\'one\', \'Outline\', $ns)\n $position = $changes.CreateElement(\'one\', \'Position\', $ns)\n $position.SetAttribute(\'x\', \'36\')\n $position.SetAttribute(\'y\', $bottom.ToString([Globalization.CultureInfo]::InvariantCulture))\n $position.SetAttribute(\'z\', \'0\')\n [void]$outline.AppendChild($position)\n $children = $changes.CreateElement(\'one\', \'OEChildren\', $ns)\n Text-Node $changes $children $text\n [void]$outline.AppendChild($children)\n [void]$page.AppendChild($outline)\n $attempted = $true\n $app.UpdatePageContent($changes.OuterXml, $expected, 2, $false)\n [string]$xml = \'\'\n $app.GetPageContent($pageId, [ref]$xml, 0, 2)\n $after = Parse-Page $xml $pageId\n $afterIds = @($after.DocumentElement.ChildNodes | Where-Object { $_ -is [Xml.XmlElement] -and $_.HasAttribute(\'objectID\') } | ForEach-Object { $_.GetAttribute(\'objectID\') })\n foreach ($id in $oldIds) { if ($id -cnotin $afterIds) { throw \'An existing page object is absent in the read-back.\' } }\n $matches = @(Nodes $after.DocumentElement \'Outline\' | Where-Object { $_.GetAttribute(\'objectID\') -cnotin $oldIds -and (Plain $_) -ceq (Normalize $text) })\n if ($matches.Count -ne 1) { throw \'The new content block could not be verified in the read-back.\' }\n if ($created -and (Plain (@(Nodes $after.DocumentElement \'Title\')[0])) -cne (Normalize ([string]$request.title))) { throw \'The page title could not be verified.\' }\n $result = @{xml=$xml;id=$pageId;operation=$request.operation;verified=$true}\n} catch {\n $failed = $true\n $exception = $_.Exception\n while ($null -ne $exception.InnerException) { $exception = $exception.InnerException }\n $code = \'{0:X8}\' -f $exception.HResult\n $kind = \'desktop_error\'\n $message = $_.Exception.Message\n if ($exception -is [ArgumentException]) { $kind = \'invalid_input\' }\n if ($code -eq \'8004201B\') { $message += \' Unlock this section in OneNote desktop and retry.\' }\n if ($attempted) {\n $kind = \'write_uncertain\'\n $message += \" The write may have taken effect. Inspect OneNote before retrying. Page ID: $pageId\"\n }\n if ($code -eq \'80042010\' -and -not $created) {\n $kind = \'conflict\'\n $message = \'The page changed before append; no content was appended. Read the page and retry deliberately.\'\n }\n $result = @{error=@{kind=$kind;message=$message;page_id=$pageId;operation=$request.operation}}\n} finally {\n if ($null -ne $app -and [Runtime.InteropServices.Marshal]::IsComObject($app)) { [void][Runtime.InteropServices.Marshal]::ReleaseComObject($app) }\n}\n$result | ConvertTo-Json -Depth 8 -Compress\nif ($failed) { exit 1 }\n";