summaryrefslogtreecommitdiffstats
path: root/podwrapper.pl.in
blob: 46c71f2e9ded77ab52fe4d3828e9e0e2e64d882d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/perl -w
# podwrapper.pl
# Copyright (C) 2010-2012 Red Hat Inc.
# @configure_input@
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use warnings;
use strict;

use Pod::Usage;
use Getopt::Long;
use Pod::Man;
use Pod::Simple;
use Pod::Simple::Text;
use Pod::Simple::XHTML;
use File::Basename;

=encoding utf8

=head1 NAME

podwrapper.pl - Generate various output formats from POD input files

=head1 SYNOPSIS

 virt-foo.1 $(top_builddir)/html/virt-foo.1.html: stamp-virt-foo.pod
 stamp-virt-foo.pod: virt-foo.pod
   $(PODWRAPPER) --man virt-foo.1 --html virt-foo.1.html $<
   touch $@

=head1 DESCRIPTION

podwrapper is a Perl script that generates various output formats
from POD input files that libguestfs uses for most documentation.

You should specify an input file, and one or more output formats.
For example:

 podwrapper.pl virt-foo.pod --man virt-foo.1

will turn C<virt-foo.pod> into a man page C<virt-foo.1>.  The output
options are I<--man>, I<--html> and I<--text> (see below).

=head1 OPTIONS

=over 4

=cut

my $help;

=item B<--help>

Display brief help.

=cut

my $html;

=item B<--html=output.html>

Write a web page to C<output.html>.  If this option is not
given, then no web page output is produced.

=cut

my @inserts;

=item B<--insert=filename:__PATTERN__>

In the input file, replace the literal text C<__PATTERN__> with the
replacement file C<filename>.  You can give this option multiple
times.

The contents of C<filename> are treated as POD.
Compare and contrast with I<--verbatim>.

Although it is conventional to use C<__...__> (double underscores) for
patterns, in fact you can use any string as the pattern.

=cut

my @licenses;

=item B<--license=GPLv2+|LGPLv2+|examples>

Add the given license to the end of the man page.  This parameter
is required.  The parameter may be given multiple times (eg. for
mixed content).

=cut

my $man;

=item B<--man=output.n>

Write a man page to C<output.n>.  If this option is not
given, then no man page output is produced.

=cut

my $name;

=item B<--name=name>

Set the name of the man page.  If not set, defaults to the basename
of the input file.

=cut

my $section;

=item B<--section=N>

Set the section of the man page (a number such as C<1> for
command line utilities or C<3> for C API documentation).  If
not set, defaults to C<1>.

=cut

my $strict_checks = 1;

=item B<--no-strict-checks>

Disable strict checks of the man page.  This is only used
when generating the translated man pages in the C<po-docs>
subdirectory.

=cut

my $text;

=item B<--text=output.txt>

Write a text file to C<output.txt>.  If this option is not
given, then no text output is produced.

=cut

my @verbatims;

=item B<--verbatim=filename:@PATTERN@>

In the input file, replace the literal text C<__PATTERN__> with the
replacement file C<filename>.  You can give this option multiple
times.

The contents of C<filename> are inserted as verbatim text, and
are I<not> interpreted as POD.
Compare and contrast with I<--insert>.

Although it is conventional to use C<__...__> (double underscores) for
patterns, in fact you can use any string as the pattern.

=cut

=back

=cut

# Clean up the program name.
my $progname = $0;
$progname =~ s{.*/}{};

# Parse options.
GetOptions ("help|?" => \$help,
            "html=s" => \$html,
            "license=s" => \@licenses,
            "insert=s" => \@inserts,
            "man=s" => \$man,
            "name=s" => \$name,
            "section=s" => \$section,
            "strict-checks!" => \$strict_checks,
            "text=s" => \$text,
            "verbatim=s" => \@verbatims
    ) or pod2usage (2);
pod2usage (1) if $help;

die "$progname: missing argument: podwrapper input.pod\n" unless @ARGV == 1;
my $input = $ARGV[0];

die "$progname: $input: missing argument: --license parameter is required\n"
    if $strict_checks && @licenses == 0;

# There should be at least one output.
die "$progname: $input: no output format specified.  Use --man and/or --html and/or --text.\n"
    unless defined $man || defined $html || defined $text;

# Default for $name and $section.
$name = basename ($input, ".pod") unless defined $name;
$section = 1 unless defined $section;

