mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Clean up CLI argument parsing (#5521)
This commit is contained in:
parent
884c02872c
commit
bf1c7db6fc
@ -33,7 +33,7 @@ const AFTER_HELP: &str = color_print::cstr!("\
|
|||||||
<s>Forum for questions:</> https://forum.typst.app/
|
<s>Forum for questions:</> https://forum.typst.app/
|
||||||
");
|
");
|
||||||
|
|
||||||
/// The Typst compiler
|
/// The Typst compiler.
|
||||||
#[derive(Debug, Clone, Parser)]
|
#[derive(Debug, Clone, Parser)]
|
||||||
#[clap(
|
#[clap(
|
||||||
name = "typst",
|
name = "typst",
|
||||||
@ -44,24 +44,16 @@ const AFTER_HELP: &str = color_print::cstr!("\
|
|||||||
max_term_width = 80,
|
max_term_width = 80,
|
||||||
)]
|
)]
|
||||||
pub struct CliArguments {
|
pub struct CliArguments {
|
||||||
/// The command to run
|
/// The command to run.
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
pub command: Command,
|
pub command: Command,
|
||||||
|
|
||||||
/// Set when to use color.
|
/// Whether to use color. When set to `auto` if the terminal to supports it.
|
||||||
/// auto = use color if a capable terminal is detected
|
#[clap(long, default_value_t = ColorChoice::Auto, default_missing_value = "always")]
|
||||||
#[clap(
|
|
||||||
long,
|
|
||||||
value_name = "WHEN",
|
|
||||||
require_equals = true,
|
|
||||||
num_args = 0..=1,
|
|
||||||
default_value = "auto",
|
|
||||||
default_missing_value = "always",
|
|
||||||
)]
|
|
||||||
pub color: ColorChoice,
|
pub color: ColorChoice,
|
||||||
|
|
||||||
/// Path to a custom CA certificate to use when making network requests.
|
/// Path to a custom CA certificate to use when making network requests.
|
||||||
#[clap(long = "cert", env = "TYPST_CERT")]
|
#[clap(long, env = "TYPST_CERT")]
|
||||||
pub cert: Option<PathBuf>,
|
pub cert: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,109 +61,48 @@ pub struct CliArguments {
|
|||||||
#[derive(Debug, Clone, Subcommand)]
|
#[derive(Debug, Clone, Subcommand)]
|
||||||
#[command()]
|
#[command()]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
/// Compiles an input file into a supported output format
|
/// Compiles an input file into a supported output format.
|
||||||
#[command(visible_alias = "c")]
|
#[command(visible_alias = "c")]
|
||||||
Compile(CompileCommand),
|
Compile(CompileCommand),
|
||||||
|
|
||||||
/// Watches an input file and recompiles on changes
|
/// Watches an input file and recompiles on changes.
|
||||||
#[command(visible_alias = "w")]
|
#[command(visible_alias = "w")]
|
||||||
Watch(CompileCommand),
|
Watch(WatchCommand),
|
||||||
|
|
||||||
/// Initializes a new project from a template
|
/// Initializes a new project from a template.
|
||||||
Init(InitCommand),
|
Init(InitCommand),
|
||||||
|
|
||||||
/// Processes an input file to extract provided metadata
|
/// Processes an input file to extract provided metadata.
|
||||||
Query(QueryCommand),
|
Query(QueryCommand),
|
||||||
|
|
||||||
/// Lists all discovered fonts in system and custom font paths
|
/// Lists all discovered fonts in system and custom font paths.
|
||||||
Fonts(FontsCommand),
|
Fonts(FontsCommand),
|
||||||
|
|
||||||
/// Self update the Typst CLI
|
/// Self update the Typst CLI.
|
||||||
#[cfg_attr(not(feature = "self-update"), clap(hide = true))]
|
#[cfg_attr(not(feature = "self-update"), clap(hide = true))]
|
||||||
Update(UpdateCommand),
|
Update(UpdateCommand),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compiles an input file into a supported output format
|
/// Compiles an input file into a supported output format.
|
||||||
#[derive(Debug, Clone, Parser)]
|
#[derive(Debug, Clone, Parser)]
|
||||||
pub struct CompileCommand {
|
pub struct CompileCommand {
|
||||||
/// Shared arguments
|
/// Arguments for compilation.
|
||||||
#[clap(flatten)]
|
#[clap(flatten)]
|
||||||
pub common: SharedArgs,
|
pub args: CompileArgs,
|
||||||
|
|
||||||
/// Path to output file (PDF, PNG or SVG). Use `-` to write output to stdout.
|
|
||||||
///
|
|
||||||
/// For output formats emitting one file per page (PNG & SVG), a page number template
|
|
||||||
/// must be present if the source document renders to multiple pages. Use `{p}` for page
|
|
||||||
/// numbers, `{0p}` for zero padded page numbers and `{t}` for page count. For example,
|
|
||||||
/// `page-{0p}-of-{t}.png` creates `page-01-of-10.png`, `page-02-of-10.png` and so on.
|
|
||||||
#[clap(
|
|
||||||
required_if_eq("input", "-"),
|
|
||||||
value_parser = make_output_value_parser(),
|
|
||||||
value_hint = ValueHint::FilePath,
|
|
||||||
)]
|
|
||||||
pub output: Option<Output>,
|
|
||||||
|
|
||||||
/// Which pages to export. When unspecified, all document pages are exported.
|
|
||||||
///
|
|
||||||
/// Pages to export are separated by commas, and can be either simple page
|
|
||||||
/// numbers (e.g. '2,5' to export only pages 2 and 5) or page ranges
|
|
||||||
/// (e.g. '2,3-6,8-' to export page 2, pages 3 to 6 (inclusive), page 8 and
|
|
||||||
/// any pages after it).
|
|
||||||
///
|
|
||||||
/// Page numbers are one-indexed and correspond to real page numbers in the
|
|
||||||
/// document (therefore not being affected by the document's page counter).
|
|
||||||
#[arg(long = "pages", value_delimiter = ',')]
|
|
||||||
pub pages: Option<Vec<PageRangeArgument>>,
|
|
||||||
|
|
||||||
/// Output a Makefile rule describing the current compilation
|
|
||||||
#[clap(long = "make-deps", value_name = "PATH")]
|
|
||||||
pub make_deps: 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 with the default viewer or a specific program after
|
|
||||||
/// compilation
|
|
||||||
///
|
|
||||||
/// Ignored if output is stdout.
|
|
||||||
#[arg(long = "open", value_name = "VIEWER")]
|
|
||||||
pub open: Option<Option<String>>,
|
|
||||||
|
|
||||||
/// The PPI (pixels per inch) to use for PNG export
|
|
||||||
#[arg(long = "ppi", default_value_t = 144.0)]
|
|
||||||
pub ppi: f32,
|
|
||||||
|
|
||||||
/// Produces performance timings of the compilation process (experimental)
|
|
||||||
///
|
|
||||||
/// The resulting JSON file can be loaded into a tracing tool such as
|
|
||||||
/// https://ui.perfetto.dev. It does not contain any sensitive information
|
|
||||||
/// apart from file names and line numbers.
|
|
||||||
#[arg(long = "timings", value_name = "OUTPUT_JSON")]
|
|
||||||
pub timings: Option<Option<PathBuf>>,
|
|
||||||
|
|
||||||
/// One (or multiple comma-separated) PDF standards that Typst will enforce
|
|
||||||
/// conformance with.
|
|
||||||
#[arg(long = "pdf-standard", value_delimiter = ',')]
|
|
||||||
pub pdf_standard: Vec<PdfStandard>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A PDF standard that Typst can enforce conformance with.
|
/// Compiles an input file into a supported output format.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
|
#[derive(Debug, Clone, Parser)]
|
||||||
#[allow(non_camel_case_types)]
|
pub struct WatchCommand {
|
||||||
pub enum PdfStandard {
|
/// Arguments for compilation.
|
||||||
/// PDF 1.7.
|
#[clap(flatten)]
|
||||||
#[value(name = "1.7")]
|
pub args: CompileArgs,
|
||||||
V_1_7,
|
|
||||||
/// PDF/A-2b.
|
|
||||||
#[value(name = "a-2b")]
|
|
||||||
A_2b,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initializes a new project from a template
|
/// Initializes a new project from a template.
|
||||||
#[derive(Debug, Clone, Parser)]
|
#[derive(Debug, Clone, Parser)]
|
||||||
pub struct InitCommand {
|
pub struct InitCommand {
|
||||||
/// The template to use, e.g. `@preview/charged-ieee`
|
/// The template to use, e.g. `@preview/charged-ieee`.
|
||||||
///
|
///
|
||||||
/// You can specify the version by appending e.g. `:0.1.0`. If no version is
|
/// You can specify the version by appending e.g. `:0.1.0`. If no version is
|
||||||
/// specified, Typst will default to the latest version.
|
/// specified, Typst will default to the latest version.
|
||||||
@ -179,34 +110,34 @@ pub struct InitCommand {
|
|||||||
/// Supports both local and published templates.
|
/// Supports both local and published templates.
|
||||||
pub template: String,
|
pub template: String,
|
||||||
|
|
||||||
/// The project directory, defaults to the template's name
|
/// The project directory, defaults to the template's name.
|
||||||
pub dir: Option<String>,
|
pub dir: Option<String>,
|
||||||
|
|
||||||
/// Arguments related to storage of packages in the system
|
/// Arguments related to storage of packages in the system.
|
||||||
#[clap(flatten)]
|
#[clap(flatten)]
|
||||||
pub package_storage_args: PackageStorageArgs,
|
pub package: PackageArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Processes an input file to extract provided metadata
|
/// Processes an input file to extract provided metadata.
|
||||||
#[derive(Debug, Clone, Parser)]
|
#[derive(Debug, Clone, Parser)]
|
||||||
pub struct QueryCommand {
|
pub struct QueryCommand {
|
||||||
/// Shared arguments
|
/// Path to input Typst file. Use `-` to read input from stdin.
|
||||||
#[clap(flatten)]
|
#[clap(value_parser = input_value_parser(), value_hint = ValueHint::FilePath)]
|
||||||
pub common: SharedArgs,
|
pub input: Input,
|
||||||
|
|
||||||
/// Defines which elements to retrieve
|
/// Defines which elements to retrieve.
|
||||||
pub selector: String,
|
pub selector: String,
|
||||||
|
|
||||||
/// Extracts just one field from all retrieved elements
|
/// Extracts just one field from all retrieved elements.
|
||||||
#[clap(long = "field")]
|
#[clap(long = "field")]
|
||||||
pub field: Option<String>,
|
pub field: Option<String>,
|
||||||
|
|
||||||
/// Expects and retrieves exactly one element
|
/// Expects and retrieves exactly one element.
|
||||||
#[clap(long = "one", default_value = "false")]
|
#[clap(long = "one", default_value = "false")]
|
||||||
pub one: bool,
|
pub one: bool,
|
||||||
|
|
||||||
/// The format to serialize in
|
/// The format to serialize in.
|
||||||
#[clap(long = "format", default_value = "json")]
|
#[clap(long = "format", default_value_t)]
|
||||||
pub format: SerializationFormat,
|
pub format: SerializationFormat,
|
||||||
|
|
||||||
/// Whether to pretty-print the serialized output.
|
/// Whether to pretty-print the serialized output.
|
||||||
@ -214,38 +145,153 @@ pub struct QueryCommand {
|
|||||||
/// Only applies to JSON format.
|
/// Only applies to JSON format.
|
||||||
#[clap(long)]
|
#[clap(long)]
|
||||||
pub pretty: bool,
|
pub pretty: bool,
|
||||||
|
|
||||||
|
/// World arguments.
|
||||||
|
#[clap(flatten)]
|
||||||
|
pub world: WorldArgs,
|
||||||
|
|
||||||
|
/// Processing arguments.
|
||||||
|
#[clap(flatten)]
|
||||||
|
pub process: ProcessArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output file format for query command
|
/// Lists all discovered fonts in system and custom font paths.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
|
#[derive(Debug, Clone, Parser)]
|
||||||
pub enum SerializationFormat {
|
pub struct FontsCommand {
|
||||||
Json,
|
/// Common font arguments.
|
||||||
Yaml,
|
#[clap(flatten)]
|
||||||
|
pub font: FontArgs,
|
||||||
|
|
||||||
|
/// Also lists style variants of each font family.
|
||||||
|
#[arg(long)]
|
||||||
|
pub variants: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Common arguments of compile, watch, and query.
|
/// Update the CLI using a pre-compiled binary from a Typst GitHub release.
|
||||||
|
#[derive(Debug, Clone, Parser)]
|
||||||
|
pub struct UpdateCommand {
|
||||||
|
/// Which version to update to (defaults to latest).
|
||||||
|
pub version: Option<Version>,
|
||||||
|
|
||||||
|
/// Forces a downgrade to an older version (required for downgrading).
|
||||||
|
#[clap(long, default_value_t = false)]
|
||||||
|
pub force: bool,
|
||||||
|
|
||||||
|
/// Reverts to the version from before the last update (only possible if
|
||||||
|
/// `typst update` has previously ran).
|
||||||
|
#[clap(
|
||||||
|
long,
|
||||||
|
default_value_t = false,
|
||||||
|
conflicts_with = "version",
|
||||||
|
conflicts_with = "force"
|
||||||
|
)]
|
||||||
|
pub revert: bool,
|
||||||
|
|
||||||
|
/// Custom path to the backup file created on update and used by `--revert`,
|
||||||
|
/// defaults to system-dependent location
|
||||||
|
#[clap(long = "backup-path", env = "TYPST_UPDATE_BACKUP_PATH", value_name = "FILE")]
|
||||||
|
pub backup_path: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Arguments for compilation and watching.
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct SharedArgs {
|
pub struct CompileArgs {
|
||||||
/// Path to input Typst file. Use `-` to read input from stdin
|
/// Path to input Typst file. Use `-` to read input from stdin.
|
||||||
#[clap(value_parser = make_input_value_parser(), value_hint = ValueHint::FilePath)]
|
#[clap(value_parser = input_value_parser(), value_hint = ValueHint::FilePath)]
|
||||||
pub input: Input,
|
pub input: Input,
|
||||||
|
|
||||||
/// Configures the project root (for absolute paths)
|
/// Path to output file (PDF, PNG, SVG, or HTML). Use `-` to write output to
|
||||||
|
/// stdout.
|
||||||
|
///
|
||||||
|
/// For output formats emitting one file per page (PNG & SVG), a page number
|
||||||
|
/// template must be present if the source document renders to multiple
|
||||||
|
/// pages. Use `{p}` for page numbers, `{0p}` for zero padded page numbers
|
||||||
|
/// and `{t}` for page count. For example, `page-{0p}-of-{t}.png` creates
|
||||||
|
/// `page-01-of-10.png`, `page-02-of-10.png`, and so on.
|
||||||
|
#[clap(
|
||||||
|
required_if_eq("input", "-"),
|
||||||
|
value_parser = output_value_parser(),
|
||||||
|
value_hint = ValueHint::FilePath,
|
||||||
|
)]
|
||||||
|
pub output: Option<Output>,
|
||||||
|
|
||||||
|
/// The format of the output file, inferred from the extension by default.
|
||||||
|
#[arg(long = "format", short = 'f')]
|
||||||
|
pub format: Option<OutputFormat>,
|
||||||
|
|
||||||
|
/// World arguments.
|
||||||
|
#[clap(flatten)]
|
||||||
|
pub world: WorldArgs,
|
||||||
|
|
||||||
|
/// Which pages to export. When unspecified, all pages are exported.
|
||||||
|
///
|
||||||
|
/// Pages to export are separated by commas, and can be either simple page
|
||||||
|
/// numbers (e.g. '2,5' to export only pages 2 and 5) or page ranges (e.g.
|
||||||
|
/// '2,3-6,8-' to export page 2, pages 3 to 6 (inclusive), page 8 and any
|
||||||
|
/// pages after it).
|
||||||
|
///
|
||||||
|
/// Page numbers are one-indexed and correspond to physical page numbers in
|
||||||
|
/// the document (therefore not being affected by the document's page
|
||||||
|
/// counter).
|
||||||
|
#[arg(long = "pages", value_delimiter = ',')]
|
||||||
|
pub pages: Option<Vec<Pages>>,
|
||||||
|
|
||||||
|
/// One (or multiple comma-separated) PDF standards that Typst will enforce
|
||||||
|
/// conformance with.
|
||||||
|
#[arg(long = "pdf-standard", value_delimiter = ',')]
|
||||||
|
pub pdf_standard: Vec<PdfStandard>,
|
||||||
|
|
||||||
|
/// The PPI (pixels per inch) to use for PNG export.
|
||||||
|
#[arg(long = "ppi", default_value_t = 144.0)]
|
||||||
|
pub ppi: f32,
|
||||||
|
|
||||||
|
/// File path to which a Makefile with the current compilation's
|
||||||
|
/// dependencies will be written.
|
||||||
|
#[clap(long = "make-deps", value_name = "PATH")]
|
||||||
|
pub make_deps: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Processing arguments.
|
||||||
|
#[clap(flatten)]
|
||||||
|
pub process: ProcessArgs,
|
||||||
|
|
||||||
|
/// Opens the output file with the default viewer or a specific program
|
||||||
|
/// after compilation. Ignored if output is stdout.
|
||||||
|
#[arg(long = "open", value_name = "VIEWER")]
|
||||||
|
pub open: Option<Option<String>>,
|
||||||
|
|
||||||
|
/// Produces performance timings of the compilation process. (experimental)
|
||||||
|
///
|
||||||
|
/// The resulting JSON file can be loaded into a tracing tool such as
|
||||||
|
/// https://ui.perfetto.dev. It does not contain any sensitive information
|
||||||
|
/// apart from file names and line numbers.
|
||||||
|
#[arg(long = "timings", value_name = "OUTPUT_JSON")]
|
||||||
|
pub timings: Option<Option<PathBuf>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Arguments for the construction of a world. Shared by compile, watch, and
|
||||||
|
/// query.
|
||||||
|
#[derive(Debug, Clone, Args)]
|
||||||
|
pub struct WorldArgs {
|
||||||
|
/// Configures the project root (for absolute paths).
|
||||||
#[clap(long = "root", env = "TYPST_ROOT", value_name = "DIR")]
|
#[clap(long = "root", env = "TYPST_ROOT", value_name = "DIR")]
|
||||||
pub root: Option<PathBuf>,
|
pub root: Option<PathBuf>,
|
||||||
|
|
||||||
/// Add a string key-value pair visible through `sys.inputs`
|
/// Add a string key-value pair visible through `sys.inputs`.
|
||||||
#[clap(
|
#[clap(
|
||||||
long = "input",
|
long = "input",
|
||||||
value_name = "key=value",
|
value_name = "key=value",
|
||||||
action = ArgAction::Append,
|
action = ArgAction::Append,
|
||||||
value_parser = ValueParser::new(parse_input_pair),
|
value_parser = ValueParser::new(parse_sys_input_pair),
|
||||||
)]
|
)]
|
||||||
pub inputs: Vec<(String, String)>,
|
pub inputs: Vec<(String, String)>,
|
||||||
|
|
||||||
/// Common font arguments
|
/// Common font arguments.
|
||||||
#[clap(flatten)]
|
#[clap(flatten)]
|
||||||
pub font_args: FontArgs,
|
pub font: FontArgs,
|
||||||
|
|
||||||
|
/// Arguments related to storage of packages in the system.
|
||||||
|
#[clap(flatten)]
|
||||||
|
pub package: PackageArgs,
|
||||||
|
|
||||||
/// The document's creation date formatted as a UNIX timestamp.
|
/// The document's creation date formatted as a UNIX timestamp.
|
||||||
///
|
///
|
||||||
@ -257,21 +303,13 @@ pub struct SharedArgs {
|
|||||||
value_parser = parse_source_date_epoch,
|
value_parser = parse_source_date_epoch,
|
||||||
)]
|
)]
|
||||||
pub creation_timestamp: Option<DateTime<Utc>>,
|
pub creation_timestamp: Option<DateTime<Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
/// The format to emit diagnostics in
|
/// Arguments for configuration the process of compilation itself.
|
||||||
#[clap(
|
#[derive(Debug, Clone, Args)]
|
||||||
long,
|
pub struct ProcessArgs {
|
||||||
default_value_t = DiagnosticFormat::Human,
|
/// Number of parallel jobs spawned during compilation. Defaults to number
|
||||||
value_parser = clap::value_parser!(DiagnosticFormat)
|
/// of CPUs. Setting it to 1 disables parallelism.
|
||||||
)]
|
|
||||||
pub diagnostic_format: DiagnosticFormat,
|
|
||||||
|
|
||||||
/// Arguments related to storage of packages in the system
|
|
||||||
#[clap(flatten)]
|
|
||||||
pub package_storage_args: PackageStorageArgs,
|
|
||||||
|
|
||||||
/// Number of parallel jobs spawned during compilation,
|
|
||||||
/// defaults to number of CPUs. Setting it to 1 disables parallelism.
|
|
||||||
#[clap(long, short)]
|
#[clap(long, short)]
|
||||||
pub jobs: Option<usize>,
|
pub jobs: Option<usize>,
|
||||||
|
|
||||||
@ -279,22 +317,20 @@ pub struct SharedArgs {
|
|||||||
/// time.
|
/// time.
|
||||||
#[arg(long = "feature", value_delimiter = ',')]
|
#[arg(long = "feature", value_delimiter = ',')]
|
||||||
pub feature: Vec<Feature>,
|
pub feature: Vec<Feature>,
|
||||||
}
|
|
||||||
|
|
||||||
/// An in-development feature that may be changed or removed at any time.
|
/// The format to emit diagnostics in.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
|
#[clap(long, default_value_t)]
|
||||||
pub enum Feature {
|
pub diagnostic_format: DiagnosticFormat,
|
||||||
Html,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Arguments related to where packages are stored in the system.
|
/// Arguments related to where packages are stored in the system.
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct PackageStorageArgs {
|
pub struct PackageArgs {
|
||||||
/// Custom path to local packages, defaults to system-dependent location
|
/// Custom path to local packages, defaults to system-dependent location.
|
||||||
#[clap(long = "package-path", env = "TYPST_PACKAGE_PATH", value_name = "DIR")]
|
#[clap(long = "package-path", env = "TYPST_PACKAGE_PATH", value_name = "DIR")]
|
||||||
pub package_path: Option<PathBuf>,
|
pub package_path: Option<PathBuf>,
|
||||||
|
|
||||||
/// Custom path to package cache, defaults to system-dependent location
|
/// Custom path to package cache, defaults to system-dependent location.
|
||||||
#[clap(
|
#[clap(
|
||||||
long = "package-cache-path",
|
long = "package-cache-path",
|
||||||
env = "TYPST_PACKAGE_CACHE_PATH",
|
env = "TYPST_PACKAGE_CACHE_PATH",
|
||||||
@ -303,13 +339,38 @@ pub struct PackageStorageArgs {
|
|||||||
pub package_cache_path: Option<PathBuf>,
|
pub package_cache_path: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a UNIX timestamp according to <https://reproducible-builds.org/specs/source-date-epoch/>
|
/// Common arguments to customize available fonts
|
||||||
fn parse_source_date_epoch(raw: &str) -> Result<DateTime<Utc>, String> {
|
#[derive(Debug, Clone, Parser)]
|
||||||
let timestamp: i64 = raw
|
pub struct FontArgs {
|
||||||
.parse()
|
/// Adds additional directories that are recursively searched for fonts.
|
||||||
.map_err(|err| format!("timestamp must be decimal integer ({err})"))?;
|
///
|
||||||
DateTime::from_timestamp(timestamp, 0)
|
/// If multiple paths are specified, they are separated by the system's path
|
||||||
.ok_or_else(|| "timestamp out of range".to_string())
|
/// separator (`:` on Unix-like systems and `;` on Windows).
|
||||||
|
#[clap(
|
||||||
|
long = "font-path",
|
||||||
|
env = "TYPST_FONT_PATHS",
|
||||||
|
value_name = "DIR",
|
||||||
|
value_delimiter = ENV_PATH_SEP,
|
||||||
|
)]
|
||||||
|
pub font_paths: Vec<PathBuf>,
|
||||||
|
|
||||||
|
/// Ensures system fonts won't be searched, unless explicitly included via
|
||||||
|
/// `--font-path`.
|
||||||
|
#[arg(long)]
|
||||||
|
pub ignore_system_fonts: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! display_possible_values {
|
||||||
|
($ty:ty) => {
|
||||||
|
impl Display for $ty {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
|
self.to_possible_value()
|
||||||
|
.expect("no values are skipped")
|
||||||
|
.get_name()
|
||||||
|
.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An input that is either stdin or a real path.
|
/// An input that is either stdin or a real path.
|
||||||
@ -321,6 +382,15 @@ pub enum Input {
|
|||||||
Path(PathBuf),
|
Path(PathBuf),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for Input {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Input::Stdin => f.pad("stdin"),
|
||||||
|
Input::Path(path) => path.display().fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An output that is either stdout or a real path.
|
/// An output that is either stdout or a real path.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Output {
|
pub enum Output {
|
||||||
@ -339,8 +409,105 @@ impl Display for Output {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Which format to use for the generated output file.
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
|
||||||
|
pub enum OutputFormat {
|
||||||
|
Pdf,
|
||||||
|
Png,
|
||||||
|
Svg,
|
||||||
|
Html,
|
||||||
|
}
|
||||||
|
|
||||||
|
display_possible_values!(OutputFormat);
|
||||||
|
|
||||||
|
/// Which format to use for diagnostics.
|
||||||
|
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
|
||||||
|
pub enum DiagnosticFormat {
|
||||||
|
#[default]
|
||||||
|
Human,
|
||||||
|
Short,
|
||||||
|
}
|
||||||
|
|
||||||
|
display_possible_values!(DiagnosticFormat);
|
||||||
|
|
||||||
|
/// An in-development feature that may be changed or removed at any time.
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
|
||||||
|
pub enum Feature {
|
||||||
|
Html,
|
||||||
|
}
|
||||||
|
|
||||||
|
display_possible_values!(Feature);
|
||||||
|
|
||||||
|
/// A PDF standard that Typst can enforce conformance with.
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
pub enum PdfStandard {
|
||||||
|
/// PDF 1.7.
|
||||||
|
#[value(name = "1.7")]
|
||||||
|
V_1_7,
|
||||||
|
/// PDF/A-2b.
|
||||||
|
#[value(name = "a-2b")]
|
||||||
|
A_2b,
|
||||||
|
}
|
||||||
|
|
||||||
|
display_possible_values!(PdfStandard);
|
||||||
|
|
||||||
|
// Output file format for query command
|
||||||
|
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, ValueEnum)]
|
||||||
|
pub enum SerializationFormat {
|
||||||
|
#[default]
|
||||||
|
Json,
|
||||||
|
Yaml,
|
||||||
|
}
|
||||||
|
|
||||||
|
display_possible_values!(SerializationFormat);
|
||||||
|
|
||||||
|
/// Implements parsing of page ranges (`1-3`, `4`, `5-`, `-2`), used by the
|
||||||
|
/// `CompileCommand.pages` argument, through the `FromStr` trait instead of a
|
||||||
|
/// value parser, in order to generate better errors.
|
||||||
|
///
|
||||||
|
/// See also: https://github.com/clap-rs/clap/issues/5065
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Pages(pub RangeInclusive<Option<NonZeroUsize>>);
|
||||||
|
|
||||||
|
impl FromStr for Pages {
|
||||||
|
type Err = &'static str;
|
||||||
|
|
||||||
|
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||||
|
match value.split('-').map(str::trim).collect::<Vec<_>>().as_slice() {
|
||||||
|
[] | [""] => Err("page export range must not be empty"),
|
||||||
|
[single_page] => {
|
||||||
|
let page_number = parse_page_number(single_page)?;
|
||||||
|
Ok(Pages(Some(page_number)..=Some(page_number)))
|
||||||
|
}
|
||||||
|
["", ""] => Err("page export range must have start or end"),
|
||||||
|
[start, ""] => Ok(Pages(Some(parse_page_number(start)?)..=None)),
|
||||||
|
["", end] => Ok(Pages(None..=Some(parse_page_number(end)?))),
|
||||||
|
[start, end] => {
|
||||||
|
let start = parse_page_number(start)?;
|
||||||
|
let end = parse_page_number(end)?;
|
||||||
|
if start > end {
|
||||||
|
Err("page export range must end at a page after the start")
|
||||||
|
} else {
|
||||||
|
Ok(Pages(Some(start)..=Some(end)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[_, _, _, ..] => Err("page export range must have a single hyphen"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses a single page number.
|
||||||
|
fn parse_page_number(value: &str) -> Result<NonZeroUsize, &'static str> {
|
||||||
|
if value == "0" {
|
||||||
|
Err("page numbers start at one")
|
||||||
|
} else {
|
||||||
|
NonZeroUsize::from_str(value).map_err(|_| "not a valid page number")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The clap value parser used by `SharedArgs.input`
|
/// The clap value parser used by `SharedArgs.input`
|
||||||
fn make_input_value_parser() -> impl TypedValueParser<Value = Input> {
|
fn input_value_parser() -> impl TypedValueParser<Value = Input> {
|
||||||
clap::builder::OsStringValueParser::new().try_map(|value| {
|
clap::builder::OsStringValueParser::new().try_map(|value| {
|
||||||
if value.is_empty() {
|
if value.is_empty() {
|
||||||
Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
|
Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
|
||||||
@ -353,7 +520,7 @@ fn make_input_value_parser() -> impl TypedValueParser<Value = Input> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The clap value parser used by `CompileCommand.output`
|
/// The clap value parser used by `CompileCommand.output`
|
||||||
fn make_output_value_parser() -> impl TypedValueParser<Value = Output> {
|
fn output_value_parser() -> impl TypedValueParser<Value = Output> {
|
||||||
clap::builder::OsStringValueParser::new().try_map(|value| {
|
clap::builder::OsStringValueParser::new().try_map(|value| {
|
||||||
// Empty value also handled by clap for `Option<Output>`
|
// Empty value also handled by clap for `Option<Output>`
|
||||||
if value.is_empty() {
|
if value.is_empty() {
|
||||||
@ -370,7 +537,7 @@ fn make_output_value_parser() -> impl TypedValueParser<Value = Output> {
|
|||||||
///
|
///
|
||||||
/// This function will return an error if the argument contains no equals sign
|
/// This function will return an error if the argument contains no equals sign
|
||||||
/// or contains the key (before the equals sign) is empty.
|
/// or contains the key (before the equals sign) is empty.
|
||||||
fn parse_input_pair(raw: &str) -> Result<(String, String), String> {
|
fn parse_sys_input_pair(raw: &str) -> Result<(String, String), String> {
|
||||||
let (key, val) = raw
|
let (key, val) = raw
|
||||||
.split_once('=')
|
.split_once('=')
|
||||||
.ok_or("input must be a key and a value separated by an equal sign")?;
|
.ok_or("input must be a key and a value separated by an equal sign")?;
|
||||||
@ -382,144 +549,11 @@ fn parse_input_pair(raw: &str) -> Result<(String, String), String> {
|
|||||||
Ok((key, val))
|
Ok((key, val))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Implements parsing of page ranges (`1-3`, `4`, `5-`, `-2`), used by the
|
/// Parses a UNIX timestamp according to <https://reproducible-builds.org/specs/source-date-epoch/>
|
||||||
/// `CompileCommand.pages` argument, through the `FromStr` trait instead of
|
fn parse_source_date_epoch(raw: &str) -> Result<DateTime<Utc>, String> {
|
||||||
/// a value parser, in order to generate better errors.
|
let timestamp: i64 = raw
|
||||||
///
|
.parse()
|
||||||
/// See also: https://github.com/clap-rs/clap/issues/5065
|
.map_err(|err| format!("timestamp must be decimal integer ({err})"))?;
|
||||||
#[derive(Debug, Clone)]
|
DateTime::from_timestamp(timestamp, 0)
|
||||||
pub struct PageRangeArgument(RangeInclusive<Option<NonZeroUsize>>);
|
.ok_or_else(|| "timestamp out of range".to_string())
|
||||||
|
|
||||||
impl PageRangeArgument {
|
|
||||||
pub fn to_range(&self) -> RangeInclusive<Option<NonZeroUsize>> {
|
|
||||||
self.0.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for PageRangeArgument {
|
|
||||||
type Err = &'static str;
|
|
||||||
|
|
||||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
|
||||||
match value.split('-').map(str::trim).collect::<Vec<_>>().as_slice() {
|
|
||||||
[] | [""] => Err("page export range must not be empty"),
|
|
||||||
[single_page] => {
|
|
||||||
let page_number = parse_page_number(single_page)?;
|
|
||||||
Ok(PageRangeArgument(Some(page_number)..=Some(page_number)))
|
|
||||||
}
|
|
||||||
["", ""] => Err("page export range must have start or end"),
|
|
||||||
[start, ""] => Ok(PageRangeArgument(Some(parse_page_number(start)?)..=None)),
|
|
||||||
["", end] => Ok(PageRangeArgument(None..=Some(parse_page_number(end)?))),
|
|
||||||
[start, end] => {
|
|
||||||
let start = parse_page_number(start)?;
|
|
||||||
let end = parse_page_number(end)?;
|
|
||||||
if start > end {
|
|
||||||
Err("page export range must end at a page after the start")
|
|
||||||
} else {
|
|
||||||
Ok(PageRangeArgument(Some(start)..=Some(end)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[_, _, _, ..] => Err("page export range must have a single hyphen"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_page_number(value: &str) -> Result<NonZeroUsize, &'static str> {
|
|
||||||
if value == "0" {
|
|
||||||
Err("page numbers start at one")
|
|
||||||
} else {
|
|
||||||
NonZeroUsize::from_str(value).map_err(|_| "not a valid page number")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Lists all discovered fonts in system and custom font paths
|
|
||||||
#[derive(Debug, Clone, Parser)]
|
|
||||||
pub struct FontsCommand {
|
|
||||||
/// Common font arguments
|
|
||||||
#[clap(flatten)]
|
|
||||||
pub font_args: FontArgs,
|
|
||||||
|
|
||||||
/// Also lists style variants of each font family
|
|
||||||
#[arg(long)]
|
|
||||||
pub variants: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Common arguments to customize available fonts
|
|
||||||
#[derive(Debug, Clone, Parser)]
|
|
||||||
pub struct FontArgs {
|
|
||||||
/// Adds additional directories that are recursively searched for fonts
|
|
||||||
///
|
|
||||||
/// If multiple paths are specified, they are separated by the system's path
|
|
||||||
/// separator (`:` on Unix-like systems and `;` on Windows).
|
|
||||||
#[clap(
|
|
||||||
long = "font-path",
|
|
||||||
env = "TYPST_FONT_PATHS",
|
|
||||||
value_name = "DIR",
|
|
||||||
value_delimiter = ENV_PATH_SEP,
|
|
||||||
)]
|
|
||||||
pub font_paths: Vec<PathBuf>,
|
|
||||||
|
|
||||||
/// Ensures system fonts won't be searched, unless explicitly included via
|
|
||||||
/// `--font-path`
|
|
||||||
#[arg(long)]
|
|
||||||
pub ignore_system_fonts: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Which format to use for diagnostics.
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
|
|
||||||
pub enum DiagnosticFormat {
|
|
||||||
Human,
|
|
||||||
Short,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for DiagnosticFormat {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
||||||
self.to_possible_value()
|
|
||||||
.expect("no values are skipped")
|
|
||||||
.get_name()
|
|
||||||
.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Update the CLI using a pre-compiled binary from a Typst GitHub release.
|
|
||||||
#[derive(Debug, Clone, Parser)]
|
|
||||||
pub struct UpdateCommand {
|
|
||||||
/// Which version to update to (defaults to latest)
|
|
||||||
pub version: Option<Version>,
|
|
||||||
|
|
||||||
/// Forces a downgrade to an older version (required for downgrading)
|
|
||||||
#[clap(long, default_value_t = false)]
|
|
||||||
pub force: bool,
|
|
||||||
|
|
||||||
/// Reverts to the version from before the last update (only possible if
|
|
||||||
/// `typst update` has previously ran)
|
|
||||||
#[clap(
|
|
||||||
long,
|
|
||||||
default_value_t = false,
|
|
||||||
conflicts_with = "version",
|
|
||||||
conflicts_with = "force"
|
|
||||||
)]
|
|
||||||
pub revert: bool,
|
|
||||||
|
|
||||||
/// Custom path to the backup file created on update and used by `--revert`,
|
|
||||||
/// defaults to system-dependent location
|
|
||||||
#[clap(long = "backup-path", env = "TYPST_UPDATE_BACKUP_PATH", value_name = "FILE")]
|
|
||||||
pub backup_path: Option<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Which format to use for the generated output file.
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
|
|
||||||
pub enum OutputFormat {
|
|
||||||
Pdf,
|
|
||||||
Png,
|
|
||||||
Svg,
|
|
||||||
Html,
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use std::fs::{self, File};
|
|||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use chrono::{Datelike, Timelike};
|
use chrono::{DateTime, Datelike, Timelike, Utc};
|
||||||
use codespan_reporting::diagnostic::{Diagnostic, Label};
|
use codespan_reporting::diagnostic::{Diagnostic, Label};
|
||||||
use codespan_reporting::term;
|
use codespan_reporting::term;
|
||||||
use ecow::{eco_format, EcoString};
|
use ecow::{eco_format, EcoString};
|
||||||
@ -19,10 +19,11 @@ use typst::WorldExt;
|
|||||||
use typst_pdf::{PdfOptions, PdfStandards};
|
use typst_pdf::{PdfOptions, PdfStandards};
|
||||||
|
|
||||||
use crate::args::{
|
use crate::args::{
|
||||||
CompileCommand, DiagnosticFormat, Input, Output, OutputFormat, PageRangeArgument,
|
CompileArgs, CompileCommand, DiagnosticFormat, Input, Output, OutputFormat,
|
||||||
PdfStandard,
|
PdfStandard,
|
||||||
};
|
};
|
||||||
use crate::timings::Timer;
|
use crate::timings::Timer;
|
||||||
|
|
||||||
use crate::watch::Status;
|
use crate::watch::Status;
|
||||||
use crate::world::SystemWorld;
|
use crate::world::SystemWorld;
|
||||||
use crate::{set_failed, terminal};
|
use crate::{set_failed, terminal};
|
||||||
@ -30,31 +31,49 @@ use crate::{set_failed, terminal};
|
|||||||
type CodespanResult<T> = Result<T, CodespanError>;
|
type CodespanResult<T> = Result<T, CodespanError>;
|
||||||
type CodespanError = codespan_reporting::files::Error;
|
type CodespanError = codespan_reporting::files::Error;
|
||||||
|
|
||||||
impl CompileCommand {
|
/// Execute a compilation command.
|
||||||
/// The output path.
|
pub fn compile(timer: &mut Timer, command: &CompileCommand) -> StrResult<()> {
|
||||||
pub fn output(&self) -> Output {
|
let mut config = CompileConfig::new(&command.args)?;
|
||||||
self.output.clone().unwrap_or_else(|| {
|
let mut world =
|
||||||
let Input::Path(path) = &self.common.input else {
|
SystemWorld::new(&command.args.input, &command.args.world, &command.args.process)
|
||||||
panic!("output must be specified when input is from stdin, as guarded by the CLI");
|
.map_err(|err| eco_format!("{err}"))?;
|
||||||
};
|
timer.record(&mut world, |world| compile_once(world, &mut config, false))?
|
||||||
Output::Path(path.with_extension(
|
}
|
||||||
match self.output_format().unwrap_or(OutputFormat::Pdf) {
|
|
||||||
OutputFormat::Pdf => "pdf",
|
|
||||||
OutputFormat::Png => "png",
|
|
||||||
OutputFormat::Svg => "svg",
|
|
||||||
OutputFormat::Html => "html",
|
|
||||||
},
|
|
||||||
))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The format to use for generated output, either specified by the user or inferred from the extension.
|
/// A preprocessed `CompileCommand`.
|
||||||
///
|
pub struct CompileConfig {
|
||||||
/// Will return `Err` if the format was not specified and could not be inferred.
|
/// Path to input Typst file or stdin.
|
||||||
pub fn output_format(&self) -> StrResult<OutputFormat> {
|
pub input: Input,
|
||||||
Ok(if let Some(specified) = self.format {
|
/// Path to output file (PDF, PNG, SVG, or HTML).
|
||||||
|
pub output: Output,
|
||||||
|
/// The format of the output file.
|
||||||
|
pub output_format: OutputFormat,
|
||||||
|
/// Which pages to export.
|
||||||
|
pub pages: Option<PageRanges>,
|
||||||
|
/// The document's creation date formatted as a UNIX timestamp.
|
||||||
|
pub creation_timestamp: Option<DateTime<Utc>>,
|
||||||
|
/// The format to emit diagnostics in.
|
||||||
|
pub diagnostic_format: DiagnosticFormat,
|
||||||
|
/// Opens the output file with the default viewer or a specific program after
|
||||||
|
/// compilation.
|
||||||
|
pub open: Option<Option<String>>,
|
||||||
|
/// One (or multiple comma-separated) PDF standards that Typst will enforce
|
||||||
|
/// conformance with.
|
||||||
|
pub pdf_standards: PdfStandards,
|
||||||
|
/// A path to write a Makefile rule describing the current compilation.
|
||||||
|
pub make_deps: Option<PathBuf>,
|
||||||
|
/// The PPI (pixels per inch) to use for PNG export.
|
||||||
|
pub ppi: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompileConfig {
|
||||||
|
/// Preprocess a `CompileCommand`, producing a compilation config.
|
||||||
|
pub fn new(args: &CompileArgs) -> StrResult<Self> {
|
||||||
|
let input = args.input.clone();
|
||||||
|
|
||||||
|
let output_format = if let Some(specified) = args.format {
|
||||||
specified
|
specified
|
||||||
} else if let Some(Output::Path(output)) = &self.output {
|
} else if let Some(Output::Path(output)) = &args.output {
|
||||||
match output.extension() {
|
match output.extension() {
|
||||||
Some(ext) if ext.eq_ignore_ascii_case("pdf") => OutputFormat::Pdf,
|
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("png") => OutputFormat::Png,
|
||||||
@ -68,40 +87,51 @@ impl CompileCommand {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
OutputFormat::Pdf
|
OutputFormat::Pdf
|
||||||
|
};
|
||||||
|
|
||||||
|
let output = args.output.clone().unwrap_or_else(|| {
|
||||||
|
let Input::Path(path) = &input else {
|
||||||
|
panic!("output must be specified when input is from stdin, as guarded by the CLI");
|
||||||
|
};
|
||||||
|
Output::Path(path.with_extension(
|
||||||
|
match output_format {
|
||||||
|
OutputFormat::Pdf => "pdf",
|
||||||
|
OutputFormat::Png => "png",
|
||||||
|
OutputFormat::Svg => "svg",
|
||||||
|
OutputFormat::Html => "html",
|
||||||
|
},
|
||||||
|
))
|
||||||
|
});
|
||||||
|
|
||||||
|
let pages = args.pages.as_ref().map(|export_ranges| {
|
||||||
|
PageRanges::new(export_ranges.iter().map(|r| r.0.clone()).collect())
|
||||||
|
});
|
||||||
|
|
||||||
|
let pdf_standards = {
|
||||||
|
let list = args
|
||||||
|
.pdf_standard
|
||||||
|
.iter()
|
||||||
|
.map(|standard| match standard {
|
||||||
|
PdfStandard::V_1_7 => typst_pdf::PdfStandard::V_1_7,
|
||||||
|
PdfStandard::A_2b => typst_pdf::PdfStandard::A_2b,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
PdfStandards::new(&list)?
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
input,
|
||||||
|
output,
|
||||||
|
output_format,
|
||||||
|
pages,
|
||||||
|
pdf_standards,
|
||||||
|
creation_timestamp: args.world.creation_timestamp,
|
||||||
|
make_deps: args.make_deps.clone(),
|
||||||
|
ppi: args.ppi,
|
||||||
|
diagnostic_format: args.process.diagnostic_format,
|
||||||
|
open: args.open.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The ranges of the pages to be exported as specified by the user.
|
|
||||||
///
|
|
||||||
/// This returns `None` if all pages should be exported.
|
|
||||||
pub fn exported_page_ranges(&self) -> Option<PageRanges> {
|
|
||||||
self.pages.as_ref().map(|export_ranges| {
|
|
||||||
PageRanges::new(
|
|
||||||
export_ranges.iter().map(PageRangeArgument::to_range).collect(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The PDF standards to try to conform with.
|
|
||||||
pub fn pdf_standards(&self) -> StrResult<PdfStandards> {
|
|
||||||
let list = self
|
|
||||||
.pdf_standard
|
|
||||||
.iter()
|
|
||||||
.map(|standard| match standard {
|
|
||||||
PdfStandard::V_1_7 => typst_pdf::PdfStandard::V_1_7,
|
|
||||||
PdfStandard::A_2b => typst_pdf::PdfStandard::A_2b,
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
PdfStandards::new(&list)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Execute a compilation command.
|
|
||||||
pub fn compile(mut timer: Timer, mut command: CompileCommand) -> StrResult<()> {
|
|
||||||
let mut world =
|
|
||||||
SystemWorld::new(&command.common).map_err(|err| eco_format!("{err}"))?;
|
|
||||||
timer.record(&mut world, |world| compile_once(world, &mut command, false))??;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a single time.
|
/// Compile a single time.
|
||||||
@ -110,17 +140,15 @@ pub fn compile(mut timer: Timer, mut command: CompileCommand) -> StrResult<()> {
|
|||||||
#[typst_macros::time(name = "compile once")]
|
#[typst_macros::time(name = "compile once")]
|
||||||
pub fn compile_once(
|
pub fn compile_once(
|
||||||
world: &mut SystemWorld,
|
world: &mut SystemWorld,
|
||||||
command: &mut CompileCommand,
|
config: &mut CompileConfig,
|
||||||
watching: bool,
|
watching: bool,
|
||||||
) -> StrResult<()> {
|
) -> StrResult<()> {
|
||||||
_ = command.output_format()?;
|
|
||||||
|
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
if watching {
|
if watching {
|
||||||
Status::Compiling.print(command).unwrap();
|
Status::Compiling.print(config).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let Warned { output, warnings } = compile_and_export(world, command, watching);
|
let Warned { output, warnings } = compile_and_export(world, config, watching);
|
||||||
|
|
||||||
match output {
|
match output {
|
||||||
// Export the PDF / PNG.
|
// Export the PDF / PNG.
|
||||||
@ -129,20 +157,20 @@ pub fn compile_once(
|
|||||||
|
|
||||||
if watching {
|
if watching {
|
||||||
if warnings.is_empty() {
|
if warnings.is_empty() {
|
||||||
Status::Success(duration).print(command).unwrap();
|
Status::Success(duration).print(config).unwrap();
|
||||||
} else {
|
} else {
|
||||||
Status::PartialSuccess(duration).print(command).unwrap();
|
Status::PartialSuccess(duration).print(config).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print_diagnostics(world, &[], &warnings, command.common.diagnostic_format)
|
print_diagnostics(world, &[], &warnings, config.diagnostic_format)
|
||||||
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
||||||
|
|
||||||
write_make_deps(world, command)?;
|
write_make_deps(world, config)?;
|
||||||
|
|
||||||
if let Some(open) = command.open.take() {
|
if let Some(open) = config.open.take() {
|
||||||
if let Output::Path(file) = command.output() {
|
if let Output::Path(file) = &config.output {
|
||||||
open_file(open.as_deref(), &file)?;
|
open_file(open.as_deref(), file)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,16 +180,11 @@ pub fn compile_once(
|
|||||||
set_failed();
|
set_failed();
|
||||||
|
|
||||||
if watching {
|
if watching {
|
||||||
Status::Error.print(command).unwrap();
|
Status::Error.print(config).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
print_diagnostics(
|
print_diagnostics(world, &errors, &warnings, config.diagnostic_format)
|
||||||
world,
|
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
||||||
&errors,
|
|
||||||
&warnings,
|
|
||||||
command.common.diagnostic_format,
|
|
||||||
)
|
|
||||||
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,17 +193,15 @@ pub fn compile_once(
|
|||||||
|
|
||||||
fn compile_and_export(
|
fn compile_and_export(
|
||||||
world: &mut SystemWorld,
|
world: &mut SystemWorld,
|
||||||
command: &mut CompileCommand,
|
config: &mut CompileConfig,
|
||||||
watching: bool,
|
watching: bool,
|
||||||
) -> Warned<SourceResult<()>> {
|
) -> Warned<SourceResult<()>> {
|
||||||
let format = command.output_format().unwrap();
|
match config.output_format {
|
||||||
|
|
||||||
match format {
|
|
||||||
OutputFormat::Html => {
|
OutputFormat::Html => {
|
||||||
let Warned { output, warnings } = typst::compile::<HtmlDocument>(world);
|
let Warned { output, warnings } = typst::compile::<HtmlDocument>(world);
|
||||||
let result = output.and_then(|document| {
|
let result = output.and_then(|document| {
|
||||||
command
|
config
|
||||||
.output()
|
.output
|
||||||
.write(typst_html::html(&document)?.as_bytes())
|
.write(typst_html::html(&document)?.as_bytes())
|
||||||
.map_err(|err| eco_format!("failed to write HTML file ({err})"))
|
.map_err(|err| eco_format!("failed to write HTML file ({err})"))
|
||||||
.at(Span::detached())
|
.at(Span::detached())
|
||||||
@ -190,7 +211,7 @@ fn compile_and_export(
|
|||||||
_ => {
|
_ => {
|
||||||
let Warned { output, warnings } = typst::compile::<PagedDocument>(world);
|
let Warned { output, warnings } = typst::compile::<PagedDocument>(world);
|
||||||
let result = output
|
let result = output
|
||||||
.and_then(|document| export_paged(world, &document, command, watching));
|
.and_then(|document| export_paged(world, &document, config, watching));
|
||||||
Warned { output: result, warnings }
|
Warned { output: result, warnings }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,17 +221,17 @@ fn compile_and_export(
|
|||||||
fn export_paged(
|
fn export_paged(
|
||||||
world: &mut SystemWorld,
|
world: &mut SystemWorld,
|
||||||
document: &PagedDocument,
|
document: &PagedDocument,
|
||||||
command: &CompileCommand,
|
config: &CompileConfig,
|
||||||
watching: bool,
|
watching: bool,
|
||||||
) -> SourceResult<()> {
|
) -> SourceResult<()> {
|
||||||
match command.output_format().at(Span::detached())? {
|
match config.output_format {
|
||||||
OutputFormat::Pdf => export_pdf(document, command),
|
OutputFormat::Pdf => export_pdf(document, config),
|
||||||
OutputFormat::Png => {
|
OutputFormat::Png => {
|
||||||
export_image(world, document, command, watching, ImageExportFormat::Png)
|
export_image(world, document, config, watching, ImageExportFormat::Png)
|
||||||
.at(Span::detached())
|
.at(Span::detached())
|
||||||
}
|
}
|
||||||
OutputFormat::Svg => {
|
OutputFormat::Svg => {
|
||||||
export_image(world, document, command, watching, ImageExportFormat::Svg)
|
export_image(world, document, config, watching, ImageExportFormat::Svg)
|
||||||
.at(Span::detached())
|
.at(Span::detached())
|
||||||
}
|
}
|
||||||
OutputFormat::Html => unreachable!(),
|
OutputFormat::Html => unreachable!(),
|
||||||
@ -218,18 +239,18 @@ fn export_paged(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Export to a PDF.
|
/// Export to a PDF.
|
||||||
fn export_pdf(document: &PagedDocument, command: &CompileCommand) -> SourceResult<()> {
|
fn export_pdf(document: &PagedDocument, config: &CompileConfig) -> SourceResult<()> {
|
||||||
let options = PdfOptions {
|
let options = PdfOptions {
|
||||||
ident: Smart::Auto,
|
ident: Smart::Auto,
|
||||||
timestamp: convert_datetime(
|
timestamp: convert_datetime(
|
||||||
command.common.creation_timestamp.unwrap_or_else(chrono::Utc::now),
|
config.creation_timestamp.unwrap_or_else(chrono::Utc::now),
|
||||||
),
|
),
|
||||||
page_ranges: command.exported_page_ranges(),
|
page_ranges: config.pages.clone(),
|
||||||
standards: command.pdf_standards().at(Span::detached())?,
|
standards: config.pdf_standards.clone(),
|
||||||
};
|
};
|
||||||
let buffer = typst_pdf::pdf(document, &options)?;
|
let buffer = typst_pdf::pdf(document, &options)?;
|
||||||
command
|
config
|
||||||
.output()
|
.output
|
||||||
.write(&buffer)
|
.write(&buffer)
|
||||||
.map_err(|err| eco_format!("failed to write PDF file ({err})"))
|
.map_err(|err| eco_format!("failed to write PDF file ({err})"))
|
||||||
.at(Span::detached())?;
|
.at(Span::detached())?;
|
||||||
@ -259,34 +280,31 @@ enum ImageExportFormat {
|
|||||||
fn export_image(
|
fn export_image(
|
||||||
world: &mut SystemWorld,
|
world: &mut SystemWorld,
|
||||||
document: &PagedDocument,
|
document: &PagedDocument,
|
||||||
command: &CompileCommand,
|
config: &CompileConfig,
|
||||||
watching: bool,
|
watching: bool,
|
||||||
fmt: ImageExportFormat,
|
fmt: ImageExportFormat,
|
||||||
) -> StrResult<()> {
|
) -> StrResult<()> {
|
||||||
let output = command.output();
|
|
||||||
// Determine whether we have indexable templates in output
|
// Determine whether we have indexable templates in output
|
||||||
let can_handle_multiple = match output {
|
let can_handle_multiple = match config.output {
|
||||||
Output::Stdout => false,
|
Output::Stdout => false,
|
||||||
Output::Path(ref output) => {
|
Output::Path(ref output) => {
|
||||||
output_template::has_indexable_template(output.to_str().unwrap_or_default())
|
output_template::has_indexable_template(output.to_str().unwrap_or_default())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let exported_page_ranges = command.exported_page_ranges();
|
|
||||||
|
|
||||||
let exported_pages = document
|
let exported_pages = document
|
||||||
.pages
|
.pages
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(i, _)| {
|
.filter(|(i, _)| {
|
||||||
exported_page_ranges.as_ref().map_or(true, |exported_page_ranges| {
|
config.pages.as_ref().map_or(true, |exported_page_ranges| {
|
||||||
exported_page_ranges.includes_page_index(*i)
|
exported_page_ranges.includes_page_index(*i)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
if !can_handle_multiple && exported_pages.len() > 1 {
|
if !can_handle_multiple && exported_pages.len() > 1 {
|
||||||
let err = match output {
|
let err = match config.output {
|
||||||
Output::Stdout => "to stdout",
|
Output::Stdout => "to stdout",
|
||||||
Output::Path(_) => {
|
Output::Path(_) => {
|
||||||
"without a page number template ({p}, {0p}) in the output path"
|
"without a page number template ({p}, {0p}) in the output path"
|
||||||
@ -302,8 +320,8 @@ fn export_image(
|
|||||||
.par_iter()
|
.par_iter()
|
||||||
.map(|(i, page)| {
|
.map(|(i, page)| {
|
||||||
// Use output with converted path.
|
// Use output with converted path.
|
||||||
let output = match output {
|
let output = match &config.output {
|
||||||
Output::Path(ref path) => {
|
Output::Path(path) => {
|
||||||
let storage;
|
let storage;
|
||||||
let path = if can_handle_multiple {
|
let path = if can_handle_multiple {
|
||||||
storage = output_template::format(
|
storage = output_template::format(
|
||||||
@ -328,7 +346,7 @@ fn export_image(
|
|||||||
Output::Stdout => Output::Stdout,
|
Output::Stdout => Output::Stdout,
|
||||||
};
|
};
|
||||||
|
|
||||||
export_image_page(command, page, &output, fmt)?;
|
export_image_page(config, page, &output, fmt)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<()>, EcoString>>()?;
|
.collect::<Result<Vec<()>, EcoString>>()?;
|
||||||
@ -367,14 +385,14 @@ mod output_template {
|
|||||||
|
|
||||||
/// Export single image.
|
/// Export single image.
|
||||||
fn export_image_page(
|
fn export_image_page(
|
||||||
command: &CompileCommand,
|
config: &CompileConfig,
|
||||||
page: &Page,
|
page: &Page,
|
||||||
output: &Output,
|
output: &Output,
|
||||||
fmt: ImageExportFormat,
|
fmt: ImageExportFormat,
|
||||||
) -> StrResult<()> {
|
) -> StrResult<()> {
|
||||||
match fmt {
|
match fmt {
|
||||||
ImageExportFormat::Png => {
|
ImageExportFormat::Png => {
|
||||||
let pixmap = typst_render::render(page, command.ppi / 72.0);
|
let pixmap = typst_render::render(page, config.ppi / 72.0);
|
||||||
let buf = pixmap
|
let buf = pixmap
|
||||||
.encode_png()
|
.encode_png()
|
||||||
.map_err(|err| eco_format!("failed to encode PNG file ({err})"))?;
|
.map_err(|err| eco_format!("failed to encode PNG file ({err})"))?;
|
||||||
@ -438,12 +456,12 @@ impl ExportCache {
|
|||||||
/// Writes a Makefile rule describing the relationship between the output and
|
/// Writes a Makefile rule describing the relationship between the output and
|
||||||
/// its dependencies to the path specified by the --make-deps argument, if it
|
/// its dependencies to the path specified by the --make-deps argument, if it
|
||||||
/// was provided.
|
/// was provided.
|
||||||
fn write_make_deps(world: &mut SystemWorld, command: &CompileCommand) -> StrResult<()> {
|
fn write_make_deps(world: &mut SystemWorld, config: &CompileConfig) -> StrResult<()> {
|
||||||
let Some(ref make_deps_path) = command.make_deps else { return Ok(()) };
|
let Some(ref make_deps_path) = config.make_deps else { return Ok(()) };
|
||||||
let Output::Path(output_path) = command.output() else {
|
let Output::Path(output_path) = &config.output else {
|
||||||
bail!("failed to create make dependencies file because output was stdout")
|
bail!("failed to create make dependencies file because output was stdout")
|
||||||
};
|
};
|
||||||
let Ok(output_path) = output_path.into_os_string().into_string() else {
|
let Some(output_path) = output_path.as_os_str().to_str() else {
|
||||||
bail!("failed to create make dependencies file because output path was not valid unicode")
|
bail!("failed to create make dependencies file because output path was not valid unicode")
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -481,13 +499,13 @@ fn write_make_deps(world: &mut SystemWorld, command: &CompileCommand) -> StrResu
|
|||||||
|
|
||||||
fn write(
|
fn write(
|
||||||
make_deps_path: &Path,
|
make_deps_path: &Path,
|
||||||
output_path: String,
|
output_path: &str,
|
||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
dependencies: impl Iterator<Item = PathBuf>,
|
dependencies: impl Iterator<Item = PathBuf>,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
let mut file = File::create(make_deps_path)?;
|
let mut file = File::create(make_deps_path)?;
|
||||||
|
|
||||||
file.write_all(munge(&output_path).as_bytes())?;
|
file.write_all(munge(output_path).as_bytes())?;
|
||||||
file.write_all(b":")?;
|
file.write_all(b":")?;
|
||||||
for dependency in dependencies {
|
for dependency in dependencies {
|
||||||
let Some(dependency) =
|
let Some(dependency) =
|
||||||
|
@ -6,8 +6,8 @@ use crate::args::FontsCommand;
|
|||||||
/// Execute a font listing command.
|
/// Execute a font listing command.
|
||||||
pub fn fonts(command: &FontsCommand) {
|
pub fn fonts(command: &FontsCommand) {
|
||||||
let fonts = Fonts::searcher()
|
let fonts = Fonts::searcher()
|
||||||
.include_system_fonts(!command.font_args.ignore_system_fonts)
|
.include_system_fonts(!command.font.ignore_system_fonts)
|
||||||
.search_with(&command.font_args.font_paths);
|
.search_with(&command.font.font_paths);
|
||||||
|
|
||||||
for (name, infos) in fonts.book.families() {
|
for (name, infos) in fonts.book.families() {
|
||||||
println!("{name}");
|
println!("{name}");
|
||||||
|
@ -15,7 +15,7 @@ use crate::package;
|
|||||||
|
|
||||||
/// Execute an initialization command.
|
/// Execute an initialization command.
|
||||||
pub fn init(command: &InitCommand) -> StrResult<()> {
|
pub fn init(command: &InitCommand) -> StrResult<()> {
|
||||||
let package_storage = package::storage(&command.package_storage_args);
|
let package_storage = package::storage(&command.package);
|
||||||
|
|
||||||
// Parse the package specification. If the user didn't specify the version,
|
// Parse the package specification. If the user didn't specify the version,
|
||||||
// we try to figure it out automatically by downloading the package index
|
// we try to figure it out automatically by downloading the package index
|
||||||
|
@ -60,11 +60,11 @@ fn main() -> ExitCode {
|
|||||||
|
|
||||||
/// Execute the requested command.
|
/// Execute the requested command.
|
||||||
fn dispatch() -> HintedStrResult<()> {
|
fn dispatch() -> HintedStrResult<()> {
|
||||||
let timer = Timer::new(&ARGS);
|
let mut timer = Timer::new(&ARGS);
|
||||||
|
|
||||||
match &ARGS.command {
|
match &ARGS.command {
|
||||||
Command::Compile(command) => crate::compile::compile(timer, command.clone())?,
|
Command::Compile(command) => crate::compile::compile(&mut timer, command)?,
|
||||||
Command::Watch(command) => crate::watch::watch(timer, command.clone())?,
|
Command::Watch(command) => crate::watch::watch(&mut timer, command)?,
|
||||||
Command::Init(command) => crate::init::init(command)?,
|
Command::Init(command) => crate::init::init(command)?,
|
||||||
Command::Query(command) => crate::query::query(command)?,
|
Command::Query(command) => crate::query::query(command)?,
|
||||||
Command::Fonts(command) => crate::fonts::fonts(command),
|
Command::Fonts(command) => crate::fonts::fonts(command),
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use typst_kit::package::PackageStorage;
|
use typst_kit::package::PackageStorage;
|
||||||
|
|
||||||
use crate::args::PackageStorageArgs;
|
use crate::args::PackageArgs;
|
||||||
use crate::download;
|
use crate::download;
|
||||||
|
|
||||||
/// Returns a new package storage for the given args.
|
/// Returns a new package storage for the given args.
|
||||||
pub fn storage(args: &PackageStorageArgs) -> PackageStorage {
|
pub fn storage(args: &PackageArgs) -> PackageStorage {
|
||||||
PackageStorage::new(
|
PackageStorage::new(
|
||||||
args.package_cache_path.clone(),
|
args.package_cache_path.clone(),
|
||||||
args.package_path.clone(),
|
args.package_path.clone(),
|
||||||
|
@ -15,7 +15,7 @@ use crate::world::SystemWorld;
|
|||||||
|
|
||||||
/// Execute a query command.
|
/// Execute a query command.
|
||||||
pub fn query(command: &QueryCommand) -> HintedStrResult<()> {
|
pub fn query(command: &QueryCommand) -> HintedStrResult<()> {
|
||||||
let mut world = SystemWorld::new(&command.common)?;
|
let mut world = SystemWorld::new(&command.input, &command.world, &command.process)?;
|
||||||
|
|
||||||
// Reset everything and ensure that the main file is present.
|
// Reset everything and ensure that the main file is present.
|
||||||
world.reset();
|
world.reset();
|
||||||
@ -29,7 +29,7 @@ pub fn query(command: &QueryCommand) -> HintedStrResult<()> {
|
|||||||
let data = retrieve(&world, command, &document)?;
|
let data = retrieve(&world, command, &document)?;
|
||||||
let serialized = format(data, command)?;
|
let serialized = format(data, command)?;
|
||||||
println!("{serialized}");
|
println!("{serialized}");
|
||||||
print_diagnostics(&world, &[], &warnings, command.common.diagnostic_format)
|
print_diagnostics(&world, &[], &warnings, command.process.diagnostic_format)
|
||||||
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ pub fn query(command: &QueryCommand) -> HintedStrResult<()> {
|
|||||||
&world,
|
&world,
|
||||||
&errors,
|
&errors,
|
||||||
&warnings,
|
&warnings,
|
||||||
command.common.diagnostic_format,
|
command.process.diagnostic_format,
|
||||||
)
|
)
|
||||||
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,8 @@ impl Timer {
|
|||||||
/// record timings for a specific function invocation.
|
/// record timings for a specific function invocation.
|
||||||
pub fn new(args: &CliArguments) -> Timer {
|
pub fn new(args: &CliArguments) -> Timer {
|
||||||
let record = match &args.command {
|
let record = match &args.command {
|
||||||
Command::Compile(command) => command.timings.clone(),
|
Command::Compile(command) => command.args.timings.clone(),
|
||||||
Command::Watch(command) => command.timings.clone(),
|
Command::Watch(command) => command.args.timings.clone(),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -13,32 +13,38 @@ use same_file::is_same_file;
|
|||||||
use typst::diag::{bail, StrResult};
|
use typst::diag::{bail, StrResult};
|
||||||
use typst::utils::format_duration;
|
use typst::utils::format_duration;
|
||||||
|
|
||||||
use crate::args::{CompileCommand, Input, Output};
|
use crate::args::{Input, Output, WatchCommand};
|
||||||
use crate::compile::compile_once;
|
use crate::compile::{compile_once, CompileConfig};
|
||||||
use crate::timings::Timer;
|
use crate::timings::Timer;
|
||||||
use crate::world::{SystemWorld, WorldCreationError};
|
use crate::world::{SystemWorld, WorldCreationError};
|
||||||
use crate::{print_error, terminal};
|
use crate::{print_error, terminal};
|
||||||
|
|
||||||
/// Execute a watching compilation command.
|
/// Execute a watching compilation command.
|
||||||
pub fn watch(mut timer: Timer, mut command: CompileCommand) -> StrResult<()> {
|
pub fn watch(timer: &mut Timer, command: &WatchCommand) -> StrResult<()> {
|
||||||
let Output::Path(output) = command.output() else {
|
let mut config = CompileConfig::new(&command.args)?;
|
||||||
|
|
||||||
|
let Output::Path(output) = &config.output else {
|
||||||
bail!("cannot write document to stdout in watch mode");
|
bail!("cannot write document to stdout in watch mode");
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create a file system watcher.
|
// Create a file system watcher.
|
||||||
let mut watcher = Watcher::new(output)?;
|
let mut watcher = Watcher::new(output.clone())?;
|
||||||
|
|
||||||
// Create the world that serves sources, files, and fonts.
|
// Create the world that serves sources, files, and fonts.
|
||||||
// Additionally, if any files do not exist, wait until they do.
|
// Additionally, if any files do not exist, wait until they do.
|
||||||
let mut world = loop {
|
let mut world = loop {
|
||||||
match SystemWorld::new(&command.common) {
|
match SystemWorld::new(
|
||||||
|
&command.args.input,
|
||||||
|
&command.args.world,
|
||||||
|
&command.args.process,
|
||||||
|
) {
|
||||||
Ok(world) => break world,
|
Ok(world) => break world,
|
||||||
Err(
|
Err(
|
||||||
ref err @ (WorldCreationError::InputNotFound(ref path)
|
ref err @ (WorldCreationError::InputNotFound(ref path)
|
||||||
| WorldCreationError::RootNotFound(ref path)),
|
| WorldCreationError::RootNotFound(ref path)),
|
||||||
) => {
|
) => {
|
||||||
watcher.update([path.clone()])?;
|
watcher.update([path.clone()])?;
|
||||||
Status::Error.print(&command).unwrap();
|
Status::Error.print(&config).unwrap();
|
||||||
print_error(&err.to_string()).unwrap();
|
print_error(&err.to_string()).unwrap();
|
||||||
watcher.wait()?;
|
watcher.wait()?;
|
||||||
}
|
}
|
||||||
@ -47,7 +53,7 @@ pub fn watch(mut timer: Timer, mut command: CompileCommand) -> StrResult<()> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Perform initial compilation.
|
// Perform initial compilation.
|
||||||
timer.record(&mut world, |world| compile_once(world, &mut command, true))??;
|
timer.record(&mut world, |world| compile_once(world, &mut config, true))??;
|
||||||
|
|
||||||
// Watch all dependencies of the initial compilation.
|
// Watch all dependencies of the initial compilation.
|
||||||
watcher.update(world.dependencies())?;
|
watcher.update(world.dependencies())?;
|
||||||
@ -61,7 +67,7 @@ pub fn watch(mut timer: Timer, mut command: CompileCommand) -> StrResult<()> {
|
|||||||
world.reset();
|
world.reset();
|
||||||
|
|
||||||
// Recompile.
|
// Recompile.
|
||||||
timer.record(&mut world, |world| compile_once(world, &mut command, true))??;
|
timer.record(&mut world, |world| compile_once(world, &mut config, true))??;
|
||||||
|
|
||||||
// Evict the cache.
|
// Evict the cache.
|
||||||
comemo::evict(10);
|
comemo::evict(10);
|
||||||
@ -267,8 +273,7 @@ pub enum Status {
|
|||||||
|
|
||||||
impl Status {
|
impl Status {
|
||||||
/// Clear the terminal and render the status message.
|
/// Clear the terminal and render the status message.
|
||||||
pub fn print(&self, command: &CompileCommand) -> io::Result<()> {
|
pub fn print(&self, config: &CompileConfig) -> io::Result<()> {
|
||||||
let output = command.output();
|
|
||||||
let timestamp = chrono::offset::Local::now().format("%H:%M:%S");
|
let timestamp = chrono::offset::Local::now().format("%H:%M:%S");
|
||||||
let color = self.color();
|
let color = self.color();
|
||||||
|
|
||||||
@ -278,7 +283,7 @@ impl Status {
|
|||||||
out.set_color(&color)?;
|
out.set_color(&color)?;
|
||||||
write!(out, "watching")?;
|
write!(out, "watching")?;
|
||||||
out.reset()?;
|
out.reset()?;
|
||||||
match &command.common.input {
|
match &config.input {
|
||||||
Input::Stdin => writeln!(out, " <stdin>"),
|
Input::Stdin => writeln!(out, " <stdin>"),
|
||||||
Input::Path(path) => writeln!(out, " {}", path.display()),
|
Input::Path(path) => writeln!(out, " {}", path.display()),
|
||||||
}?;
|
}?;
|
||||||
@ -286,7 +291,7 @@ impl Status {
|
|||||||
out.set_color(&color)?;
|
out.set_color(&color)?;
|
||||||
write!(out, "writing to")?;
|
write!(out, "writing to")?;
|
||||||
out.reset()?;
|
out.reset()?;
|
||||||
writeln!(out, " {output}")?;
|
writeln!(out, " {}", config.output)?;
|
||||||
|
|
||||||
writeln!(out)?;
|
writeln!(out)?;
|
||||||
writeln!(out, "[{timestamp}] {}", self.message())?;
|
writeln!(out, "[{timestamp}] {}", self.message())?;
|
||||||
|
@ -17,7 +17,7 @@ use typst_kit::fonts::{FontSlot, Fonts};
|
|||||||
use typst_kit::package::PackageStorage;
|
use typst_kit::package::PackageStorage;
|
||||||
use typst_timing::timed;
|
use typst_timing::timed;
|
||||||
|
|
||||||
use crate::args::{Feature, Input, SharedArgs};
|
use crate::args::{Feature, Input, ProcessArgs, WorldArgs};
|
||||||
use crate::compile::ExportCache;
|
use crate::compile::ExportCache;
|
||||||
use crate::download::PrintDownload;
|
use crate::download::PrintDownload;
|
||||||
use crate::package;
|
use crate::package;
|
||||||
@ -56,9 +56,13 @@ pub struct SystemWorld {
|
|||||||
|
|
||||||
impl SystemWorld {
|
impl SystemWorld {
|
||||||
/// Create a new system world.
|
/// Create a new system world.
|
||||||
pub fn new(command: &SharedArgs) -> Result<Self, WorldCreationError> {
|
pub fn new(
|
||||||
|
input: &Input,
|
||||||
|
world_args: &WorldArgs,
|
||||||
|
process_args: &ProcessArgs,
|
||||||
|
) -> Result<Self, WorldCreationError> {
|
||||||
// Set up the thread pool.
|
// Set up the thread pool.
|
||||||
if let Some(jobs) = command.jobs {
|
if let Some(jobs) = process_args.jobs {
|
||||||
rayon::ThreadPoolBuilder::new()
|
rayon::ThreadPoolBuilder::new()
|
||||||
.num_threads(jobs)
|
.num_threads(jobs)
|
||||||
.use_current_thread()
|
.use_current_thread()
|
||||||
@ -67,7 +71,7 @@ impl SystemWorld {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Resolve the system-global input path.
|
// Resolve the system-global input path.
|
||||||
let input = match &command.input {
|
let input = match input {
|
||||||
Input::Stdin => None,
|
Input::Stdin => None,
|
||||||
Input::Path(path) => {
|
Input::Path(path) => {
|
||||||
Some(path.canonicalize().map_err(|err| match err.kind() {
|
Some(path.canonicalize().map_err(|err| match err.kind() {
|
||||||
@ -81,7 +85,7 @@ impl SystemWorld {
|
|||||||
|
|
||||||
// Resolve the system-global root directory.
|
// Resolve the system-global root directory.
|
||||||
let root = {
|
let root = {
|
||||||
let path = command
|
let path = world_args
|
||||||
.root
|
.root
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.or_else(|| input.as_deref().and_then(|i| i.parent()))
|
.or_else(|| input.as_deref().and_then(|i| i.parent()))
|
||||||
@ -106,13 +110,13 @@ impl SystemWorld {
|
|||||||
|
|
||||||
let library = {
|
let library = {
|
||||||
// Convert the input pairs to a dictionary.
|
// Convert the input pairs to a dictionary.
|
||||||
let inputs: Dict = command
|
let inputs: Dict = world_args
|
||||||
.inputs
|
.inputs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(k, v)| (k.as_str().into(), v.as_str().into_value()))
|
.map(|(k, v)| (k.as_str().into(), v.as_str().into_value()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let features = command
|
let features = process_args
|
||||||
.feature
|
.feature
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&feature| match feature {
|
.map(|&feature| match feature {
|
||||||
@ -124,10 +128,10 @@ impl SystemWorld {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let fonts = Fonts::searcher()
|
let fonts = Fonts::searcher()
|
||||||
.include_system_fonts(!command.font_args.ignore_system_fonts)
|
.include_system_fonts(!world_args.font.ignore_system_fonts)
|
||||||
.search_with(&command.font_args.font_paths);
|
.search_with(&world_args.font.font_paths);
|
||||||
|
|
||||||
let now = match command.creation_timestamp {
|
let now = match world_args.creation_timestamp {
|
||||||
Some(time) => Now::Fixed(time),
|
Some(time) => Now::Fixed(time),
|
||||||
None => Now::System(OnceLock::new()),
|
None => Now::System(OnceLock::new()),
|
||||||
};
|
};
|
||||||
@ -140,7 +144,7 @@ impl SystemWorld {
|
|||||||
book: LazyHash::new(fonts.book),
|
book: LazyHash::new(fonts.book),
|
||||||
fonts: fonts.fonts,
|
fonts: fonts.fonts,
|
||||||
slots: Mutex::new(HashMap::new()),
|
slots: Mutex::new(HashMap::new()),
|
||||||
package_storage: package::storage(&command.package_storage_args),
|
package_storage: package::storage(&world_args.package),
|
||||||
now,
|
now,
|
||||||
export_cache: ExportCache::new(),
|
export_cache: ExportCache::new(),
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user