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