# Note that these @...@ are substituted by ./configure.
my $abs_top_srcdir = "@abs_top_srcdir@";
my $abs_top_builddir = "@abs_top_builddir@";
my $package_name = "@PACKAGE_NAME@";
my $package_version = "@PACKAGE_VERSION@";

die "$progname: ./configure substitutions were not performed"
    unless $abs_top_srcdir && $abs_top_builddir &&
    $package_name && $package_version;

# Create a stable date (thanks Hilko Bengen).
my $date;
my $filename = "$abs_top_srcdir/ChangeLog";
if (-r $filename) {
    open FILE, $filename or die "$progname: $filename: $!";
    $_ = <FILE>;
    close FILE;
    $date = $1 if /^(\d+-\d+-\d+)\s/;
}
$filename = "$abs_top_srcdir/.git";
if (!$date && -d $filename) {
    local $ENV{GIT_DIR} = $filename;
    $_ = `git show -s --format=%ci`;
    $date = $1 if /^(\d+-\d+-\d+)\s/;
}
if (!$date) {
    my ($day, $month, $year) = (localtime)[3,4,5];
    $date = sprintf ("%04d-%02d-%02d", $year+1900, $month+1, $day);
}

# Create a release string.
my $release = "$package_name-$package_version";

#print "input=$input\n";
#print "name=$name\n";
#print "section=$section\n";
#print "date=$date\n";

# Read the input.
my $content = read_whole_file ($input);

# Perform @inserts.
foreach (@inserts) {
    my @a = split /:/, $_, 2;
    die "$progname: $input: no colon in parameter of --insert\n" unless @a >= 2;
    my $replacement = read_whole_file ($a[0]);
    $content =~ s/$a[1]/$replacement/ge;
}

# Perform @verbatims.
foreach (@verbatims) {
    my @a = split /:/, $_, 2;
    die "$progname: $input: no colon in parameter of --verbatim\n" unless @a >= 2;
    my $replacement = read_verbatim_file ($a[0]);
    $content =~ s/$a[1]/$replacement/ge;
}

if ($strict_checks) {
    # Verify sections present / not present.
    die "$progname: $input: missing AUTHOR or AUTHORS section\n"
        unless $content =~ /^=head1 AUTHOR/m;
    die "$progname: $input: missing SEE ALSO section\n"
        unless $content =~ /^=head1 SEE ALSO/m;
    die "$progname: $input: missing COPYRIGHT section\n"
        unless $content =~ /^=head1 COPYRIGHT/m;
    die "$progname: $input: BUGS is now added automatically, do not add it to the POD file\n"
        if $content =~ /^=head1 (REPORTING )?BUGS/m;
    die "$progname: $input: LICENSE is now added automatically, do not add it to the POD file\n"
        if $content =~ /^=head1 LICENSE/m;
    die "$progname: $input: GPL/LGPL should be specified using the --license parameter, not included in the POD file\n"
        if $content =~ /^This program is free software/ ||
        $content =~ /^This library is free software/;
}

# Add standard LICENSE and BUGS sections.
my $LGPLv2plus =
"This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
";

my $GPLv2plus =
"This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
";

my $examples_license =
"This manual page contains examples which we hope you will use in
your programs.  The examples may be freely copied, modified and
distributed for any purpose without any restrictions.
";

my $reporting_bugs =
"=head1 BUGS

To get a list of bugs against libguestfs, use this link:
L<https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools>

To report a new bug against libguestfs, use this link:
L<https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools>

When reporting a bug, please supply:

\=over 4

\=item *

The version of libguestfs.

\=item *

Where you got libguestfs (eg. which Linux distro, compiled from source, etc)

\=item *

Describe the bug accurately and give a way to reproduce it.

\=item *

Run L<libguestfs-test-tool(1)> and paste the B<complete, unedited>
output into the bug report.

\=back
";

$content .= "\n\n=head1 LICENSE\n\n";

foreach (@licenses) {
    if ($_ eq "LGPLv2+") {
        $content .= $LGPLv2plus . "\n\n";
    }
    elsif ($_ eq "GPLv2+") {
        $content .= $GPLv2plus . "\n\n";
    }
    elsif ($_ eq "examples") {
        $content .= $examples_license . "\n\n";
    }
    else {
        die "$progname: $input: invalid --license parameter: $_\n";
    }
}

$content .= "\n\n$reporting_bugs";

# Output man page.
SUBMAN: {
    package Podwrapper::Man;

    use vars qw(@ISA $VERSION);
    @ISA = qw(Pod::Man);
    $VERSION = $package_version;

    # Override the L<> method.
    sub cmd_l
    {
        my ($self, $attrs, $text) = @_;
        return $text;
    }
}

