sequoia_man/
man.rs

1use std::path::{Path, PathBuf};
2
3use roff::{bold, italic, roman, Inline, Roff};
4
5/// The "manual" the manual page is meant for. The full Unix
6/// documentation is (or was) divided into separate manuals, some of
7/// which don't consist of manual pages.
8const MANUAL: &str = "User Commands";
9
10/// The "source" of the manual: who produced the manual.
11const SOURCE: &str = "Sequoia PGP";
12
13/// Emits a warning.
14macro_rules! warn {
15    {$($exp: expr),*} => {
16        println!("cargo:warning={}",
17                 format_args!($($exp),*));
18    };
19}
20
21/// Emits a warning and exits with an error.
22macro_rules! fail {
23    {$($exp: expr),*} => {
24        warn!($($exp),*);
25        std::process::exit(1);
26    };
27}
28
29/// Build a ManualPage or several.
30//
31/// The main command is sq itself. It can have multiple levels of
32/// subcommands, and we treat the leaves of the subcommand tree
33/// specially: every leaf gets a manual page, and every intermediate
34/// node gets an overview page. For example, "sq encrypt" is a leaf,
35/// as is "sq key generate", but "sq key" is not.
36pub struct Builder {
37    title: String,
38    section: String,
39    date: Option<String>,
40    source: Option<String>,
41    manual: Option<String>,
42    version: Option<String>,
43    see_also: Vec<String>,
44    maincmd: Command,
45    // The set of top-level, global options.  This is derived from
46    // maincmd.
47    top_level_global_args: Vec<CommandOption>,
48}
49
50impl Builder {
51    /// Return a builder to generate manual page.
52    ///
53    /// `cmd` is a `clap::Command` that has been built to represent
54    /// the sq command line interface. The manual pages are generated
55    /// automatically from that information.
56    ///
57    /// `version` is the bare version, which is usually obtained from
58    /// `env!("CARGO_PKG_VERSION")`.
59    ///
60    /// If `extra_version` is `Some`, then the version is created
61    /// `version (extra_version)`.
62    ///
63    /// For sequoia packages, this is usually:
64    /// `format!("sequoia-openpgp {}", sequoia_openpgp::VERSION)`.
65    pub fn new(cmd: &mut clap::Command,
66               version: &str, extra_version: Option<&str>)
67        -> Self
68    {
69        cmd.build();
70
71        let version = if let Some(extra_version) = extra_version {
72            format!("{} ({})", version, extra_version)
73        } else {
74            version.to_string()
75        };
76
77        let maincmd = Command::from_command(&[], cmd);
78        let options = maincmd.get_options();
79
80        // Also consider --help a global argument.  This argument is
81        // added by clap, so applications can't mark it as such.
82        let top_level_global_args = options.into_iter()
83            .filter(|a| a.global || a.long.as_deref() == Some("--help"))
84            .collect::<Vec<_>>();
85
86        Self {
87            title: cmd.get_name().into(),
88            section: "1".into(),
89            date: Some(version.clone()),
90            source: Some(SOURCE.to_string()),
91            manual: Some(MANUAL.to_string()),
92            version: Some(version),
93            see_also: Vec::new(),
94            maincmd,
95            top_level_global_args,
96        }
97    }
98
99    /// Returns whether the specified argument is a top-level global
100    /// argument.
101    fn is_top_level_global_arg(&self, arg: &CommandOption) -> bool {
102        self.top_level_global_args
103            .binary_search_by_key(&arg.sort_key(),
104                                  |a: &CommandOption| a.sort_key())
105            .is_ok()
106    }
107
108    /// Set the date for the manual page. This is typically typeset in
109    /// the center of the footer of the page.
110    pub fn date(&mut self, date: &str) -> &mut Self{
111        self.date = Some(date.into());
112        self
113    }
114
115    /// Set the source of the manual page. This is typically typeset on
116    /// left of the footer of the page.
117    pub fn source(&mut self, source: &str) -> &mut Self {
118        self.source = Some(source.into());
119        self
120    }
121
122    /// Set the manual this page belongs to. This is typically typeset
123    /// on the center of the header of the page.
124    pub fn manual(&mut self, manual: &str) -> &mut Self {
125        self.manual = Some(manual.into());
126        self
127    }
128
129    /// Return a one-line summary of the command. This goes in the NAME
130    /// section of the manual page.
131    fn summary(about: &str) -> String {
132        let line = if let Some(line) = about.lines().next() {
133            line
134        } else {
135            ""
136        };
137        line.to_string()
138    }
139
140    /// Text that is added to the see also section.
141    ///
142    /// For example:
143    ///
144    /// ```text
145    /// For the full documentation see <https://book.sequoia-pgp.org>.
146    /// ```
147    pub fn see_also<S>(&mut self, see_also: &[S]) -> &mut Self
148        where S: AsRef<str>
149    {
150        self.see_also = see_also.into_iter()
151            .map(|s| {
152                s.as_ref().to_string()
153            })
154            .collect();
155        self
156    }
157
158    /// Returns the name of the binary, e.g., `sq`, or `sq-git`.
159    pub fn binary(&self) -> String {
160        self.maincmd.name()
161    }
162
163    /// Build all manual pages for sq and one for each leaf subcommand.
164    pub fn build(&self) -> Vec<ManualPage> {
165        let mut pages = vec![];
166        self.maincmd.build(self, &mut pages);
167        pages
168    }
169
170    /// Set the title of the page.
171    fn th(&self, man: &mut ManualPage) {
172        let empty = String::new();
173        man.th(
174            &self.title.to_uppercase(),
175            &self.section.to_uppercase(),
176            self.date.as_ref().unwrap_or(&empty),
177            self.source.as_ref().unwrap_or(&empty),
178            self.manual.as_ref().unwrap_or(&empty),
179        )
180    }
181}
182
183/// The command for which we generate a manual page.
184//
185/// We collect all the information about a command here so that it's
186/// handy when we generate various parts of a manual page that includes
187/// this command.
188#[derive(Debug, PartialEq, Eq)]
189struct Command {
190    command_words: Vec<String>,
191    before_help: Option<String>,
192    after_help: Option<String>,
193    about: Option<String>,
194    long_about: Option<String>,
195    options: Vec<CommandOption>,
196    args: Vec<String>,
197    examples: Vec<String>,
198    exit_status: Vec<String>,
199    subcommands: Vec<Command>,
200}
201
202impl Command {
203    /// Create a new `Command`. The command words are the part of
204    /// the command line that invokes this command. For sq itself,
205    /// they're `["sq"]`, but for a subcommand they might be `["sq",
206    /// "key", "generate"]` for example.
207    fn new(command_words: Vec<String>) -> Self {
208        assert!(! command_words.is_empty());
209        Self {
210            command_words,
211            before_help: None,
212            after_help: None,
213            about: None,
214            long_about: None,
215            options: vec![],
216            args: vec![],
217            examples: vec![],
218            exit_status: vec![],
219            subcommands: vec![],
220        }
221    }
222
223    /// Returns whether the command is the top-level command.
224    fn top_level(&self) -> bool {
225        self.command_words.len() == 1
226    }
227
228    /// Returns whether the command is a leaf command.
229    ///
230    /// A leaf command is not the top-level command and has no subcommands.
231    fn leaf(&self) -> bool {
232        ! self.top_level() && self.subcommands.is_empty()
233    }
234
235    /// Returns whether this it command is a singleton, i.e., it is
236    /// the top-level command, and it has no subcommands.
237    fn singleton(&self) -> bool {
238        self.top_level() && self.subcommands.is_empty()
239    }
240
241    /// Return the name of the command, with command words separated by
242    /// spaces. This is suitable for, say, the NAME section.
243    fn name(&self) -> String {
244        self.command_words.join(" ")
245    }
246
247    /// Return name of the subcommand, without the main command name.
248    fn subcommand_name(&self) -> String {
249        let mut words = self.command_words.clone();
250        words.remove(0);
251        words.join(" ")
252    }
253
254    /// Return the name of the manual page for this command. This is
255    /// the command words separated by dashes. Thus "sq key generate"
256    /// would return "sq-key-generate". Manual page names mustn't
257    /// contain spaces, thus the dash.
258    fn manpage_name(&self) -> String {
259        self.command_words.join("-")
260    }
261
262    /// Return the description of the command. This is collected from
263    /// the various about and help texts given to `clap`.
264    fn description(&self) -> String {
265        let mut desc = String::new();
266        if let Some(text) = &self.before_help {
267            desc.push_str(text);
268            desc.push('\n');
269        }
270
271        if let Some(text) = &self.long_about {
272            desc.push_str(text);
273            desc.push('\n');
274        } else if let Some(text) = &self.about {
275            desc.push_str(text);
276            desc.push('\n');
277        }
278
279        if let Some(text) = &self.after_help {
280            desc.push_str(text);
281            desc.push('\n');
282        }
283        desc
284    }
285
286    /// Add the `before_help` help text for this command.
287    fn before_help(&mut self, help: &str) {
288        self.before_help = Some(self.extract_all(help));
289    }
290
291    /// Add the `after_help` help text for this command.
292    fn after_help(&mut self, help: &str) {
293        self.after_help = Some(self.extract_all(help));
294    }
295
296    /// Add the `about` help text for this command.
297    fn about(&mut self, help: &str) {
298        self.about = Some(self.extract_all(help));
299    }
300
301    /// Add the `long_about` help text for this command.
302    fn long_about(&mut self, help: &str) {
303        self.long_about = Some(self.extract_all(help));
304    }
305
306    /// Add an option to this command.
307    fn option(&mut self, opt: CommandOption) {
308        self.options.push(opt);
309    }
310
311    /// Add a positional argument to this command.
312    fn arg(&mut self, arg: &str) {
313        self.args.push(arg.into());
314    }
315
316    /// Extracts all additional sections.
317    fn extract_all(&mut self, text: &str) -> String {
318        let text = self.extract_example(text);
319        let text = self.extract_exit_status(&text);
320        text
321    }
322
323    /// Extract examples from help text: anything that follows a line
324    /// consisting of "Examples:".
325    ///
326    /// This is a convention specific to sq, not something that comes
327    /// from `clap`.
328    fn extract_example(&mut self, text: &str) -> String {
329        Self::extract("Examples:\n", &mut self.examples, text)
330    }
331
332    /// Extract exit status from help text: anything that follows a line
333    /// consisting of "Exit status:".
334    ///
335    /// This is a convention specific to sq, not something that comes
336    /// from `clap`.
337    fn extract_exit_status(&mut self, text: &str) -> String {
338        Self::extract("Exit status:\n", &mut self.exit_status, text)
339    }
340
341    /// Extracts sections, like examples, putting them into `into`.
342    fn extract(marker: &str, into: &mut Vec<String>, text: &str) -> String {
343        if let Some(pos) = text.find(marker) {
344            let (text, ex) = text.split_at(pos);
345            if let Some(ex) = ex.strip_prefix(marker) {
346                into.push(ex.into());
347            } else {
348                into.push(ex.into());
349            }
350            text.into()
351        } else {
352            text.into()
353        }
354    }
355
356    /// Does this command have any options?
357    fn has_options(&self) -> bool {
358        !self.options.is_empty()
359    }
360
361    /// Get the list of options for this command.
362    fn get_options(&self) -> Vec<CommandOption> {
363        let mut opts = self.options.clone();
364        opts.sort_by_cached_key(|opt| opt.sort_key());
365        opts
366    }
367
368    /// Does this command have examples?
369    fn has_examples(&self) -> bool {
370        !self.examples.is_empty()
371    }
372
373    /// Create a new `Command` from a `clap::Command` structure.
374    fn from_command(parent: &[String], cmd: &clap::Command) -> Self {
375        let mut words: Vec<String> = parent.into();
376        words.push(cmd.get_name().to_string());
377        let mut new = Self::new(words);
378        if let Some(text) = cmd.get_before_help() {
379            new.before_help(&text.to_string());
380        }
381        if let Some(text) = cmd.get_after_help() {
382            new.after_help(&text.to_string());
383        }
384        if let Some(text) = cmd.get_about() {
385            new.about(&text.to_string());
386        }
387        if let Some(text) = cmd.get_long_about() {
388            new.long_about(&text.to_string());
389        }
390        for arg in cmd.get_arguments() {
391            new.option(CommandOption::from_arg(arg));
392        }
393        for arg in cmd.get_positionals() {
394            if let Some(names) = arg.get_value_names() {
395                for name in names {
396                    new.arg(name);
397                }
398            }
399        }
400        let mut parent = parent.to_vec();
401        parent.push(cmd.get_name().into());
402        new.subcommands = cmd.get_subcommands()
403            .filter(|cmd| cmd.get_name() != "help")
404            .map(|cmd| Command::from_command(&parent, cmd))
405            .collect();
406        new
407    }
408
409    /// Builds manpages recursively.
410    fn build(&self, builder: &Builder, acc: &mut Vec<ManualPage>) {
411        acc.push(self.build_command(builder));
412        for cmd in &self.subcommands {
413            cmd.build(builder, acc);
414        }
415    }
416
417    /// Build a manual page for an intermediate (i.e. non-leaf) command.
418    fn build_command(&self, builder: &Builder) -> ManualPage {
419        let filename = format!("{}.{}", self.manpage_name(), builder.section);
420        let mut man = ManualPage::new(PathBuf::from(filename));
421        builder.th(&mut man);
422
423        let about = &self.about.clone().unwrap();
424        let summary = Builder::summary(about);
425        man.name_section(&self.manpage_name(), &summary);
426
427        man.section("SYNOPSIS");
428        let bin_name = builder.binary();
429        if self.singleton() || self.leaf() {
430            // Show the command.
431            man.subcommand_synopsis(
432                &bin_name,
433                &self.subcommand_name(),
434                self.has_options(),
435                &self.args,
436                true,
437            );
438        } else {
439            // Show the subcommands.
440            for sub in &self.subcommands {
441                man.subcommand_synopsis(
442                    &bin_name,
443                    &sub.subcommand_name(),
444                    sub.has_options(),
445                    &sub.args,
446                    sub.leaf(),
447                );
448            }
449        }
450
451        man.section("DESCRIPTION");
452        man.text_with_period(&self.description());
453
454        // Show the options for top-level and leaf commands.
455        if self.top_level() || self.leaf()  {
456            let mut showed_options_header = false;
457
458            // Show command-specific arguments.
459            let mut self_opts = self.get_options();
460            if ! self.singleton() {
461                self_opts = self_opts.into_iter()
462                    .filter(|o| ! builder.is_top_level_global_arg(o))
463                    .collect::<Vec<_>>();
464            }
465
466            if ! self_opts.is_empty() {
467                if ! showed_options_header {
468                    showed_options_header = true;
469                    man.section("OPTIONS");
470                }
471
472                if ! self.top_level() {
473                    // This is not the top-level command.
474                    man.subsection("Subcommand options");
475                }
476
477                for opt in self_opts.iter() {
478                    man.option(opt);
479                }
480            }
481
482            // Show the global arguments at the top-level.  Otherwise,
483            // reference the top-level command.
484            let global_options = &builder.top_level_global_args;
485            if ! self.singleton() && ! global_options.is_empty() {
486                #[allow(unused_assignments)]
487                if ! showed_options_header {
488                    showed_options_header = true;
489                    man.section("OPTIONS");
490                }
491
492                man.subsection("Global options");
493                if self.top_level() {
494                    // At the top-level, we print out the top-level
495                    // global options.
496                    for opt in global_options.iter() {
497                        man.option(opt);
498                    }
499                } else {
500                    // Non-top-level commands just reference the top
501                    // level's documentation.
502                    man.roff.text(vec![
503                        roman("See "),
504                        bold(bin_name), roman("("), roman("1"), roman(")"),
505                        roman(" for a description of the global options."),
506                    ]);
507                }
508            }
509        }
510
511        if ! self.subcommands.is_empty() {
512            man.section("SUBCOMMANDS");
513            for sub in &self.subcommands {
514                let desc = sub.description();
515                if !desc.is_empty() {
516                    man.subsection(&sub.name());
517                    man.text_with_period(&desc);
518                }
519            }
520        }
521
522        man.exit_status_section(self);
523
524        // Maybe add environment section.
525        if self == &builder.maincmd {
526            let opts = self.get_options();
527            let mut envs: Vec<_> =
528                opts.iter().filter(|o| o.env.is_some()).collect();
529            envs.sort_by_key(|o| o.env.as_ref());
530
531            if ! envs.is_empty() {
532                man.section("ENVIRONMENT");
533                for opt in envs {
534                    man.env_option(opt);
535                }
536            }
537        }
538
539        if self.leaf() {
540            man.examples_section(&[self]);
541        } else {
542            man.examples_section(&self.subcommands.iter().collect::<Vec<_>>());
543        }
544
545        let mut see_also_shown = false;
546        let names: Vec<String> = (1..self.command_words.len())
547            .map(|n| self.command_words[0..n].join("-"))
548            .chain(self.subcommands.iter().map(|sub| sub.manpage_name()))
549            .collect();
550        if ! names.is_empty() {
551            if ! see_also_shown {
552                man.section("SEE ALSO");
553                see_also_shown = true;
554            }
555
556            man.man_page_refs(&names, &builder.section);
557            man.paragraph();
558        }
559        if ! builder.see_also.is_empty() {
560            #[allow(unused_assignments)]
561            if ! see_also_shown {
562                man.section("SEE ALSO");
563                see_also_shown = true;
564            }
565
566            for (i, see_also) in builder.see_also.iter().enumerate() {
567                if i > 0 {
568                    man.paragraph();
569                }
570                man.text(see_also);
571            }
572        }
573
574        man.version_section(&builder.version);
575
576        man
577    }
578}
579
580/// Represent a command line option for manual page generation.
581//
582/// This doesn't capture all the things that `clap` allows, but is
583/// sufficient for what sq actually uses.
584#[derive(Clone, Debug, PartialEq, Eq)]
585struct CommandOption {
586    /// Index for positional arguments.
587    index: Option<usize>,
588    short: Option<String>,
589    long: Option<String>,
590    /// Whether the global flag is set.
591    global: bool,
592    env: Option<String>,
593    value_names: Values,
594    possible_values: Vec<String>,
595    default_values: Vec<String>,
596    help: Option<String>,
597}
598
599/// Formal arguments for a command option.
600#[derive(Clone, Debug, PartialEq, Eq)]
601enum Values {
602    /// This option does not take any arguments.
603    None,
604
605    /// This option takes the given arguments.
606    Some(Vec<String>),
607
608    /// This option optionally takes the given arguments.
609    ///
610    /// XXX: Currently, this is all or nothing, i.e. either all
611    /// arguments are mandatory, or all are optional.  Let's see
612    /// whether we need something more complicated.
613    Optional(Vec<String>),
614}
615
616impl CommandOption {
617    /// Return a key for sorting a list of options.
618    ///
619    /// Manual pages list options in various places, and it enables
620    /// quicker lookup by readers if they lists are sorted
621    /// alphabetically.  By convention, such lists are sorted by short
622    /// option first, if one exists.  And, positional arguments are
623    /// sorted to the bottom, and in the order they appear on the
624    /// command line.
625    fn sort_key(&self) -> (usize, String) {
626        let mut key = String::new();
627        if let Some(name) = &self.short {
628            key.push_str(name.strip_prefix('-').unwrap());
629            key.push(',');
630        }
631        if let Some(name) = &self.long {
632            key.push_str(name.strip_prefix("--").unwrap());
633        }
634        (self.index.unwrap_or(0), key)
635    }
636}
637
638impl CommandOption {
639    /// Create a `CommandOption` from a `clap::Arg`.
640    fn from_arg(arg: &clap::Arg) -> Self {
641        let num_args = arg.get_num_args().unwrap_or_default();
642        let value_names = if num_args.takes_values() {
643            let names = arg.get_value_names()
644                .unwrap_or(&[])
645                .into_iter()
646                .map(|s| s.to_string())
647                .collect::<Vec<String>>();
648
649            if num_args.min_values() == 0 {
650                Values::Optional(names)
651            } else {
652                Values::Some(names)
653            }
654        } else {
655            // Flag.
656            Values::None
657        };
658
659        let long = arg.get_long().map(|o| format!("--{}", o));
660        let global = arg.is_global_set();
661        if global {
662            assert!(long.is_some(),
663                    "Global options must have a long name.");
664        }
665
666        Self {
667            index: arg.get_index(),
668            short: arg.get_short().map(|o| format!("-{}", o)),
669            long,
670            global,
671            value_names,
672            env: arg.get_env().and_then(|e| e.to_str().map(Into::into)),
673            possible_values: arg.get_possible_values().iter()
674                .map(|o| o.get_name().into()).collect(),
675            default_values: if num_args.takes_values() {
676                    // At least one argument.
677                    arg.get_default_values().iter()
678                        .map(|s| s.to_string_lossy().to_string())
679                        .collect()
680                } else {
681                    // No argument, i.e. a flag.  We don't want to
682                    // output `[default: false]` in that case.
683                    vec![]
684                },
685            help: arg.get_long_help().or(arg.get_help())
686                .map(|s| s.to_string()),
687        }
688    }
689}
690
691/// Troff code for a manual page.
692///
693/// The code is in [`troff`](https://en.wikipedia.org/wiki/Troff)
694/// format, as is usual for Unix manual page documentation. It's using
695/// the `man` macro package for `troff`.
696pub struct ManualPage {
697    filename: PathBuf,
698    roff: Roff,
699
700    // Are we in code mode (emitted .nf)?
701    in_code: bool,
702}
703
704impl ManualPage {
705    fn new(filename: PathBuf) -> Self {
706        Self {
707            filename,
708            roff: Roff::new(),
709            in_code: false,
710        }
711    }
712
713    /// Set the title of the manual page. The "TH" macro takes five
714    /// arguments: name of the command; section of the manual; the date
715    /// of latest update; the source of manual; and the name of the manual.
716    fn th(&mut self, name: &str, section: &str, date: &str, source: &str, manual: &str) {
717        self.roff
718            .control("TH", [name, section, date, source, manual]);
719    }
720
721    /// Typeset the NAME section: the title, and a line with the name
722    /// of the command, followed by a dash, and a one-line. The dash
723    /// should be escaped with backslash, but the `roff` crate does
724    /// that for us.
725    fn name_section(&mut self, name: &str, summary: &str) {
726        self.section("NAME");
727        self.roff.text([roman(&format!("{} - {}", name, summary))]);
728    }
729
730    /// Typeset code blocks, joining adjacent blocks.
731    fn code(&mut self, code: bool) {
732        match (self.in_code, code) {
733            (false, false) => (),
734            (false, true) => {
735                self.roff.control("nf", []);
736                self.in_code = true;
737            },
738            (true, false) => {
739                self.roff.control("fi", []);
740                self.in_code = false;
741            },
742            (true, true) => (),
743        }
744    }
745
746    /// Typeset the synopsis of a command. This is going to be part of
747    /// the SYNOPSIS section. There are conventions for how it should
748    /// be typeset. For sq, we simplify them by summarizing options
749    /// into a placeholder, and only listing command words and
750    /// positional arguments.
751    fn subcommand_synopsis(
752        &mut self,
753        bin: &str,
754        sub: &str,
755        sub_options: bool,
756        args: &[String],
757        is_leaf: bool,
758    ) {
759        let mut line = vec![
760            bold(if sub.is_empty() {
761                bin.to_string()
762            } else {
763                format!("{} {}", bin, sub)
764            }),
765            roman(" ["), italic("OPTIONS"), roman("] "),
766        ];
767
768        for (i, arg) in args.iter().enumerate() {
769            if i > 0 || ! sub_options {
770                line.push(roman(" "));
771            }
772            line.push(italic(arg));
773        }
774
775        if args.is_empty() {
776            line.push(roman(" "));
777        }
778
779        if ! is_leaf {
780            line.push(italic("SUBCOMMAND"));
781        }
782
783        self.roff.control("br", []);
784        self.roff.text(line);
785    }
786
787    /// Typeset an option, for the OPTIONS section. This is typeset
788    /// using "tagged paragraphs", where the first line lists the
789    /// aliases of the option, and any values it may take, and the rest
790    /// is indented paragraphs of text explaining what the option does.
791    fn option(&mut self, opt: &CommandOption) {
792        let mut line = vec![];
793
794        if let Some(short) = &opt.short {
795            line.push(bold(short));
796        }
797        if let Some(long) = &opt.long {
798            if opt.short.is_some() {
799                line.push(roman(", "));
800            }
801            line.push(bold(long));
802        }
803
804        match &opt.value_names {
805            Values::None => (),
806            Values::Some(values) | Values::Optional(values) => {
807                if matches!(opt.value_names, Values::Optional(_)) {
808                    line.push(roman("["));
809                }
810
811                if (opt.short.is_some() || opt.long.is_some())
812                    && values.len() == 1
813                {
814                    line.push(roman("="));
815                    line.push(italic(&values[0]));
816                } else {
817                    for value in values {
818                        line.push(roman(" "));
819                        line.push(italic(value));
820                    }
821                }
822
823                if matches!(opt.value_names, Values::Optional(_)) {
824                    line.push(roman("]"));
825                }
826            },
827        }
828
829        self.tagged_paragraph(line, &opt.help);
830
831        if ! opt.default_values.is_empty() {
832            self.indented_paragraph();
833            let mut line = vec![];
834            line.push(roman("[default: "));
835            for (i, v) in opt.default_values.iter().enumerate() {
836                if i > 0 {
837                    line.push(roman(", "));
838                }
839                line.push(bold(v));
840            }
841            line.push(roman("]"));
842            self.roff.text(line);
843        }
844
845        if ! opt.possible_values.is_empty() {
846            self.indented_paragraph();
847            let mut line = vec![];
848            line.push(roman("[possible values: "));
849            for (i, v) in opt.possible_values.iter().enumerate() {
850                if i > 0 {
851                    line.push(roman(", "));
852                }
853                line.push(bold(v));
854            }
855            line.push(roman("]"));
856            self.roff.text(line);
857        }
858    }
859
860    /// Typeset an option, for the ENVIRONMENT section.
861    ///
862    /// This is typeset using "tagged paragraphs", where the first
863    /// line lists the aliases of the option, and any values it may
864    /// take, and the rest is indented paragraphs of text explaining
865    /// what the option does.
866    fn env_option(&mut self, opt: &CommandOption) {
867        let mut line = vec![
868            bold(opt.env.as_ref().expect("must be an env")),
869        ];
870
871        match &opt.value_names {
872            Values::None => (),
873            Values::Some(values) | Values::Optional(values) => {
874                assert_eq!(values.len(), 1);
875                line.push(roman("="));
876                line.push(italic(&values[0]));
877            },
878        }
879
880        self.tagged_paragraph(line, &opt.help);
881
882        if ! opt.default_values.is_empty() {
883            self.indented_paragraph();
884            let mut line = vec![];
885            line.push(roman("[default: "));
886            for (i, v) in opt.default_values.iter().enumerate() {
887                if i > 0 {
888                    line.push(roman(", "));
889                }
890                line.push(bold(v));
891            }
892            line.push(roman("]"));
893            self.roff.text(line);
894        }
895
896        if ! opt.possible_values.is_empty() {
897            self.indented_paragraph();
898            let mut line = vec![];
899            line.push(roman("[possible values: "));
900            for (i, v) in opt.possible_values.iter().enumerate() {
901                if i > 0 {
902                    line.push(roman(", "));
903                }
904                line.push(bold(v));
905            }
906            line.push(roman("]"));
907            self.roff.text(line);
908        }
909    }
910
911    /// Typeset an EXIT STATUS section, if available.
912    fn exit_status_section(&mut self, c: &Command) {
913        if c.exit_status.is_empty() {
914            return;
915        }
916
917        self.section("EXIT STATUS");
918        for chunk in &c.exit_status {
919            self.text(&chunk);
920        }
921    }
922
923    /// Typeset an EXAMPLES section, if a command has examples.
924    fn examples_section(&mut self, subs: &[&Command]) {
925        let leaves = subs.iter().filter(|s| s.leaf()).collect::<Vec<_>>();
926        if !leaves.iter().any(|leaf| leaf.has_examples()) {
927            return;
928        }
929
930        self.section("EXAMPLES");
931        let mut need_para = false;
932        let need_subsections = leaves.len() > 1;
933        for leaf in leaves.iter() {
934            if need_para {
935                self.paragraph();
936                need_para = false;
937            }
938
939            if !leaf.examples.is_empty() {
940                if need_subsections {
941                    self.subsection(&leaf.name());
942                    need_para = false;
943                }
944
945                for ex in leaf.examples.iter() {
946                    // Was the last line a continuation?
947                    let mut continuation = false;
948
949                    // Was the last line part of the description?
950                    let mut description = false;
951
952                    for line in ex.lines() {
953                        if ! continuation
954                            && ! (description && line.starts_with("#"))
955                        {
956                            self.paragraph();
957                        }
958
959                        const TARGET_LINE_LENGTH: usize = 78;
960                        const RS_INDENTATION: usize = 7;
961                        const EXAMPLE_COMMAND_MAX_WIDTH: usize =
962                            TARGET_LINE_LENGTH - 2 * RS_INDENTATION;
963                        const EXAMPLE_CONTINUATION_MAX_WIDTH: usize =
964                            TARGET_LINE_LENGTH - 3 * RS_INDENTATION;
965
966                        if let Some(line) = line.strip_prefix("# ") {
967                            self.code(false);
968                            self.roff.text([roman(line)]);
969                        } else if let Some(line) = line.strip_prefix("$ ") {
970                            let line = line.trim();
971                            if line.len() > EXAMPLE_COMMAND_MAX_WIDTH {
972                                warn!("Command in example exceeds {} chars:",
973                                      EXAMPLE_COMMAND_MAX_WIDTH);
974                                fail!("{} ({} chars)", line, line.len());
975                            }
976                            self.code(true);
977                            self.roff.control("RS", []);
978                            self.roff.text([roman(line)]);
979                            self.roff.control("RE", []);
980                        } else if continuation {
981                            let line = line.trim();
982                            if line.len() > EXAMPLE_CONTINUATION_MAX_WIDTH {
983                                warn!("Continuation in example exceeds {} chars:",
984                                      EXAMPLE_CONTINUATION_MAX_WIDTH);
985                                fail!("{} ({} chars)", line, line.len());
986                            }
987                            self.code(true);
988                            self.roff.control("RS", []);
989                            self.roff.control("RS", []);
990                            self.roff.text([roman(line)]);
991                            self.roff.control("RE", []);
992                            self.roff.control("RE", []);
993                        } else {
994                            self.code(false);
995                            self.roff.text([roman(line)]);
996                        }
997
998                        // Update continuation for the next loop iteration.
999                        continuation = line.ends_with("\\");
1000
1001                        // Update description for the next loop iteration.
1002                        description = line.starts_with("#");
1003
1004                        // We emitted at least one example, make sure
1005                        // to add a new paragraph before going to the
1006                        // next command.
1007                        need_para = true;
1008                    }
1009                }
1010
1011                self.code(false);
1012            }
1013        }
1014    }
1015
1016    /// Typeset the VERSION section, if the main command has a version
1017    /// set.
1018    fn version_section(&mut self, version: &Option<String>) {
1019        if let Some(v) = version {
1020            self.section("VERSION");
1021            self.roff.text([roman(v)]);
1022        }
1023    }
1024
1025    /// Start a new section with the SH troff command.
1026    fn section(&mut self, heading: &str) {
1027        self.roff.control("SH", [heading]);
1028    }
1029
1030    /// Start a new subsection with the SS troff command.
1031    fn subsection(&mut self, heading: &str) {
1032        self.roff.control("SS", [heading]);
1033    }
1034
1035    /// Start a new paragraph with the PP troff command.
1036    fn paragraph(&mut self) {
1037        self.roff.control("PP", []);
1038    }
1039
1040    /// Start a new indented paragraph with the IP troff command.
1041    fn indented_paragraph(&mut self) {
1042        self.roff.control("IP", []);
1043    }
1044
1045    /// Start a tagged paragraph with th TP troff command.
1046    ///
1047    /// This command takes the line after the command and typesets it,
1048    /// and the line after that starts an indented paragraph.
1049    ///
1050    /// Additionally, if `text` is given, it is rendered after the
1051    /// `line`, indenting it.
1052    fn tagged_paragraph(&mut self, line: Vec<Inline>, text: &Option<String>) {
1053        self.roff.control("TP", []);
1054        self.roff.text(line);
1055
1056        if let Some(text) = text {
1057            let mut paras = text.split("\n\n");
1058            if let Some(first) = paras.next() {
1059                self.roff.text([roman(first)]);
1060            }
1061            for para in paras {
1062                self.roff.control("IP", []);
1063                self.roff.text([roman(para)]);
1064            }
1065        }
1066    }
1067
1068    /// Typeset a list of references to manual pages, suitable for the
1069    /// SEE ALSO section. Manual page references are, by convention,
1070    /// typeset with the name of manual page in bold, and the section
1071    /// of the page in normal ("roman") font, enclosed in parentheses.
1072    /// The references are separated by commas, in normal font.
1073    fn man_page_refs(&mut self, names: &[String], section: &str) {
1074        let mut line = vec![];
1075        for name in names.iter() {
1076            if !line.is_empty() {
1077                line.push(roman(", "));
1078            }
1079            line.push(bold(name));
1080            line.push(roman("("));
1081            line.push(roman(section));
1082            line.push(roman(")"));
1083        }
1084        line.push(roman("."));
1085
1086        self.roff.control("nh", []);
1087        self.roff.text(line);
1088        self.roff.control("hy", []);
1089    }
1090
1091    /// Typeset normal text consisting of paragraphs. Paragraphs are
1092    /// separated by an empty line. All but the first paragraph are
1093    /// preceded by the troff paragraph command. The first one is not,
1094    /// to avoid unwanted empty lines in the output.
1095    fn text(&mut self, text: &str) {
1096        let mut paras = text.split("\n\n");
1097        if let Some(first) = paras.next() {
1098            self.roff.text([roman(first)]);
1099        }
1100        for para in paras {
1101            self.paragraph();
1102            self.roff.text([roman(para)]);
1103        }
1104    }
1105
1106    /// Like [`ManualPage::text`], but add a period, if missing, to the end of
1107    /// the first paragraph. In `clap` about texts, the first line
1108    /// conventionally doesn't end in a period, but in manual pages,
1109    /// when that text is used in a DESCRIPTION section, it should have
1110    /// a period.
1111    fn text_with_period(&mut self, text: &str) {
1112        let mut paras = text.split("\n\n");
1113        if let Some(first) = paras.next() {
1114            let first = if let Some(prefix) = first.strip_suffix(".\n") {
1115                format!("{}.", prefix)
1116            } else if let Some(prefix) = first.strip_suffix('\n') {
1117                format!("{}.", prefix)
1118            } else if first.ends_with('.') {
1119                first.to_string()
1120            } else {
1121                format!("{}.", first)
1122            };
1123            self.roff.text([roman(first)]);
1124        }
1125        for para in paras {
1126            self.paragraph();
1127            self.roff.text([roman(para)]);
1128        }
1129    }
1130
1131    /// What should the filename be, on disk, for this manual page?
1132    pub fn filename(&self) -> &Path {
1133        &self.filename
1134    }
1135
1136    /// Return the `troff` source code for the manual page.
1137    pub fn troff_source(&self) -> String {
1138        self.roff.to_roff()
1139    }
1140}