|
- <%
- '-----------------------------------------------------------------------------------------------------------------------
- ' 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 <title>, or ""
- '
- ' Kept structure: h1-h6 as # headings, paragraphs and block elements as blank-line separated blocks, li as "- " items,
- ' br as line breaks, pre as fenced code (whitespace kept), table cells separated by " | ". Entities are decoded
- ' (named common ones and numeric); code points above U+FFFF become U+FFFD, as everywhere in this runtime.
- ' Works on the whole text with RegExp/Replace: builtins cost time proportional to the string length (AGENTS.md).
- '-----------------------------------------------------------------------------------------------------------------------
- Const HTML_PRE_MARK = "@@amkpre"
-
- Function Html_Re(pattern)
- Dim re
- Set re = New RegExp
- re.Pattern = pattern
- re.Global = True
- re.IgnoreCase = True
- Set Html_Re = re
- End Function
-
- Function Html_Title(html)
- Dim m
- Html_Title = ""
- For Each m In Html_Re("<title\b[^>]*>([\s\S]*?)</title\s*>").Execute(html)
- Html_Title = Trim(Html_Re("\s+").Replace(Html_Decode(Html_Re("<[^>]*>").Replace(m.SubMatches(0), "")), " "))
- Exit For
- Next
- End Function
-
- Function Html_ToMarkdown(html)
- Dim s, tag, m, pres(), nPre, i, inner, lvl, txt, blocks
- s = Replace(Replace(html, vbCr & vbLf, vbLf), vbCr, vbLf)
-
- ' 1. Drop comments and content that is not visible text.
- s = Html_Re("<!--[\s\S]*?-->").Replace(s, " ")
- s = Html_Re("<!\[CDATA\[[\s\S]*?\]\]>").Replace(s, " ")
- For Each tag In Array("script", "style", "noscript", "template", "svg", "iframe", "object", "head", "select", "canvas", "nav")
- s = Html_Re("<" & tag & "\b[^>]*>[\s\S]*?</" & tag & "\s*>").Replace(s, " ")
- s = Html_Re("<" & tag & "\b[^>]*/>").Replace(s, " ")
- Next
- s = Html_Re("<!doctype[^>]*>").Replace(s, "")
-
- ' 2. Keep <pre> blocks verbatim (as fenced code) behind placeholders while whitespace is collapsed.
- nPre = 0
- ReDim pres(0)
- For Each m In Html_Re("<pre\b[^>]*>([\s\S]*?)</pre\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("<h([1-6])\b[^>]*>([\s\S]*?)</h[1-6]\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("<img\b[^>]*\balt\s*=\s*""([^""]*)""[^>]*>").Replace(s, " $1 ")
- s = Html_Re("<img\b[^>]*\balt\s*=\s*'([^']*)'[^>]*>").Replace(s, " $1 ")
- s = Html_Re("<br\b[^>]*>").Replace(s, vbLf)
- s = Html_Re("<li\b[^>]*>").Replace(s, vbLf & "- ")
- s = Html_Re("</(td|th)\s*>").Replace(s, " | ")
- s = Html_Re("<hr\b[^>]*>").Replace(s, vbLf & vbLf & "---" & vbLf & vbLf)
- blocks = "p|div|section|article|header|footer|main|aside|ul|ol|dl|dt|dd|table|thead|tbody|tfoot|tr|caption|blockquote|figure|figcaption|address|form|fieldset|details|summary"
- s = Html_Re("</?(" & blocks & ")\b[^>]*>").Replace(s, vbLf & vbLf)
-
- ' 6. Remove every remaining tag, then decode entities (after tag removal, so "<b>" stays text).
- s = Html_Re("<[^>]*>").Replace(s, "")
- s = Html_Decode(s)
-
- ' 7. Tidy lines: trim, drop empty table separators, at most one blank line between blocks.
- s = Html_Re(" *\n *").Replace(s, vbLf)
- s = Html_Re(" {2,}").Replace(s, " ")
- s = Html_Re("\n(\| )+\n").Replace(s, vbLf)
- s = Html_Re(" \|\s*\n").Replace(s, vbLf)
- s = Html_Re("\n- *\n").Replace(s, vbLf)
- s = Html_Re("\n{3,}").Replace(s, vbLf & vbLf)
-
- ' 8. Restore <pre> 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
- %>
|