summaryrefslogtreecommitdiff
path: root/crates/typst-cli/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/typst-cli/src')
-rw-r--r--crates/typst-cli/src/compile.rs27
-rw-r--r--crates/typst-cli/src/world.rs8
2 files changed, 31 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,
diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs
index 500b64e5..81480e62 100644
--- a/crates/typst-cli/src/world.rs
+++ b/crates/typst-cli/src/world.rs
@@ -25,6 +25,8 @@ use crate::package::prepare_package;
pub struct SystemWorld {
/// The working directory.
workdir: Option<PathBuf>,
+ /// The canonical path to the input file.
+ input: PathBuf,
/// The root relative to which absolute paths are resolved.
root: PathBuf,
/// The input path.
@@ -78,6 +80,7 @@ impl SystemWorld {
Ok(Self {
workdir: std::env::current_dir().ok(),
+ input,
root,
main: FileId::new(None, main_path),
library: Prehashed::new(typst_library::build()),
@@ -123,6 +126,11 @@ impl SystemWorld {
self.now.take();
}
+ /// Return the canonical path to the input file.
+ pub fn input(&self) -> &PathBuf {
+ &self.input
+ }
+
/// Lookup a source file by id.
#[track_caller]
pub fn lookup(&self, id: FileId) -> Source {