Skip to content

Commit

Permalink
feat: export pdf with timestamp (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Nov 28, 2023
1 parent 136effc commit c4db172
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
21 changes: 17 additions & 4 deletions cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use typst_ts_core::{
};
use typst_ts_svg_exporter::DefaultExportFeature;

use crate::CompileArgs;
use crate::{CompileArgs, ExportArgs};

type GroupDocExporter = GroupExporter<typst::doc::Document>;

Expand Down Expand Up @@ -52,7 +52,11 @@ fn exit_by_unknown_format(f: &str) -> ! {
}

/// With the given arguments, prepare exporters for the compilation.
fn prepare_exporters_impl(out: PathBuf, mut formats: Vec<String>) -> GroupDocExporter {
fn prepare_exporters_impl(
args: ExportArgs,
out: PathBuf,
mut formats: Vec<String>,
) -> GroupDocExporter {
let mut doc: ExporterVec<Doc> = vec![];

/// connect export flow from $x to $y
Expand All @@ -74,6 +78,13 @@ fn prepare_exporters_impl(out: PathBuf, mut formats: Vec<String>) -> GroupDocExp
<$exporter>::default(),
)));
}};
(|| $exporter:tt as $ser:ty as $exporters:ident, $output_dir:ident @@ $extension:literal) => {{
let output_path = $output_dir.with_extension($extension);
$exporters.push(Box::new(FsPathExporter::<$ser, _>::new(
output_path,
$exporter,
)));
}};
}

// sink exporters according to the given formats
Expand All @@ -85,7 +96,9 @@ fn prepare_exporters_impl(out: PathBuf, mut formats: Vec<String>) -> GroupDocExp
"nothing" => (),
"ast" => sink_path!(WithAst as _ as doc, out @@ "ast.ansi.text"),
#[cfg(feature = "pdf")]
"pdf" => sink_path!(WithPdf as _ as doc, out @@ "pdf"),
"pdf" => sink_path!(|| {
WithPdf::default().with_timestamp(args.pdf_timestamp)
} as _ as doc, out @@ "pdf"),
#[cfg(feature = "svg")]
"svg" => sink_path!(WithSvg as _ as doc, out @@ "artifact.svg"),
#[cfg(feature = "svg")]
Expand Down Expand Up @@ -135,5 +148,5 @@ pub fn prepare_exporters(args: &CompileArgs, entry_file: &Path) -> GroupDocExpor
formats
};

prepare_exporters_impl(output_dir, formats)
prepare_exporters_impl(args.export.clone(), output_dir, formats)
}
11 changes: 11 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,24 @@ pub struct CompileOnceArgs {
pub output: String,
}

#[derive(Default, Debug, Clone, Parser)]
#[clap(next_help_heading = "Export options")]
pub struct ExportArgs {
/// Export pdf with timestamp.
#[clap(long, default_value_t = false)]
pub pdf_timestamp: bool,
}

#[derive(Default, Debug, Clone, Parser)]
#[clap(next_help_heading = "Compile options")]
pub struct CompileArgs {
/// compile arguments before query.
#[clap(flatten)]
pub compile: CompileOnceArgs,

#[clap(flatten)]
pub export: ExportArgs,

/// Watch mode.
#[clap(long)]
pub watch: bool,
Expand Down
19 changes: 15 additions & 4 deletions exporter/pdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ use typst_ts_core::Exporter;
use typst::{diag::SourceResult, World};

#[derive(Debug, Clone, Default)]
pub struct PdfDocExporter {}
pub struct PdfDocExporter {
with_timestamp: bool,
}

impl PdfDocExporter {
pub fn with_timestamp(mut self, enable: bool) -> Self {
self.with_timestamp = enable;
self
}
}

impl Exporter<typst::doc::Document, Vec<u8>> for PdfDocExporter {
fn export(
&self,
_world: &dyn World,
world: &dyn World,
output: Arc<typst::doc::Document>,
) -> SourceResult<Vec<u8>> {
// todo: ident and timestamp option
Ok(typst::export::pdf(output.as_ref(), None, None))
// todo: ident option

let timestamp = self.with_timestamp.then(|| world.today(None)).flatten();
Ok(typst::export::pdf(output.as_ref(), None, timestamp))
}
}

0 comments on commit c4db172

Please sign in to comment.