10 my $canonical_url = 'http://www.exim.org/';
13 my %opt = parse_arguments();
16 do_doc( 'spec', $_ ) foreach @{ $opt{spec} || [] };
17 do_doc( 'filter', $_ ) foreach @{ $opt{filter} || [] };
18 do_web() if exists $opt{web};
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" );
24 ## Generate the website files
27 ## Make sure the template web directory exists
28 die "No such directory: $opt{tmpl}/web\n" unless -d "$opt{tmpl}/web";
30 ## Scan the web templates
33 my ($path) = substr( $File::Find::name, length("$opt{tmpl}/web"), length($File::Find::name) ) =~ m#^/*(.*)$#;
35 if ( -d "$opt{tmpl}/web/$path" ) {
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";
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" );
50 elsif ( -f "$opt{tmpl}/web/$path" ) {
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];
56 print "Copying to : docroot:/$path\n";
57 copy( "$opt{tmpl}/web/$path", "$opt{docroot}/$path" ) or die "$path: $!";
60 utime( time, ( stat("$opt{tmpl}/web/$path") )[9], "$opt{docroot}/$path" );
69 ## Generate index/chapter files for a doc
71 my ( $type, $xml_path ) = @_;
73 ## Read and validate the XML file
74 my $xml = XML::LibXML->new()->parse_file($xml_path) or die $!;
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+)*$/;
80 ## Prepend chapter filenames?
81 my $prepend_chapter = $type eq 'filter' ? 'filter_' : '';
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" );
89 xref_fixup( $xml, $prepend_chapter );
91 ## Generate the front page
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", );
98 ## Generate a Table of Contents XML file
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", );
105 ## Generate the chapters
107 foreach my $chapter ( map { $_->cloneNode(1) } $xml->findnodes('/book/chapter') ) {
109 ## Add a <chapter_id>N</chapter_id> node for the stylesheet to use
110 $chapter->appendTextChild( 'chapter_id', ++$counter );
112 ## Add previous/next/canonical urls for nav
114 $chapter->appendTextChild( 'prev_url',
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 ) );
125 ## Create an XML document from the chapter
126 my $doc = XML::LibXML::Document->createDocument( '1.0', 'UTF-8' );
127 $doc->setDocumentElement($chapter);
129 ## Transform the chapter into html
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", );
140 my ( $xml, $prepend_chapter ) = @_;
144 ## Add the "prepend_chapter" info
145 ( $xml->findnodes('/book') )[0]->appendTextChild( 'prepend_chapter', $prepend_chapter );
147 ## Iterate over each chapter
148 my $chapter_counter = 0;
149 foreach my $chapter ( $xml->findnodes('/book/chapter') ) {
152 my $chapter_id = $chapter->getAttribute('id');
153 my $chapter_title = $chapter->findvalue('title');
155 $index{$chapter_id} = { chapter_id => $chapter_counter, chapter_title => $chapter_title };
157 ## Iterate over each section
158 my $section_counter = 0;
159 foreach my $section ( $chapter->findnodes('section') ) {
162 my $section_id = $section->getAttribute('id');
163 my $section_title = $section->findvalue('title');
165 $index{$section_id} =
166 { chapter_id => $chapter_counter, chapter_title => $chapter_title, section_id => $section_counter };
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 : '' ) );
184 ## Handle the transformation
186 my ( $xml, $xsl_path, $out_path ) = @_;
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') );
194 ## Add the current version of Exim to the XML
195 $xml->documentElement()->appendTextChild( 'current_version', $opt{latest} );
197 ## Parse the ".xsl" file as XML
198 my $xsl = XML::LibXML->new()->parse_file($xsl_path) or die $!;
200 ## Generate a stylesheet from the ".xsl" XML.
201 my $stylesheet = XML::LibXSLT->new()->parse_stylesheet($xsl);
203 ## Generate a doc from the XML transformed with the XSL
204 my $doc = $stylesheet->transform($xml);
206 ## Make the containing directory if it doesn't exist
207 mkdirp( ( $out_path =~ /^(.+)\/.+$/ )[0] );
209 ## Write out the document
210 open my $out, '>', $out_path or die $!;
211 print $out $stylesheet->output_as_bytes($doc);
220 foreach ( split( /\//, $path ) ) {
222 my $make = join( '/', @parts );
223 next unless length($make);
225 mkdir($make) or die "Unable to mkdir $make: $!\n";
230 sub parse_arguments {
234 help(0) if int(@ARGV) == 0 || grep( /^--help|-h$/, @ARGV );
236 my @collection = @ARGV;
237 while (@collection) {
238 my $key = shift @collection;
240 if ( $key eq '--web' ) {
245 elsif ( $key =~ /^--(spec|filter)$/ ) {
247 ## --spec and --filter
249 while ( $continue && @collection ) {
250 my $value = shift @collection;
252 if ( $value =~ /^--/ ) {
253 unshift @collection, $value;
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} } );
262 help( 1, 'Missing value for ' . $key ) unless exists $opt{$1};
264 elsif ( $key eq '--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;
272 elsif ( $key =~ /^--(tmpl|docroot)$/ ) {
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;
283 help( 1, 'Bad argument: ' . $key );
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{$_};
298 my ( $exit_code, $msg ) = @_;
300 print "$msg\n\n" if defined $msg;
304 --help or -h : Print this help information and then exit
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
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.
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
318 If CSS::Minifier::XS is installed, then CSS will be minified.
319 If JavaScript::Minifier::XS is installed, then JavaScript will be minified.
323 ./gen.pl --latest 4.72
325 --spec spec.xml 4.71/spec.xml
326 --filter filter.xml 4.71/filter.xml
327 --tmpl ~/www/templates
328 --docroot ~/www/docroot