lyngweb: introduce reusable editor and highlighting utilities

This commit is contained in:
sergeych committed 2025-11-22 00:34:45 +01:00
1 parent 72c6dc2bde
commit faead76688
11 files changed
+853 -636

No files matched your search

+3 -173
View File
@@ -304,8 +304,8 @@ fun highlightSearchHits(root: HTMLElement, terms: List<String>): Int {
}
fun renderMarkdown(src: String): String =
highlightLyngHtml(
ensureBootstrapCodeBlocks(
net.sergeych.lyngweb.highlightLyngHtml(
net.sergeych.lyngweb.ensureBootstrapCodeBlocks(
ensureBootstrapTables(
ensureDefinitionLists(
marked.parse(src)
@@ -929,177 +929,7 @@ fun ensureBootstrapCodeBlocks(html: String): String {
}
}
// ---- Lyng syntax highlighting over rendered HTML ----
// This post-processor finds <pre><code class="language-lyng">…</code></pre> blocks and replaces the
// inner code HTML with token-wrapped spans using the common Lyng highlighter.
// It performs a minimal HTML entity decode on the code content to obtain the original text,
// runs the highlighter, then escapes segments back and wraps with <span class="hl-…">.
internal fun highlightLyngHtml(html: String): String {
// Regex to find <pre> ... <code class="language-lyng ...">(content)</code> ... </pre>
val preCodeRegex = Regex(
pattern = """<pre(\s+[^>]*)?>\s*<code([^>]*)>([\s\S]*?)</code>\s*</pre>""",
options = setOf(RegexOption.IGNORE_CASE)
)
val classAttrRegex = Regex("""\bclass\s*=\s*(["'])(.*?)\1""", RegexOption.IGNORE_CASE)
return preCodeRegex.replace(html) { m ->
val preAttrs = m.groups[1]?.value ?: ""
val codeAttrs = m.groups[2]?.value ?: ""
val codeHtml = m.groups[3]?.value ?: ""
val codeHasLyng = run {
val cls = classAttrRegex.find(codeAttrs)?.groupValues?.getOrNull(2) ?: ""
cls.split("\\s+".toRegex()).any { it.equals("language-lyng", ignoreCase = true) }
}
// If not explicitly Lyng, check if the <code> has any language class; if none, treat as Lyng by default
val hasAnyLanguage = run {
val cls = classAttrRegex.find(codeAttrs)?.groupValues?.getOrNull(2) ?: ""
cls.split("\\s+".toRegex()).any { it.startsWith("language-", ignoreCase = true) }
}
val treatAsLyng = codeHasLyng || !hasAnyLanguage
if (!treatAsLyng) return@replace m.value // leave untouched for non-Lyng languages
val text = htmlUnescape(codeHtml)
// If block has no explicit language (unfenced/indented), support doctest tail (trailing lines starting with ">>>")
val (headText, tailTextOrNull) = if (!codeHasLyng && !hasAnyLanguage) splitDoctestTail(text) else text to null
val headHighlighted = try {
applyLyngHighlightToText(headText)
} catch (_: Throwable) {
return@replace m.value
}
val tailHighlighted = tailTextOrNull?.let { renderDoctestTailAsComments(it) } ?: ""
val highlighted = headHighlighted + tailHighlighted
// Preserve original attrs; ensure <pre> has existing attrs (Bootstrap '.code' was handled earlier)
"<pre$preAttrs><code$codeAttrs>$highlighted</code></pre>"
}
}
// Split trailing doctest tail: consecutive lines at the end whose trimmedStart starts with ">>>".
// Returns Pair(head, tail) where tail is null if no doctest lines found.
private fun splitDoctestTail(text: String): Pair<String, String?> {
if (text.isEmpty()) return "" to null
// Normalize to \n for splitting; remember if original ended with newline
val hasTrailingNewline = text.endsWith("\n")
val lines = text.split("\n")
var i = lines.size - 1
// Skip trailing completely empty lines before looking for doctest markers
while (i >= 0 && lines[i].isEmpty()) i--
var count = 0
while (i >= 0) {
val line = lines[i]
// If last line is empty due to trailing newline, include it into tail only if there are already doctest lines
val trimmed = line.trimStart()
if (trimmed.isNotEmpty() && !trimmed.startsWith(">>>")) break
// Accept empty line only if it follows some doctest lines (keeps spacing), else stop
if (trimmed.isEmpty()) {
if (count == 0) break else { count++; i--; continue }
}
// doctest line
count++
i--
}
if (count == 0) return text to null
val splitIndex = lines.size - count
val head = buildString {
for (idx in 0 until splitIndex) {
append(lines[idx])
if (idx < lines.size - 1 || hasTrailingNewline) append('\n')
}
}
val tail = buildString {
for (idx in splitIndex until lines.size) {
append(lines[idx])
if (idx < lines.size - 1 || hasTrailingNewline) append('\n')
}
}
return head to tail
}
// Render the doctest tail as comment-highlighted lines. Expects the original textual tail including newlines.
private fun renderDoctestTailAsComments(tail: String): String {
if (tail.isEmpty()) return ""
val sb = StringBuilder(tail.length + 32)
var start = 0
while (start <= tail.lastIndex) {
val nl = tail.indexOf('\n', start)
val line = if (nl >= 0) tail.substring(start, nl) else tail.substring(start)
// Wrap the whole line in comment styling
sb.append("<span class=\"hl-cmt\">")
sb.append(htmlEscape(line))
sb.append("</span>")
if (nl >= 0) sb.append('\n')
if (nl < 0) break else start = nl + 1
}
return sb.toString()
}
// Apply Lyng highlighter to raw code text, producing HTML with span classes.
internal fun applyLyngHighlightToText(text: String): String {
val highlighter = net.sergeych.lyng.highlight.SimpleLyngHighlighter()
// Use spans as produced by the fixed lynglib highlighter (comments already extend to EOL there)
val spans = highlighter.highlight(text)
if (spans.isEmpty()) return htmlEscape(text)
val sb = StringBuilder(text.length + spans.size * 16)
var pos = 0
for (s in spans) {
if (s.range.start > pos) {
sb.append(htmlEscape(text.substring(pos, s.range.start)))
}
val cls = cssClassForKind(s.kind)
sb.append('<').append("span class=\"").append(cls).append('\"').append('>')
sb.append(htmlEscape(text.substring(s.range.start, s.range.endExclusive)))
sb.append("</span>")
pos = s.range.endExclusive
}
if (pos < text.length) sb.append(htmlEscape(text.substring(pos)))
return sb.toString()
}
// Note: No site-side span post-processing — we rely on lynglib's SimpleLyngHighlighter for correctness.
private fun cssClassForKind(kind: net.sergeych.lyng.highlight.HighlightKind): String = when (kind) {
net.sergeych.lyng.highlight.HighlightKind.Keyword -> "hl-kw"
net.sergeych.lyng.highlight.HighlightKind.TypeName -> "hl-ty"
net.sergeych.lyng.highlight.HighlightKind.Identifier -> "hl-id"
net.sergeych.lyng.highlight.HighlightKind.Number -> "hl-num"
net.sergeych.lyng.highlight.HighlightKind.String -> "hl-str"
net.sergeych.lyng.highlight.HighlightKind.Char -> "hl-ch"
net.sergeych.lyng.highlight.HighlightKind.Regex -> "hl-rx"
net.sergeych.lyng.highlight.HighlightKind.Comment -> "hl-cmt"
net.sergeych.lyng.highlight.HighlightKind.Operator -> "hl-op"
net.sergeych.lyng.highlight.HighlightKind.Punctuation -> "hl-punc"
net.sergeych.lyng.highlight.HighlightKind.Label -> "hl-lbl"
net.sergeych.lyng.highlight.HighlightKind.Directive -> "hl-dir"
net.sergeych.lyng.highlight.HighlightKind.Error -> "hl-err"
}
// Minimal HTML escaping for text nodes
fun htmlEscape(s: String): String = buildString(s.length) {
for (ch in s) when (ch) {
'<' -> append("&lt;")
'>' -> append("&gt;")
'&' -> append("&amp;")
'"' -> append("&quot;")
'\'' -> append("&#39;")
else -> append(ch)
}
}
// Minimal unescape for code inner HTML produced by marked
private fun htmlUnescape(s: String): String {
// handle common entities only
return s
.replace("&lt;", "<")
.replace("&gt;", ">")
.replace("&amp;", "&")
.replace("&quot;", "\"")
.replace("&#39;", "'")
}
// moved highlighting utilities to :lyngweb
fun rewriteImages(root: HTMLElement, basePath: String) {
val imgs = root.querySelectorAll("img")