File: improve-man.diff

package info (click to toggle)
rust-coreutils 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 505,620 kB
  • sloc: ansic: 103,594; asm: 28,570; sh: 8,910; python: 5,581; makefile: 472; cpp: 97; javascript: 72
file content (82 lines) | stat: -rw-r--r-- 3,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
--- a/src/bin/uudoc.rs
+++ b/src/bin/uudoc.rs
@@ -79,25 +79,45 @@
         .get_matches_from(std::iter::once(OsString::from("manpage")).chain(args));
 
     let utility = matches.get_one::<String>("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 {
-            if 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)
     };
 
-    let man = Man::new(command);
-    man.render(&mut io::stdout())
+    let stdout = &mut io::stdout();
+    Man::new(command)
+        .render(stdout)
         .expect("Man page generation failed");
-    io::stdout().flush().unwrap();
+    if let Some(content) = raw_examples {
+        use clap_mangen::roff::{bold, roman, Roff};
+        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 <https://tldr.sh> under the CC BY 4.0 License. Please note that, as uutils is a work in progress, some examples might fail.",
+        )]);
+        roff.to_writer(stdout).expect("Man page generation failed");
+    }
+    stdout.flush().unwrap();
     process::exit(0);
 }
 
@@ -555,6 +575,21 @@
     }
 }
 
+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<impl Read + Seek>, name: &str) -> Option<String> {