All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.docs.examples.kt Maven / Gradle / Ivy

There is a newer version: 0.77.0
Show newest version
package de.peekandpoke.ultra.common.docs

interface Example {
    /** The title of the example */
    val title: String

    /** The description of the example that is shown after the title */
    val description: String

    /** Additional information that is shown after the example code and output */
    val additionalInfo: String get() = ""

    /**
     * Runs the example code
     */
    fun run()

    /**
     * Runs the example code while trapping all console output
     */
    fun runAndRecordOutput(): String
}

abstract class SimpleExample : Example {

    private var builder: StringBuilder? = null

    override fun runAndRecordOutput(): String {
        builder = StringBuilder()

        run()

        return builder.toString()
    }

    fun markdownKotlinCode(code: String) = """
```kotlin
$code
```
    """

    fun print(message: Any) {
        when (val b = builder) {
            null -> kotlin.io.print(message)
            else -> b.append(message)
        }
    }

    fun println() {
        println("")
    }

    fun println(message: Any) {
        when (val b = builder) {
            null -> kotlin.io.println(message)
            else -> b.appendLine(message)
        }
    }
}

interface ExampleChapter {
    val title: String
    val description: String get() = ""
    val examples: List
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy