Upgrade Module::Install::RTx so static files are copied on RT 4.2.x
[manu/RT-Extension-UserDetails.git] / inc / Module / Install / ReadmeFromPod.pm
1 #line 1
2 package Module::Install::ReadmeFromPod;
3
4 use 5.006;
5 use strict;
6 use warnings;
7 use base qw(Module::Install::Base);
8 use vars qw($VERSION);
9
10 $VERSION = '0.22';
11
12 sub readme_from {
13   my $self = shift;
14   return unless $self->is_admin;
15
16   # Input file
17   my $in_file  = shift || $self->_all_from
18     or die "Can't determine file to make readme_from";
19
20   # Get optional arguments
21   my ($clean, $format, $out_file, $options);
22   my $args = shift;
23   if ( ref $args ) {
24     # Arguments are in a hashref
25     if ( ref($args) ne 'HASH' ) {
26       die "Expected a hashref but got a ".ref($args)."\n";
27     } else {
28       $clean    = $args->{'clean'};
29       $format   = $args->{'format'};
30       $out_file = $args->{'output_file'};
31       $options  = $args->{'options'};
32     }
33   } else {
34     # Arguments are in a list
35     $clean    = $args;
36     $format   = shift;
37     $out_file = shift;
38     $options  = \@_;
39   }
40
41   # Default values;
42   $clean  ||= 0;
43   $format ||= 'txt';
44
45   # Generate README
46   print "readme_from $in_file to $format\n";
47   if ($format =~ m/te?xt/) {
48     $out_file = $self->_readme_txt($in_file, $out_file, $options);
49   } elsif ($format =~ m/html?/) {
50     $out_file = $self->_readme_htm($in_file, $out_file, $options);
51   } elsif ($format eq 'man') {
52     $out_file = $self->_readme_man($in_file, $out_file, $options);
53   } elsif ($format eq 'pdf') {
54     $out_file = $self->_readme_pdf($in_file, $out_file, $options);
55   }
56
57   if ($clean) {
58     $self->clean_files($out_file);
59   }
60
61   return 1;
62 }
63
64
65 sub _readme_txt {
66   my ($self, $in_file, $out_file, $options) = @_;
67   $out_file ||= 'README';
68   require Pod::Text;
69   my $parser = Pod::Text->new( @$options );
70   open my $out_fh, '>', $out_file or die "Could not write file $out_file:\n$!\n";
71   $parser->output_fh( *$out_fh );
72   $parser->parse_file( $in_file );
73   close $out_fh;
74   return $out_file;
75 }
76
77
78 sub _readme_htm {
79   my ($self, $in_file, $out_file, $options) = @_;
80   $out_file ||= 'README.htm';
81   require Pod::Html;
82   Pod::Html::pod2html(
83     "--infile=$in_file",
84     "--outfile=$out_file",
85     @$options,
86   );
87   # Remove temporary files if needed
88   for my $file ('pod2htmd.tmp', 'pod2htmi.tmp') {
89     if (-e $file) {
90       unlink $file or warn "Warning: Could not remove file '$file'.\n$!\n";
91     }
92   }
93   return $out_file;
94 }
95
96
97 sub _readme_man {
98   my ($self, $in_file, $out_file, $options) = @_;
99   $out_file ||= 'README.1';
100   require Pod::Man;
101   my $parser = Pod::Man->new( @$options );
102   $parser->parse_from_file($in_file, $out_file);
103   return $out_file;
104 }
105
106
107 sub _readme_pdf {
108   my ($self, $in_file, $out_file, $options) = @_;
109   $out_file ||= 'README.pdf';
110   eval { require App::pod2pdf; }
111     or die "Could not generate $out_file because pod2pdf could not be found\n";
112   my $parser = App::pod2pdf->new( @$options );
113   $parser->parse_from_file($in_file);
114   open my $out_fh, '>', $out_file or die "Could not write file $out_file:\n$!\n";
115   select $out_fh;
116   $parser->output;
117   select STDOUT;
118   close $out_fh;
119   return $out_file;
120 }
121
122
123 sub _all_from {
124   my $self = shift;
125   return unless $self->admin->{extensions};
126   my ($metadata) = grep {
127     ref($_) eq 'Module::Install::Metadata';
128   } @{$self->admin->{extensions}};
129   return unless $metadata;
130   return $metadata->{values}{all_from} || '';
131 }
132
133 'Readme!';
134
135 __END__
136
137 #line 254
138