Skip to content

Commit 3e277d4

Browse files
feat: Get-MathML ( Fixes #2 )
1 parent e62ae01 commit 3e277d4

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

Commands/Get-MathML.ps1

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
function Get-MathML
2+
{
3+
<#
4+
.SYNOPSIS
5+
Gets MathML
6+
.DESCRIPTION
7+
Gets MathML from a file or page
8+
.EXAMPLE
9+
MathML https://dlmf.nist.gov/2.1
10+
#>
11+
[Alias('MathML')]
12+
param(
13+
# A url or file path that hopefully contains MathML
14+
# The response from this URL will be cached.
15+
[Parameter(ValueFromPipelineByPropertyName)]
16+
[Alias('Uri')]
17+
[string]
18+
$Url,
19+
20+
# If set, will request the URL, even if it has been cached.
21+
[Parameter(ValueFromPipelineByPropertyName)]
22+
[switch]
23+
$Force,
24+
25+
# If set, will use chromium to request the page, and will
26+
[Parameter(ValueFromPipelineByPropertyName)]
27+
[switch]
28+
$UseChromium,
29+
30+
# The path to a chromium browser.
31+
[Parameter(ValueFromPipelineByPropertyName)]
32+
[string]
33+
$ChromiumPath = 'chromium'
34+
)
35+
36+
begin {
37+
if (-not $script:MathMLCache) {
38+
$script:MathMLCache = [Ordered]@{}
39+
}
40+
41+
$mathMlPattern = [Regex]::new('<math[\s\S]+?</math>','IgnoreCase')
42+
}
43+
44+
process {
45+
if (-not $PSBoundParameters.Url) {
46+
$mathMLValues = @($script:MathMLCache.Values.MathML)
47+
if ($mathMLValues) {
48+
foreach ($value in $mathMLValues) {
49+
if (-not $value) { continue }
50+
$value
51+
}
52+
} else {
53+
54+
}
55+
return
56+
}
57+
58+
if (-not $script:MathMLCache["$url"] -or $Force) {
59+
$script:MathMLCache["$url"] = [Ordered]@{
60+
Response =
61+
if ($url -as [xml]) {
62+
$url -as [xml]
63+
} elseif (Test-Path $url) {
64+
Get-Content -Raw $Url
65+
} elseif (-not $UseChromium) {
66+
Invoke-RestMethod $url
67+
} else {
68+
& $ChromiumPath --headless --dump-dom --disable-gpu --no-sandbox "$url" *>&1 |
69+
Where-Object { $_ -notmatch '^\[\d+:\d+' } |
70+
Out-String -Width 1mb
71+
}
72+
}
73+
}
74+
75+
if (
76+
$script:MathMLCache["$url"].Response -and -not
77+
$script:MathMLCache["$url"].MathML
78+
) {
79+
$script:MathMLCache["$url"].MathML =
80+
@(foreach ($match in $mathMlPattern.Matches("$(
81+
$script:MathMLCache["$url"].Response
82+
)")) {
83+
$matchXml = $match.Value -as [xml]
84+
if ($matchXml) {
85+
$matchXml.pstypenames.insert(0, 'MathML')
86+
}
87+
$matchXml
88+
})
89+
90+
}
91+
92+
$script:MathMLCache["$url"].MathML
93+
}
94+
}
95+

0 commit comments

Comments
 (0)