Skip to content

Instantly share code, notes, and snippets.

@eed3si9n
Last active February 14, 2021 03:37
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eed3si9n/fc1aa881bd28b48843e3 to your computer and use it in GitHub Desktop.
Save eed3si9n/fc1aa881bd28b48843e3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sbt -Dsbt.version=1.4.7 -Dsbt.main.class=sbt.ScriptMain -Dsbt.supershell=false -error
/***
// $ chmod +x scripts.scala
// $ ./scripts.scala
ThisBuild / scalaVersion := "2.13.4"
libraryDependencies += "org.scala-sbt" %% "io" % "1.4.0"
*/
import sbt.io._
import sbt.io.syntax._
import java.io.File
import java.net.{URI, URL}
import sys.process._
def file(s: String): File = new File(s)
def uri(s: String): URI = new URI(s)
def stdinStr = IO.readStream(System.in)
def removeRole(role: String): String => String =
_.replaceAll("""(:""" + role + """:)(\`[^`]+\`)""", """$2""")
def nTicks(n: Int): String = """(\`{""" + n.toString + """})"""
def toSingleTicks: String => String =
_.replaceAll(nTicks(2), "`")
def toDoubleTicks: String => String =
_.replaceAll(nTicks(1), "``")
val preprocessRest: String => String =
removeRole("doc") andThen removeRole("key") andThen removeRole("ref") andThen toSingleTicks andThen toDoubleTicks
val isVerboseLine: String => Boolean = _.startsWith(" ")
val processLine: String => String = {
case s if isVerboseLine(s) => identity(s)
case s => preprocessRest(s)
}
def runPandoc(f: File): Seq[String] =
Process(Seq("pandoc", "-f", "rst", "-t", "markdown", f.toString)).lazyLines.toVector
val outDir = file("./target/")
val srcDir = file("./src/")
val toOut = Path.rebase(srcDir, outDir)
def processFile(f: File): Unit = {
val (base, ext) = f.baseAndExt
val newParent = toOut(f.getParentFile) getOrElse {sys.error("wat")}
val file1 = newParent / (base + "." + ext)
val file2 = newParent / (base + ".md")
println(s"""$f => $file2""")
val xs = IO.readLines(f) map { processLine }
IO.writeLines(file1, xs)
val mdLines = runPandoc(file1)
IO.delete(file1)
IO.writeLines(file2, mdLines)
}
IO.createDirectory(outDir / "sphinx")
val fs: Seq[File] = (srcDir ** "*.rst").get()
fs foreach { processFile }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment