Skip to content

Commit

Permalink
add basic mdbook rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Dec 8, 2023
1 parent c30d5e8 commit 535167b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 13 deletions.
4 changes: 4 additions & 0 deletions complete/src/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::{Command, ValueHint};

/// Create completion script for `fish`
pub fn render(c: &Command) -> String {
let mut out = String::new();
let name = &c.name;
Expand Down Expand Up @@ -52,6 +53,7 @@ mod test {
help: "some flag".into(),
..Arg::default()
}],
..Command::default()
};
assert_eq!(render(&c), "complete -c test -s a -d 'some flag'\n",)
}
Expand All @@ -65,6 +67,7 @@ mod test {
help: "some flag".into(),
..Arg::default()
}],
..Command::default()
};
assert_eq!(render(&c), "complete -c test -l all -d 'some flag'\n",)
}
Expand Down Expand Up @@ -96,6 +99,7 @@ mod test {
help: "some flag".into(),
value: Some(hint),
}],
..Command::default()
};
assert_eq!(
render(&c),
Expand Down
11 changes: 9 additions & 2 deletions complete/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

mod fish;
mod zsh;
mod md;

#[derive(Default)]
pub struct Command {
pub name: String,
pub summary: String,
pub version: String,
pub after_options: String,
pub args: Vec<Arg>,
}

Expand All @@ -31,9 +36,11 @@ pub enum ValueHint {

pub fn render(c: &Command, shell: &str) -> String {
match shell {
"md" => md::render(c),
"fish" => fish::render(c),
"zsh" => zsh::render(c),
"sh" | "bash" | "csh" | "elvish" | "powershell" => panic!("shell '{shell}' completion is not supported yet!"),
_ => panic!("unknown shell '{shell}'!"),
"man" => panic!("manpages are not implemented yet"),
"sh" | "bash" | "csh" | "elvish" | "powershell" => panic!("shell '{shell}' completion is not implemented yet!"),
_ => panic!("unknown option '{shell}'! Expected one of: \"md\", \"fish\", \"zsh\", \"man\", \"sh\", \"bash\", \"csh\", \"elvish\", \"powershell\""),
}
}
57 changes: 57 additions & 0 deletions complete/src/md.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use crate::Command;

/// Render command to a markdown file for mdbook
pub fn render(c: &Command) -> String {
let mut out = String::new();
out.push_str(&title(c));
out.push_str(&additional(c));
out.push_str(&c.summary);
out.push_str("\n\n");
out.push_str(&options(c));
out.push_str("\n\n");
out.push_str(&c.after_options);
out.push_str("\n");
out
}

fn title(c: &Command) -> String {
format!("# {}\n\n", c.name)
}

fn additional(c: &Command) -> String {
let version = &c.version;
format!(
"\
<div class=\"additional\">\
{version}\
</div>\n\n\
"
)
}

fn options(c: &Command) -> String {
let mut out = String::from("## Options\n\n");
out.push_str("<dl>\n");
for arg in &c.args {
out.push_str("<dt>");

let mut flags = Vec::new();

for long in &arg.long {
flags.push(format!("<code>--{long}</code>"));
}

for short in &arg.short {
flags.push(format!("<code>-{short}</code>"))
}

out.push_str(&flags.join(", "));
out.push_str("</dt>\n");
out.push_str(&format!("<dd>\n\n{}\n\n</dd>\n", arg.help));
}
out.push_str("</dl>");
out
}
13 changes: 5 additions & 8 deletions complete/src/zsh.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use crate::{Command, Arg};
use crate::{Arg, Command};

/// Create completion script for `zsh`
pub fn render(c: &Command) -> String {
template(&c.name, &render_args(&c.args))
}
Expand All @@ -13,14 +14,10 @@ fn render_args(args: &[Arg]) -> String {
for arg in args {
let help = &arg.help;
for short in &arg.short {
out.push_str(
&format!("{indent}'-{short}[{help}]' \\\n")
);
out.push_str(&format!("{indent}'-{short}[{help}]' \\\n"));
}
for long in &arg.long {
out.push_str(
&format!("{indent}'--{long}[{help}]' \\\n")
);
out.push_str(&format!("{indent}'--{long}[{help}]' \\\n"));
}
}
out
Expand Down Expand Up @@ -55,4 +52,4 @@ else
compdef _{name} {name}
fi"
)
}
}
11 changes: 10 additions & 1 deletion derive/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ use crate::{
use proc_macro2::TokenStream;
use quote::quote;

pub fn complete(args: &[Argument]) -> TokenStream {
pub fn complete(args: &[Argument], file: &Option<String>,) -> TokenStream {
let mut arg_specs = Vec::new();

let (summary, _usage, after_options) = if let Some(file) = file {
crate::help::read_help_file(file)
} else {
("".into(), "{} [OPTIONS] [ARGUMENTS]".into(), "".into())
};

for Argument {
help,
field,
Expand Down Expand Up @@ -60,6 +66,9 @@ pub fn complete(args: &[Argument]) -> TokenStream {

quote!(Command {
name: String::from(option_env!("CARGO_BIN_NAME").unwrap_or(env!("CARGO_PKG_NAME"))),
summary: String::from(#summary),
after_options: String::from(#after_options),
version: String::from(env!("CARGO_PKG_VERSION")),
args: vec![#(#arg_specs),*]
})
}
2 changes: 1 addition & 1 deletion derive/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn help_string(
)
}

fn read_help_file(file: &str) -> (String, String, String) {
pub fn read_help_file(file: &str) -> (String, String, String) {
let path = Path::new(file);
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let mut location = PathBuf::from(manifest_dir);
Expand Down
2 changes: 1 addition & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn arguments(input: TokenStream) -> TokenStream {
&arguments_attr.version_flags,
&arguments_attr.file,
);
let complete_command = complete::complete(&arguments);
let complete_command = complete::complete(&arguments, &arguments_attr.file);
let help = help_handling(&arguments_attr.help_flags);
let version = version_handling(&arguments_attr.version_flags);
let version_string = quote!(format!(
Expand Down

0 comments on commit 535167b

Please sign in to comment.