4c545f52cdf477158d5a2a3c044c8f186e47758f
[exim-website.git] / script / gen.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use CSS::Minifier::XS;
5 use File::Copy;
6 use File::Find;
7 use File::Slurp;
8 use File::Spec;
9 use JavaScript::Minifier::XS;
10 use XML::LibXML;
11 use XML::LibXSLT;
12
13 my $canonical_url = 'http://www.exim.org/';
14
15 ## Parse arguments
16 my %opt = parse_arguments();
17
18 ## Generate the pages
19 do_doc( 'spec',   $_ ) foreach @{ $opt{spec}   || [] };
20 do_doc( 'filter', $_ ) foreach @{ $opt{filter} || [] };
21 do_web() if exists $opt{web};
22
23 ## Add the exim-html-current symlink
24 print "Symlinking exim-html-current to exim-html-$opt{latest}\n";
25 symlink( "$opt{docroot}/exim-html-$opt{latest}", "$opt{docroot}/exim-html-current" );
26
27 ## Generate the website files
28 sub do_web {
29
30     ## Make sure the template web directory exists
31     die "No such directory: $opt{tmpl}/web\n" unless -d "$opt{tmpl}/web";
32
33     ## Scan the web templates
34     find(
35         sub {
36             my ($path) = substr( $File::Find::name, length("$opt{tmpl}/web"), length($File::Find::name) ) =~ m#^/*(.*)$#;
37
38             if ( -d "$opt{tmpl}/web/$path" ) {
39
40                 ## Create the directory in the doc root if it doesn't exist
41                 if ( !-d "$opt{docroot}/$path" ) {
42                     mkdir("$opt{docroot}/$path") or die "Unable to make $opt{docroot}/$path: $!\n";
43                 }
44
45             }
46             else {
47
48                 ## Build HTML from XSL files and simply copy static files which have changed
49                 if ( $path =~ /(.+)\.xsl$/ ) {
50                     print "Generating  : docroot:/$1.html\n";
51                     transform( undef, "$opt{tmpl}/web/$path", "$opt{docroot}/$1.html" );
52                 }
53                 elsif ( -f "$opt{tmpl}/web/$path" ) {
54
55                     ## Skip if the file hasn't changed (mtime based)
56                     return if -f "$opt{docroot}/$path" && ( stat("$opt{tmpl}/web/$path") )[9] == ( stat("$opt{docroot}/$path") )[9];
57
58                     if ( $path =~ /(.+)\.css$/ ) {
59                         print "CSS to  : docroot:/$path\n";
60                         my $content = read_file("$opt{tmpl}/web/$path");
61                         write_file( "$opt{docroot}/$path", CSS::Minifier::XS::minify($content) );
62                     }
63                     elsif ( $path =~ /(.+)\.js$/ ) {
64                         print "JS to  : docroot:/$path\n";
65                         my $content = read_file("$opt{tmpl}/web/$path");
66                         write_file( "$opt{docroot}/$path", JavaScript::Minifier::XS::minify($content) );
67                     }
68                     else {
69                         ## Copy
70                         print "Copying to  : docroot:/$path\n";
71                         copy( "$opt{tmpl}/web/$path", "$opt{docroot}/$path" ) or die "$path: $!";
72                     }
73                     ## Set mtime
74                     utime( time, ( stat("$opt{tmpl}/web/$path") )[9], "$opt{docroot}/$path" );
75                 }
76             }
77
78         },
79         "$opt{tmpl}/web"
80     );
81 }
82
83 ## Generate index/chapter files for a doc
84 sub do_doc {
85     my ( $type, $xml_path ) = @_;
86
87     ## Read and validate the XML file
88     my $xml = XML::LibXML->new()->parse_file($xml_path) or die $!;
89
90     ## Get the version number
91     my $version = $xml->findvalue('/book/bookinfo/revhistory/revision/revnumber');
92     die "Unable to get version number\n" unless defined $version && $version =~ /^\d+(\.\d+)*$/;
93
94     ## Prepend chapter filenames?
95     my $prepend_chapter = $type eq 'filter' ? 'filter_' : '';
96
97     ## Add the canonical url for this document
98     $xml->documentElement()
99       ->appendTextChild( 'canonical_url',
100         "${canonical_url}exim-html-current/doc/html/spec_html/" . ( $type eq 'spec' ? 'index' : 'filter' ) . ".html" );
101
102     ## Fixup the XML
103     xref_fixup( $xml, $prepend_chapter );
104
105     ## Generate the front page
106     {
107         my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? $type : 'index' ) . ".html";
108         print "Generating  : docroot:/$path\n";
109         transform( $xml, "$opt{tmpl}/doc/index.xsl", "$opt{docroot}/$path", );
110     }
111
112     ## Generate a Table of Contents XML file
113     {
114         my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? 'filter_toc' : 'index_toc' ) . ".xml";
115         print "Generating  : docroot:/$path\n";
116         transform( $xml, "$opt{tmpl}/doc/toc.xsl", "$opt{docroot}/$path", );
117     }
118
119     ## Generate the chapters
120     my $counter = 0;
121     foreach my $chapter ( map { $_->cloneNode(1) } $xml->findnodes('/book/chapter') ) {
122
123         ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
124         $chapter->appendTextChild( 'chapter_id', ++$counter );
125
126         ## Add previous/next/canonical urls for nav
127         {
128             $chapter->appendTextChild( 'prev_url',
129                   $counter == 1
130                 ? $type eq 'filter'
131                       ? 'filter.html'
132                       : 'index.html'
133                 : sprintf( '%sch%02d.html', $prepend_chapter, $counter - 1 ) );
134             $chapter->appendTextChild( 'next_url', sprintf( '%sch%02d.html', $prepend_chapter, $counter + 1 ) );
135             $chapter->appendTextChild( 'canonical_url',
136                 sprintf( 'http://www.exim.org/exim-html-current/doc/html/spec_html/%sch%02d.html', $prepend_chapter, $counter ) );
137         }
138
139         ## Create an XML document from the chapter
140         my $doc = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
141         $doc->setDocumentElement($chapter);
142
143         ## Transform the chapter into html
144         {
145             my $path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch%02d.html', $version, $prepend_chapter, $counter );
146             print "Generating  : docroot:/$path\n";
147             transform( $doc, "$opt{tmpl}/doc/chapter.xsl", "$opt{docroot}/$path", );
148         }
149     }
150 }
151
152 ## Fixup xref tags
153 sub xref_fixup {
154     my ( $xml, $prepend_chapter ) = @_;
155
156     my %index = ();
157
158     ## Add the "prepend_chapter" info
159     ( $xml->findnodes('/book') )[0]->appendTextChild( 'prepend_chapter', $prepend_chapter );
160
161     ## Iterate over each chapter
162     my $chapter_counter = 0;
163     foreach my $chapter ( $xml->findnodes('/book/chapter') ) {
164         ++$chapter_counter;
165
166         my $chapter_id    = $chapter->getAttribute('id');
167         my $chapter_title = $chapter->findvalue('title');
168
169         $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
170
171         ## Iterate over each section
172         my $section_counter = 0;
173         foreach my $section ( $chapter->findnodes('section') ) {
174             ++$section_counter;
175
176             my $section_id    = $section->getAttribute('id');
177             my $section_title = $section->findvalue('title');
178
179             $index{$section_id} =
180               { chapter_id => $chapter_counter, chapter_title => $chapter_title, section_id => $section_counter };
181         }
182     }
183
184     ## Replace all of the xrefs in the XML
185     foreach my $xref ( $xml->findnodes('//xref') ) {
186         my $linkend = $xref->getAttribute('linkend');
187         if ( exists $index{$linkend} ) {
188             $xref->setAttribute( 'chapter_id',    $index{$linkend}{'chapter_id'} );
189             $xref->setAttribute( 'chapter_title', $index{$linkend}{'chapter_title'} );
190             $xref->setAttribute( 'section_id',    $index{$linkend}{'section_id'} ) if $index{$linkend}{'section_id'};
191             $xref->setAttribute( 'url',
192                 sprintf( '%sch%02d.html', $prepend_chapter, $index{$linkend}{'chapter_id'} )
193                   . ( $index{$linkend}{'section_id'} ? '#' . $linkend : '' ) );
194         }
195     }
196 }
197
198 ## Handle the transformation
199 sub transform {
200     my ( $xml, $xsl_path, $out_path ) = @_;
201
202     ## Build an empty XML structure if an undefined $xml was passed
203     unless ( defined $xml ) {
204         $xml = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
205         $xml->setDocumentElement( $xml->createElement('content') );
206     }
207
208     ## Add the current version of Exim to the XML
209     $xml->documentElement()->appendTextChild( 'current_version', $opt{latest} );
210
211     ## Parse the ".xsl" file as XML
212     my $xsl = XML::LibXML->new()->parse_file($xsl_path) or die $!;
213
214     ## Generate a stylesheet from the ".xsl" XML.
215     my $stylesheet = XML::LibXSLT->new()->parse_stylesheet($xsl);
216
217     ## Generate a doc from the XML transformed with the XSL
218     my $doc = $stylesheet->transform($xml);
219
220     ## Make the containing directory if it doesn't exist
221     mkdirp( ( $out_path =~ /^(.+)\/.+$/ )[0] );
222
223     ## Write out the document
224     open my $out, '>', $out_path or die $!;
225     print $out $stylesheet->output_as_bytes($doc);
226     close $out;
227 }
228
229 ## "mkdir -p "
230 sub mkdirp {
231     my $path = shift;
232
233     my @parts = ();
234     foreach ( split( /\//, $path ) ) {
235         push @parts, $_;
236         my $make = join( '/', @parts );
237         next unless length($make);
238         next if -d $make;
239         mkdir($make) or die "Unable to mkdir $make: $!\n";
240     }
241 }
242
243 ## Parse arguments
244 sub parse_arguments {
245     my %opt = ();
246
247     ## --help
248     help(0) if int(@ARGV) == 0 || grep( /^--help|-h$/, @ARGV );
249
250     my @collection = @ARGV;
251     while (@collection) {
252         my $key = shift @collection;
253
254         if ( $key eq '--web' ) {
255
256             ## --web
257             $opt{web} = 1;
258         }
259         elsif ( $key =~ /^--(spec|filter)$/ ) {
260
261             ## --spec and --filter
262             my $continue = 1;
263             while ( $continue && @collection ) {
264                 my $value = shift @collection;
265
266                 if ( $value =~ /^--/ ) {
267                     unshift @collection, $value;
268                     $continue = 0;
269                 }
270                 else {
271                     $value = File::Spec->rel2abs($value);
272                     help( 1, 'No such file: ' . $value ) unless -f $value;
273                     push @{ $opt{$1} }, $value unless grep( $_ eq $value, @{ $opt{$1} } );
274                 }
275             }
276             help( 1, 'Missing value for ' . $key ) unless exists $opt{$1};
277         }
278         elsif ( $key eq '--latest' ) {
279
280             ## --latest
281             my $value = shift @collection;
282             help( 1, 'Missing value for ' . $key ) unless defined $value;
283             help( 1, 'Invalid value for ' . $key ) unless $value =~ /^\d+(?:\.\d+)*$/;
284             $opt{latest} = $value;
285         }
286         elsif ( $key =~ /^--(tmpl|docroot)$/ ) {
287
288             ## --tmpl and --docroot
289             my $value = shift @collection;
290             help( 1, 'Missing value for ' . $key ) unless defined $value;
291             $value = File::Spec->rel2abs($value);
292             help( 1, 'No such directory: ' . $value ) unless -d $value;
293             $opt{$1} = $value;
294             $opt{$1} =~ s#/+$##;
295         }
296         else {
297             help( 1, 'Bad argument: ' . $key );
298         }
299     }
300
301     help( 1, 'Must include at least one of --web, --spec or --filter' )
302       unless exists $opt{web} || exists $opt{spec} || exists $opt{filter};
303     foreach (qw( latest tmpl docroot )) {
304         help( 1, 'Missing argument: --' . $_ ) unless exists $opt{$_};
305     }
306
307     return %opt;
308 }
309
310 ## Help information
311 sub help {
312     my ( $exit_code, $msg ) = @_;
313
314     print "$msg\n\n" if defined $msg;
315
316     print << "END_HELP";
317 Options:
318    --help or -h  : Print this help information and then exit
319
320    --web         : Generate the general website pages
321    --spec PATH   : Generate the spec pages. PATH is the path to the spec.xml
322    --filter PATH : Generate the filter pages. PATH is the path to the filter.xml
323
324    One or more of the above three options are required. --spec and --filter can
325    take multiple values to generate different sets of documentation for
326    different versions at the same time.
327
328    --latest VERSION : Required. Specify the latest stable version of Exim.
329    --tmpl PATH      : Required. Path to the templates directory
330    --docroot PATH   : Required. Path to the website document root
331
332    If CSS::Minifier::XS is installed, then CSS will be minified.
333    If JavaScript::Minifier::XS is installed, then JavaScript will be minified.
334
335 Example:
336
337    ./gen.pl --latest 4.72
338             --web
339             --spec spec.xml 4.71/spec.xml
340             --filter filter.xml 4.71/filter.xml
341             --tmpl ~/www/templates
342             --docroot ~/www/docroot
343 END_HELP
344
345     exit($exit_code);
346 }