-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCompare-OpenSpecToLiveHtml.ps1
More file actions
144 lines (132 loc) · 6.13 KB
/
Compare-OpenSpecToLiveHtml.ps1
File metadata and controls
144 lines (132 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function Compare-OpenSpecToLiveHtml {
<#
.SYNOPSIS
Compares converted markdown structure to the live HTML spec page on learn.microsoft.com.
.DESCRIPTION
For each converted spec, fetches the live spec page HTML and extracts its structure
(headings, section IDs). Compares with the converted markdown structure and reports
missing sections, heading/ID mismatches, and suggested manual review items.
#>
[CmdletBinding()]
param(
[string]$OutputPath = (Join-Path -Path (Get-Location) -ChildPath 'converted-specs'),
[string[]]$ProtocolId
)
if (-not (Test-Path -LiteralPath $OutputPath)) {
throw "Output path '$OutputPath' was not found."
}
$catalog = @{}
try {
foreach ($e in (Get-OpenSpecCatalog)) {
$catalog[$e.ProtocolId] = $e
$norm = $e.ProtocolId -replace '-', '_'
if (-not $catalog[$norm]) { $catalog[$norm] = $e }
}
}
catch {
Write-Warning "Could not fetch catalog: $($_.Exception.Message)"
}
$reports = Get-OpenSpecConversionReport -OutputPath $OutputPath -ProtocolId $ProtocolId
$results = New-Object System.Collections.Generic.List[object]
foreach ($report in $reports) {
try {
$protocolId = $report.ProtocolId
if ([string]::IsNullOrWhiteSpace($protocolId)) { continue }
$mdPath = $report.MarkdownPath
if ([string]::IsNullOrWhiteSpace($mdPath)) {
$mdPath = Join-Path (Join-Path $OutputPath $protocolId) "$protocolId.md"
}
$markdown = ''
if ($mdPath -and (Test-Path -LiteralPath $mdPath -PathType Leaf -ErrorAction SilentlyContinue)) {
$markdown = Get-Content -LiteralPath $mdPath -Raw -ErrorAction SilentlyContinue
}
$mdHeadings = New-Object System.Collections.Generic.List[object]
$mdAnchors = New-Object System.Collections.Generic.HashSet[string]([System.StringComparer]::OrdinalIgnoreCase)
$mdHeadingRegex = [regex]::new('(?m)^(#{1,6})\s+(.+)$')
foreach ($m in $mdHeadingRegex.Matches($markdown)) {
$level = $m.Groups[1].Value.Length
$text = $m.Groups[2].Value.Trim()
[void]$mdHeadings.Add([pscustomobject]@{ Level = $level; Text = $text })
}
foreach ($m in [regex]::Matches($markdown, '<a\s+id="([^"]+)"\s*>\s*</a>')) {
[void]$mdAnchors.Add($m.Groups[1].Value)
}
$liveHeadings = New-Object System.Collections.Generic.List[object]
$liveAnchors = New-Object System.Collections.Generic.HashSet[string]([System.StringComparer]::OrdinalIgnoreCase)
$liveUrl = $null
$fetchError = $null
$entry = $catalog[$protocolId]
if ($entry) {
$liveUrl = $entry.SpecPageUrl
try {
$response = Invoke-OpenSpecRequest -Uri $liveUrl
$html = $response.Content
$hRegex = [regex]::new('(?is)<h([1-6])(?:\s[^>]*)?\s+id="([^"]+)"[^>]*>([^<]*)</h\1>')
foreach ($m in $hRegex.Matches($html)) {
$level = [int]$m.Groups[1].Value
$id = $m.Groups[2].Value
$text = (ConvertFrom-OpenSpecHtml -Html $m.Groups[3].Value).Trim()
[void]$liveHeadings.Add([pscustomobject]@{ Level = $level; Id = $id; Text = $text })
[void]$liveAnchors.Add($id)
}
$altHRegex = [regex]::new('(?is)<h([1-6])[^>]*>([^<]*)</h\1>')
foreach ($m in $altHRegex.Matches($html)) {
$level = [int]$m.Groups[1].Value
$text = (ConvertFrom-OpenSpecHtml -Html $m.Groups[2].Value).Trim()
$lastText = if ($liveHeadings.Count -gt 0) { $liveHeadings[$liveHeadings.Count - 1].Text } else { $null }
if ($text.Length -gt 0 -and $text -ne $lastText) {
[void]$liveHeadings.Add([pscustomobject]@{ Level = $level; Id = $null; Text = $text })
}
}
}
catch {
$fetchError = $_.Exception.Message
}
}
else {
$fetchError = "Protocol not found in catalog"
}
$missingInMd = New-Object System.Collections.Generic.List[string]
$missingInLive = New-Object System.Collections.Generic.List[string]
$mdAnchorList = @($mdAnchors)
foreach ($aid in $mdAnchorList) {
if ($aid -notmatch '^_Toc\d+$' -and $liveAnchors.Count -gt 0 -and -not $liveAnchors.Contains($aid)) {
[void]$missingInLive.Add($aid)
}
}
foreach ($aid in @($liveAnchors)) {
if (-not $mdAnchors.Contains($aid)) {
[void]$missingInMd.Add($aid)
}
}
$suggestReview = $false
if ($fetchError) { $suggestReview = $true }
if ($missingInMd.Count -gt 5 -or $missingInLive.Count -gt 5) { $suggestReview = $true }
if ($liveHeadings.Count -eq 0 -and $mdHeadings.Count -gt 0) { $suggestReview = $true }
[void]$results.Add([pscustomobject]@{
PSTypeName = 'AwakeCoding.OpenSpecs.LiveHtmlCompareResult'
ProtocolId = $protocolId
MarkdownPath = $mdPath
LiveUrl = $liveUrl
FetchError = $fetchError
MarkdownHeadingCount = $mdHeadings.Count
MarkdownAnchorCount = $mdAnchors.Count
LiveHeadingCount = $liveHeadings.Count
LiveAnchorCount = $liveAnchors.Count
MissingInMarkdown = @($missingInMd)
MissingInLive = @($missingInLive)
SuggestManualReview = $suggestReview
IssueCount = $report.IssueCount
})
} catch {
Write-Warning "Compare failed for $($report.ProtocolId): $_"
[void]$results.Add([pscustomobject]@{
PSTypeName = 'AwakeCoding.OpenSpecs.LiveHtmlCompareResult'
ProtocolId = $report.ProtocolId
FetchError = $_.Exception.Message
SuggestManualReview = $true
})
}
}
$results
}