1use std::path::{Path, PathBuf};
2
3use roff::{bold, italic, roman, Inline, Roff};
4
5const MANUAL: &str = "User Commands";
9
10const SOURCE: &str = "Sequoia PGP";
12
13macro_rules! warn {
15 {$($exp: expr),*} => {
16 println!("cargo:warning={}",
17 format_args!($($exp),*));
18 };
19}
20
21macro_rules! fail {
23 {$($exp: expr),*} => {
24 warn!($($exp),*);
25 std::process::exit(1);
26 };
27}
28
29pub 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 top_level_global_args: Vec<CommandOption>,
48}
49
50impl Builder {
51 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 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 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 pub fn date(&mut self, date: &str) -> &mut Self{
111 self.date = Some(date.into());
112 self
113 }
114
115 pub fn source(&mut self, source: &str) -> &mut Self {
118 self.source = Some(source.into());
119 self
120 }
121
122 pub fn manual(&mut self, manual: &str) -> &mut Self {
125 self.manual = Some(manual.into());
126 self
127 }
128
129 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 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 pub fn binary(&self) -> String {
160 self.maincmd.name()
161 }
162
163 pub fn build(&self) -> Vec<ManualPage> {
165 let mut pages = vec![];
166 self.maincmd.build(self, &mut pages);
167 pages
168 }
169
170 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#[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 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 fn top_level(&self) -> bool {
225 self.command_words.len() == 1
226 }
227
228 fn leaf(&self) -> bool {
232 ! self.top_level() && self.subcommands.is_empty()
233 }
234
235 fn singleton(&self) -> bool {
238 self.top_level() && self.subcommands.is_empty()
239 }
240
241 fn name(&self) -> String {
244 self.command_words.join(" ")
245 }
246
247 fn subcommand_name(&self) -> String {
249 let mut words = self.command_words.clone();
250 words.remove(0);
251 words.join(" ")
252 }
253
254 fn manpage_name(&self) -> String {
259 self.command_words.join("-")
260 }
261
262 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 fn before_help(&mut self, help: &str) {
288 self.before_help = Some(self.extract_all(help));
289 }
290
291 fn after_help(&mut self, help: &str) {
293 self.after_help = Some(self.extract_all(help));
294 }
295
296 fn about(&mut self, help: &str) {
298 self.about = Some(self.extract_all(help));
299 }
300
301 fn long_about(&mut self, help: &str) {
303 self.long_about = Some(self.extract_all(help));
304 }
305
306 fn option(&mut self, opt: CommandOption) {
308 self.options.push(opt);
309 }
310
311 fn arg(&mut self, arg: &str) {
313 self.args.push(arg.into());
314 }
315
316 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 fn extract_example(&mut self, text: &str) -> String {
329 Self::extract("Examples:\n", &mut self.examples, text)
330 }
331
332 fn extract_exit_status(&mut self, text: &str) -> String {
338 Self::extract("Exit status:\n", &mut self.exit_status, text)
339 }
340
341 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 fn has_options(&self) -> bool {
358 !self.options.is_empty()
359 }
360
361 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 fn has_examples(&self) -> bool {
370 !self.examples.is_empty()
371 }
372
373 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 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 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 man.subcommand_synopsis(
432 &bin_name,
433 &self.subcommand_name(),
434 self.has_options(),
435 &self.args,
436 true,
437 );
438 } else {
439 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 if self.top_level() || self.leaf() {
456 let mut showed_options_header = false;
457
458 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 man.subsection("Subcommand options");
475 }
476
477 for opt in self_opts.iter() {
478 man.option(opt);
479 }
480 }
481
482 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 for opt in global_options.iter() {
497 man.option(opt);
498 }
499 } else {
500 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 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#[derive(Clone, Debug, PartialEq, Eq)]
585struct CommandOption {
586 index: Option<usize>,
588 short: Option<String>,
589 long: Option<String>,
590 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#[derive(Clone, Debug, PartialEq, Eq)]
601enum Values {
602 None,
604
605 Some(Vec<String>),
607
608 Optional(Vec<String>),
614}
615
616impl CommandOption {
617 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 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 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 arg.get_default_values().iter()
678 .map(|s| s.to_string_lossy().to_string())
679 .collect()
680 } else {
681 vec![]
684 },
685 help: arg.get_long_help().or(arg.get_help())
686 .map(|s| s.to_string()),
687 }
688 }
689}
690
691pub struct ManualPage {
697 filename: PathBuf,
698 roff: Roff,
699
700 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 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 fn name_section(&mut self, name: &str, summary: &str) {
726 self.section("NAME");
727 self.roff.text([roman(&format!("{} - {}", name, summary))]);
728 }
729
730 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 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 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 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 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 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 let mut continuation = false;
948
949 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 continuation = line.ends_with("\\");
1000
1001 description = line.starts_with("#");
1003
1004 need_para = true;
1008 }
1009 }
1010
1011 self.code(false);
1012 }
1013 }
1014 }
1015
1016 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 fn section(&mut self, heading: &str) {
1027 self.roff.control("SH", [heading]);
1028 }
1029
1030 fn subsection(&mut self, heading: &str) {
1032 self.roff.control("SS", [heading]);
1033 }
1034
1035 fn paragraph(&mut self) {
1037 self.roff.control("PP", []);
1038 }
1039
1040 fn indented_paragraph(&mut self) {
1042 self.roff.control("IP", []);
1043 }
1044
1045 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 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 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 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 pub fn filename(&self) -> &Path {
1133 &self.filename
1134 }
1135
1136 pub fn troff_source(&self) -> String {
1138 self.roff.to_roff()
1139 }
1140}