Fix #338: re.sub() flag argument at wrong position.
[docutils.git] / prest / Makefile.PL
blobee5b1aa6f039b9587f7659badd1a705a78344a27
1 require 5.6.1;
3 use strict;
4 use warnings;
6 use ExtUtils::MakeMaker;
8 # $Id$
10 # Usage: perl Makefile.PL [options]
11 # Options:
12 #  -f file   Use file for default configuration values (default is config.log)
13 #  -h        Print help
14 #  -k        Keep same values as config.log (or file specified with -f)
16 ## Be sure to edit the version number in lib/Text/Restructured.pm for
17 ## every release.
18 my $Version;
19 open PM, "lib/Text/Restructured.pm";
20 while (<PM>) {
21     if (/\$VERSION\s*=/) {
22         my $VERSION;            # N.B.: Needed so $VERSION passes strict
23         $Version = eval $_;
24     }
26 close PM;
28 use vars qw($opt_f $opt_h $opt_k);
29 my $OUTPUT_CFG_FILE = $opt_f = 'config.log';
31 use Getopt::Std;
32 usage() if ! getopts("f:hk") || $opt_h;
34 ##### Do all user prompts first
35 my %CFG_LIST =
36     (taint => {default => 'No',
37                desc => 'Run perl tainted (not required for safe operation)',
38                checkfail => \&yesno,
39                ,},
40      defaultcss => {
41          default => 'None',
42          desc => 'URL for default cascading style sheet (or "none")',
43          explain => <<'EOS',
44 Generated documents need a style sheet to look good.  It is recommended
45 to serve a local copy of lib/Text/Restructured/default.css as an http URL.
46 You can also specify 'None', in which case the default stylesheet will
47 be embedded within every document.
48 EOS
49          checkfail => \&isurl,
50          ,},
51      docurl => {
52          default => 'None',
53          desc => 'URL where documentation will be installed (or "none")',
54          checkfail => \&isurl,
55          ,},
56      );
57 my @CFG_LIST = qw(defaultcss taint docurl);
58 warn "\@CFG_LIST and \%CFG_LIST have different number of elements"
59     if @CFG_LIST != keys %CFG_LIST;
61 my %CONFIG;                     # Our final configuration
62 my %DEFAULTS;                   # Default values
64 # First read the config file if it exists
65 if (-f $opt_f) {
66     open CF, $opt_f or die "Cannot open $opt_f";
67     my %cfg = eval(join('',<CF>));
68     @DEFAULTS{keys %cfg} = values %cfg;
69     close CF;
71 else {
72     # Set the defaults from %CFG_LIST
73     @DEFAULTS{keys %CFG_LIST} = map($_->{default}, values %CFG_LIST);
76 # Do the user prompts
77 while (! $opt_k) {
78     foreach my $cfg_item (@CFG_LIST) {
79         my $message = $CFG_LIST{$cfg_item}{desc};
80         $message = "$CFG_LIST{$cfg_item}{explain}\n$message"
81             if defined $CFG_LIST{$cfg_item}{explain};
82         while (1) {
83             my $val = prompt ($message, $DEFAULTS{$cfg_item});
84             $val =~ s/^\s*(.*?)\s*$/$1/;
85             $CONFIG{$cfg_item} = $val;
86             last unless 
87                 defined $CFG_LIST{$cfg_item}{checkfail} &&
88                 &{$CFG_LIST{$cfg_item}{checkfail}}($CONFIG{$cfg_item});
89             $message = $CFG_LIST{$cfg_item}{desc};
90         }
91         $DEFAULTS{$cfg_item} = $CONFIG{$cfg_item};
92     }
94     print "\n";
95     printsummary();
97     my $okay = prompt("Does this look right?");
98     last if ($okay !~ m/^[n0]/i);
100 if ($opt_k) {
101     @CONFIG{@CFG_LIST} = @DEFAULTS{@CFG_LIST};
102     printsummary();
105 # Add the prest version to the config
106 $CONFIG{version} = $Version;
107 # Add the perl executable to the config
108 $CONFIG{perl} = $^X;
110 # Write the configuration file (after saving the old one)
111 rename "$OUTPUT_CFG_FILE", "$OUTPUT_CFG_FILE.bak";
112 open (CL, ">$OUTPUT_CFG_FILE") or die "Cannot write to $OUTPUT_CFG_FILE.";
113 print CL map(qq('$_'=>'$CONFIG{$_}',\n), sort keys %CONFIG);
114 close CL;
116 ##### Figure out what version of make to use.  We *require* GNU make.
117 my @path = split /:/, $ENV{PATH};
118 my ($make) = grep -x $_, map("$_/gmake", @path), map("$_/make", @path);
120 #### Write out all the .t files
121 my $generic_t = << 'EOS';
122 #!/usr/local/bin/perl5.8.8
123 use Slay::Makefile::Gress qw(do_tests);
124 do_tests('../../Common.smak', @ARGV);
127 my @TESTS;
128 my @generic_t_dirs = grep -d $_, <t/*>;
129 # Create generic .t files
130 foreach my $dir (@generic_t_dirs) {
131     opendir DIR, $dir;
132     my @need_t = grep s/\.init$/.t/, readdir(DIR);
133     closedir DIR;
134     foreach my $t (@need_t) {
135         open T, ">$dir/$t";
136         print T $generic_t;
137         close T;
138         push @TESTS, "$dir/$t";
139     }
142 # Make sure PrestConfig.pm gets rebuilt
143 unlink "lib/Text/Restructured/PrestConfig.pm";
145 #### Finally, create the Makefile
146 # Get list of perl modules in lib subdirectory
147 chomp (my @pm_files =
148        grep ! /\.svn|\bCVS\b|~\Z|\.PL\Z/, `find lib -type f`);
149 my %pm_files;
150 @pm_files{@pm_files} = map "blib/$_", @pm_files;
152 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
153 # the contents of the Makefile that is written.
154 my $pm_filter = "$^X -pe 'END { print qq{\\044VERSION = $Version\\012} }'";
155 WriteMakefile(
156     NAME           => 'Text::Restructured',
157     AUTHOR         => 'Mark Nodine <mnodine@alum.mit.edu>',
158     ABSTRACT       => 'Perl implementation of reStructuredText parser',
159     VERSION        => $Version,
160     EXE_FILES      => [qw(prest)],
161     MAN1PODS       => {},
162     MAN3PODS       => {},
163     PREREQ_PM      => { 'Text::ASCIIMathML'=>0.5,
164                         'Slay::Makefile::Gress'=>0,
165                         'Safe::World'=>0,
166                         'HTML::Entities'=>0,
167                         }, # e.g., Module::Name => 1.1
168     PL_FILES       => {'lib/Text/Restructured/PrestConfig.pm.PL' =>
169                        'lib/Text/Restructured/PrestConfig.pm'},
170     PM             => {'lib/Text/Restructured/PrestConfig.pm' =>
171                        'blib/lib/Text/Restructured/PrestConfig.pm',
172                        %pm_files},
173 #    PM_FILTER      =>  qq($^X -pe "END{print qq{our \\044VERSION=$Version\\;\\n}}"),
174 #    FIXIN          => 'cp',
175     test           => { TESTS => join ' ',@TESTS },
176     clean          => { FILES =>(join(' ', (@TESTS, map(/(.*)\.t$/ && "$1.run",
177                                                         @TESTS))) .
178                                  q[ t/Common.mk lib/Text/Restructured/PrestConfig.pm]) },
179     dist           => { COMPRESS => 'gzip', SUFFIX => '.gz' },
180     realclean      => { FILES => q(config.log config.log.bak) },
183 sub MY::libscan {
184     my ($self, $path) = @_;
185     return $path !~ /\.svn/ && $path;
188 sub MY::postamble {
189     return <<'MAKE_FRAG';
190 FIXIN = $(PERLRUNINST) insertperl.pl
192 $(INST_SCRIPT)/prest : lib/Text/Restructured/PrestConfig.pm
194 .PHONY: doc
195 doc ::
196         cd doc/src; $(MAKE)
197 MAKE_FRAG
200 #### Random support subroutines
202 # Prints a summary of the configuration
203 sub printsummary {
204     print "Here is the summary of the configuration:\n";
205     foreach my $cfg_item (@CFG_LIST) {
206         print "  $CFG_LIST{$cfg_item}{desc}: $CONFIG{$cfg_item}\n";
207     }
210 # This subroutine extracts usage information
211 sub usage {
212   my($what,$end) = @_;
213   $what = "Usage" if ! $what;
214   my $print;
215   if (open(ME,$0) == 1) {
216     while (<ME>) {
217       $print = 1 if /^# $what/o;
218       $print = 0 if ! /^#/o || ($end && /^# $end/o);
219       print substr($_,2) if $print;
220     }
221     close(ME);
222   }
223   else {
224     print STDERR "Usage not available.\n";
225   }
226   exit;
229 # Checks for valid URL or "none"
230 sub isurl {
231     my $fail = $_[0] !~ /^\w+:|none/i;
232     print STDERR "Must be either a URL reference or 'None'\n"
233         if $fail;
234     return $fail;
237 # Checks for yes or no answer
238 sub yesno {
239     my $fail = !($_[0] =~ s/^y.*/Yes/i || $_[0] =~ s/^n.*/No/i);
240     print "Must be 'yes' or 'no'\n" if $fail;
241     return $fail;