diff --git a/Cargo.lock b/Cargo.lock index 0572a8d..558760c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2495,6 +2495,7 @@ dependencies = [ "serde_yaml", "typst", "typst-eval", + "typst-html", "typst-kit", "typst-pdf", "typst-render", diff --git a/Cargo.toml b/Cargo.toml index 39cbf97..8bc7dc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,3 +34,4 @@ typst-pdf = "0.13.0" typst-svg = "0.13.0" typst-render = "0.13.0" typst-eval = "0.13.0" +typst-html = "0.13.0" diff --git a/src/compiler.rs b/src/compiler.rs index b888f32..a8ee35c 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -4,6 +4,7 @@ use codespan_reporting::term::{self, termcolor}; use ecow::{eco_format, EcoString}; use typst::diag::{Severity, SourceDiagnostic, StrResult, Warned}; use typst::foundations::Datetime; +use typst::html::HtmlDocument; use typst::layout::PagedDocument; use typst::syntax::{FileId, Source, Span}; use typst::{World, WorldExt}; @@ -38,6 +39,10 @@ impl SystemWorld { )?]), "png" => Ok(export_image(&document, ImageExportFormat::Png, ppi)?), "svg" => Ok(export_image(&document, ImageExportFormat::Svg, ppi)?), + "html" => { + let Warned { output, warnings } = typst::compile::<HtmlDocument>(self); + Ok(vec![export_html(&output.unwrap(), self)?]) + } fmt => Err(eco_format!("unknown format: {fmt}")), } } @@ -46,6 +51,18 @@ impl SystemWorld { } } +/// Export to a html. +#[inline] +fn export_html(document: &HtmlDocument, world: &SystemWorld) -> StrResult<Vec<u8>> { + let buffer = + typst_html::html(document).map_err(|e| match format_diagnostics(world, &e, &[]) { + Ok(e) => EcoString::from(e), + Err(err) => eco_format!("failed to print diagnostics ({err})"), + })?; + + Ok(buffer.into()) +} + /// Export to a PDF. #[inline] fn export_pdf( diff --git a/src/world.rs b/src/world.rs index 415fb67..d755939 100644 --- a/src/world.rs +++ b/src/world.rs @@ -186,7 +186,12 @@ impl SystemWorldBuilder { input, root: self.root, main: FileId::new(None, main_path), - library: LazyHash::new(LibraryBuilder::default().with_inputs(self.inputs).build()), + library: LazyHash::new( + LibraryBuilder::default() + .with_features(vec![typst::Feature::Html].into_iter().collect()) + .with_inputs(self.inputs) + .build(), + ), book: LazyHash::new(fonts.book), fonts: fonts.fonts, slots: Mutex::default(),