6 use CSS::Minifier::XS 0.07;
9 use File::Path qw(make_path);
13 use JavaScript::Minifier::XS;
18 my $canonical_url = 'http://www.exim.org/';
21 my %opt = parse_arguments();
24 do_doc( 'spec', $_ ) foreach @{ $opt{spec} || [] };
25 do_doc( 'filter', $_ ) foreach @{ $opt{filter} || [] };
26 do_web() if exists $opt{web};
28 ## Add the exim-html-current symlink
29 print "Symlinking exim-html-current to exim-html-$opt{latest}\n";
30 unlink("$opt{docroot}/exim-html-current") if ( -l "$opt{docroot}/exim-html-current" );
31 symlink( "exim-html-$opt{latest}", "$opt{docroot}/exim-html-current" )
32 || die "symlink to $opt{docroot}/exim-html-current failed";
34 ## Generate the website files
37 ## Make sure the template web directory exists
38 die "No such directory: $opt{tmpl}/web\n" unless -d "$opt{tmpl}/web";
40 ## Scan the web templates
43 my ($path) = substr( $File::Find::name, length("$opt{tmpl}/web"), length($File::Find::name) ) =~ m#^/*(.*)$#;
45 if ( -d "$opt{tmpl}/web/$path" ) {
47 ## Create the directory in the doc root if it doesn't exist
48 if ( !-d "$opt{docroot}/$path" ) {
49 mkdir("$opt{docroot}/$path") or die "Unable to make $opt{docroot}/$path: $!\n";
55 ## Build HTML from XSL files and simply copy static files which have changed
56 if ( $path =~ /(.+)\.xsl$/ ) {
57 print "Generating : docroot:/$1.html\n" if ($opt{verbose});
58 transform( undef, "$opt{tmpl}/web/$path", "$opt{docroot}/$1.html" );
60 elsif ( -f "$opt{tmpl}/web/$path" ) {
62 ## Skip if the file hasn't changed (mtime based)
63 return if -f "$opt{docroot}/$path" && ( stat("$opt{tmpl}/web/$path") )[9] == ( stat("$opt{docroot}/$path") )[9];
65 if ( $path =~ /(.+)\.css$/ ) {
66 print "CSS to : docroot:/$path\n" if ($opt{verbose});
67 my $content = read_file("$opt{tmpl}/web/$path");
68 write_file( "$opt{docroot}/$path", $opt{minify} ? CSS::Minifier::XS::minify($content) : $content );
70 elsif ( $path =~ /(.+)\.js$/ ) {
71 print "JS to : docroot:/$path\n" if ($opt{verbose});
72 my $content = read_file("$opt{tmpl}/web/$path");
73 write_file( "$opt{docroot}/$path", $opt{minify} ? JavaScript::Minifier::XS::minify($content) : $content );
77 print "Copying to : docroot:/$path\n" if ($opt{verbose});
78 copy( "$opt{tmpl}/web/$path", "$opt{docroot}/$path" ) or die "$path: $!";
81 utime( time, ( stat("$opt{tmpl}/web/$path") )[9], "$opt{docroot}/$path" );
90 ## Generate index/chapter files for a doc
92 my ( $type, $xml_path ) = @_;
94 ## Read and validate the XML file
95 my $xml = XML::LibXML->new()->parse_file($xml_path) or die $!;
97 ## Get the version number
98 my $version = $xml->findvalue('/book/bookinfo/revhistory/revision/revnumber');
99 die "Unable to get version number\n" unless defined $version && $version =~ /^\d+(\.\d+)*$/;
101 ## Prepend chapter filenames?
102 my $prepend_chapter = $type eq 'filter' ? 'filter_' : '';
104 ## Add the canonical url for this document
105 $xml->documentElement()
106 ->appendTextChild( 'canonical_url',
107 "${canonical_url}exim-html-current/doc/html/spec_html/" . ( $type eq 'spec' ? 'index' : 'filter' ) . ".html" );
110 xref_fixup( $xml, $prepend_chapter );
112 ## Generate the front page
114 my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? $type : 'index' ) . ".html";
115 print "Generating : docroot:/$path\n";
116 transform( $xml, "$opt{tmpl}/doc/index.xsl", "$opt{docroot}/$path", );
119 ## Generate a Table of Contents XML file
121 my $path = "exim-html-$version/doc/html/spec_html/" . ( $type eq 'filter' ? 'filter_toc' : 'index_toc' ) . ".xml";
122 print "Generating : docroot:/$path\n" if ($opt{verbose});
123 transform( $xml, "$opt{tmpl}/doc/toc.xsl", "$opt{docroot}/$path", );
126 ## Generate the chapters
128 foreach my $chapter ( map { $_->cloneNode(1) } $xml->findnodes('/book/chapter') ) {
130 ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
131 $chapter->appendTextChild( 'chapter_id', ++$counter );
133 ## Add previous/next/canonical urls for nav
135 $chapter->appendTextChild( 'prev_url',
140 : sprintf( '%sch%02d.html', $prepend_chapter, $counter - 1 ) );
141 $chapter->appendTextChild( 'next_url', sprintf( '%sch%02d.html', $prepend_chapter, $counter + 1 ) );
142 $chapter->appendTextChild( 'canonical_url',
143 sprintf( 'http://www.exim.org/exim-html-current/doc/html/spec_html/%sch%02d.html', $prepend_chapter, $counter ) );
146 ## Create an XML document from the chapter
147 my $doc = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
148 $doc->setDocumentElement($chapter);
150 ## Transform the chapter into html
152 my $path = sprintf( 'exim-html-%s/doc/html/spec_html/%sch%02d.html', $version, $prepend_chapter, $counter );
153 print "Generating : docroot:/$path\n" if ($opt{verbose});
154 transform( $doc, "$opt{tmpl}/doc/chapter.xsl", "$opt{docroot}/$path", );
161 my ( $xml, $prepend_chapter ) = @_;
165 ## Add the "prepend_chapter" info
166 ( $xml->findnodes('/book') )[0]->appendTextChild( 'prepend_chapter', $prepend_chapter );
168 ## Iterate over each chapter
169 my $chapter_counter = 0;
170 foreach my $chapter ( $xml->findnodes('/book/chapter') ) {
173 my $chapter_id = $chapter->getAttribute('id');
174 unless ($chapter_id) { # synthesise missing id
175 $chapter_id = sprintf( 'chapter_noid_%04d', $chapter_counter );
176 $chapter->setAttribute( 'id', $chapter_id );
178 my $chapter_title = $chapter->findvalue('title');
180 $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
182 ## Iterate over each section
183 my $section_counter = 0;
184 foreach my $section ( $chapter->findnodes('section') ) {
187 my $section_id = $section->getAttribute('id');
188 unless ($section_id) { # synthesise missing id
189 $section_id = sprintf( 'section_noid_%04d_%04d', $chapter_counter, $section_counter );
190 $section->setAttribute( 'id', $section_id );
192 my $section_title = $section->findvalue('title');
194 $index{$section_id} = {
195 chapter_id => $chapter_counter,
196 chapter_title => $chapter_title,
197 section_id => $section_counter,
198 section_title => $section_title
202 ## Build indexes as new chapters
203 build_indexes( $xml, $prepend_chapter, \%index );
205 ## Replace all of the xrefs in the XML
206 foreach my $xref ( $xml->findnodes('//xref') ) {
207 my $linkend = $xref->getAttribute('linkend');
208 if ( exists $index{$linkend} ) {
209 $xref->setAttribute( 'chapter_id', $index{$linkend}{'chapter_id'} );
210 $xref->setAttribute( 'chapter_title', $index{$linkend}{'chapter_title'} );
211 $xref->setAttribute( 'section_id', $index{$linkend}{'section_id'} ) if ( $index{$linkend}{'section_id'} );
212 $xref->setAttribute( 'section_title', $index{$linkend}{'section_title'} ) if ( $index{$linkend}{'section_title'} );
213 $xref->setAttribute( 'url',
214 sprintf( '%sch%02d.html', $prepend_chapter, $index{$linkend}{'chapter_id'} )
215 . ( $index{$linkend}{'section_id'} ? '#' . $linkend : '' ) );
222 my ( $xml, $prepend_chapter, $xref ) = @_;
226 foreach my $node ( $xml->findnodes('//section | //chapter | //indexterm') ) {
227 if ( $node->nodeName eq 'indexterm' ) {
228 my $role = $node->getAttribute('role') || 'concept';
229 my $primary = $node->findvalue('child::primary');
230 my $first = ( $primary =~ /^[A-Za-z]/ ) ? uc( substr( $primary, 0, 1 ) ) : ''; # first letter or marker
231 my $secondary = $node->findvalue('child::secondary') || '';
232 next unless ( $primary || $secondary ); # skip blank entries for now...
233 $index_hash->{$role}{$first}{$primary}{$secondary} ||= [];
234 push @{ $index_hash->{$role}{$first}{$primary}{$secondary} }, $current_id;
237 $current_id = $node->getAttribute('id');
241 # now we build a set of new chapters with the index data in
242 my $book = ( $xml->findnodes('/book') )[0];
243 foreach my $role ( sort { $a cmp $b } keys %{$index_hash} ) {
244 my $chapter = XML::LibXML::Element->new('chapter');
245 $book->appendChild($chapter);
246 $chapter->setAttribute( 'id', join( '_', 'index', $role ) );
247 $chapter->setAttribute( 'class', 'index' );
248 $chapter->appendTextChild( 'title', ( ucfirst($role) . ' Index' ) );
249 foreach my $first ( sort { $a cmp $b } keys %{ $index_hash->{$role} } ) {
250 my $section = XML::LibXML::Element->new('section');
251 my $list = XML::LibXML::Element->new('variablelist');
252 $chapter->appendChild($section);
253 $section->setAttribute( 'id', join( '_', 'index', $role, $first ) );
254 $section->setAttribute( 'class', 'index' );
255 $section->appendTextChild( 'title', $first ? $first : 'Symbols' );
256 $section->appendChild($list);
257 foreach my $primary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first} } ) {
258 my $entry = XML::LibXML::Element->new('varlistentry');
259 my $item = XML::LibXML::Element->new('listitem');
260 $list->appendChild($entry)->appendTextChild( 'term', $primary );
261 $entry->appendChild($item);
263 foreach my $secondary ( sort { $a cmp $b } keys %{ $index_hash->{$role}{$first}{$primary} } ) {
264 my $para = XML::LibXML::Element->new('para');
265 if ( $secondary eq '' ) {
266 $item->appendChild($para); # skip having extra layer of heirarchy
270 $slist = XML::LibXML::Element->new('variablelist');
271 $item->appendChild($slist);
273 my $sentry = XML::LibXML::Element->new('varlistentry');
274 my $sitem = XML::LibXML::Element->new('listitem');
275 $slist->appendChild($sentry)->appendTextChild( 'term', $secondary );
276 $sentry->appendChild($sitem)->appendChild($para);
279 foreach my $ref ( @{ $index_hash->{$role}{$first}{$primary}{$secondary} } ) {
280 $para->appendText(', ')
282 my $xrefel = XML::LibXML::Element->new('xref');
283 $xrefel->setAttribute( linkend => $ref );
284 $xrefel->setAttribute( longref => 1 );
285 $para->appendChild($xrefel);
293 ## Handle the transformation
295 my ( $xml, $xsl_path, $out_path ) = @_;
297 ## Build an empty XML structure if an undefined $xml was passed
298 unless ( defined $xml ) {
299 $xml = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
300 $xml->setDocumentElement( $xml->createElement('content') );
303 ## Add the current version of Exim to the XML
304 $xml->documentElement()->appendTextChild( 'current_version', $opt{latest} );
306 ## Parse the ".xsl" file as XML
307 my $xsl = XML::LibXML->new()->parse_file($xsl_path) or die $!;
309 ## Generate a stylesheet from the ".xsl" XML.
310 my $stylesheet = XML::LibXSLT->new()->parse_stylesheet($xsl);
312 ## Generate a doc from the XML transformed with the XSL
313 my $doc = $stylesheet->transform($xml);
315 ## Make the containing directory if it doesn't exist
316 make_path( ( $out_path =~ /^(.+)\/.+$/ )[0], { verbose => 1 } );
318 ## Write out the document
319 open my $out, '>', $out_path or die "Unable to write $out_path - $!";
320 print $out $stylesheet->output_as_bytes($doc);
329 pod2usage( -exitval => 1, -verbose => 0 );
333 sub parse_arguments {
335 my %opt = ( spec => [], filter => [], help => 0, man => 0, web => 0, minify => 1, verbose => 0 );
336 GetOptions( \%opt, 'help|h!', 'man!', 'web!', 'spec=s{1,}', 'filter=s{1,}', 'latest=s', 'tmpl=s', 'docroot=s', 'minify!', 'verbose!' )
337 || pod2usage( -exitval => 1, -verbose => 0 );
340 pod2usage(0) if ( $opt{help} );
341 pod2usage( -verbose => 2 ) if ( $opt{man} );
343 ## --spec and --filter lists
344 foreach my $set (qw[spec filter]) {
345 $opt{$set} = [ map { my $f = File::Spec->rel2abs($_); help( 1, 'No such file: ' . $_ ) unless -f $f; $f } @{ $opt{$set} } ];
348 error_help('Missing value for latest') unless ( exists( $opt{latest} ) && defined( $opt{latest} ) );
349 error_help('Invalid value for latest') unless $opt{latest} =~ /^\d+(?:\.\d+)*$/;
351 ## --tmpl and --docroot
352 foreach my $set (qw[tmpl docroot]) {
353 error_help( 'Missing value for ' . $set ) unless ( exists( $opt{$set} ) && defined( $opt{$set} ) );
354 my $f = File::Spec->rel2abs( $opt{$set} );
355 error_help( 'No such directory: ' . $opt{$set} ) unless -d $f;
358 error_help('Excess arguments') if ( scalar(@ARGV) );
360 error_help('Must include at least one of --web, --spec or --filter')
361 unless ( $opt{web} || scalar( @{ $opt{spec} || [] } ) || scalar( @{ $opt{filter} || [] } ) );
372 gen.pl - Generate exim html documentation and website
379 --help display this help and exits
380 --man displays man page
381 --spec file... spec docbook/XML source files
382 --filter file... filter docbook/XML source files
383 --web Generate the general website pages
384 --latest VERSION Required. Specify the latest stable version of Exim.
385 --tmpl PATH Required. Path to the templates directory
386 --docroot PATH Required. Path to the website document root
387 --[no-]minify [Don't] minify CSS and Javascript
395 Display help and exits
401 =item B<--spec> I<file...>
403 List of files that make up the specification documentation
404 docbook/XML source files.
406 =item B<--filter> I<file...>
408 List of files that make up the filter documentation docbook/XML
413 Generate the website from the template files.
415 =item B<--latest> I<version>
417 Specify the current exim version. This is used to create links to
418 the current documentation.
420 This option is I<required>
422 =item B<--tmpl> I<directory>
424 Specify the directory that the templates are kept in.
426 This option is I<required>
428 =item B<--docroot> I<directory>
430 Specify the directory that the output should be generated into.
431 This is the website C<docroot> directory.
433 This option is I<required>
437 If this option is set then both the CSS and Javascript files
438 processed are minified using L<CSS::Minifier::XS> and
439 L<JavaScript::Minifier::XS> respectively.
441 This option is set by default - to disable it specify C<--no-minify>
447 Generates the exim website and HTML documentation.
453 --spec docbook/*/spec.xml \
454 --filter docbook/*/filter.xml \
457 --docroot /tmp/website
463 Nigel Metheringham <nigel@exim.org> - mostly broke the framework
468 Copyright 2010 Exim Maintainers. All rights reserved.