Skip to content

Commit

Permalink
dev: manual generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Nov 13, 2023
1 parent 2c68f76 commit de8ef8b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
3 changes: 3 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -56,6 +57,7 @@ anyhow.workspace = true
vergen.workspace = true

[features]
gen-manual = ["dep:clap_mangen"]
embedded-fonts = []
embedded-cjk-fonts = []
embedded-emoji-fonts = []
Expand All @@ -70,6 +72,7 @@ default = [
"serde-json",
"serde-rmp",
"svg",
"gen-manual",
"embedded-fonts",
"embedded-cjk-fonts",
"embedded-emoji-fonts",
Expand Down
19 changes: 18 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
17 changes: 12 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions cli/src/manual.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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<str> = 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(())
}

0 comments on commit de8ef8b

Please sign in to comment.