25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

161 satır
7.5KB

  1. <%
  2. '-----------------------------------------------------------------------------------------------------------------------
  3. ' HTML source adapter (spec section 11, FR-021): converts an HTML page to Markdown text before ingestion, so the
  4. ' Markdown chunker can use its headings for heading paths and locators. The page is treated as untrusted data: active
  5. ' and invisible content (script, style, template, svg, iframe, object, head, comments) and navigation (nav) are dropped, every remaining tag
  6. ' is removed, and nothing is fetched (links keep their text only; images keep their alt text).
  7. '
  8. ' Html_ToMarkdown(html) -> Markdown text ("" when the page has no text)
  9. ' Html_Title(html) -> text of <title>, or ""
  10. '
  11. ' Kept structure: h1-h6 as # headings, paragraphs and block elements as blank-line separated blocks, li as "- " items,
  12. ' br as line breaks, pre as fenced code (whitespace kept), table cells separated by " | ". Entities are decoded
  13. ' (named common ones and numeric); code points above U+FFFF become U+FFFD, as everywhere in this runtime.
  14. ' Works on the whole text with RegExp/Replace: builtins cost time proportional to the string length (AGENTS.md).
  15. '-----------------------------------------------------------------------------------------------------------------------
  16. Const HTML_PRE_MARK = "@@amkpre"
  17. Function Html_Re(pattern)
  18. Dim re
  19. Set re = New RegExp
  20. re.Pattern = pattern
  21. re.Global = True
  22. re.IgnoreCase = True
  23. Set Html_Re = re
  24. End Function
  25. Function Html_Title(html)
  26. Dim m
  27. Html_Title = ""
  28. For Each m In Html_Re("<title\b[^>]*>([\s\S]*?)</title\s*>").Execute(html)
  29. Html_Title = Trim(Html_Re("\s+").Replace(Html_Decode(Html_Re("<[^>]*>").Replace(m.SubMatches(0), "")), " "))
  30. Exit For
  31. Next
  32. End Function
  33. Function Html_ToMarkdown(html)
  34. Dim s, tag, m, pres(), nPre, i, inner, lvl, txt, blocks
  35. s = Replace(Replace(html, vbCr & vbLf, vbLf), vbCr, vbLf)
  36. ' 1. Drop comments and content that is not visible text.
  37. s = Html_Re("<!--[\s\S]*?-->").Replace(s, " ")
  38. s = Html_Re("<!\[CDATA\[[\s\S]*?\]\]>").Replace(s, " ")
  39. For Each tag In Array("script", "style", "noscript", "template", "svg", "iframe", "object", "head", "select", "canvas", "nav")
  40. s = Html_Re("<" & tag & "\b[^>]*>[\s\S]*?</" & tag & "\s*>").Replace(s, " ")
  41. s = Html_Re("<" & tag & "\b[^>]*/>").Replace(s, " ")
  42. Next
  43. s = Html_Re("<!doctype[^>]*>").Replace(s, "")
  44. ' 2. Keep <pre> blocks verbatim (as fenced code) behind placeholders while whitespace is collapsed.
  45. nPre = 0
  46. ReDim pres(0)
  47. For Each m In Html_Re("<pre\b[^>]*>([\s\S]*?)</pre\s*>").Execute(s)
  48. ReDim Preserve pres(nPre)
  49. inner = Html_Decode(Html_Re("<[^>]*>").Replace(m.SubMatches(0), ""))
  50. If Left(inner, 1) = vbLf Then
  51. inner = Mid(inner, 2)
  52. End If
  53. pres(nPre) = vbLf & vbLf & "```" & vbLf & Html_TrimEnd(inner) & vbLf & "```" & vbLf & vbLf
  54. s = Replace(s, m.Value, " " & HTML_PRE_MARK & nPre & "@@ ", 1, 1)
  55. nPre = nPre + 1
  56. Next
  57. ' 3. Source whitespace carries no structure in HTML.
  58. s = Html_Re("[ \t\n\f\v]+").Replace(s, " ")
  59. ' 4. Headings on their own lines, inner markup removed.
  60. For Each m In Html_Re("<h([1-6])\b[^>]*>([\s\S]*?)</h[1-6]\s*>").Execute(s)
  61. lvl = CLng(m.SubMatches(0))
  62. txt = Trim(Html_Re(" +").Replace(Html_Re("<[^>]*>").Replace(m.SubMatches(1), " "), " "))
  63. If Len(txt) > 0 Then
  64. s = Replace(s, m.Value, vbLf & vbLf & String(lvl, "#") & " " & txt & vbLf & vbLf, 1, 1)
  65. Else
  66. s = Replace(s, m.Value, " ", 1, 1)
  67. End If
  68. Next
  69. ' 5. Images keep their alt text; line breaks, list items, cells and blocks become line structure.
  70. s = Html_Re("<img\b[^>]*\balt\s*=\s*""([^""]*)""[^>]*>").Replace(s, " $1 ")
  71. s = Html_Re("<img\b[^>]*\balt\s*=\s*'([^']*)'[^>]*>").Replace(s, " $1 ")
  72. s = Html_Re("<br\b[^>]*>").Replace(s, vbLf)
  73. s = Html_Re("<li\b[^>]*>").Replace(s, vbLf & "- ")
  74. s = Html_Re("</(td|th)\s*>").Replace(s, " | ")
  75. s = Html_Re("<hr\b[^>]*>").Replace(s, vbLf & vbLf & "---" & vbLf & vbLf)
  76. 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"
  77. s = Html_Re("</?(" & blocks & ")\b[^>]*>").Replace(s, vbLf & vbLf)
  78. ' 6. Remove every remaining tag, then decode entities (after tag removal, so "&lt;b&gt;" stays text).
  79. s = Html_Re("<[^>]*>").Replace(s, "")
  80. s = Html_Decode(s)
  81. ' 7. Tidy lines: trim, drop empty table separators, at most one blank line between blocks.
  82. s = Html_Re(" *\n *").Replace(s, vbLf)
  83. s = Html_Re(" {2,}").Replace(s, " ")
  84. s = Html_Re("\n(\| )+\n").Replace(s, vbLf)
  85. s = Html_Re(" \|\s*\n").Replace(s, vbLf)
  86. s = Html_Re("\n- *\n").Replace(s, vbLf)
  87. s = Html_Re("\n{3,}").Replace(s, vbLf & vbLf)
  88. ' 8. Restore <pre> blocks.
  89. For i = 0 To nPre - 1
  90. s = Replace(s, HTML_PRE_MARK & i & "@@", pres(i))
  91. Next
  92. s = Html_Re("\n{3,}").Replace(s, vbLf & vbLf)
  93. Html_ToMarkdown = Trim(Html_Re("^\s+|\s+$").Replace(s, ""))
  94. End Function
  95. Function Html_TrimEnd(s)
  96. Html_TrimEnd = Html_Re("\s+$").Replace(s, "")
  97. End Function
  98. ' Decodes numeric and common named character references. Unknown names are left as they are.
  99. Function Html_Decode(text)
  100. Dim s, m, code, ch, names, i, hx
  101. s = text
  102. If InStr(s, "&") = 0 Then
  103. Html_Decode = s
  104. Exit Function
  105. End If
  106. For Each m In Html_Re("&#(x[0-9a-f]{1,6}|[0-9]{1,7});").Execute(s)
  107. If LCase(Left(m.SubMatches(0), 1)) = "x" Then
  108. hx = Mid(m.SubMatches(0), 2)
  109. Do While Len(hx) > 1 And Left(hx, 1) = "0"
  110. hx = Mid(hx, 2)
  111. Loop
  112. If Len(hx) > 4 Then
  113. code = 65536 ' outside the Basic Multilingual Plane
  114. Else
  115. code = CLng("&H" & hx)
  116. If code < 0 Then
  117. code = code + 65536 ' CLng("&Hxxxx") is signed (AGENTS.md)
  118. End If
  119. End If
  120. Else
  121. code = CDbl(m.SubMatches(0))
  122. End If
  123. If code < 32 And code <> 9 And code <> 10 Then
  124. ch = " "
  125. ElseIf code > 65535 Or (code >= 55296 And code <= 57343) Then
  126. ch = ChrW(65533)
  127. Else
  128. ch = ChrW(CLng(code))
  129. End If
  130. s = Replace(s, m.Value, ch)
  131. Next
  132. names = Array("nbsp", " ", "lt", "<", "gt", ">", "quot", """", "apos", "'", "copy", ChrW(169), "reg", ChrW(174), _
  133. "trade", ChrW(8482), "hellip", ChrW(8230), "mdash", ChrW(8212), "ndash", ChrW(8211), "lsquo", ChrW(8216), _
  134. "rsquo", ChrW(8217), "ldquo", ChrW(8220), "rdquo", ChrW(8221), "bull", ChrW(8226), "middot", ChrW(183), _
  135. "euro", ChrW(8364), "pound", ChrW(163), "yen", ChrW(165), "cent", ChrW(162), "sect", ChrW(167), "deg", ChrW(176), _
  136. "plusmn", ChrW(177), "times", ChrW(215), "divide", ChrW(247), "laquo", ChrW(171), "raquo", ChrW(187), _
  137. "auml", ChrW(228), "ouml", ChrW(246), "uuml", ChrW(252), "Auml", ChrW(196), "Ouml", ChrW(214), "Uuml", ChrW(220), _
  138. "szlig", ChrW(223), "eacute", ChrW(233), "egrave", ChrW(232), "aacute", ChrW(225), "agrave", ChrW(224), _
  139. "ccedil", ChrW(231), "ntilde", ChrW(241), "rarr", ChrW(8594), "larr", ChrW(8592), "shy", "")
  140. For i = 0 To UBound(names) - 1 Step 2
  141. ' Case matters for named references (&Auml; vs &auml;), so Replace with binary compare.
  142. s = Replace(s, "&" & names(i) & ";", names(i + 1), 1, -1, vbBinaryCompare)
  143. Next
  144. s = Replace(s, "&amp;", "&", 1, -1, vbBinaryCompare)
  145. Html_Decode = s
  146. End Function
  147. %>

Powered by TurnKey Linux.