| 1 |
=================================================================== |
| 2 |
--- a/src/bin/uudoc.rs |
| 3 |
+++ b/src/bin/uudoc.rs |
| 4 |
@@ -144,25 +144,45 @@ fn gen_manpage<T: Args>( |
| 5 |
.get_matches_from(std::iter::once(OsString::from("manpage")).chain(args)); |
| 6 |
|
| 7 |
let utility = matches.get_one::<String>("utility").unwrap(); |
| 8 |
- let command = if utility == "coreutils" { |
| 9 |
- gen_coreutils_app(util_map) |
| 10 |
+ let (command, raw_examples) = if utility == "coreutils" { |
| 11 |
+ (gen_coreutils_app(util_map), None) |
| 12 |
} else { |
| 13 |
validation::setup_localization_or_exit(utility); |
| 14 |
let mut cmd = util_map.get(utility).unwrap().1(); |
| 15 |
cmd.set_bin_name(utility.clone()); |
| 16 |
- let mut cmd = cmd.display_name(utility); |
| 17 |
- if let Some(zip) = tldr |
| 18 |
- && let Ok(examples) = write_zip_examples(zip, utility, false) |
| 19 |
- { |
| 20 |
- cmd = cmd.after_help(examples); |
| 21 |
- } |
| 22 |
- cmd |
| 23 |
+ let cmd = cmd.display_name(utility); |
| 24 |
+ let raw = tldr.as_mut().and_then(|zip| { |
| 25 |
+ get_zip_content(zip, &format!("pages/common/{utility}.md")) |
| 26 |
+ .or_else(|| get_zip_content(zip, &format!("pages/linux/{utility}.md"))) |
| 27 |
+ }); |
| 28 |
+ (cmd, raw) |
| 29 |
}; |
| 30 |
|
| 31 |
// Generate the manpage to a buffer first so we can post-process it |
| 32 |
let mut buffer = Vec::new(); |
| 33 |
let man = Man::new(command); |
| 34 |
man.render(&mut buffer).expect("Man page generation failed"); |
| 35 |
+ if let Some(content) = raw_examples { |
| 36 |
+ use clap_mangen::roff::{Roff, bold, roman}; |
| 37 |
+ let mut roff = Roff::default(); |
| 38 |
+ roff.control("SH", ["EXAMPLES"]); |
| 39 |
+ for line in content.lines().skip_while(|l| !l.starts_with('-')) { |
| 40 |
+ if let Some(desc) = line.strip_prefix("- ") { |
| 41 |
+ roff.text([roman(desc)]); |
| 42 |
+ } else if line.starts_with('`') { |
| 43 |
+ let cmd = strip_placeholders(line.trim_matches('`')); |
| 44 |
+ roff.text([bold(cmd)]); |
| 45 |
+ } else if line.is_empty() { |
| 46 |
+ roff.control("PP", []); |
| 47 |
+ } |
| 48 |
+ } |
| 49 |
+ roff.control("PP", []); |
| 50 |
+ roff.text([roman( |
| 51 |
+ "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.", |
| 52 |
+ )]); |
| 53 |
+ roff.to_writer(&mut buffer) |
| 54 |
+ .expect("Man page generation failed"); |
| 55 |
+ } |
| 56 |
|
| 57 |
// Convert to string for processing |
| 58 |
let manpage = String::from_utf8(buffer).expect("Invalid UTF-8 in manpage"); |
| 59 |
@@ -637,6 +657,21 @@ impl MDWriter<'_, '_> { |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
+fn strip_placeholders(s: &str) -> String { |
| 64 |
+ let mut result = s.to_string(); |
| 65 |
+ loop { |
| 66 |
+ if let (Some(start), Some(rel_end)) = (result.find("{{"), result.find("}}")) { |
| 67 |
+ if start < rel_end { |
| 68 |
+ let inner = result[start + 2..rel_end].to_string(); |
| 69 |
+ result = format!("{}{}{}", &result[..start], inner, &result[rel_end + 2..]); |
| 70 |
+ continue; |
| 71 |
+ } |
| 72 |
+ } |
| 73 |
+ break; |
| 74 |
+ } |
| 75 |
+ result |
| 76 |
+} |
| 77 |
+ |
| 78 |
/// # Panics |
| 79 |
/// Panics if the archive is not ok |
| 80 |
fn get_zip_content(archive: &mut ZipArchive<impl Read + Seek>, name: &str) -> Option<String> { |