rivet-typst/src/schema.typ

65 lines
1.5 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
}
}
2024-05-19 15:29:16 +00:00
#let parse-raw(schema) = {
let lang = schema.lang
let content = schema.text
if not lang in valid-extensions {
2024-05-19 15:34:55 +00:00
let fmts = valid-extensions.join(", ")
2024-05-19 15:29:16 +00:00
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) = {
2024-05-19 10:11:41 +00:00
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
}
2024-06-13 10:02:31 +00:00
#let render(structures, width: 100%, config: auto) = {
2024-05-19 10:11:41 +00:00
if config == auto {
config = conf.config()
}
let renderer_ = renderer.make(config)
2024-06-13 10:02:31 +00:00
(renderer_.render)(structures, width: width)
2024-05-19 10:11:41 +00:00
}