diff --git a/docs/.gitignore b/docs/.gitignore index f2b5c7168..c836306f5 100644 --- a/docs/.gitignore +++ b/docs/.gitignore @@ -1,3 +1,4 @@ book src/utils src/SUMMARY.md +tldr/ diff --git a/docs/Makefile b/docs/Makefile index dd700bcb0..23901b755 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -1,4 +1,6 @@ +# spell-checker:ignore tldr clean: - rm -rf _build + rm -rf book rm -f src/SUMMARY.md rm -f src/utils/* + rm -rf tldr diff --git a/src/bin/uudoc.rs b/src/bin/uudoc.rs index 71bbb2684..5658f491c 100644 --- a/src/bin/uudoc.rs +++ b/src/bin/uudoc.rs @@ -2,15 +2,33 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +// spell-checker:ignore tldr use clap::App; use std::ffi::OsString; use std::fs::File; -use std::io::{self, Write}; +use std::io::{self, Read, Write}; +use std::process::Command; include!(concat!(env!("OUT_DIR"), "/uutils_map.rs")); fn main() -> io::Result<()> { + let _ = std::fs::create_dir("docs/tldr"); + println!("Downloading tldr archive"); + Command::new("curl") + .arg("https://tldr.sh/assets/tldr.zip") + .arg("--output") + .arg("docs/tldr/tldr.zip") + .output()?; + + println!("Unzipping tldr archive"); + Command::new("unzip") + .arg("-o") + .arg("docs/tldr/tldr.zip") + .arg("-d") + .arg("docs/tldr") + .output()?; + let utils = util_map::>>(); match std::fs::create_dir("docs/src/utils/") { Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(()), @@ -55,6 +73,7 @@ fn write_markdown(mut w: impl Write, app: &mut App, name: &str) -> io::Result<() write_version(&mut w, app)?; write_usage(&mut w, app, name)?; write_description(&mut w, app)?; + write_examples(&mut w, name)?; write_options(&mut w, app) } @@ -82,6 +101,35 @@ fn write_description(w: &mut impl Write, app: &App) -> io::Result<()> { } } +fn write_examples(w: &mut impl Write, name: &str) -> io::Result<()> { + if let Ok(mut file) = std::fs::File::open(format!("docs/tldr/pages/common/{}.md", name)) + .or_else(|_| std::fs::File::open(format!("docs/tldr/pages/linux/{}.md", name))) + { + let mut content = String::new(); + file.read_to_string(&mut content)?; + + writeln!(w, "## Examples")?; + writeln!(w)?; + for line in content.lines().skip_while(|l| !l.starts_with('-')) { + if let Some(l) = line.strip_prefix("- ") { + writeln!(w, "{}", l)?; + } else if line.starts_with('`') { + writeln!(w, "```shell\n{}\n```", line.trim_matches('`'))?; + } else if line.is_empty() { + writeln!(w)?; + } else { + println!("Not sure what to do with this line:"); + println!("{}", line); + } + } + writeln!(w)?; + writeln!(w, "> The examples are provided by the [tldr-pages project](https://tldr.sh) under the [CC BY 4.0 License](https://github.com/tldr-pages/tldr/blob/main/LICENSE.md).")?; + } else { + println!("No examples found for: {}", name); + } + Ok(()) +} + fn write_options(w: &mut impl Write, app: &App) -> io::Result<()> { writeln!(w, "

Options

")?; write!(w, "
")?;