2024-10-30 14:22:23 -05:00
|
|
|
import std/[streams, tables, strutils, os]
|
|
|
|
import yaml, hwylterm
|
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
Config = object
|
|
|
|
components {.defaultVal: @[]}: seq[Table[string, string]]
|
|
|
|
templates {.defaultVal: initTable[string, string]()}: Table[string, string]
|
|
|
|
|
|
|
|
|
|
|
|
proc typstGen(configPath: string) =
|
|
|
|
|
|
|
|
var config: Config
|
|
|
|
var s = newFileStream(configPath)
|
|
|
|
load(s, config)
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
var output: string
|
|
|
|
|
|
|
|
if not config.templates.hasKey("raw"):
|
|
|
|
config.templates["raw"] = "$text"
|
|
|
|
|
|
|
|
|
|
|
|
func componentToFmtArgs(t: Table[string, string]): seq[string] =
|
|
|
|
for k, v in t.pairs:
|
|
|
|
result.add k
|
|
|
|
result.add v
|
|
|
|
|
|
|
|
for component in config.components:
|
|
|
|
var kind = "figure"
|
|
|
|
var component = component
|
|
|
|
discard component.pop("kind", kind)
|
|
|
|
output &= config.templates[kind] % componentToFmtArgs(component)
|
|
|
|
|
|
|
|
echo output
|
|
|
|
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
import hwylterm/[cli, parseopt3]
|
|
|
|
proc writeHelp() =
|
|
|
|
echo newHwylCli(
|
|
|
|
"[bold]typstgen[/] [[[faint]-h[/]]",
|
|
|
|
"""
|
|
|
|
typstgen --config typstgen.yml""",
|
|
|
|
[
|
|
|
|
("h", "help", "show this help"),
|
|
|
|
("c","config", "path to config file"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
var p = initOptParser()
|
|
|
|
var configPath = "typstgen.yml"
|
|
|
|
for kind, key, val in p.getopt():
|
|
|
|
case kind
|
|
|
|
of cmdError: quit($bb"[red]cli error[/]: " & p.message)
|
|
|
|
of cmdEnd: assert false
|
|
|
|
of cmdArgument: quit($bb"[red] unexpected argument[/]: " & key)
|
|
|
|
of cmdShortOption, cmdLongOption:
|
|
|
|
case key
|
|
|
|
of "help", "h":
|
|
|
|
writeHelp(); quit 0
|
|
|
|
of "config", "c":
|
|
|
|
configPath = val
|
|
|
|
|
|
|
|
if not fileExists(configPath):
|
2024-10-30 14:25:09 -05:00
|
|
|
quit($bbfmt("file: [b]{configPath}[/] does not exist"))
|
2024-10-30 14:22:23 -05:00
|
|
|
typstGen(configPath)
|
|
|
|
|
|
|
|
|