pixie/tools/gen_readme.nim
Ryan Oldenburg f2b3c67d4b morepretty
2021-02-16 00:35:36 -06:00

46 lines
1.2 KiB
Nim

import algorithm, os, osproc, sequtils, strutils
proc cutBetween(str, a, b: string): string =
let
cutA = str.find(a)
cutB = str.find(b)
if cutA == -1 or cutB == -1:
return ""
return str[cutA + a.len..<cutB]
var md: seq[string]
var exampleFiles = [
"examples/square.nim",
"examples/rounded_rectangle.nim",
"examples/heart.nim",
"examples/shadow.nim",
"examples/blur.nim",
"examples/tiger.nim"
]
for path in exampleFiles:
discard execCmd("nim c -r -d:danger " & path)
let code = readFile(path)
let innerCode = code.cutBetween("image.fill(rgba(255, 255, 255, 255))", "image.writeFile")
if innerCode != "":
let path = path.replace("\\", "/")
md.add "### " & path.splitFile().name.replace("_", " ").capitalizeAscii()
md.add "[" & path & "](" & path & ")"
md.add "```nim"
md.add innerCode.strip()
md.add "```"
md.add "![example output](" & path.replace(".nim", ".png").replace("\\",
"/") & ")"
md.add ""
var readme = readFile("README.md")
let at = readme.find("## Examples")
if at != -1:
readme = readme[0 .. at]
readme.add("# Examples\n\n")
readme.add(md.join("\n"))
writeFile("README.md", readme)