summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Fellenz <matt@felle.nz>2023-08-23 13:44:03 -0700
committerGitHub <noreply@github.com>2023-08-23 22:44:03 +0200
commitff5dc9191e15d032c8e87065c68c232c650419a8 (patch)
tree5a9cbbbe3070f101ed3d118ca709d1bc6370402b
parent3fcb5ea73cbae20500f4695685df4caf0071d45a (diff)
Add format arg to CLI (#1985)
-rw-r--r--crates/typst-cli/src/args.rs30
-rw-r--r--crates/typst-cli/src/compile.rs43
2 files changed, 54 insertions, 19 deletions
diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs
index 6bc58443..2696a031 100644
--- a/crates/typst-cli/src/args.rs
+++ b/crates/typst-cli/src/args.rs
@@ -46,6 +46,10 @@ pub struct CompileCommand {
/// Path to output file (PDF, PNG, or SVG)
pub output: Option<PathBuf>,
+ /// The format of the output file, inferred from the extension by default
+ #[arg(long = "format", short = 'f')]
+ pub format: Option<OutputFormat>,
+
/// Opens the output file using the default viewer after compilation
#[arg(long = "open")]
pub open: Option<Option<String>>,
@@ -59,15 +63,6 @@ pub struct CompileCommand {
pub flamegraph: Option<Option<PathBuf>>,
}
-impl CompileCommand {
- /// The output path.
- pub fn output(&self) -> PathBuf {
- self.output
- .clone()
- .unwrap_or_else(|| self.common.input.with_extension("pdf"))
- }
-}
-
/// Processes an input file to extract provided metadata
#[derive(Debug, Clone, Parser)]
pub struct QueryCommand {
@@ -158,3 +153,20 @@ impl Display for DiagnosticFormat {
.fmt(f)
}
}
+
+/// Which format to use for the generated output file.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
+pub enum OutputFormat {
+ Pdf,
+ Png,
+ Svg,
+}
+
+impl Display for OutputFormat {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ self.to_possible_value()
+ .expect("no values are skipped")
+ .get_name()
+ .fmt(f)
+ }
+}
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs
index 5b51f422..4c4c24be 100644
--- a/crates/typst-cli/src/compile.rs
+++ b/crates/typst-cli/src/compile.rs
@@ -1,5 +1,5 @@
use std::fs;
-use std::path::Path;
+use std::path::{Path, PathBuf};
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::term::{self, termcolor};
@@ -11,7 +11,7 @@ use typst::geom::Color;
use typst::syntax::{FileId, Source};
use typst::World;
-use crate::args::{CompileCommand, DiagnosticFormat};
+use crate::args::{CompileCommand, DiagnosticFormat, OutputFormat};
use crate::watch::Status;
use crate::world::SystemWorld;
use crate::{color_stream, set_failed};
@@ -19,6 +19,33 @@ use crate::{color_stream, set_failed};
type CodespanResult<T> = Result<T, CodespanError>;
type CodespanError = codespan_reporting::files::Error;
+impl CompileCommand {
+ /// The output path.
+ pub fn output(&self) -> PathBuf {
+ self.output
+ .clone()
+ .unwrap_or_else(|| self.common.input.with_extension("pdf"))
+ }
+
+ /// The format to use for generated output, either specified by the user or inferred from the extension.
+ ///
+ /// Will return `Err` if the format was not specified and could not be inferred.
+ pub fn output_format(&self) -> StrResult<OutputFormat> {
+ Ok(if let Some(specified) = self.format {
+ specified
+ } else if let Some(output) = &self.output {
+ match output.extension() {
+ 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()),
+ }
+ } else {
+ OutputFormat::Pdf
+ })
+ }
+}
+
/// Execute a compilation command.
pub fn compile(mut command: CompileCommand) -> StrResult<()> {
let mut world = SystemWorld::new(&command.common)?;
@@ -97,14 +124,10 @@ pub fn compile_once(
/// Export into the target format.
fn export(document: &Document, command: &CompileCommand) -> StrResult<()> {
- match command.output().extension() {
- Some(ext) if ext.eq_ignore_ascii_case("png") => {
- export_image(document, command, ImageExportFormat::Png)
- }
- Some(ext) if ext.eq_ignore_ascii_case("svg") => {
- export_image(document, command, ImageExportFormat::Svg)
- }
- _ => export_pdf(document, command),
+ match command.output_format()? {
+ OutputFormat::Png => export_image(document, command, ImageExportFormat::Png),
+ OutputFormat::Svg => export_image(document, command, ImageExportFormat::Svg),
+ OutputFormat::Pdf => export_pdf(document, command),
}
}