summaryrefslogtreecommitdiff
path: root/crates/typst-cli/src/compile.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-09-30 14:43:29 +0200
committerGitHub <noreply@github.com>2024-09-30 12:43:29 +0000
commit788ae10a07619278d8d9e8e31bc0f40635b1dc68 (patch)
tree006df6809593f56c16b97bcd20dbf6ae572aaece /crates/typst-cli/src/compile.rs
parentd5b1bf314e50b96207a898f02dac2c8cdca3a568 (diff)
PDF export diagnostics (#5073)
Diffstat (limited to 'crates/typst-cli/src/compile.rs')
-rw-r--r--crates/typst-cli/src/compile.rs41
1 files changed, 27 insertions, 14 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs
index cc85d920..58745c80 100644
--- a/crates/typst-cli/src/compile.rs
+++ b/crates/typst-cli/src/compile.rs
@@ -8,12 +8,15 @@ use codespan_reporting::term;
use ecow::{eco_format, EcoString};
use parking_lot::RwLock;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
-use typst::diag::{bail, Severity, SourceDiagnostic, StrResult, Warned};
+use typst::diag::{
+ bail, At, Severity, SourceDiagnostic, SourceResult, StrResult, Warned,
+};
use typst::foundations::{Datetime, Smart};
use typst::layout::{Frame, Page, PageRanges};
use typst::model::Document;
use typst::syntax::{FileId, Source, Span};
use typst::WorldExt;
+use typst_pdf::PdfOptions;
use crate::args::{
CompileCommand, DiagnosticFormat, Input, Output, OutputFormat, PageRangeArgument,
@@ -54,7 +57,11 @@ impl CompileCommand {
Some(ext) if ext.eq_ignore_ascii_case("pdf") => OutputFormat::Pdf,
Some(ext) if ext.eq_ignore_ascii_case("png") => OutputFormat::Png,
Some(ext) if ext.eq_ignore_ascii_case("svg") => OutputFormat::Svg,
- _ => bail!("could not infer output format for path {}.\nconsider providing the format manually with `--format/-f`", output.display()),
+ _ => bail!(
+ "could not infer output format for path {}.\n\
+ consider providing the format manually with `--format/-f`",
+ output.display()
+ ),
}
} else {
OutputFormat::Pdf
@@ -96,11 +103,11 @@ pub fn compile_once(
}
let Warned { output, warnings } = typst::compile(world);
+ let result = output.and_then(|document| export(world, &document, command, watching));
- match output {
+ match result {
// Export the PDF / PNG.
- Ok(document) => {
- export(world, &document, command, watching)?;
+ Ok(()) => {
let duration = start.elapsed();
if watching {
@@ -150,29 +157,35 @@ fn export(
document: &Document,
command: &CompileCommand,
watching: bool,
-) -> StrResult<()> {
- match command.output_format()? {
+) -> SourceResult<()> {
+ match command.output_format().at(Span::detached())? {
OutputFormat::Png => {
export_image(world, document, command, watching, ImageExportFormat::Png)
+ .at(Span::detached())
}
OutputFormat::Svg => {
export_image(world, document, command, watching, ImageExportFormat::Svg)
+ .at(Span::detached())
}
OutputFormat::Pdf => export_pdf(document, command),
}
}
/// Export to a PDF.
-fn export_pdf(document: &Document, command: &CompileCommand) -> StrResult<()> {
- let timestamp = convert_datetime(
- command.common.creation_timestamp.unwrap_or_else(chrono::Utc::now),
- );
- let exported_page_ranges = command.exported_page_ranges();
- let buffer = typst_pdf::pdf(document, Smart::Auto, timestamp, exported_page_ranges);
+fn export_pdf(document: &Document, command: &CompileCommand) -> SourceResult<()> {
+ let options = PdfOptions {
+ ident: Smart::Auto,
+ timestamp: convert_datetime(
+ command.common.creation_timestamp.unwrap_or_else(chrono::Utc::now),
+ ),
+ page_ranges: command.exported_page_ranges(),
+ };
+ let buffer = typst_pdf::pdf(document, &options)?;
command
.output()
.write(&buffer)
- .map_err(|err| eco_format!("failed to write PDF file ({err})"))?;
+ .map_err(|err| eco_format!("failed to write PDF file ({err})"))
+ .at(Span::detached())?;
Ok(())
}