Separate inline code and block code tags
authorLanius Trolling <lanius@laniustrolling.dev>
Fri, 1 Mar 2024 20:34:46 +0000 (15:34 -0500)
committerLanius Trolling <lanius@laniustrolling.dev>
Fri, 1 Mar 2024 20:34:46 +0000 (15:34 -0500)
src/jvmMain/kotlin/info/mechyrdia/lore/parser.kt
src/jvmMain/kotlin/info/mechyrdia/lore/parser_tags.kt

index 17230a08a6e7b70f9d7e53122e683d312aa37275..d3379c1c2c3daa8c3fa02cf12aeabf73c80d7076 100644 (file)
@@ -60,13 +60,18 @@ sealed class TextParserState<TContext>(
                appendTextRaw(newline)
        }
        
+       protected fun lastParagraph() {
+               if (!internalState.suppressEndParagraph)
+                       appendTextRaw("</p>")
+       }
+       
        protected fun cancelEndParagraph() {
                internalState.suppressEndParagraph = true
        }
        
        protected fun cancelStartParagraph() {
                if (scope.write.endsWith("<p>"))
-                       scope.write.deleteRange(scope.write.length - 3, scope.write.length)
+                       scope.write.deleteRange(scope.write.length - "<p>".length, scope.write.length)
        }
        
        class Initial<TContext>(scope: TextParserScope<TContext>) : TextParserState<TContext>(scope, listOf(), TextParserInternalState()) {
@@ -102,7 +107,7 @@ sealed class TextParserState<TContext>(
                override fun processEndOfText() {
                        appendText(text.removeSuffix("\n"))
                        if (text.isNotBlank())
-                               appendTextRaw("</p>")
+                               lastParagraph()
                }
        }
        
@@ -123,7 +128,7 @@ sealed class TextParserState<TContext>(
                override fun processEndOfText() {
                        appendText(text.removeSuffix("\n"))
                        if (text.isNotBlank())
-                               appendTextRaw("</p>")
+                               lastParagraph()
                }
        }
        
@@ -158,7 +163,7 @@ sealed class TextParserState<TContext>(
                
                override fun processEndOfText() {
                        appendText("[$tag")
-                       appendTextRaw("</p>")
+                       lastParagraph()
                }
        }
        
@@ -185,7 +190,7 @@ sealed class TextParserState<TContext>(
                
                override fun processEndOfText() {
                        appendText("[$tag=$param")
-                       appendTextRaw("</p>")
+                       lastParagraph()
                }
        }
        
@@ -213,7 +218,7 @@ sealed class TextParserState<TContext>(
                
                override fun processEndOfText() {
                        appendText("[/$tag")
-                       appendTextRaw("</p>")
+                       lastParagraph()
                }
        }
        
index a978416b128c910e26754c6ad2e4179da57d8080..22806f0c4a7c8af0b8a19f04e70351c9aa0b1916 100644 (file)
@@ -112,8 +112,15 @@ enum class TextParserFormattingTag(val type: TextParserTagType<Unit>) {
        CODE(
                TextParserTagType.Direct(
                        false,
-                       { _, _ -> "<span style='font-family: JetBrains Mono'><pre>" },
-                       { _ -> "</pre></span>" },
+                       { _, _ -> "<span style='font-family: JetBrains Mono'>" },
+                       { _ -> "</span>" },
+               )
+       ),
+       CODE_BLOCK(
+               TextParserTagType.Direct(
+                       true,
+                       { _, _ -> "<div style='font-family: JetBrains Mono'><pre>" },
+                       { _ -> "</pre></div>" },
                )
        ),
        H1(