1#!/usr/bin/env perl
2
3#
4# Generate indexes for html documentation
5#
6
7use strict;
8use warnings;
9
10use Getopt::Long;
11use IO::File;
12use File::Basename;
13
14Getopt::Long::Configure('bundling');
15
16@ARGV >= 2 or die;
17
18our @docs;
19our @dirs;
20our %index;
21
22our $outdir;
23
24GetOptions("i=s" => sub { read_index(@_);} )
25    or die;
26
27($outdir,@docs) = @ARGV;
28
29sub write_file ($$) {
30    my ($opath, $odata) = @_;
31    print STDOUT "Writing: $opath\n";
32    my $out = new IO::File "$opath.new", '>' or die "$opath $!";
33    print $out $odata or die $!;
34    rename "$opath.new", "$opath" or die "$opath $!";
35}
36
37sub make_page ($$$) {
38    my ($file,$title,$content) = @_;
39    my $o = '';
40    my $h1;
41    if ( $title eq "" )
42    {
43        $title = $h1 = "Xen Documentation";
44    }
45    else
46    {
47        $h1 = "<a href=\"../index.html\">Xen Documentation</a> - $title";
48        $title = "Xen Documentation - $title";
49    }
50    $o .= <<END;
51<html><head><title>$title</title></head>
52<body>
53<h1>$h1</h1>
54<ul>
55$content
56</ul>
57</body></html>
58END
59    write_file($file, $o);
60}
61
62sub make_linktext ($) {
63    my ($l) = @_;
64    return "$1($2)" if $l =~ m,^man/(.*)\.([0-9].*)\.html,;
65    $l =~ s/.(?:html|txt)$//g;
66    return $index{$l} if exists $index{$l};
67    return basename($l);
68}
69
70sub make_link ($$) {
71    my ($ref,$base) = @_;
72
73    my $txt = make_linktext($ref);
74    $ref =~ s,^$base/,, if $base; #/
75
76    return "<li><a href=\"$ref\">$txt</a></li>\n";
77}
78
79sub make_links ($@) {
80    my ($dir,@docs) = @_;
81    my $idx = '';
82    foreach my $of (sort { make_linktext($a) cmp make_linktext($b) } @docs) {
83        $idx .= make_link($of,$dir);
84    }
85    return $idx;
86}
87
88sub read_index ($$) {
89    my ($opt, $val) = @_;
90    my $idx = new IO::File "$val", '<' or die "$val $!";
91    while ($_ = $idx->getline()) {
92	s/^\s+//;
93	s/\s+$//;
94	next if m/^\#/;
95	next unless m/\S/;
96	m/^(\S+)\s+(\S.*)$/ or die;
97        $index{$1} = $2;
98    }
99}
100
101sub uniq (@) {
102    my %h;
103    foreach (@_) { $h{$_} = 1; }
104    return keys %h;
105}
106
107for (@docs) { s,^\Q$outdir\E/,, }
108
109@docs = grep { -e "$outdir/$_" && (make_linktext($_) ne "NO-INDEX") } @docs;
110
111my $top = '';
112
113# Return a list of all directories leading to $path
114sub dirs($)
115{
116    my ($path) = @_;
117    my @dirs;
118    while ( $path =~ m,/, )
119    {
120	$path =~ m,/([^/]+)$,;
121	push @dirs, $`;#`
122	$path = $`;#`
123    }
124    return @dirs;
125}
126
127foreach my $od (sort { $a cmp $b } uniq map { dirs($_) } @docs) {
128    my @d = (grep /^\Q$od\E/, @docs);
129    if ( @d == 1 and $d[0] eq "$od/index.html" )
130    {
131        next if $d[0] =~ m,/,;#/ linked to from the subdirectory entry.
132        $top .= make_link("$od/index.html", 0);
133    }
134    else
135    {
136	my $links = make_links(undef,@d);
137	my $secttitle = make_linktext($od);
138	$top .= <<END;
139<li><a href=\"${od}/index.html\">$secttitle</a></li>
140<ul>
141$links
142</ul>
143END
144
145	$links = make_links($od,@d);
146	my $idx = '';
147	$idx .= <<END;
148<li>$secttitle</li>
149<ul>
150$links
151</ul>
152END
153        make_page("$outdir/$od/index.html", $secttitle, $idx);
154    }
155}
156
157make_page("$outdir/index.html", "", $top);
158