9 use JavaScript::Minifier::XS;
13 my $canonical_url = 'http://www.exim.org/';
16 my %opt = parse_arguments();
19 do_doc( 'spec', $_ ) foreach @{ $opt{spec} || [] };
20 do_doc( 'filter', $_ ) foreach @{ $opt{filter} || [] };
21 do_web() if exists $opt{web};
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" );
27 ## Generate the website files
30 ## Make sure the template web directory exists
31 die "No such directory: $opt{tmpl}/web\n" unless -d "$opt{tmpl}/web";
33 ## Scan the web templates
36 my ($path) = substr( $File::Find::name, length("$opt{tmpl}/web"), length($File::Find::name) ) =~ m#^/*(.*)$#;
38 if ( -d "$opt{tmpl}/web/$path" ) {
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";
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" );
53 elsif ( -f "$opt{tmpl}/web/$path" ) {
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];
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) );
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) );
70 print "Copying to : docroot:/$path\n";
71 copy( "$opt{tmpl}/web/$path", "$opt{docroot}/$path" ) or die "$path: $!";
74 utime( time, ( stat("$opt{tmpl}/web/$path") )[9], "$opt{docroot}/$path" );
83 ## Generate index/chapter files for a doc
85 my ( $type, $xml_path ) = @_;
87 ## Read and validate the XML file
88 my $xml = XML::LibXML->new()->parse_file($xml_path) or die $!;
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+)*$/;
94 ## Prepend chapter filenames?
95 my $prepend_chapter = $type eq 'filter' ? 'filter_' : '';
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" );
103 xref_fixup( $xml, $prepend_chapter );
105 ## Generate the front page
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", );
112 ## Generate a Table of Contents XML file
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", );
119 ## Generate the chapters
121 foreach my $chapter ( map { $_->cloneNode(1) } $xml->findnodes('/book/chapter') ) {
123 ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
124 $chapter->appendTextChild( 'chapter_id', ++$counter );
126 ## Add previous/next/canonical urls for nav
128 $chapter->appendTextChild( 'prev_url',
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 ) );
139 ## Create an XML document from the chapter
140 my $doc = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
141 $doc->setDocumentElement($chapter);
143 ## Transform the chapter into html
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", );
154 my ( $xml, $prepend_chapter ) = @_;
158 ## Add the "prepend_chapter" info
159 ( $xml->findnodes('/book') )[0]->appendTextChild( 'prepend_chapter', $prepend_chapter );
161 ## Iterate over each chapter
162 my $chapter_counter = 0;
163 foreach my $chapter ( $xml->findnodes('/book/chapter') ) {
166 my $chapter_id = $chapter->getAttribute('id');
167 unless ($chapter_id) { # synthesise missing id
168 $chapter_id = sprintf( 'chapter_noid_%04d', $chapter_counter );
169 $chapter->setAttribute( 'id', $chapter_id );
171 my $chapter_title = $chapter->findvalue('title');
173 $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
175 ## Iterate over each section
176 my $section_counter = 0;
177 foreach my $section ( $chapter->findnodes('section') ) {
180 my $section_id = $section->getAttribute('id');
181 unless ($section_id) { # synthesise missing id
182 $section_id = sprintf( 'section_noid_%04d_%04d', $chapter_counter, $section_counter );
183 $section->setAttribute( 'id', $section_id );
185 my $section_title = $section->findvalue('title');
187 $index{$section_id} =
188 { chapter_id => $chapter_counter, chapter_title => $chapter_title, section_id => $section_counter };
192 ## Replace all of the xrefs in the XML
193 foreach my $xref ( $xml->findnodes('//xref') ) {
194 my $linkend = $xref->getAttribute('linkend');
195 if ( exists $index{$linkend} ) {
196 $xref->setAttribute( 'chapter_id', $index{$linkend}{'chapter_id'} );
197 $xref->setAttribute( 'chapter_title', $index{$linkend}{'chapter_title'} );
198 $xref->setAttribute( 'section_id', $index{$linkend}{'section_id'} ) if $index{$linkend}{'section_id'};
199 $xref->setAttribute( 'url',
200 sprintf( '%sch%02d.html', $prepend_chapter, $index{$linkend}{'chapter_id'} )
201 . ( $index{$linkend}{'section_id'} ? '#' . $linkend : '' ) );
206 ## Handle the transformation
208 my ( $xml, $xsl_path, $out_path ) = @_;
210 ## Build an empty XML structure if an undefined $xml was passed
211 unless ( defined $xml ) {
212 $xml = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
213 $xml->setDocumentElement( $xml->createElement('content') );
216 ## Add the current version of Exim to the XML
217 $xml->documentElement()->appendTextChild( 'current_version', $opt{latest} );
219 ## Parse the ".xsl" file as XML
220 my $xsl = XML::LibXML->new()->parse_file($xsl_path) or die $!;
222 ## Generate a stylesheet from the ".xsl" XML.
223 my $stylesheet = XML::LibXSLT->new()->parse_stylesheet($xsl);
225 ## Generate a doc from the XML transformed with the XSL
226 my $doc = $stylesheet->transform($xml);
228 ## Make the containing directory if it doesn't exist
229 mkdirp( ( $out_path =~ /^(.+)\/.+$/ )[0] );
231 ## Write out the document
232 open my $out, '>', $out_path or die $!;
233 print $out $stylesheet->output_as_bytes($doc);
242 foreach ( split( /\//, $path ) ) {
244 my $make = join( '/', @parts );
245 next unless length($make);
247 mkdir($make) or die "Unable to mkdir $make: $!\n";
252 sub parse_arguments {
256 help(0) if int(@ARGV) == 0 || grep( /^--help|-h$/, @ARGV );
258 my @collection = @ARGV;
259 while (@collection) {
260 my $key = shift @collection;
262 if ( $key eq '--web' ) {
267 elsif ( $key =~ /^--(spec|filter)$/ ) {
269 ## --spec and --filter
271 while ( $continue && @collection ) {
272 my $value = shift @collection;
274 if ( $value =~ /^--/ ) {
275 unshift @collection, $value;
279 $value = File::Spec->rel2abs($value);
280 help( 1, 'No such file: ' . $value ) unless -f $value;
281 push @{ $opt{$1} }, $value unless grep( $_ eq $value, @{ $opt{$1} } );
284 help( 1, 'Missing value for ' . $key ) unless exists $opt{$1};
286 elsif ( $key eq '--latest' ) {
289 my $value = shift @collection;
290 help( 1, 'Missing value for ' . $key ) unless defined $value;
291 help( 1, 'Invalid value for ' . $key ) unless $value =~ /^\d+(?:\.\d+)*$/;
292 $opt{latest} = $value;
294 elsif ( $key =~ /^--(tmpl|docroot)$/ ) {
296 ## --tmpl and --docroot
297 my $value = shift @collection;
298 help( 1, 'Missing value for ' . $key ) unless defined $value;
299 $value = File::Spec->rel2abs($value);
300 help( 1, 'No such directory: ' . $value ) unless -d $value;
305 help( 1, 'Bad argument: ' . $key );
309 help( 1, 'Must include at least one of --web, --spec or --filter' )
310 unless exists $opt{web} || exists $opt{spec} || exists $opt{filter};
311 foreach (qw( latest tmpl docroot )) {
312 help( 1, 'Missing argument: --' . $_ ) unless exists $opt{$_};
320 my ( $exit_code, $msg ) = @_;
322 print "$msg\n\n" if defined $msg;
326 --help or -h : Print this help information and then exit
328 --web : Generate the general website pages
329 --spec PATH : Generate the spec pages. PATH is the path to the spec.xml
330 --filter PATH : Generate the filter pages. PATH is the path to the filter.xml
332 One or more of the above three options are required. --spec and --filter can
333 take multiple values to generate different sets of documentation for
334 different versions at the same time.
336 --latest VERSION : Required. Specify the latest stable version of Exim.
337 --tmpl PATH : Required. Path to the templates directory
338 --docroot PATH : Required. Path to the website document root
340 If CSS::Minifier::XS is installed, then CSS will be minified.
341 If JavaScript::Minifier::XS is installed, then JavaScript will be minified.
345 ./gen.pl --latest 4.72
347 --spec spec.xml 4.71/spec.xml
348 --filter filter.xml 4.71/filter.xml
349 --tmpl ~/www/templates
350 --docroot ~/www/docroot