rivet-typst/src/schema.typ

47 lines
1.1 KiB
Plaintext
Raw Normal View History

2024-05-19 10:11:41 +00:00
#import "config.typ" as conf
#import "renderer.typ"
#import "structure.typ"
2024-05-19 10:47:52 +00:00
#import "xml-loader.typ"
2024-05-19 10:11:41 +00:00
#let valid-extensions = ("yaml", "json", "xml")
#let parse-file(path) = {
let ext = path.split(".").last()
if not ext in valid-extensions {
let fmts = valid-extensions.map(fmt => "." + fmt).join(", ")
fmts = "(" + fmts + ")"
panic("." + ext + " files are not supported. Valid formats: " + fmts)
}
if ext == "yaml" {
return yaml(path)
} else if ext == "json" {
return json(path)
} else if ext == "xml" {
2024-05-19 10:47:52 +00:00
return xml-loader.load(path)
2024-05-19 10:11:41 +00:00
}
}
#let load(path-or-schema, config: auto) = {
let schema = if type(path-or-schema) == str {
parse-file(path-or-schema)
} else {
parse-raw(path-or-schema)
}
let structures = (:)
for (id, data) in schema.structures {
id = str(id)
structures.insert(id, structure.load(id, data))
}
return structures
}
#let render(structures, config: auto) = {
if config == auto {
config = conf.config()
}
let renderer_ = renderer.make(config)
(renderer_.render)(structures)
}