1
0
mirror of https://github.com/uutils/coreutils synced 2024-07-03 07:58:37 +00:00

docs: add examples from tldr-pages

This commit is contained in:
Terts Diepraam 2022-02-17 18:29:05 +01:00
parent f15dd8f599
commit f57e3470ae
3 changed files with 53 additions and 2 deletions

1
docs/.gitignore vendored
View File

@ -1,3 +1,4 @@
book
src/utils
src/SUMMARY.md
tldr/

View File

@ -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

View File

@ -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::<Box<dyn Iterator<Item = OsString>>>();
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, "<h2>Options</h2>")?;
write!(w, "<dl>")?;