Commit cff7b65c authored by Holger Brandl's avatar Holger Brandl

continued rerender filter

parent e0d14c46
......@@ -14,7 +14,11 @@ cd /Users/brandl/Dropbox/projects/datautils/R/rnblight
mdInput=example.md
mdBase=$(basename $mdInput .md)
mv $mdInput ${mdBase}.Rmd
cp $mdInput ${mdBase}.Rmd
kscript --idea strip_chunk_results.kts
kscript strip_chunk_results.kts ${mdBase}.md
Rscript - <<EOF
knitr::knit('${mdBase}.Rmd', '${mdBase}.md')
......@@ -23,6 +27,4 @@ EOF
idea .
kscript --idea strip_chunk_results.kts
kscript strip_chunk_results.kts ${mdBase}.md
```
\ No newline at end of file
#!/usr/bin/env kscript
@file:DependsOn()
import java.io.File
val kotlin.Boolean.int
get() = if (this) 1 else 0
val kotlin.Boolean.int get() = if (this) 1 else 0
enum class TripleType { start, end }
fun main(args: Array<String>) {
//val mdFile = File(args[0])
val mdFile = File("/Users/brandl/Dropbox/projects/datautils/R/rnblight/example.md")
//
//var chunkCounter = 0
//var isInChunk = false
//val filtMd = mdFile.readLines().groupBy{ line->
//
// if(line.startsWith("```")) {
// chunkCounter++
// isInChunk = !isInChunk
// }
//
// chunkCounter
//}.filterNot { (_, group) ->
// group.filterNot{it.startsWith("```")}.all { it.startsWith("## ") }
//}
//
//File("result.md").writeText( filtMd.flatMap { it.value }.joinToString("\n"))
fun <T : Number> List<T>.cumSum(removeNA: Boolean = false): Iterable<Double> {
return drop(1).fold(listOf(first().toDouble()), { list, curVal -> list + (list.last().toDouble() + curVal.toDouble()) })
val lines = File("example.md").readLines()
// val lines = mdFile.readLines().take(50)
// val lines = """
// foo
// ```bash
// ls
// ```
//
// bla bla
// ```r
// ```
// more blabla
//
// """.trimIndent().lines()
data class CodeChunk(val start: Int, val end: Int) {
val isResult = lines
.withIndex()
.filter { it.index in start..end }
.map { it.value }
.drop(1)
.dropLast(1)
.all { it.startsWith("##") }
}
val lines = mdFile.readLines().take(50)
lines.map { it.startsWith("```").int }
lines.map { it.startsWith("```").int }.windowed(2) { (a, b) -> if (a > b) a else b }
lines.map { it.startsWith("```").int }.cumSum().zipWithNext { a, b -> a }
// 1. remove result chunks
val resultLines = lines
.withIndex()
.filter { it.value.startsWith("```") }
.windowed(2)
.map { CodeChunk(it.first().index, it.last().index) }
.filter { it.isResult }
.flatMap { it.start..it.end }
// remove all result chunks
var filtMD = lines.filterIndexed{ idx, line -> !resultLines.contains(idx)}
// also remove figures
filtMD = filtMD.filter { !it.contains("figure/") }
println("filtered nb is")
println(filtMD.joinToString("\n"))
// 2. join code chunks of same type if there's just empty space inbetween
File("example_filt.md").writeText(filtMD.joinToString("\n"))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment