=================================================================== --- a/src/bin/uudoc.rs +++ b/src/bin/uudoc.rs @@ -144,25 +144,45 @@ fn gen_manpage( .get_matches_from(std::iter::once(OsString::from("manpage")).chain(args)); let utility = matches.get_one::("utility").unwrap(); - let command = if utility == "coreutils" { - gen_coreutils_app(util_map) + let (command, raw_examples) = if utility == "coreutils" { + (gen_coreutils_app(util_map), None) } else { validation::setup_localization_or_exit(utility); let mut cmd = util_map.get(utility).unwrap().1(); cmd.set_bin_name(utility.clone()); - let mut cmd = cmd.display_name(utility); - if let Some(zip) = tldr - && let Ok(examples) = write_zip_examples(zip, utility, false) - { - cmd = cmd.after_help(examples); - } - cmd + let cmd = cmd.display_name(utility); + let raw = tldr.as_mut().and_then(|zip| { + get_zip_content(zip, &format!("pages/common/{utility}.md")) + .or_else(|| get_zip_content(zip, &format!("pages/linux/{utility}.md"))) + }); + (cmd, raw) }; // Generate the manpage to a buffer first so we can post-process it let mut buffer = Vec::new(); let man = Man::new(command); man.render(&mut buffer).expect("Man page generation failed"); + if let Some(content) = raw_examples { + use clap_mangen::roff::{Roff, bold, roman}; + let mut roff = Roff::default(); + roff.control("SH", ["EXAMPLES"]); + for line in content.lines().skip_while(|l| !l.starts_with('-')) { + if let Some(desc) = line.strip_prefix("- ") { + roff.text([roman(desc)]); + } else if line.starts_with('`') { + let cmd = strip_placeholders(line.trim_matches('`')); + roff.text([bold(cmd)]); + } else if line.is_empty() { + roff.control("PP", []); + } + } + roff.control("PP", []); + roff.text([roman( + "The examples are provided by the tldr-pages project under the CC BY 4.0 License. Please note that, as uutils is a work in progress, some examples might fail.", + )]); + roff.to_writer(&mut buffer) + .expect("Man page generation failed"); + } // Convert to string for processing let manpage = String::from_utf8(buffer).expect("Invalid UTF-8 in manpage"); @@ -637,6 +657,21 @@ impl MDWriter<'_, '_> { } } +fn strip_placeholders(s: &str) -> String { + let mut result = s.to_string(); + loop { + if let (Some(start), Some(rel_end)) = (result.find("{{"), result.find("}}")) { + if start < rel_end { + let inner = result[start + 2..rel_end].to_string(); + result = format!("{}{}{}", &result[..start], inner, &result[rel_end + 2..]); + continue; + } + } + break; + } + result +} + /// # Panics /// Panics if the archive is not ok fn get_zip_content(archive: &mut ZipArchive, name: &str) -> Option {