diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-09-30 14:43:29 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-30 12:43:29 +0000 |
| commit | 788ae10a07619278d8d9e8e31bc0f40635b1dc68 (patch) | |
| tree | 006df6809593f56c16b97bcd20dbf6ae572aaece /crates/typst-cli/src | |
| parent | d5b1bf314e50b96207a898f02dac2c8cdca3a568 (diff) | |
PDF export diagnostics (#5073)
Diffstat (limited to 'crates/typst-cli/src')
| -rw-r--r-- | crates/typst-cli/src/compile.rs | 41 | ||||
| -rw-r--r-- | crates/typst-cli/src/fonts.rs | 5 | ||||
| -rw-r--r-- | crates/typst-cli/src/main.rs | 2 | ||||
| -rw-r--r-- | crates/typst-cli/src/update.rs | 2 |
4 files changed, 30 insertions, 20 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(()) } diff --git a/crates/typst-cli/src/fonts.rs b/crates/typst-cli/src/fonts.rs index f5aa9826..01b0d9f7 100644 --- a/crates/typst-cli/src/fonts.rs +++ b/crates/typst-cli/src/fonts.rs @@ -1,11 +1,10 @@ -use typst::diag::StrResult; use typst::text::FontVariant; use typst_kit::fonts::Fonts; use crate::args::FontsCommand; /// Execute a font listing command. -pub fn fonts(command: &FontsCommand) -> StrResult<()> { +pub fn fonts(command: &FontsCommand) { let fonts = Fonts::searcher() .include_system_fonts(!command.font_args.ignore_system_fonts) .search_with(&command.font_args.font_paths); @@ -19,6 +18,4 @@ pub fn fonts(command: &FontsCommand) -> StrResult<()> { } } } - - Ok(()) } diff --git a/crates/typst-cli/src/main.rs b/crates/typst-cli/src/main.rs index bc1c30a6..283d17e2 100644 --- a/crates/typst-cli/src/main.rs +++ b/crates/typst-cli/src/main.rs @@ -54,7 +54,7 @@ fn dispatch() -> HintedStrResult<()> { Command::Watch(command) => crate::watch::watch(timer, command.clone())?, Command::Init(command) => crate::init::init(command)?, Command::Query(command) => crate::query::query(command)?, - Command::Fonts(command) => crate::fonts::fonts(command)?, + Command::Fonts(command) => crate::fonts::fonts(command), Command::Update(command) => crate::update::update(command)?, } diff --git a/crates/typst-cli/src/update.rs b/crates/typst-cli/src/update.rs index adec4a2c..b2b3932a 100644 --- a/crates/typst-cli/src/update.rs +++ b/crates/typst-cli/src/update.rs @@ -28,7 +28,7 @@ pub fn update(command: &UpdateCommand) -> StrResult<()> { if version < &Version::new(0, 8, 0) { eprintln!( - "Note: Versions older than 0.8.0 will not have \ + "note: versions older than 0.8.0 will not have \ the update command available." ); } |
