pixie/tools/gen_readme.nim

52 lines
1.3 KiB
Nim
Raw Normal View History

2021-02-14 18:27:32 +00:00
import algorithm, os, osproc, sequtils, strutils
2020-12-01 17:49:41 +00:00
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]
2021-02-14 04:45:27 +00:00
var exampleFiles = [
2021-05-11 21:35:44 +00:00
"examples/text.nim",
"examples/text_spans.nim",
2021-02-14 04:45:27 +00:00
"examples/square.nim",
2021-02-18 18:14:55 +00:00
"examples/line.nim",
2021-02-14 04:45:27 +00:00
"examples/rounded_rectangle.nim",
"examples/heart.nim",
2021-02-25 16:51:09 +00:00
"examples/masking.nim",
2021-02-25 16:57:49 +00:00
"examples/gradient.nim",
"examples/image_tiled.nim",
2021-02-14 04:45:27 +00:00
"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 "```"
2021-02-14 18:27:32 +00:00
md.add "![example output](" & path.replace(".nim", ".png").replace("\\",
"/") & ")"
2021-02-14 04:45:27 +00:00
md.add ""
2020-12-01 17:49:41 +00:00
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)