Initial release
[manu/RT-Extension-ImportCustomFieldValues.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.30';
11
12 {
13
14     # these aren't defined until after _require_admin is run, so
15     # define them so prototypes are available during compilation.
16     sub io;
17     sub capture(&;@);
18
19 #line 28
20
21     my $done = 0;
22
23     sub _require_admin {
24
25         # do this once to avoid redefinition warnings from IO::All
26         return if $done;
27
28         require IO::All;
29         IO::All->import( '-binary' );
30
31         require Capture::Tiny;
32         Capture::Tiny->import ( 'capture' );
33
34         return;
35     }
36
37 }
38
39 sub readme_from {
40   my $self = shift;
41   return unless $self->is_admin;
42
43   _require_admin;
44
45   # Input file
46   my $in_file  = shift || $self->_all_from
47     or die "Can't determine file to make readme_from";
48
49   # Get optional arguments
50   my ($clean, $format, $out_file, $options);
51   my $args = shift;
52   if ( ref $args ) {
53     # Arguments are in a hashref
54     if ( ref($args) ne 'HASH' ) {
55       die "Expected a hashref but got a ".ref($args)."\n";
56     } else {
57       $clean    = $args->{'clean'};
58       $format   = $args->{'format'};
59       $out_file = $args->{'output_file'};
60       $options  = $args->{'options'};
61     }
62   } else {
63     # Arguments are in a list
64     $clean    = $args;
65     $format   = shift;
66     $out_file = shift;
67     $options  = \@_;
68   }
69
70   # Default values;
71   $clean  ||= 0;
72   $format ||= 'txt';
73
74   # Generate README
75   print "readme_from $in_file to $format\n";
76   if ($format =~ m/te?xt/) {
77     $out_file = $self->_readme_txt($in_file, $out_file, $options);
78   } elsif ($format =~ m/html?/) {
79     $out_file = $self->_readme_htm($in_file, $out_file, $options);
80   } elsif ($format eq 'man') {
81     $out_file = $self->_readme_man($in_file, $out_file, $options);
82   } elsif ($format eq 'md') {
83     $out_file = $self->_readme_md($in_file, $out_file, $options);
84   } elsif ($format eq 'pdf') {
85     $out_file = $self->_readme_pdf($in_file, $out_file, $options);
86   }
87
88   if ($clean) {
89     $self->clean_files($out_file);
90   }
91
92   return 1;
93 }
94
95
96 sub _readme_txt {
97   my ($self, $in_file, $out_file, $options) = @_;
98   $out_file ||= 'README';
99   require Pod::Text;
100   my $parser = Pod::Text->new( @$options );
101   my $io = io->file($out_file)->open(">");
102   my $out_fh = $io->io_handle;
103   $parser->output_fh( *$out_fh );
104   $parser->parse_file( $in_file );
105   return $out_file;
106 }
107
108
109 sub _readme_htm {
110   my ($self, $in_file, $out_file, $options) = @_;
111   $out_file ||= 'README.htm';
112   require Pod::Html;
113   my ($o) = capture {
114     Pod::Html::pod2html(
115       "--infile=$in_file",
116       "--outfile=-",
117       @$options,
118     );
119   };
120   io->file($out_file)->print($o);
121   # Remove temporary files if needed
122   for my $file ('pod2htmd.tmp', 'pod2htmi.tmp') {
123     if (-e $file) {
124       unlink $file or warn "Warning: Could not remove file '$file'.\n$!\n";
125     }
126   }
127   return $out_file;
128 }
129
130
131 sub _readme_man {
132   my ($self, $in_file, $out_file, $options) = @_;
133   $out_file ||= 'README.1';
134   require Pod::Man;
135   my $parser = Pod::Man->new( @$options );
136   my $io = io->file($out_file)->open(">");
137   my $out_fh = $io->io_handle;
138   $parser->output_fh( *$out_fh );
139   $parser->parse_file( $in_file );
140   return $out_file;
141 }
142
143
144 sub _readme_pdf {
145   my ($self, $in_file, $out_file, $options) = @_;
146   $out_file ||= 'README.pdf';
147   eval { require App::pod2pdf; }
148     or die "Could not generate $out_file because pod2pdf could not be found\n";
149   my $parser = App::pod2pdf->new( @$options );
150   $parser->parse_from_file($in_file);
151   my ($o) = capture { $parser->output };
152   io->file($out_file)->print($o);
153   return $out_file;
154 }
155
156 sub _readme_md {
157   my ($self, $in_file, $out_file, $options) = @_;
158   $out_file ||= 'README.md';
159   require Pod::Markdown;
160   my $parser = Pod::Markdown->new( @$options );
161   my $io = io->file($out_file)->open(">");
162   my $out_fh = $io->io_handle;
163   $parser->output_fh( *$out_fh );
164   $parser->parse_file( $in_file );
165   return $out_file;
166 }
167
168
169 sub _all_from {
170   my $self = shift;
171   return unless $self->admin->{extensions};
172   my ($metadata) = grep {
173     ref($_) eq 'Module::Install::Metadata';
174   } @{$self->admin->{extensions}};
175   return unless $metadata;
176   return $metadata->{values}{all_from} || '';
177 }
178
179 'Readme!';
180
181 __END__
182
183 #line 316
184