Skip to content

Commit 25a5dcc

Browse files
initial working html output (#86)
1 parent 516f97a commit 25a5dcc

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ typst-pdf = "0.13.0"
3434
typst-svg = "0.13.0"
3535
typst-render = "0.13.0"
3636
typst-eval = "0.13.0"
37+
typst-html = "0.13.0"

src/compiler.rs

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use codespan_reporting::term::{self, termcolor};
44
use ecow::{eco_format, EcoString};
55
use typst::diag::{Severity, SourceDiagnostic, StrResult, Warned};
66
use typst::foundations::Datetime;
7+
use typst::html::HtmlDocument;
78
use typst::layout::PagedDocument;
89
use typst::syntax::{FileId, Source, Span};
910
use typst::{World, WorldExt};
@@ -38,6 +39,10 @@ impl SystemWorld {
3839
)?]),
3940
"png" => Ok(export_image(&document, ImageExportFormat::Png, ppi)?),
4041
"svg" => Ok(export_image(&document, ImageExportFormat::Svg, ppi)?),
42+
"html" => {
43+
let Warned { output, warnings } = typst::compile::<HtmlDocument>(self);
44+
Ok(vec![export_html(&output.unwrap(), self)?])
45+
}
4146
fmt => Err(eco_format!("unknown format: {fmt}")),
4247
}
4348
}
@@ -46,6 +51,18 @@ impl SystemWorld {
4651
}
4752
}
4853

54+
/// Export to a html.
55+
#[inline]
56+
fn export_html(document: &HtmlDocument, world: &SystemWorld) -> StrResult<Vec<u8>> {
57+
let buffer =
58+
typst_html::html(document).map_err(|e| match format_diagnostics(world, &e, &[]) {
59+
Ok(e) => EcoString::from(e),
60+
Err(err) => eco_format!("failed to print diagnostics ({err})"),
61+
})?;
62+
63+
Ok(buffer.into())
64+
}
65+
4966
/// Export to a PDF.
5067
#[inline]
5168
fn export_pdf(

src/world.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ impl SystemWorldBuilder {
186186
input,
187187
root: self.root,
188188
main: FileId::new(None, main_path),
189-
library: LazyHash::new(LibraryBuilder::default().with_inputs(self.inputs).build()),
189+
library: LazyHash::new(
190+
LibraryBuilder::default()
191+
.with_features(vec![typst::Feature::Html].into_iter().collect())
192+
.with_inputs(self.inputs)
193+
.build(),
194+
),
190195
book: LazyHash::new(fonts.book),
191196
fonts: fonts.fonts,
192197
slots: Mutex::default(),

0 commit comments

Comments
 (0)