From de8ef8b4cab3918be7c4d28c2bdae5e41baa7125 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Sat, 11 Nov 2023 15:28:15 +0800 Subject: [PATCH] dev: manual generation + https://github.com/typst/typst/pull/582 --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + cli/Cargo.toml | 3 +++ cli/src/lib.rs | 19 ++++++++++++++++++- cli/src/main.rs | 17 ++++++++++++----- cli/src/manual.rs | 25 +++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 cli/src/manual.rs diff --git a/Cargo.lock b/Cargo.lock index 268d50666..f6df63110 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -532,6 +532,16 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" +[[package]] +name = "clap_mangen" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3be86020147691e1d2ef58f75346a3d4d94807bfc473e377d52f09f0f7d77f7" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "clipboard-win" version = "4.5.0" @@ -2911,6 +2921,12 @@ dependencies = [ "serde", ] +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "roxmltree" version = "0.18.1" @@ -4204,6 +4220,7 @@ dependencies = [ "clap", "clap_complete", "clap_complete_fig", + "clap_mangen", "codespan-reporting", "comemo", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 4ff4c5082..e9f74510c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,6 +100,7 @@ ansi_term = "0.12.1" clap = { version = "4.4", features = ["derive", "env", "unicode", "wrap_help"] } clap_complete = "4.4" clap_complete_fig = "4.4" +clap_mangen = { vesrion = "0.2.15" } human-panic = "1.1.4" rustyline = { version = "12.0.0", features = ["derive"] } diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 4d312dc90..5d0303320 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -16,6 +16,7 @@ typst-ide.workspace = true clap.workspace = true clap_complete.workspace = true clap_complete_fig.workspace = true +clap_mangen = { workspace = true, optional = true } rustyline.workspace = true @@ -56,6 +57,7 @@ anyhow.workspace = true vergen.workspace = true [features] +gen-manual = ["dep:clap_mangen"] embedded-fonts = [] embedded-cjk-fonts = [] embedded-emoji-fonts = [] @@ -70,6 +72,7 @@ default = [ "serde-json", "serde-rmp", "svg", + "gen-manual", "embedded-fonts", "embedded-cjk-fonts", "embedded-emoji-fonts", diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 063eb2172..ae1624f8e 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -3,13 +3,15 @@ use std::path::PathBuf; pub mod compile; pub mod export; pub mod font; +#[cfg(feature = "gen-manual")] +pub mod manual; pub mod query; pub mod query_repl; pub mod tracing; pub mod utils; pub mod version; -use clap::{ArgAction, Parser, Subcommand, ValueEnum}; +use clap::{ArgAction, Args, Command, Parser, Subcommand, ValueEnum}; use typst_ts_core::build_info::VERSION; use version::VersionFormat; @@ -47,6 +49,9 @@ pub enum Subcommands { #[clap(about = "Generate shell completion script.")] Completion(CompletionArgs), + #[clap(about = "Generate manual.")] + Manual(ManualArgs), + #[clap(about = "Dump Client Environment.")] Env(EnvArgs), @@ -229,6 +234,13 @@ pub struct CompletionArgs { pub shell: clap_complete::Shell, } +/// Generate shell completion script. +#[derive(Debug, Clone, Parser)] +pub struct ManualArgs { + /// Path to output directory + pub dest: PathBuf, +} + /// Dump Client Environment. #[derive(Debug, Clone, Parser)] pub struct EnvArgs { @@ -267,3 +279,8 @@ pub struct GenPackagesDocArgs { #[clap(long)] pub dynamic_layout: bool, } + +pub fn get_cli(sub_command_required: bool) -> Command { + let cli = Command::new("$").disable_version_flag(true); + Opts::augment_args(cli).subcommand_required(sub_command_required) +} diff --git a/cli/src/main.rs b/cli/src/main.rs index da4bdf320..d0594769b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -4,12 +4,14 @@ use std::{ sync::Arc, }; -use clap::{Args, Command, FromArgMatches}; +use clap::FromArgMatches; use typst::{doc::Document, font::FontVariant, World}; use typst_ts_cli::{ compile::compile_export, font::EMBEDDED_FONT, + get_cli, + manual::generate_manual, query::serialize, utils::{self, make_absolute, UnwrapOrExit}, version::intercept_version, @@ -21,13 +23,10 @@ use typst_ts_compiler::TypstSystemWorld; use typst_ts_core::exporter_builtins::GroupExporter; use typst_ts_core::{ config::CompileOpts, + error::prelude::*, exporter_utils::map_err, path::{unix_slash, PathClean}, }; -fn get_cli(sub_command_required: bool) -> Command { - let cli = Command::new("$").disable_version_flag(true); - Opts::augment_args(cli).subcommand_required(sub_command_required) -} fn help_sub_command() { Opts::from_arg_matches(&get_cli(true).get_matches()).unwrap(); @@ -60,6 +59,14 @@ fn main() { Some(Subcommands::Query(args)) => query(args), Some(Subcommands::QueryRepl(args)) => query_repl(args), Some(Subcommands::Completion(args)) => generate_completion(args), + #[cfg(feature = "gen-manual")] + Some(Subcommands::Manual(args)) => { + generate_manual(get_cli(true), &args.dest) + .map_err(typst_ts_core::error_once_map_string!("generation failed")) + .unwrap_or_exit(); + + exit(0); + } Some(Subcommands::Env(args)) => match args.key { EnvKey::Features => { intercept_version(false, typst_ts_cli::version::VersionFormat::Features) diff --git a/cli/src/manual.rs b/cli/src/manual.rs new file mode 100644 index 000000000..782c92e42 --- /dev/null +++ b/cli/src/manual.rs @@ -0,0 +1,25 @@ +use clap_mangen::Man; +use std::fs::{create_dir_all, File}; +use std::path::Path; +use std::sync::Arc; + +pub fn generate_manual(cmd: clap::Command, out: &Path) -> Result<(), Box> { + create_dir_all(out)?; + + Man::new(cmd.clone()).render(&mut File::create(out.join("typst-ts-cli.1")).unwrap())?; + + let mut borrow_str = vec![]; + + for subcmd in cmd.get_subcommands() { + let name: Arc = format!("typst-ts-cli-{}", subcmd.get_name()).into(); + Man::new( + subcmd + .clone() + .name(unsafe { std::mem::transmute::<&str, &'static str>(name.as_ref()) }), + ) + .render(&mut File::create(out.join(format!("{name}.1")))?)?; + borrow_str.push(name); + } + + Ok(()) +}