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 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 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 #undef $ENV{'GROFF_NO_SGR'};
52 #delete $ENV{'GROFF_NO_SGR'};
53 $ENV{'GROFF_NO_SGR'} = '1';
54 $ENV{'TERM'} = 'vt100'; # does this matter ?
56 my %htmlqh = qw/& & < < > > ' ' " "/;
57 # do html quotation to $_[0] (which is an alias to the given arg)
60 $_[0] =~ s/([&<>'"])/$htmlqh{$1}/ge;
66 #warn keys %fhash, "\n";
68 while (my ($k, $v) = each %fhash)
70 #next if -l $v; # skip symlinks here. -- not... references there may be.
73 #open I, '-|', qw/groff -man -T utf8/, $v;
74 open I, '-|', qw/groff -man -T latin1/, $v; # this and GROFF_NO_SGR='1'
76 my ($emptyline, $pre, $hl) = (0, 0, 'h1');
82 s/(?<=\S)\s{8,}.*//; # $hl = 'h1' if s/(?<=\S)\s{8,}.*//;
85 s/((?:_\010[^_])+)/<u>$1<\/u>/g;
87 s/((?:.\010.)+)/<b>$1<\/b>/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;
114 print STDOUT 'Writing ', "'$k'\n";
119 if (s/^(\s+)<b>([^<]+)<\/b>\((\d+)\)//) {
120 my $link = maymakelink "$pe-$2-$3";
121 $link = maymakelink "$pe$2-$3" unless $link;
123 print "<a href='$link'>$pe-</a>\n";
124 print "$1<a href='$link'>$2</a>($3)";
127 print "<b>$pe-</b>\n";
128 print "$1<b>$2</b>($3)";
131 print "<b>$pe-</b>\n";
135 s/<b>([^<]+)<\/b>\((\d+)\)/mayconvert($1, $2)/ge;
136 $pe = $1 if s/<b>([^<]+)-<\/b>\s*$//;
144 return "../$_[0]/" if exists $fhash{$_[0]};
150 my $f = "$_[0]-$_[1]";
152 return "<a href='../$f/'>$_[0]</a>($_[1])" if exists $fhash{$f};
153 return "<b>$_[0]</b>($_[1])";
156 # Finally, make manpages.mdwn
158 open O, '>', $ARGV[1] . '/manpages.mdwn' or die $!;
159 print STDOUT "Writing '$ARGV[1]/manpages.mdwn'\n";
161 print "Manual page index\n";
162 print "=================\n\n";
164 sub srt { my ($x, $y) = ($a, $b); $x =~ tr/./-/; $y =~ tr/./-/; $x cmp $y; }
166 foreach (sort srt values %fhash)
169 open I, '<', $in or die $!;
172 if (/^\s*[.]TH\s+\S+\s+(\S+)/) {
178 last if /^\s*[.]SH NAME/
183 if (/\s*(\S+)\s+(.*)/) {
185 # Ignoring the NAME in file, get from file name instead.
186 #my $on = (-l $in)? readlink $in: $in;
188 $on =~ tr/./-/; $on =~ s|.*/||;
189 my $n = $in; $n =~ s|.*/||; $n =~ tr/./-/; $n =~ s/-[^-]+$//;
190 $line = "<a href='$on/'>$n</a>($s) $e\n";
194 die "No NAME in '$in'\n" unless $line;