Skip to content

Commit

Permalink
feat: properly raise error on missing toc end
Browse files Browse the repository at this point in the history
If there is a TOC start but no TOC end, bitdowntoc now raises a proper
exception instead of outputting an empty document.
  • Loading branch information
derlin committed Jun 23, 2024
1 parent f624c62 commit 5456ea5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/commonMain/kotlin/ch/derlin/bitdowntoc/BitGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ object BitGenerator {
this.map { it.trim() }.any { it == tocMarker || commenter.isTocStart(it) }

private fun MutableListIterator<String>.consumeToc(commenter: Commenter) {
do {
this.remove() // remove toc start
while (this.hasNext()) {
if (commenter.isTocEnd(this.next())) return
this.remove()
} while (this.hasNext() && !commenter.isTocEnd(this.next()))
}
throw MissingTocEndException()
}

private fun String.isCodeStart(codeMarker: String) =
Expand Down
5 changes: 5 additions & 0 deletions src/commonMain/kotlin/ch/derlin/bitdowntoc/exceptions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ch.derlin.bitdowntoc

open class BitException(override val message: String) : RuntimeException(message)

class MissingTocEndException : BitException("The document has a TOC start, but is missing a TOC end.")
21 changes: 21 additions & 0 deletions src/commonTest/kotlin/ch.derlin.bitdowntoc/GenerateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ch.derlin.bitdowntoc.BitGenerator.Params
import ch.derlin.bitdowntoc.CommentStyle.LIQUID
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class GenerateTest {

Expand Down Expand Up @@ -484,6 +485,26 @@ class GenerateTest {
)
}

@Test
fun testMissingTocEnd() {
val input = """
<!-- TOC start (generated with $BITDOWNTOC_URL) -->
- [h1](#h1)
# h1
Use [TOC] to control the toc:
```
[TOC]
```
""".trimIndent()

assertFailsWith<MissingTocEndException>(message = "The document has a TOC start, but is missing a TOC end") {
BitGenerator.generate(input, Params(generateAnchors = false))

}
}


private fun assertDoesNotChangeToc(body: String) {
val input = "## heading 1\n${body}\n## heading 2"
Expand Down
7 changes: 6 additions & 1 deletion src/jsMain/kotlin/toc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,13 @@ fun loadOptions() {
console.log("options saved")
}

fun generate(text: String) =
fun generate(text: String): String = try {
BitGenerator.generate(text, getParams())
} catch (e: BitException) {
"\nError!\n${e.message}"
}.also {
console.log("returned $it")
}

fun getParams() = BitGenerator.Params(
indentChars = BitOptions.indentChars.getValue(),
Expand Down
9 changes: 7 additions & 2 deletions src/jvmMain/kotlin/ch/derlin/bitdowntoc/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ class Cli(
profile?.applyToParams(it) ?: it
}

BitGenerator.generate(inputText, params).let {
(output?.writeText(it)) ?: echo(it)
try {

BitGenerator.generate(inputText, params).let {
(output?.writeText(it)) ?: echo(it)
}
} catch (e: BitException) {
throw CliktError("[Error] ${e.message}")
}
}

Expand Down

0 comments on commit 5456ea5

Please sign in to comment.