Compare commits
No commits in common. "2cc899e9c6beba282a4c2ad571e49640b972020e" and "53f6c36c77863c0180595005a2e449bc16b316e7" have entirely different histories.
2cc899e9c6
...
53f6c36c77
2 changed files with 25 additions and 86 deletions
|
@ -2,8 +2,6 @@ task build, "build":
|
||||||
switch("outdir", "bin")
|
switch("outdir", "bin")
|
||||||
setCommand "c", "src/typstgen.nim"
|
setCommand "c", "src/typstgen.nim"
|
||||||
|
|
||||||
task dev, "dev":
|
|
||||||
exec "nimble setup -l"
|
|
||||||
|
|
||||||
# begin Nimble config (version 2)
|
# begin Nimble config (version 2)
|
||||||
--noNimblePath
|
--noNimblePath
|
||||||
|
|
109
src/typstgen.nim
109
src/typstgen.nim
|
@ -1,90 +1,39 @@
|
||||||
import std/[os, sequtils, streams, strutils, tables]
|
import std/[streams, tables, strutils, os]
|
||||||
import yaml, hwylterm
|
import yaml, hwylterm
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
Component = Table[string, string]
|
|
||||||
TypstTemplate = object
|
|
||||||
keys: seq[string]
|
|
||||||
src: string
|
|
||||||
Config = object
|
Config = object
|
||||||
components {.defaultVal: @[]}: seq[Component]
|
components {.defaultVal: @[]}: seq[Table[string, string]]
|
||||||
templates {.defaultVal: initTable[string, TypstTemplate]()}: Table[string, TypstTemplate]
|
templates {.defaultVal: initTable[string, string]()}: Table[string, string]
|
||||||
|
|
||||||
func `%`(t: TypstTemplate, a: openArray[string]): string =
|
|
||||||
t.src % a
|
|
||||||
|
|
||||||
proc checkTemplates(templates: Table[string, TypstTemplate]) =
|
|
||||||
## quit if any templates have the same keys
|
|
||||||
var keyTable: Table[string, seq[string]]
|
|
||||||
|
|
||||||
for name, templ in templates.pairs():
|
|
||||||
let k = templ.keys.join(";")
|
|
||||||
if k in keyTable:
|
|
||||||
keyTable[k].add name
|
|
||||||
else:
|
|
||||||
keyTable[k] = @[name]
|
|
||||||
|
|
||||||
for keys, names in keyTable.pairs():
|
|
||||||
if names.len > 1:
|
|
||||||
quit("non-unique key combos in templates: " & names.join(","))
|
|
||||||
|
|
||||||
|
|
||||||
proc addDefaultTemplates(c: var Config) =
|
proc typstGen(configPath: string) =
|
||||||
# TODO: more default templates?
|
|
||||||
if "raw" notin c.templates:
|
|
||||||
c.templates["raw"] = TypstTemplate(keys: @["raw"], src: "$raw")
|
|
||||||
|
|
||||||
if "image" notin c.templates:
|
|
||||||
c.templates["image"] = TypstTemplate(keys: @["image"], src: """
|
|
||||||
#figure(
|
|
||||||
image("$image"),
|
|
||||||
caption: [$image]
|
|
||||||
)
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
if "figure" notin c.templates:
|
|
||||||
c.templates["figure"] = TypstTemplate(keys: @["image", "caption"], src: """
|
|
||||||
#align(center)[
|
|
||||||
#figure(
|
|
||||||
image("$image"),
|
|
||||||
caption: [
|
|
||||||
$image
|
|
||||||
]
|
|
||||||
)
|
|
||||||
$caption
|
|
||||||
]
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
proc loadConfig(configPath: string): Config =
|
|
||||||
|
|
||||||
|
var config: Config
|
||||||
var s = newFileStream(configPath)
|
var s = newFileStream(configPath)
|
||||||
load(s, result)
|
load(s, config)
|
||||||
close s
|
s.close()
|
||||||
|
|
||||||
addDefaultTemplates result
|
var output: string
|
||||||
checkTemplates result.templates
|
|
||||||
|
|
||||||
proc getTemplate(c: Config, component: Component): TypstTemplate =
|
if not config.templates.hasKey("raw"):
|
||||||
let k = component.keys().toSeq()
|
config.templates["raw"] = "$text"
|
||||||
for _, templ in c.templates.pairs():
|
|
||||||
if templ.keys == k:
|
|
||||||
return templ
|
|
||||||
|
|
||||||
quit($bb("[red]error[/] failed to find template for component:\n") & $component)
|
|
||||||
|
|
||||||
func toFmtArgs(comp: Component): seq[string] =
|
func componentToFmtArgs(t: Table[string, string]): seq[string] =
|
||||||
for k, v in comp.pairs: result.add k; result.add v
|
for k, v in t.pairs:
|
||||||
|
result.add k
|
||||||
|
result.add v
|
||||||
|
|
||||||
proc applyTemplate(c: Config, component: Component): string =
|
for component in config.components:
|
||||||
let templ = c.getTemplate(component)
|
var kind = "figure"
|
||||||
result = templ % component.toFmtArgs()
|
var component = component
|
||||||
|
discard component.pop("kind", kind)
|
||||||
|
output &= config.templates[kind] % componentToFmtArgs(component)
|
||||||
|
|
||||||
|
echo output
|
||||||
|
|
||||||
proc typstGen(c: Config): string =
|
|
||||||
for component in c.components:
|
|
||||||
result &= c.applyTemplate(component)
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
import hwylterm/[cli, parseopt3]
|
import hwylterm/[cli, parseopt3]
|
||||||
|
@ -96,13 +45,10 @@ when isMainModule:
|
||||||
[
|
[
|
||||||
("h", "help", "show this help"),
|
("h", "help", "show this help"),
|
||||||
("c","config", "path to config file"),
|
("c","config", "path to config file"),
|
||||||
("","check", "load config and exit"),
|
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
var
|
var p = initOptParser()
|
||||||
p = initOptParser(longNoVal= @["check"])
|
var configPath = "typstgen.yml"
|
||||||
configPath = "typstgen.yml"
|
|
||||||
check: bool
|
|
||||||
for kind, key, val in p.getopt():
|
for kind, key, val in p.getopt():
|
||||||
case kind
|
case kind
|
||||||
of cmdError: quit($bb"[red]cli error[/]: " & p.message)
|
of cmdError: quit($bb"[red]cli error[/]: " & p.message)
|
||||||
|
@ -114,14 +60,9 @@ when isMainModule:
|
||||||
writeHelp(); quit 0
|
writeHelp(); quit 0
|
||||||
of "config", "c":
|
of "config", "c":
|
||||||
configPath = val
|
configPath = val
|
||||||
of "check":
|
|
||||||
check = true
|
|
||||||
|
|
||||||
if not fileExists(configPath):
|
if not fileExists(configPath):
|
||||||
quit($bbfmt("file: [b]{configPath}[/] does not exist"))
|
quit($bbfmt("file: [b]{configPath}[/] does not exist"))
|
||||||
|
typstGen(configPath)
|
||||||
let config = loadConfig(configPath)
|
|
||||||
if check: quit 0
|
|
||||||
echo typstGen(config)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue