rivet-typst/src/schema.typ

65 lines
1.5 KiB
Plaintext

#import "config.typ" as conf
#import "renderer.typ"
#import "structure.typ"
#import "xml-loader.typ"
#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" {
return xml-loader.load(path)
}
}
#let parse-raw(schema) = {
let lang = schema.lang
let content = schema.text
if not lang in valid-extensions {
let fmts = valid-extensions.join(", ")
fmts = "(" + fmts + ")"
panic("Unsupported format '" + lang + "'. Valid formats: " + fmts)
}
if lang == "yaml" {
return yaml.decode(content)
} else if lang == "json" {
return json.decode(content)
} else if lang == "xml" {
return xml-loader.parse(xml.decode(content).first())
}
}
#let load(path-or-schema) = {
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, width: 100%, config: auto) = {
if config == auto {
config = conf.config()
}
let renderer_ = renderer.make(config)
(renderer_.render)(structures, width: width)
}