fixed highlighting and styles

This commit is contained in:
sergeych committed 2025-11-19 17:52:48 +01:00
1 parent 646a676b3e
commit 918534afb5
18 files changed
+1087 -70

No files matched your search

@@ -0,0 +1,38 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class CommentSpanExtendTest {
@Test
fun singleLineCommentCoversTillEol() {
val line = "// see the difference: apply changes this to newly created Point:"
val md = """
```lyng
$line
```
""".trimIndent()
val html = renderMarkdown(md)
// Entire line must be inside the comment span
assertTrue(html.contains("<span class=\"hl-cmt\">$line</span>"), "Comment should extend to EOL: $html")
// There must be no stray tail like </span>nt: (regression case)
assertFalse(html.contains("</span>nt:"), "No trailing tail should remain outside comment span: $html")
}
}
@@ -0,0 +1,60 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.site
import net.sergeych.lyng.highlight.HighlightKind
import net.sergeych.lyng.highlight.HighlightSpan
import net.sergeych.lyng.highlight.SimpleLyngHighlighter
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertTrue
class HighlightSmokeTest {
private fun spansToLabeled(text: String, spans: List<HighlightSpan>): List<Pair<String, HighlightKind>> =
spans.map { text.substring(it.range.start, it.range.endExclusive) to it.kind }
@Test
fun highlightAssertEqualsSnippet() {
val text = "assertEquals( [9,10], r.takeLast(2).toList() )"
val spans = SimpleLyngHighlighter().highlight(text)
val labeled = spansToLabeled(text, spans)
// Basic sanity: identifier assertEquals present and not split
assertTrue(labeled.any { it.first == "assertEquals" && it.second == HighlightKind.Identifier })
// Basic numbers detection
assertTrue(labeled.any { it.first == "9" && it.second == HighlightKind.Number })
assertTrue(labeled.any { it.first == "10" && it.second == HighlightKind.Number })
assertTrue(labeled.any { it.first == "2" && it.second == HighlightKind.Number })
}
@Test
fun renderHtmlContainsCorrectClasses() {
val text = "assertEquals( [9,10], r.takeLast(2).toList() )"
val html = SiteHighlight.renderHtml(text)
// Ensure important parts are wrapped with expected classes
assertContains(html, "<span class=\"hl-id\">assertEquals</span>")
assertContains(html, "<span class=\"hl-num\">9</span>")
assertContains(html, "<span class=\"hl-num\">10</span>")
assertContains(html, "<span class=\"hl-num\">2</span>")
// Punctuation and operators appear; allow either combined or separate, just ensure class exists
assertTrue(html.contains("hl-punc"))
assertTrue(html.contains("hl-op"))
}
}
@@ -0,0 +1,63 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class LyngHighlightTest {
@Test
fun highlightsLyngFencedBlock() {
val md = """
```lyng
and or not constructor property a+b
```
""".trimIndent()
val html = renderMarkdown(md)
// Should produce span classes for keywords and operator
assertTrue(html.contains("hl-kw"), "Expected keyword class in highlighted Lyng block: $html")
assertTrue(html.contains("hl-op"), "Expected operator class in highlighted Lyng block: $html")
// Ensure code is inside <pre><code ... language-lyng>
assertTrue(html.contains("language-lyng", ignoreCase = true), "Expected language-lyng class retained: $html")
}
@Test
fun nonLyngBlocksAreUntouched() {
val md = """
```kotlin
println("Hi")
```
""".trimIndent()
val html = renderMarkdown(md)
// Should not include our Lyng-specific classes
assertFalse(html.contains("hl-kw"), "Non-Lyng block should not be Lyng-highlighted: $html")
assertTrue(html.contains("<pre"), "Expected <pre> present: $html")
}
@Test
fun escapesAngleBracketsInsideSpans() {
val md = """
```lyng
a<b
```
""".trimIndent()
val html = renderMarkdown(md)
// the '<' should be escaped in HTML
assertTrue(html.contains("&lt;"), "Expected escaped < inside highlighted HTML: $html")
}
}
@@ -0,0 +1,61 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class UnfencedLyngTest {
@Test
fun indentedCodeBlocksAreLyngByDefault() {
val md = """
Some text paragraph.
and or not a+b
Next paragraph.
""".trimIndent()
val html = renderMarkdown(md)
// Should contain a code block and Lyng highlight classes
assertTrue(html.contains("<pre", ignoreCase = true), "Expected <pre> in rendered HTML: $html")
assertTrue(html.contains("<code", ignoreCase = true), "Expected <code> in rendered HTML: $html")
assertTrue(html.contains("hl-kw"), "Expected keyword highlight in indented code block: $html")
assertTrue(html.contains("hl-op"), "Expected operator highlight in indented code block: $html")
// Should not introduce a non-Lyng language class; language-lyng may or may not be present, but other languages shouldn't
assertFalse(html.contains("language-kotlin", ignoreCase = true), "Should not mark indented block as another language: $html")
}
@Test
fun doctestTailLinesRenderedAsComments() {
val md = """
Intro paragraph.
a + b
>>> 3
>>> ok
""".trimIndent()
val html = renderMarkdown(md)
// Lyng highlight should appear for the code line (the '+')
assertTrue(html.contains("hl-op"), "Expected operator highlighting for head part: $html")
// The doctest tail lines should be wrapped as comments
assertTrue(html.contains("hl-cmt"), "Expected comment highlighting for doctest tail: $html")
// Ensure the markers are inside the comment span content
assertTrue(html.contains("<span class=\"hl-cmt\">&gt;&gt;&gt;"), "Doctest lines should start with >>> inside comment span: $html")
}
}