From f78a8f5d4863260d05cbabe2711324489be47242 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Fri, 27 Oct 2023 18:33:23 +0200 Subject: Add IDs and creation date to PDFs (#2374) --- crates/typst-cli/src/compile.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'crates/typst-cli/src/compile.rs') 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 { + 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, -- cgit v1.2.3