Skip to content

Commit

Permalink
support suspendingBlocl
Browse files Browse the repository at this point in the history
  • Loading branch information
jillesvangurp committed Jul 28, 2022
1 parent 6be9fb8 commit 1544f79
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ aFunctionThatReturnsAnInt()
Note how that captured the return value and printed that
without us using `print` or `println`.

You can also use suspendingBlock if you use co-routines

```kotlin
suspend fun foo() {}

suspendingBlock {
// call some suspending logic
foo()
}
```

## This README is generated

This README.md is actually created from kotlin code that
Expand Down Expand Up @@ -137,8 +148,18 @@ val readme by k4ERepo.md {
+"""
Note how that captured the return value and printed that
without us using `print` or `println`.
You can also use suspendingBlock if you use co-routines
"""

block(runBlock = false) {
suspend fun foo() {}

suspendingBlock {
// call some suspending logic
foo()
}
}
}

section("This README is generated") {
Expand Down
76 changes: 76 additions & 0 deletions src/main/kotlin/com/jillesvangurp/kotlin4example/Kotlin4Example.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.jillesvangurp.kotlin4example

import kotlinx.coroutines.runBlocking
import mu.KLogger
import mu.KotlinLogging
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -255,6 +256,81 @@ class Kotlin4Example(
}
}

fun <T> suspendingBlock(
runBlock: Boolean = true,
type: String = "kotlin",
allowLongLines: Boolean = false,
wrap: Boolean = false,
printStdOut: Boolean = true,
stdOutPrefix: String = "Captured Output:",
returnValuePrefix: String = "->",
lineLength: Int = 80,
block: suspend BlockOutputCapture.() -> T
) {
val state = BlockOutputCapture()
suspendingBlock(
allowLongLines = allowLongLines,
type = type,
wrap = wrap,
lineLength = lineLength,
runBlock = runBlock,
block = block,
blockCapture = state,
returnValuePrefix = returnValuePrefix,
printStdOut = printStdOut,
stdOutPrefix = stdOutPrefix
)
}
fun <T> suspendingBlock(
runBlock: Boolean = true,
type: String = "kotlin",
allowLongLines: Boolean = false,
wrap: Boolean = false,
printStdOut: Boolean = true,
stdOutPrefix: String = "Captured Output:",
returnValuePrefix: String = "->",
lineLength: Int = 80,
blockCapture: BlockOutputCapture,
block: suspend BlockOutputCapture.() -> T
) {
val callerSourceBlock =
getCallerSourceBlock() ?: throw IllegalStateException("source block could not be extracted")
mdCodeBlock(
code = callerSourceBlock,
allowLongLines = allowLongLines,
type = type,
wrap = wrap,
lineLength = lineLength
)

if (runBlock) {
val response = runBlocking {
block.invoke(blockCapture)
}

if (response !is Unit) {
buf.appendLine("$returnValuePrefix\n")
mdCodeBlock(response.toString(), type = "")
}
}

// if you have runBlock == false, no output can be produced
if (printStdOut && runBlock) {
val output = blockCapture.output()
blockCapture.reset()
if (output.isNotBlank()) {
buf.appendLine("$stdOutPrefix\n")
mdCodeBlock(
code = output,
allowLongLines = allowLongLines,
wrap = wrap,
lineLength = lineLength,
type = ""
)
}
}
}

private fun findContentInSourceFiles(sourceFile: String) =
sourceRepository.sourcePaths.map { File(it, sourceFile).absolutePath }
// the calculated fileName for the .class file does not match the source file for inner classes
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/jillesvangurp/kotlin4example/Page.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.io.File
data class Page(
val title: String,
val outputDir: String = ".",
val fileName: String = "${title.toLowerCase().replace("""\s+""", "-")}.md"
val fileName: String = "${title.lowercase().replace("""\s+""", "-")}.md"
) {
val file = File(outputDir,fileName)

Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/com/jillesvangurp/kotlin4example/docs/readme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,18 @@ val readme by k4ERepo.md {
+"""
Note how that captured the return value and printed that
without us using `print` or `println`.
You can also use suspendingBlock if you use co-routines
"""

block(runBlock = false) {
suspend fun foo() {}

suspendingBlock {
// call some suspending logic
foo()
}
}
}

section("This README is generated") {
Expand Down

0 comments on commit 1544f79

Please sign in to comment.