@@ -7,13 +7,25 @@ function Get-MathML
77 Gets MathML from a file or page
88 . EXAMPLE
99 MathML https://dlmf.nist.gov/2.1
10+ . EXAMPLE
11+ MathML "<math xmlns='http://www.w3.org/1998/Math/MathML'>
12+ <semantics>
13+ <mrow>
14+ <mn>1</mn>
15+ <mo>+</mo>
16+ <mn>1</mn>
17+ <mo>=</mo>
18+ <mn>2</mn>
19+ </mrow>
20+ </semantics>
21+ </math>"
1022 #>
1123 [Alias (' MathML' )]
1224 param (
1325 # A url or file path that hopefully contains MathML
1426 # The response from this URL will be cached.
1527 [Parameter (ValueFromPipelineByPropertyName )]
16- [Alias (' Uri' )]
28+ [Alias (' Uri' , ' FilePath ' , ' Fullname ' , ' Xml ' , ' Text ' )]
1729 [string ]
1830 $Url ,
1931
@@ -42,54 +54,85 @@ function Get-MathML
4254 }
4355
4456 process {
57+ # If we have no URL
4558 if (-not $PSBoundParameters.Url ) {
59+ # get any loaded MathML
4660 $mathMLValues = @ ($script :MathMLCache.Values.MathML )
4761 if ($mathMLValues ) {
62+ # unroll each result
4863 foreach ($value in $mathMLValues ) {
4964 if (-not $value ) { continue }
65+ # and return non-null values
5066 $value
5167 }
52- } else {
53-
5468 }
5569 return
5670 }
5771
72+ # If we have not yet cached this URL, or we are using the `-Force`
5873 if (-not $script :MathMLCache [" $url " ] -or $Force ) {
74+ # Create a cache object
5975 $script :MathMLCache [" $url " ] = [Ordered ]@ {
6076 Response =
77+ # If the URL could be XML
6178 if ($url -as [xml ]) {
62- $url -as [xml ]
63- } elseif (Test-Path $url ) {
79+ # use that as the source.
80+ ($url -as [xml ]).OuterXml
81+ }
82+ # If the URL was actually a file path
83+ elseif (Test-Path $url )
84+ {
85+ # get it's content.
6486 Get-Content - Raw $Url
65- } elseif (-not $UseChromium ) {
87+ }
88+ # If we are not using chromium,
89+ elseif (-not $UseChromium )
90+ {
91+ # use Invoke-RestMethod to get the URL
6692 Invoke-RestMethod $url
67- } else {
68- & $ChromiumPath -- headless -- dump- dom -- disable-gpu -- no- sandbox " $url " * > & 1 |
93+ }
94+ # If we are using chromium
95+ else
96+ {
97+ # Call chromium in headless mode and dump DOM
98+ & $ChromiumPath -- headless -- disable-gpu -- no- sandbox -- dump- dom " $url " * > & 1 |
99+ # strip out any chromium trace messages
69100 Where-Object { $_ -notmatch ' ^\[\d+:\d+' } |
101+ # and stringify the whole response.
70102 Out-String - Width 1 mb
71103 }
72104 }
73105 }
74106
107+ # If we have a response for this URL, but no MathML yet
75108 if (
76109 $script :MathMLCache [" $url " ].Response -and -not
77110 $script :MathMLCache [" $url " ].MathML
78111 ) {
79112 $script :MathMLCache [" $url " ].MathML =
113+ # find any matches for our pattern
80114 @ (foreach ($match in $mathMlPattern.Matches (" $ (
81115 $script :MathMLCache [" $url " ].Response
82116 ) " )) {
117+ # and cast them into XML.
83118 $matchXml = $match.Value -as [xml ]
84- if ($matchXml ) {
85- $matchXml.pstypenames.insert (0 , ' MathML' )
119+
120+ if (-not $matchXML ) { continue }
121+ # If they do not have the xml namespace
122+ if (-not $matchXML.math.xmlns ) {
123+ # add it
124+ $matchXML.math.setAttribute (' xmlns' , ' http://www.w3.org/1998/Math/MathML' )
86125 }
87- $matchXml
126+ # decorate the return as MathML
127+ $matchXml.pstypenames.insert (0 , ' MathML' )
128+ # and output it to the cache
129+ $matchXml
88130 })
89131
90132 }
91133
92- $script :MathMLCache [" $url " ].MathML
134+ # Last but not least, output any MathML objects in the cache for this URL.
135+ $script :MathMLCache [" $url " ].MathML
93136 }
94137}
95138
0 commit comments