if ($man) {
    my $parser = Podwrapper::Man->new (
        name => $name,
        release => $release, section => $section,
        center => "Virtualization Support",
        date => $date,
        stderr => 1, utf8 => 1
    );
    my $output;
    $parser->output_string (\$output);
    $parser->parse_string_document ($content)
        or die "$progname: could not parse input document";
    open OUT, ">$man" or die "$progname: $man: $!";
    print OUT $output or die "$progname: $man: $!";
    close OUT or die "$progname: $man: $!";
    print "$progname: wrote $man\n";
}

# Output HTML.
SUBHTML: {
    # Subclass Pod::Simple::XHTML.  See the documentation.
    package Podwrapper::XHTML;

    use vars qw(@ISA $VERSION);
    @ISA = qw(Pod::Simple::XHTML);
    $VERSION = $package_version;

    # Pod::Simple::XHTML returns uppercase identifiers, whereas the
    # old pod2html returns lowercase ones.
    sub idify
    {
        my $self = shift;
        my $id = $self->SUPER::idify (@_);
        lc ($id);
    }

    sub is_a_libguestfs_page
    {
        local $_ = shift;

        return 1 if /^Sys::Guestfs/;
        return 1 if /^virt-/;
        return 1 if /^libguestf/;
        return 1 if /^guestf/;
        return 1 if /^guestmount/;
        return 1 if /^hivex/;
        return 1 if /^febootstrap/;
        return 0;
    }

    sub resolve_pod_page_link
    {
        my $self = shift;
        my $podname = $_[0]; # eg. "Sys::Guestfs", can be undef
        my $anchor = $_[1];  # eg. "SYNOPSIS", can be undef
        my $r = "";
        if (defined $podname) {
            return $self->SUPER::resolve_pod_page_link (@_)
                unless is_a_libguestfs_page ($podname);
            $r .= "$podname.3.html"
        }
        $r .= "#" . $self->idify ($anchor, 1) if defined $anchor;
        $r;
    }

    sub resolve_man_page_link
    {
        my $self = shift;
        my $name = $_[0];   # eg. "virt-make-fs(1)", can be undef
        my $anchor = $_[1]; # eg. "SYNOPSIS", can be undef
        my $r = "";
        if (defined $name) {
            return $self->SUPER::resolve_man_page_link (@_)
                unless is_a_libguestfs_page ($name);
            $name =~ s/\((.*)\)$/.$1/;
            $r .= "$name.html";
        }
        $r .= "#" . $self->idify ($anchor, 1) if defined $anchor;
        $r;
    }

    # For some reason Pod::Simple::XHTML usually cannot find a
    # title for the page.  This defaults the HTML <title> field
    # to the same as the man page name.
    sub default_title { $name }
}

if ($html) {
    mkdir "$abs_top_builddir/html";

    my $parser = Podwrapper::XHTML->new;
    my $output;
    $parser->output_string (\$output);
    # Added in Pod::Simple 3.16, 2011-03-14.
    eval { $parser->html_charset ("UTF-8") };
    $parser->html_css ("pod.css");
    $parser->index (1);
    $parser->parse_string_document ($content);

    # Hack for Perl 5.16.
    $output =~ s{/>pod.css<}{/>\n<};

    open OUT, ">$html" or die "$progname: $html: $!";
    print OUT $output or die "$progname: $html: $!";
    close OUT or die "$progname: $html: $!";
    print "$progname: wrote $html\n";
}

# Output text.
if ($text) {
    my $parser = Pod::Simple::Text->new;
    my $output;
    $parser->output_string (\$output);
    $parser->parse_string_document ($content);
    open OUT, ">$text" or die "$progname: $text: $!";
    print OUT $output or die "$progname: $text: $!";
    close OUT or die "$progname: $text: $!";
    print "$progname: wrote $text\n";
}

sub read_whole_file
{
    my $input = shift;
    local $/ = undef;

    open FILE, $input or die "$progname: $input: $!";
    $_ = <FILE>;
    close FILE;
    $_;
}

sub read_verbatim_file
{
    my $input = shift;
    my $r = "";

    open FILE, $input or die "$progname: $input: $!";
    while (<FILE>) {
        $r .= " $_";
    }
    close FILE;
    $r;
}

=head1 AUTHOR

Richard W.M. Jones.

=head1 SEE ALSO

libguestfs.git/README,
L<Pod::Simple>