Skip to content

Commit 3e8114d

Browse files
committed
uudoc: render tldr examples as proper roff EXAMPLES section in man pages
1 parent ace511f commit 3e8114d

1 file changed

Lines changed: 57 additions & 27 deletions

File tree

src/bin/uudoc.rs

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore mangen tldr
6+
// spell-checker:ignore mangen tldr roff
77

88
use std::{
99
collections::HashMap,
@@ -79,25 +79,45 @@ fn gen_manpage<T: Args>(
7979
.get_matches_from(std::iter::once(OsString::from("manpage")).chain(args));
8080

8181
let utility = matches.get_one::<String>("utility").unwrap();
82-
let command = if utility == "coreutils" {
83-
gen_coreutils_app(util_map)
82+
let (command, raw_examples) = if utility == "coreutils" {
83+
(gen_coreutils_app(util_map), None)
8484
} else {
8585
validation::setup_localization_or_exit(utility);
8686
let mut cmd = util_map.get(utility).unwrap().1();
8787
cmd.set_bin_name(utility.clone());
88-
let mut cmd = cmd.display_name(utility);
89-
if let Some(zip) = tldr {
90-
if let Ok(examples) = write_zip_examples(zip, utility, false) {
91-
cmd = cmd.after_help(examples);
92-
}
93-
}
94-
cmd
88+
let cmd = cmd.display_name(utility);
89+
let raw = tldr.as_mut().and_then(|zip| {
90+
get_zip_content(zip, &format!("pages/common/{utility}.md"))
91+
.or_else(|| get_zip_content(zip, &format!("pages/linux/{utility}.md")))
92+
});
93+
(cmd, raw)
9594
};
9695

97-
let man = Man::new(command);
98-
man.render(&mut io::stdout())
96+
let stdout = &mut io::stdout();
97+
Man::new(command)
98+
.render(stdout)
9999
.expect("Man page generation failed");
100-
io::stdout().flush().unwrap();
100+
if let Some(content) = raw_examples {
101+
use clap_mangen::roff::{Roff, bold, roman};
102+
let mut roff = Roff::default();
103+
roff.control("SH", ["EXAMPLES"]);
104+
for line in content.lines().skip_while(|l| !l.starts_with('-')) {
105+
if let Some(desc) = line.strip_prefix("- ") {
106+
roff.text([roman(desc)]);
107+
} else if line.starts_with('`') {
108+
let cmd = strip_placeholders(line.trim_matches('`'));
109+
roff.text([bold(cmd)]);
110+
} else if line.is_empty() {
111+
roff.control("PP", []);
112+
}
113+
}
114+
roff.control("PP", []);
115+
roff.text([roman(
116+
"The examples are provided by the tldr-pages project <https://tldr.sh> under the CC BY 4.0 License. Please note that, as uutils is a work in progress, some examples might fail.",
117+
)]);
118+
roff.to_writer(stdout).expect("Man page generation failed");
119+
}
120+
stdout.flush().unwrap();
101121
process::exit(0);
102122
}
103123

@@ -596,21 +616,29 @@ fn write_zip_examples(
596616
}
597617
}
598618

619+
/// Strip tldr placeholder markers `{{` and `}}` from a command line.
620+
fn strip_placeholders(s: &str) -> String {
621+
s.replace("{{", "").replace("}}", "")
622+
}
623+
599624
/// Format examples using std::fmt::Write
600625
fn format_examples(content: String, output_markdown: bool) -> Result<String, std::fmt::Error> {
601626
use std::fmt::Write;
602627
let mut s = String::new();
603-
writeln!(s)?;
604-
writeln!(s, "## Examples")?;
605-
writeln!(s)?;
628+
if output_markdown {
629+
writeln!(s)?;
630+
writeln!(s, "## Examples")?;
631+
writeln!(s)?;
632+
}
606633
for line in content.lines().skip_while(|l| !l.starts_with('-')) {
607634
if let Some(l) = line.strip_prefix("- ") {
608635
writeln!(s, "{l}")?;
609636
} else if line.starts_with('`') {
637+
let cmd = strip_placeholders(line.trim_matches('`'));
610638
if output_markdown {
611-
writeln!(s, "```shell\n{}\n```", line.trim_matches('`'))?;
639+
writeln!(s, "```shell\n{cmd}\n```")?;
612640
} else {
613-
writeln!(s, "{}", line.trim_matches('`'))?;
641+
writeln!(s, "{cmd}")?;
614642
}
615643
} else if line.is_empty() {
616644
writeln!(s)?;
@@ -620,14 +648,16 @@ fn format_examples(content: String, output_markdown: bool) -> Result<String, std
620648
}
621649
}
622650
writeln!(s)?;
623-
writeln!(
624-
s,
625-
"> 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)."
626-
)?;
627-
writeln!(s, ">")?;
628-
writeln!(
629-
s,
630-
"> Please note that, as uutils is a work in progress, some examples might fail."
631-
)?;
651+
if output_markdown {
652+
writeln!(
653+
s,
654+
"> 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). Please note that, as uutils is a work in progress, some examples might fail."
655+
)?;
656+
} else {
657+
writeln!(
658+
s,
659+
"The examples are provided by the tldr-pages project <https://tldr.sh> under the CC BY 4.0 License. Please note that, as uutils is a work in progress, some examples might fail."
660+
)?;
661+
}
632662
Ok(s)
633663
}

0 commit comments

Comments
 (0)