3 # Generate manual pages for sgt-puzzles by running extracts of puzzles.but
12 my $package = 'sgt-puzzles';
13 my $language = $ARGV[0] or die 'mkmanpages.pl: no language specified';
15 # Fake up gettext without compilation or locales
16 my $po_map = Locale::PO->load_file_ashash("po/$language.po");
19 my $po = $po_map->{Locale::PO->quote($msgid)};
20 return $po ? Locale::PO->dequote($po->msgstr()) : $msgid;
24 my $package_roff = $package;
25 $package_roff =~ s/-/\\-/g;
27 # Translator: conventional name for manual section 6
28 my $section = gettext('Games');
30 my $man_dir = "doc/man-$language";
34 my $gamedesc = new IO::File('gamedesc.txt', 'r');
36 (my $name, undef, undef, my $desc) = split /:/;
37 $commands{$name} = $ENV{BINPREFIX} . $name;
38 $short_descs{$name} = $desc;
42 # We should be able to look these up with strftime('%A') but that
43 # requires the relevant locale to be installed on the build host
44 my @MONTHS = (gettext('January'), gettext('February'), gettext('March'),
45 gettext('April'), gettext('May'), gettext('June'),
46 gettext('July'), gettext('August'), gettext('September'),
47 gettext('October'), gettext('November'), gettext('December'));
49 # Chapter name, initialised to dummy value to capture header
50 my $name = '__HEADER__';
52 # Contents of each chapter/appendix
55 # Gather chapters from the original documentation
57 $language eq 'en' ? 'preprocessed.but' : "doc/preprocessed.but.$language";
58 my $source = new IO::File($source_name, 'r') or die "$source_name: $!";
60 # Look for chapter/appendix heading
61 if (/^\\[AC]{([^}]+)}\s*/) {
63 # The odd one out - chapter name doesn't match command name
64 if ($name eq 'rectangles') {
68 # Look for version ID with date
69 if (/^\\versionid .* (\d{4})(\d{2})\d{2}\./) {
70 $date = "${MONTHS[$2-1]} $1";
72 $contents{$name} .= $_;
76 # Remove all normal text from the header
77 $contents{__HEADER__} =~ s/^(?!\\(?:cfg|define|title){).*$//gm;
79 # Remove introduction from "common features" chapter
80 $contents{common} =~ s/^.*?(?=\\H\{)//s;
82 for my $short_name (keys %commands) {
83 my $command = $commands{$short_name};
84 print "Generating $command.6\n";
86 my $text_name = $language eq 'en' ? 'puzzles.txt' : "puzzles.txt.$language";
88 "\\cfg{man-mindepth}{1}\n" # don't show original chapter headings
89 . "\\cfg{man-identity}{".uc($command)."}{$section_no}{$date}{$command ($package_roff)}{$section}\n\n"
90 . "\\cfg{man-charset}{UTF-8}\n" # output encoding
91 . $contents{__HEADER__}
92 . "\\C{man-$command} $command\n\n" # dummy chapter
93 . "\\H{man-$command-name} " . gettext('NAME') . "\n\n"
94 . "\\c{$command} \\- $short_descs{$short_name}\n\n"
95 . "\\H{man-$command-synopsis} " . gettext('SYNOPSIS') . "\n\n"
96 # Translator: abbreviation for 'number'
97 . "\\cw{$command} [\\cw{--generate }\\e{" . gettext('n') . "}]\n"
98 # Translator: abbreviation for 'width'
99 . "[\\cw{--print }\\e{" . gettext('w') . "}\\cw{x}\\e{"
100 # Translator: abbreviation for 'height'
101 . gettext('h') . "} [\\cw{--with-solutions}]\n"
102 . "[\\cw{--scale }\\e{" . gettext('n') . "}] [\\cw{--colour}]]\n"
103 . "[\\e{" . gettext('game-parameters') . "}|\\e{" . gettext('game-ID')
104 . "}|\\e{" . gettext('random-seed') . "}]\n\n"
105 . "\\cw{$command --version}\n\n"
106 . "\\H{man-$command-desc} " . gettext('DESCRIPTION') . "\n\n"
107 . $contents{$short_name}
109 . "\\H{man-$command-see-also} " . gettext('SEE ALSO') . "\n\n"
110 # Translator: "Full documentation in <filename>."
111 . sprintf(gettext("Full documentation in %s."),
112 "/usr/share/doc/$package/$text_name.gz")
115 # Kluge cross-references
117 my ($above, $target, $below) = @_;
118 # If the target is an earlier or later section in the current page, say
119 # it's above or below.
120 if ($above =~ m/\\(?:[CHA]|S\d*){$target}/) {
121 # Translator: earlier in the manual page
123 } elsif ($below =~ m/\\(?:[CHA]|S\d*){$target}/) {
124 # Translator: later in the manual page
127 # Else if the target is a bibliographic entry, include the entry directly.
128 elsif ($below =~ m/\\B\{$target\}\s*(.*?)\s*(?:\\(?:[BCHA]|S\d*|$))/s) {
131 # Else if it appears to refer to another game, convert to a customary
132 # cross-manual-page reference.
133 elsif ($target =~ /(\w+)/ && exists $commands{$1}) {
134 "\\e{$commands{$1}}($section_no)";
136 # Otherwise (and this shouldn't happen), show the reference target.
138 print STDERR "Failed to resolve reference to $target\n";
142 $contents =~ s/(?:\bin\s+)?\\[kK]{([^}]+)}/replace_ref($`, $1, $')/eg;
144 # Run through halibut. It does not default to using stdin or stdout,
145 # and /dev/std{in,out} apparently don't exist on some systems, so we
146 # can't reliably do this with a pipeline.
147 my ($temp_but, $temp_but_name) = mkstemp "/tmp/sgt-puzzles-but-XXXXXX"
149 my $man_name = "$command.$section_no";
150 print $temp_but $contents or die "$!";
152 system "halibut --man=$man_dir/$man_name --input-charset=UTF-8 $temp_but_name";
153 unlink $temp_but_name;
154 -s "$man_dir/$man_name" or die "halibut produced an empty $man_name";