Imported Upstream version 1.40.11
[platform/upstream/help2man.git] / help2man.html.PL
1 #!/usr/bin/perl
2
3 #
4 # Combine generated html page with GNU boilerplate.
5 #
6
7 use strict;
8 use warnings;
9 use File::Temp;
10 use Getopt::Long;
11
12 my %opts;
13 die "Usage: $0 [--quiet] [--stdout]\n"
14     unless GetOptions \%opts, qw(quiet stdout) and !@ARGV;
15
16 undef $/;
17
18 # Fetch GNU boilerplate
19 my $boilerplate;
20 my $url = 'http://www.gnu.org/server/standards/boilerplate-source.html';
21 do {
22     open my $b, '-|', 'curl', '-sL', $url or die "curl: $!";
23     $boilerplate = <$b>;
24     ($url) = $boilerplate =~ /<meta\s+http-equiv=["']?refresh["']?\s+
25                               content=["']\d+;\s+url=["']?(http[^"'>]*)/xi;
26 } while $url;
27
28 for ($boilerplate)
29 {
30     s#\$Revision:\s+(\S+)\s+\$#$1#;
31     s#<!-- This is the template document.*?-->\s+##s;
32     s#<!-- Instructions for adapting.*?-->\s*(<!-- \d+\. .*?-->\s*)*##s;
33     s#<title>Baz\s+(- GNU Project)#<title>help2man $1#s;
34     s#<h2>GNU\sBaz</h2>.*(</div><!--\s+for\s+id="content")#%body%$1#s;
35 }
36
37 my ($header, $footer) = split /%body%/, $boilerplate;
38 die "can't parse boilerplate" unless $footer;
39
40 # Generate manual from texinfo
41 my $texi_tmp = File::Temp->new();
42 system 'makeinfo', '--html', '--no-number-sections', '--no-headers',
43     '--no-split', '--output=' . $texi_tmp->filename, 'help2man.texi';
44
45 my $gnu_standards = "http://www.gnu.org/prep/standards/standards.html";
46 my $body = <$texi_tmp>;
47 for ($body)
48 {
49     s#^.*<body>##s;
50     s#</body>.*##s;
51
52     # Fixup references
53     s#<a\s+href="standards\.html#<a href="$gnu_standards#g;
54     s#<a\s+href="\*manpages\*\.html\#perlre"
55         #<a href="http://perldoc.perl.org/perlre.html"#xg;
56
57     # Drop heading sizes by one, as h1 is quite loud.
58     s#<(/?)h(\d)\b#"<${1}h" . ($2 + 1)#ge;
59 }
60
61 # Write output
62 my $target = $0;
63 my $tmp;
64 if ($opts{stdout})
65 {
66     *OUT = *STDOUT;
67     $opts{quiet} = 1;
68 }
69 else
70 {
71     $target =~ s!.*/!!;
72     $target =~ s/\.PL$// or die "$0: can't determine target name\n";
73     $tmp = "$target.tmp$$";
74     unlink $tmp          or die "$0: can't unlink $tmp ($!)\n" if -e $tmp;
75     open OUT, ">$tmp"    or die "$0: can't create $tmp ($!)\n";
76 }
77
78 print "Extracting $target (with GNU boilerplate)\n"
79     unless $opts{quiet};
80
81 print OUT $header, $body, $footer;
82
83 # Fix output file permissions
84 unless ($opts{stdout})
85 {
86     close OUT;
87     rename $tmp, $target or die "$0: can't rename $tmp to $target ($!)\n";
88     chmod 0444, $target or warn "$0: can't change mode of $target ($!)\n";
89 }
90
91 exit 0;