|
| 1 | +function Export-MathML { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Exports MathML |
| 5 | + .DESCRIPTION |
| 6 | + Exports MathML into a file |
| 7 | + .EXAMPLE |
| 8 | + MathML https://dlmf.nist.gov/2.1 | |
| 9 | + Export-MathML ./dlmf.2.1.html |
| 10 | + #> |
| 11 | + [Alias('Save-MathML')] |
| 12 | + param( |
| 13 | + # The export file path. |
| 14 | + [Parameter(Mandatory)] |
| 15 | + [Alias('Fullname')] |
| 16 | + [string] |
| 17 | + $FilePath, |
| 18 | + |
| 19 | + # Any input objects. |
| 20 | + [Parameter(ValueFromPipeline)] |
| 21 | + [PSObject[]] |
| 22 | + $InputObject, |
| 23 | + |
| 24 | + # If set, will force an export, even if a file already exists. |
| 25 | + [switch] |
| 26 | + $Force |
| 27 | + ) |
| 28 | + |
| 29 | + # Gather all the input |
| 30 | + $allInput = @($input) |
| 31 | + |
| 32 | + # If nothing was passed |
| 33 | + if ($allInput.Length -eq 0) { |
| 34 | + # briefly check for non-piped -InputObject |
| 35 | + if ($PSBoundParameters.InputObject) { |
| 36 | + $allInput = @($PSBoundParameters.InputObject | . { process { $_ } }) |
| 37 | + } |
| 38 | + # If we still have no input, return (there is nothing to export) |
| 39 | + if ($allInput.Length -eq 0) {return} |
| 40 | + } |
| 41 | + |
| 42 | + # Find the full path, but do not resolve it |
| 43 | + $unresolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($FilePath) |
| 44 | + # If it already existed, and we are not using the `-Force` |
| 45 | + if ((Test-Path $unresolvedPath) -and -not $Force) { |
| 46 | + # write an error |
| 47 | + Write-Error "$unresolvedPath already exists, use -Force" |
| 48 | + # and return |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + # IF we have one MathML |
| 54 | + if ($allInput.Length -eq 1 -and $allInput[0] -is [xml]) { |
| 55 | + # save that to a file |
| 56 | + $newFile = New-Item -Path $unresolvedPath -Force -ItemType File |
| 57 | + # If the extension was .svg or .html, and the input has an SVG |
| 58 | + if ($newFile.Extension -in '.svg', '.html' -and $allInput[0].SVG -is [xml]) { |
| 59 | + # save the SVG to the file |
| 60 | + $allInput[0].SVG.Save($newFile.FullName) |
| 61 | + } else { |
| 62 | + # otherwise, save the XML to the file |
| 63 | + $allInput[0].Save($newFile.FullName) |
| 64 | + } |
| 65 | + } |
| 66 | + # If we have multiple MathML |
| 67 | + else { |
| 68 | + # we can store them in an XHTML file |
| 69 | + $html = @( |
| 70 | + # construct a simple header |
| 71 | + "<html><title>MathML</title><body>" |
| 72 | + foreach ($in in $allInput) { |
| 73 | + # and put each MathML within a div |
| 74 | + "<div>" |
| 75 | + |
| 76 | + # If it was XML |
| 77 | + if ($in -is [xml]) { |
| 78 | + $in.OuterXml # put it inline |
| 79 | + } |
| 80 | + # If there was a SVG property |
| 81 | + elseif ($in.SVG) { |
| 82 | + # put that inline |
| 83 | + $in.SVG.OuterXml |
| 84 | + } |
| 85 | + # If there was a HTML property |
| 86 | + elseif ($in.HTML) { |
| 87 | + # put that inline (if it was unbalanced, export will not work) |
| 88 | + $in.HTML |
| 89 | + } |
| 90 | + # last but not least, escape any text |
| 91 | + else { |
| 92 | + [Security.SecurityElement]::Escape("$in") |
| 93 | + } |
| 94 | + "</div>" |
| 95 | + } |
| 96 | + "</body></html>" |
| 97 | + ) -join [Environment]::NewLine |
| 98 | + |
| 99 | + # Create a new file containing the HTML |
| 100 | + New-Item -Path $unresolvedPath -Force -ItemType File -Value $html |
| 101 | + } |
| 102 | +} |
0 commit comments