mirror of
https://github.com/typst/typst
synced 2025-08-23 11:14:13 +08:00
refactor: extract methods for converting output format from/to file extension
This commit is contained in:
parent
c4f5ba84d1
commit
0369b83013
@ -1,3 +1,4 @@
|
|||||||
|
use std::ffi::OsStr;
|
||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
use std::num::NonZeroUsize;
|
use std::num::NonZeroUsize;
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
@ -443,6 +444,27 @@ pub enum OutputFormat {
|
|||||||
Html,
|
Html,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl OutputFormat {
|
||||||
|
pub fn from_file_ext(ext: &OsStr) -> Option<Self> {
|
||||||
|
match ext {
|
||||||
|
ext if ext.eq_ignore_ascii_case("pdf") => Some(OutputFormat::Pdf),
|
||||||
|
ext if ext.eq_ignore_ascii_case("png") => Some(OutputFormat::Png),
|
||||||
|
ext if ext.eq_ignore_ascii_case("svg") => Some(OutputFormat::Svg),
|
||||||
|
ext if ext.eq_ignore_ascii_case("html") => Some(OutputFormat::Html),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_file_ext(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Pdf => "pdf",
|
||||||
|
Self::Png => "png",
|
||||||
|
Self::Svg => "svg",
|
||||||
|
Self::Html => "html",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
display_possible_values!(OutputFormat);
|
display_possible_values!(OutputFormat);
|
||||||
|
|
||||||
/// Which format to use for diagnostics.
|
/// Which format to use for diagnostics.
|
||||||
|
@ -96,12 +96,9 @@ impl CompileConfig {
|
|||||||
let output_format = if let Some(specified) = args.format {
|
let output_format = if let Some(specified) = args.format {
|
||||||
specified
|
specified
|
||||||
} else if let Some(Output::Path(output)) = &args.output {
|
} else if let Some(Output::Path(output)) = &args.output {
|
||||||
match output.extension() {
|
match output.extension().and_then(OutputFormat::from_file_ext) {
|
||||||
Some(ext) if ext.eq_ignore_ascii_case("pdf") => OutputFormat::Pdf,
|
Some(format) => format,
|
||||||
Some(ext) if ext.eq_ignore_ascii_case("png") => OutputFormat::Png,
|
None => bail!(
|
||||||
Some(ext) if ext.eq_ignore_ascii_case("svg") => OutputFormat::Svg,
|
|
||||||
Some(ext) if ext.eq_ignore_ascii_case("html") => OutputFormat::Html,
|
|
||||||
_ => bail!(
|
|
||||||
"could not infer output format for path {}.\n\
|
"could not infer output format for path {}.\n\
|
||||||
consider providing the format manually with `--format/-f`",
|
consider providing the format manually with `--format/-f`",
|
||||||
output.display()
|
output.display()
|
||||||
@ -116,12 +113,7 @@ impl CompileConfig {
|
|||||||
let Input::Path(path) = &input else {
|
let Input::Path(path) = &input else {
|
||||||
panic!("output must be specified when input is from stdin, as guarded by the CLI");
|
panic!("output must be specified when input is from stdin, as guarded by the CLI");
|
||||||
};
|
};
|
||||||
Output::Path(path.with_extension(match output_format {
|
Output::Path(path.with_extension(output_format.as_file_ext()))
|
||||||
OutputFormat::Pdf => "pdf",
|
|
||||||
OutputFormat::Png => "png",
|
|
||||||
OutputFormat::Svg => "svg",
|
|
||||||
OutputFormat::Html => "html",
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
// Check if a [`Path`] has a trailing `/` (or on `windows` a `\`) character,
|
// Check if a [`Path`] has a trailing `/` (or on `windows` a `\`) character,
|
||||||
// indicating that the output should be written to `{output}/{input_file_name}.{ext}`
|
// indicating that the output should be written to `{output}/{input_file_name}.{ext}`
|
||||||
@ -146,12 +138,7 @@ impl CompileConfig {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
path.push(file_name);
|
path.push(file_name);
|
||||||
path.set_extension(match output_format {
|
path.set_extension(output_format.as_file_ext());
|
||||||
OutputFormat::Pdf => "pdf",
|
|
||||||
OutputFormat::Png => "png",
|
|
||||||
OutputFormat::Svg => "svg",
|
|
||||||
OutputFormat::Html => "html",
|
|
||||||
});
|
|
||||||
Output::Path(path)
|
Output::Path(path)
|
||||||
}
|
}
|
||||||
Some(output) => output,
|
Some(output) => output,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user