diff options
| author | Martin Haug <mhaug@live.de> | 2023-10-27 18:33:23 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-27 18:33:23 +0200 |
| commit | f78a8f5d4863260d05cbabe2711324489be47242 (patch) | |
| tree | 7cfa0bdd805ae3c385812ef4f74aa14e867fc7fc /crates/typst-cli/src/compile.rs | |
| parent | 4163b2eabc1df29741228838fdce9d428b662500 (diff) | |
Add IDs and creation date to PDFs (#2374)
Diffstat (limited to 'crates/typst-cli/src/compile.rs')
| -rw-r--r-- | crates/typst-cli/src/compile.rs | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index 6a5ca21e..b5cf0e54 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -1,12 +1,13 @@ use std::fs; use std::path::{Path, PathBuf}; +use chrono::{Datelike, Timelike}; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::term::{self, termcolor}; use termcolor::{ColorChoice, StandardStream}; use typst::diag::{bail, Severity, SourceDiagnostic, StrResult}; use typst::doc::Document; -use typst::eval::{eco_format, Tracer}; +use typst::eval::{eco_format, Datetime, Tracer}; use typst::geom::Color; use typst::syntax::{FileId, Source, Span}; use typst::{World, WorldExt}; @@ -141,19 +142,37 @@ fn export( OutputFormat::Svg => { export_image(world, document, command, watching, ImageExportFormat::Svg) } - OutputFormat::Pdf => export_pdf(document, command), + OutputFormat::Pdf => export_pdf(document, command, world), } } /// Export to a PDF. -fn export_pdf(document: &Document, command: &CompileCommand) -> StrResult<()> { +fn export_pdf( + document: &Document, + command: &CompileCommand, + world: &SystemWorld, +) -> StrResult<()> { + let ident = world.input().to_string_lossy(); + let buffer = typst::export::pdf(document, Some(&ident), now()); let output = command.output(); - let buffer = typst::export::pdf(document); fs::write(output, buffer) .map_err(|err| eco_format!("failed to write PDF file ({err})"))?; Ok(()) } +/// Get the current date and time in UTC. +fn now() -> Option<Datetime> { + let now = chrono::Local::now().naive_utc(); + Datetime::from_ymd_hms( + now.year(), + now.month().try_into().ok()?, + now.day().try_into().ok()?, + now.hour().try_into().ok()?, + now.minute().try_into().ok()?, + now.second().try_into().ok()?, + ) +} + /// An image format to export in. enum ImageExportFormat { Png, |
