4 # License: same as notmuch
6 # This program is used to generate mdwn-formatted notmuch manual pages
7 # for notmuch wiki. Example run:
9 # $ ./devel/man-to-mdwn.pl doc/_build/man ../notmuch-wiki
11 # In case taken into more generic use, modify these comments and examples.
18 warn "\n$0 <source-directory> <destination-directory>\n\n";
19 # Remove/edit this comment if this script is taken into generic use.
20 warn "Example: ./devel/man-to-mdwn.pl doc/_build/man ../notmuch-wiki\n\n";
24 die "'$ARGV[0]': no such source directory\n" unless -d $ARGV[0];
25 die "'$ARGV[1]': no such destination directory\n" unless -d $ARGV[1];
27 #die "'manpages' exists\n" if -e 'manpages';
28 #die "'manpages.mdwn' exists\n" if -e 'manpages.mdwn';
30 die "Expecting '$ARGV[1]/manpages' to exist.\n" .
31 "Please create it first or adjust <destination-directory>.\n"
32 unless -d $ARGV[1] . '/manpages';
37 open P, '-|', 'find', $ARGV[0], qw/-name *.[0-9] -print/;
41 next unless -f $_; # follows symlink.
42 $ev = 1, warn "'$_': no such file\n" unless -f $_;
43 my ($in, $on) = ($_, $_);
44 $on =~ s|.*/||; $on =~ tr/./-/;
46 $ev = 1, warn "'$in' collides with '$f' ($on.mdwn)\n" if defined $f;
51 my %htmlqh = qw/& & < < > > ' ' " "/;
52 # do html quotation to $_[0] (which is an alias to the given arg)
55 $_[0] =~ s/([&<>'"])/$htmlqh{$1}/ge;
61 #warn keys %fhash, "\n";
63 while (my ($k, $v) = each %fhash)
65 #next if -l $v; # skip symlinks here. -- not... references there may be.
68 open I, '-|', qw/env -i/, "PATH=$ENV{PATH}",
69 qw/TERM=vt100 LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8/,
70 qw/GROFF_NO_SGR=1 MAN_KEEP_FORMATTING=1 MANWIDTH=80/,
71 qw/man/, $v or die "$!";
74 my ($emptyline, $pre, $hl) = (0, 0, 'h1');
80 # keep only leftmost in lines like 'NOTMUCH(1) notmuch NOTMUCH(1)'
81 s/\S\K\s{8,}\S.+\s{8,}\S.*//; # $hl = 'h1' if s/(?<=\S)\s{8,}.*//;
83 s/((?:_\010[^_])+)/\001u\002$1\001\/u\002/g;
85 s/((?:.\010.)+)/\001b\002$1\001\/b\002/g;
88 s/\001/</g; s/\002/>/g;
91 $pre = 0, push @lines, "</pre>\n" if $pre;
94 $_ = "\n<$hl>$_</$hl>\n";
99 $pre = 0, push @lines, "</pre>\n" if $pre;
100 s/(?:^\s+)?<\/?b>//g;
102 $_ = "\n<h3> $_</h3>\n";
106 $pre = 1, push @lines, "<pre>\n" unless $pre;
107 $emptyline = 0, push @lines, "\n" if $emptyline;
111 $lines[0] =~ s/^\n//;
112 $k = "$ARGV[1]/manpages/$k.mdwn";
113 open O, '>', $k or die;
115 print STDOUT 'Writing ', "'$k'\n";
117 my ($pe, $hyphen) = ('', '');
121 if (s/^(\s+)<b>([^<]+)\((\d+)\)<\/b>//) {
122 my $link = maymakelink "$pe-$2-$3";
123 $link = maymakelink "$pe$2-$3" unless $link;
125 print "<a href='$link'>$pe$hyphen</a>\n";
126 print "$1<a href='$link'>$2</a>($3)";
129 print "<b>$pe-</b>\n";
130 print "$1<b>$2</b>($3)";
133 print "<b>$pe-</b>\n";
137 s/<b>([^<]+)\((\d+)\)<\/b>/mayconvert($1, $2)/ge;
138 ($pe, $hyphen) = ($1, $2) if s/<b>([^<]+)([-\x{2010}])<\/b>\s*$//;
146 return "../$_[0]/" if exists $fhash{$_[0]};
152 my $f = "$_[0]-$_[1]";
154 return "<a href='../$f/'>$_[0]</a>($_[1])" if exists $fhash{$f};
155 return "<b>$_[0]</b>($_[1])";
158 # Finally, make manpages.mdwn
160 open O, '>', $ARGV[1] . '/manpages.mdwn' or die $!;
161 print STDOUT "Writing '$ARGV[1]/manpages.mdwn'\n";
163 print "Manual page index\n";
164 print "=================\n\n";
166 sub srt { my ($x, $y) = ($a, $b); $x =~ tr/./-/; $y =~ tr/./-/; $x cmp $y; }
168 foreach (sort srt values %fhash)
171 open I, '<', $in or die $!;
174 if (/^\s*[.]TH\s+\S+\s+"?(\S+?)"?\s/) {
180 last if /^\s*[.]SH NAME/
185 if (/\s*(\S+)\s+(.*)/) {
187 # Ignoring the NAME in file, get from file name instead.
188 #my $on = (-l $in)? readlink $in: $in;
190 $on =~ tr/./-/; $on =~ s|.*/||;
191 my $n = $in; $n =~ s|.*/||; $n =~ tr/./-/; $n =~ s/-[^-]+$//;
192 $line = "<a href='$on/'>$n</a>($s) $e\n";
196 die "No NAME in '$in'\n" unless $line;
202 The manual pages are licensed under
203 [the GNU General Public License](https://www.gnu.org/licenses/gpl.txt),
204 either version 3.0 or at your option any later version.