% '----------------------------------------------------------------------------------------------------------------------- ' HTML source adapter (spec section 11, FR-021): converts an HTML page to Markdown text before ingestion, so the ' Markdown chunker can use its headings for heading paths and locators. The page is treated as untrusted data: active ' and invisible content (script, style, template, svg, iframe, object, head, comments) and navigation (nav) are dropped, every remaining tag ' is removed, and nothing is fetched (links keep their text only; images keep their alt text). ' ' Html_ToMarkdown(html) -> Markdown text ("" when the page has no text) ' Html_Title(html) -> text of
blocks verbatim (as fenced code) behind placeholders while whitespace is collapsed.
nPre = 0
ReDim pres(0)
For Each m In Html_Re("]*>([\s\S]*?)
").Execute(s)
ReDim Preserve pres(nPre)
inner = Html_Decode(Html_Re("<[^>]*>").Replace(m.SubMatches(0), ""))
If Left(inner, 1) = vbLf Then
inner = Mid(inner, 2)
End If
pres(nPre) = vbLf & vbLf & "```" & vbLf & Html_TrimEnd(inner) & vbLf & "```" & vbLf & vbLf
s = Replace(s, m.Value, " " & HTML_PRE_MARK & nPre & "@@ ", 1, 1)
nPre = nPre + 1
Next
' 3. Source whitespace carries no structure in HTML.
s = Html_Re("[ \t\n\f\v]+").Replace(s, " ")
' 4. Headings on their own lines, inner markup removed.
For Each m In Html_Re("]*>([\s\S]*?) ").Execute(s)
lvl = CLng(m.SubMatches(0))
txt = Trim(Html_Re(" +").Replace(Html_Re("<[^>]*>").Replace(m.SubMatches(1), " "), " "))
If Len(txt) > 0 Then
s = Replace(s, m.Value, vbLf & vbLf & String(lvl, "#") & " " & txt & vbLf & vbLf, 1, 1)
Else
s = Replace(s, m.Value, " ", 1, 1)
End If
Next
' 5. Images keep their alt text; line breaks, list items, cells and blocks become line structure.
s = Html_Re("
]*\balt\s*=\s*""([^""]*)""[^>]*>").Replace(s, " $1 ")
s = Html_Re("
]*\balt\s*=\s*'([^']*)'[^>]*>").Replace(s, " $1 ")
s = Html_Re("
]*>").Replace(s, vbLf)
s = Html_Re(" blocks.
For i = 0 To nPre - 1
s = Replace(s, HTML_PRE_MARK & i & "@@", pres(i))
Next
s = Html_Re("\n{3,}").Replace(s, vbLf & vbLf)
Html_ToMarkdown = Trim(Html_Re("^\s+|\s+$").Replace(s, ""))
End Function
Function Html_TrimEnd(s)
Html_TrimEnd = Html_Re("\s+$").Replace(s, "")
End Function
' Decodes numeric and common named character references. Unknown names are left as they are.
Function Html_Decode(text)
Dim s, m, code, ch, names, i, hx
s = text
If InStr(s, "&") = 0 Then
Html_Decode = s
Exit Function
End If
For Each m In Html_Re("(x[0-9a-f]{1,6}|[0-9]{1,7});").Execute(s)
If LCase(Left(m.SubMatches(0), 1)) = "x" Then
hx = Mid(m.SubMatches(0), 2)
Do While Len(hx) > 1 And Left(hx, 1) = "0"
hx = Mid(hx, 2)
Loop
If Len(hx) > 4 Then
code = 65536 ' outside the Basic Multilingual Plane
Else
code = CLng("&H" & hx)
If code < 0 Then
code = code + 65536 ' CLng("&Hxxxx") is signed (AGENTS.md)
End If
End If
Else
code = CDbl(m.SubMatches(0))
End If
If code < 32 And code <> 9 And code <> 10 Then
ch = " "
ElseIf code > 65535 Or (code >= 55296 And code <= 57343) Then
ch = ChrW(65533)
Else
ch = ChrW(CLng(code))
End If
s = Replace(s, m.Value, ch)
Next
names = Array("nbsp", " ", "lt", "<", "gt", ">", "quot", """", "apos", "'", "copy", ChrW(169), "reg", ChrW(174), _
"trade", ChrW(8482), "hellip", ChrW(8230), "mdash", ChrW(8212), "ndash", ChrW(8211), "lsquo", ChrW(8216), _
"rsquo", ChrW(8217), "ldquo", ChrW(8220), "rdquo", ChrW(8221), "bull", ChrW(8226), "middot", ChrW(183), _
"euro", ChrW(8364), "pound", ChrW(163), "yen", ChrW(165), "cent", ChrW(162), "sect", ChrW(167), "deg", ChrW(176), _
"plusmn", ChrW(177), "times", ChrW(215), "divide", ChrW(247), "laquo", ChrW(171), "raquo", ChrW(187), _
"auml", ChrW(228), "ouml", ChrW(246), "uuml", ChrW(252), "Auml", ChrW(196), "Ouml", ChrW(214), "Uuml", ChrW(220), _
"szlig", ChrW(223), "eacute", ChrW(233), "egrave", ChrW(232), "aacute", ChrW(225), "agrave", ChrW(224), _
"ccedil", ChrW(231), "ntilde", ChrW(241), "rarr", ChrW(8594), "larr", ChrW(8592), "shy", "")
For i = 0 To UBound(names) - 1 Step 2
' Case matters for named references (Ä vs ä), so Replace with binary compare.
s = Replace(s, "&" & names(i) & ";", names(i + 1), 1, -1, vbBinaryCompare)
Next
s = Replace(s, "&", "&", 1, -1, vbBinaryCompare)
Html_Decode = s
End Function
%>