From 951035e49ceadb1aa1a3f27115aa2577e760adfb Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Thu, 20 Jan 2022 15:00:16 +0100 Subject: [PATCH] mdbook docs --- Cargo.toml | 4 ++ docs/docs/.gitignore | 1 + docs/docs/book.toml | 6 ++ docs/docs/src/SUMMARY.md | 3 + docs/docs/src/chapter_1.md | 1 + src/bin/uudoc.rs | 114 +++++++++++++++++++++++++++++++++++++ 6 files changed, 129 insertions(+) create mode 100644 docs/docs/.gitignore create mode 100644 docs/docs/book.toml create mode 100644 docs/docs/src/SUMMARY.md create mode 100644 docs/docs/src/chapter_1.md create mode 100644 src/bin/uudoc.rs diff --git a/Cargo.toml b/Cargo.toml index a099115a6..cbb91bf32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -389,3 +389,7 @@ unix_socket = "0.5.0" [[bin]] name = "coreutils" path = "src/bin/coreutils.rs" + +[[bin]] +name = "uudoc" +path = "src/bin/uudoc.rs" diff --git a/docs/docs/.gitignore b/docs/docs/.gitignore new file mode 100644 index 000000000..7585238ef --- /dev/null +++ b/docs/docs/.gitignore @@ -0,0 +1 @@ +book diff --git a/docs/docs/book.toml b/docs/docs/book.toml new file mode 100644 index 000000000..fe32ddf98 --- /dev/null +++ b/docs/docs/book.toml @@ -0,0 +1,6 @@ +[book] +authors = ["Terts Diepraam"] +language = "en" +multilingual = false +src = "src" +title = "Uutils Documentation" diff --git a/docs/docs/src/SUMMARY.md b/docs/docs/src/SUMMARY.md new file mode 100644 index 000000000..7390c8289 --- /dev/null +++ b/docs/docs/src/SUMMARY.md @@ -0,0 +1,3 @@ +# Summary + +- [Chapter 1](./chapter_1.md) diff --git a/docs/docs/src/chapter_1.md b/docs/docs/src/chapter_1.md new file mode 100644 index 000000000..b743fda35 --- /dev/null +++ b/docs/docs/src/chapter_1.md @@ -0,0 +1 @@ +# Chapter 1 diff --git a/src/bin/uudoc.rs b/src/bin/uudoc.rs new file mode 100644 index 000000000..65da4c7cc --- /dev/null +++ b/src/bin/uudoc.rs @@ -0,0 +1,114 @@ +// This file is part of the uutils coreutils package. +// +// (c) Michael Gehring +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +use clap::App; +use clap::Arg; +use clap::Shell; +use std::cmp; +use std::collections::hash_map::HashMap; +use std::ffi::OsStr; +use std::ffi::OsString; +use std::io::{self, Write}; +use std::path::{Path, PathBuf}; +use std::process; +use uucore::display::Quotable; + +const VERSION: &str = env!("CARGO_PKG_VERSION"); + +include!(concat!(env!("OUT_DIR"), "/uutils_map.rs")); + +fn usage(utils: &UtilityMap, name: &str) { + println!("{} {}\n", name, VERSION); + println!("Generate markdown documentation for uutils"); + println!("Usage: {} [util]\n", name); + println!("Currently defined functions:\n"); + #[allow(clippy::map_clone)] + let mut utils: Vec<&str> = utils.keys().map(|&s| s).collect(); + utils.sort_unstable(); + let display_list = utils.join(", "); + let width = cmp::min(textwrap::termwidth(), 100) - 4 * 2; // (opinion/heuristic) max 100 chars wide with 4 character side indentions + println!( + "{}", + textwrap::indent(&textwrap::fill(&display_list, width), " ") + ); +} + +fn binary_path(args: &mut impl Iterator) -> PathBuf { + match args.next() { + Some(ref s) if !s.is_empty() => PathBuf::from(s), + _ => std::env::current_exe().unwrap(), + } +} + +fn name(binary_path: &Path) -> &str { + binary_path.file_stem().unwrap().to_str().unwrap() +} + +fn main() { + uucore::panic::mute_sigpipe_panic(); + + let utils = util_map(); + let mut args = uucore::args_os(); + + let binary = binary_path(&mut args); + let binary_as_util = name(&binary); + + // binary name equals util name? + if let Some(&(uumain, _)) = utils.get(binary_as_util) { + process::exit(uumain((vec![binary.into()].into_iter()).chain(args))); + } + + // binary name equals prefixed util name? + // * prefix/stem may be any string ending in a non-alphanumeric character + let util_name = if let Some(util) = utils.keys().find(|util| { + binary_as_util.ends_with(*util) + && !(&binary_as_util[..binary_as_util.len() - (*util).len()]) + .ends_with(char::is_alphanumeric) + }) { + // prefixed util => replace 0th (aka, executable name) argument + Some(OsString::from(*util)) + } else { + // unmatched binary name => regard as multi-binary container and advance argument list + uucore::set_utility_is_second_arg(); + args.next() + }; + + // 0th argument equals util name? + if let Some(util_os) = util_name { + fn not_found(util: &OsStr) -> ! { + println!("{}: function/utility not found", util.maybe_quote()); + process::exit(1); + } + + let util = match util_os.to_str() { + Some(util) => util, + None => not_found(&util_os), + }; + + match utils.get(util) { + Some(&(uumain, app)) => { + print_markdown(app); + } + None => { + if util == "--help" || util == "-h" { + usage(&utils, binary_as_util); + process::exit(0); + } else { + not_found(&util_os); + } + } + } + } else { + // no arguments provided + usage(&utils, binary_as_util); + process::exit(0); + } +} + +fn print_markdown(app: &App) { + for arg in app.get_arguments() {} +}