>()
.try_into()
.ok();
}
}
if !matches!(lang, "example" | "typ") {
let mut buf = String::from("");
escape_html(&mut buf, &display).unwrap();
buf.push_str("
");
return Html::new(buf);
}
let root = typst::syntax::parse(&display);
let highlighted = Html::new(typst::ide::highlight_html(&root));
if lang == "typ" {
return Html::new(format!("{}
", highlighted.as_str()));
}
let source = Source::new(SourceId::from_u16(0), Path::new("main.typ"), compile);
let world = DocWorld(source);
let mut frames = match typst::compile(&world) {
Ok(doc) => doc.pages,
Err(err) => {
let msg = &err[0].message;
panic!("while trying to compile:\n{text}:\n\nerror: {msg}");
}
};
if let Some([x, y, w, h]) = zoom {
frames[0].translate(Point::new(-x, -y));
*frames[0].size_mut() = Size::new(w, h);
}
if single {
frames.truncate(1);
}
resolver.example(highlighted, &frames)
}
/// World for example compilations.
struct DocWorld(Source);
impl World for DocWorld {
fn library(&self) -> &Prehashed {
&LIBRARY
}
fn main(&self) -> &Source {
&self.0
}
fn resolve(&self, _: &Path) -> FileResult {
unimplemented!()
}
fn source(&self, id: SourceId) -> &Source {
assert_eq!(id.into_u16(), 0, "invalid source id");
&self.0
}
fn book(&self) -> &Prehashed {
&FONTS.0
}
fn font(&self, id: usize) -> Option {
Some(FONTS.1[id].clone())
}
fn file(&self, path: &Path) -> FileResult {
Ok(FILES
.get_file(path)
.unwrap_or_else(|| panic!("failed to load {path:?}"))
.contents()
.into())
}
}