/usr/sbin/dwww-build-menu is in dwww 1.11.7.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | #!/usr/bin/perl
# vim:ft=perl:cindent:ts=4:sts=4:sw=4:et:fdm=marker:cms=\ #\ %s
#
# dwww-build-menu - create Debian Documentation Menu
# Copyright (C) 2003 Robert Luberda <robert@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# $Id: dwww-build-menu 547 2011-01-15 15:29:55Z robert $
#
use strict;
use warnings;
use Debian::Dwww::Utils;
use Debian::Dwww::DocBase;
use Debian::Dwww::Common;
use Debian::Dwww::Initialize;
use File::Path qw/mkpath rmtree/;
use File::Temp 'tempdir';
my $dwwwconf = &DwwwInitialize("/etc/dwww/dwww.conf");
my $out_dir = $dwwwconf->{'DWWW_DOCROOTDIR'} . "/dwww/menu";
my $out_dir_tmp = tempdir('dwww-build-menu.XXXXXX',
DIR => $dwwwconf->{'DWWW_TMPDIR'},
CLEANUP => 1) . "/menu.temp.$$";
my $title = $dwwwconf->{'DWWW_TITLE'};
my $dwww_regdocs_cache = $dwwwconf->{'DWWW_REGDOCS_DB'};
my $dwww_db2pkg_cache = $dwwwconf->{'DWWW_DOCBASE2PKG_DB'};
$dwww_regdocs_cache = undef if "$dwww_regdocs_cache" eq "";
$dwww_db2pkg_cache = undef if "$dwww_db2pkg_cache" eq "" and not ( -r "$dwww_db2pkg_cache" );
undef %{$dwwwconf};
my $templates_dir = "/usr/share/dwww/templates";
my $template_single_start = "$templates_dir/menu.single.start";
my $template_single_end = "$templates_dir/menu.end";
my $template_all_start = "$templates_dir/menu.all.start";
my $template_all_end = "$templates_dir/menu.end";
my $template_error_start = "$templates_dir/menu.errors.start";
my $template_error_end = "$templates_dir/menu.errors.end";
my $template_nosections = "$templates_dir/menu.nosections";
my $single_index = 'one';
my $all_index = 'all';
my $doc_base_fmt = 'doc-base'; # special format indicating that dwww
# should generate index of files containing
# the registered files
my @known_formats=(
'html',
'info',
'text',
'rtf',
'debiandoc-sgml',
'docbook-xml',
'dvi',
'latex',
'linuxdoc-sgml',
'pdf',
'postscript',
'ps',
'sgml',
'tar',
'texinfo',
'dwww-url' # internal dwww's format
);
my %sections = ();
my %section_names = (); # real names of sections
my %node_section = (); # is node section ?
my %map_section = (); # for section without docs, the nearest section with docs
my %doc_list = (); # list of documents
my %files = (); # list of files
my %dbfile2pkg = (); # maps doc-base file names to package names (filled by ReadDocb2PkgCache)
my $opt_verbose = 0;
my $opt_debug = 0;
my $last_err_file = "";
umask 022;
&mkpath($out_dir_tmp) or die "Can't create directory $out_dir_tmp: $!\n";
open ERRFILE , ">$out_dir_tmp/errors.html" or &CleanAndDie("Can't create file errros.html: $!");
print ERRFILE &TemplateFile($template_error_start, { 'TITLE' => &HTMLEncode($title) });
$ErrorProc = \&ErrorHandle;
&ParseDocDir("/var/lib/doc-base/documents");
&FindMapSections;
&ReadDocb2PkgCache;
&BuildMenus;
&CreateIndex;
&rmtree($out_dir);
&RenameDir($out_dir_tmp, $out_dir);
print ERRFILE &TemplateFile($template_error_end, {});
close ERRFILE;
exit (0);
#########################################################################
#
# Local functions
#
sub CleanAndDie { # {{{
my $msg = shift;
die "$msg";
exit(1);
} # }}}
## ErrorHandle($file, $message)
sub ErrorHandle { # {{{
my $file = shift;
my $msg = shift;
print ERRFILE "<dt><br></dt>\n" unless $last_err_file eq $file;
print ERRFILE "<dt><a href=\"file://" . &URLEncode($file) . "\">"
. &HTMLEncode($file) . "</a></dt>\n";
print ERRFILE "<dd>" . &HTMLEncode($msg) . "</dd>\n";
$last_err_file = $file;
} # }}}
sub ParseDocDir { # {{{
my $dir = shift;
if (not (opendir DOCBASEDIR, $dir)) {
print STDERR "Can't open directory $dir: $!\n" if $opt_verbose;
return;
}
while (my $f = readdir(DOCBASEDIR))
{
next if $f =~ /^\./;
next if -d $f;
if (defined (my $entry = &ParseDocBaseFile("$dir/$f"))) {
&AddNewEntry("$dir/$f", $entry);
undef %{$entry};
}
}
} # }}}
sub ReadDocb2PkgCache { # {{{
return unless defined $dwww_db2pkg_cache;
return unless open CACHE, "<$dwww_db2pkg_cache";
while (<CACHE>) {
chomp();
my ($docb_file, $pkg) = split (/\001/, $_, 2);
$dbfile2pkg{$docb_file} = $pkg;
}
close CACHE;
} # }}}
# Adds %entry to @documents
sub AddNewEntry() { # {{{
my $file = shift;
my $entry = shift;
my $doc_entry = { };
my $known = 0;
my $sec = undef;
if (exists $entry->{'dwww-section'}) {
&DwwwSection2Section($entry);
}
if (defined $entry->{'document'}) {
$doc_entry->{'document'} = $entry->{'document'};
}
if ((not defined $entry->{'title'}) or $entry->{'title'} =~ m/^\s*$/) {
&ErrorHandle($file,"No (or empty) title!");
$doc_entry->{'title'} = "(No title)";
} else {
$doc_entry->{'title'} = $entry->{'title'};
}
if ((not defined $entry->{'abstract'}) or $entry->{'abstract'} =~ m/^\s*$/) {
&ErrorHandle($file, "Warning: no (or empty) abstract!");
$doc_entry->{'abstract'} = "";
} else {
$doc_entry->{'abstract'} = $entry->{'abstract'}
}
if ((not defined ($sec = $entry->{'section'})) or $sec =~ m/^\s*$/)
{
&ErrorHandle($file, "No (or empty) section!");
$sec = 'unknown';
}
foreach my $f (@known_formats) {
my $w;
if (defined ( $w = $entry->{'formats'}->{$f}->{'index'})
or defined ($w = $entry->{'formats'}->{$f}->{'files'})) {
if (exists $files{$w})
{
&ErrorHandle($file, "File already known: \"$w\"");
return;
}
if ( $f eq 'dwww-url' or -r $w) {
$files{$w} = '1';
$doc_entry->{'frm_name_' . $known} = $f;
$doc_entry->{'frm_file_' . $known} = $w;
$known++;
} elsif (glob($w)) {
$files{$w} = '1';
$doc_entry->{'frm_name_' . $known} = $doc_base_fmt;
$doc_entry->{'frm_file_' . $known} = $f . '/' . $entry->{'document'};
$known++;
} else {
&ErrorHandle($file, "Can't read \"$w\": $!");
}
}
}
if ($known == 0 ) {
&ErrorHandle($file, "No known formats!");
return;
}
$sec = &AddCanonSection($sec);
$doc_entry->{'section'} = $sec;
$doc_entry->{'frm_count'} = $known;
push(@{$doc_list{$sec}}, $doc_entry);
} # }}}
sub sort_documents { # {{{
my $res;
$res = $a->{'section'} cmp $b->{'section'};
$res = lc $a->{'title'} cmp lc $b->{'title'} if $res == 0;
return $res;
} # }}}
sub AddCanonSection ($) { # {{{
my $arg = shift;
my $sec = '';
my $nsec = '';
my $prev = '';
foreach my $s (split /\/+/, $arg) {
$sec .= '/' unless $sec eq '';
$nsec .= '/' unless $nsec eq '';
$sec .= lc $s;
$nsec .= ucfirst $s;
$sections{$sec} = 0 unless exists $sections{$sec};
$section_names{$sec} = $nsec;
$node_section{$prev} = 1 unless $prev eq '';
$prev = $sec;
}
$sections{$sec}++;
return $sec;
} # }}}
sub FindMapSections() { # {{{
my @ss = sort keys %sections;
undef %map_section;
for (my $i=$#ss; $i >= 0; $i--) {
my $sec = $ss[$i];
if ($sections{$ss[$i]} == 0)
{
for (my $j = $i; $j <= $#ss; $j++){
if( exists $map_section{$ss[$j]})
{
$map_section{$sec} = $map_section{$ss[$j]};
last;
}
}
}
else
{
$map_section{$sec} = $sec;
}
}
} # }}}
sub SectionToFileName() { # {{{
$_ = shift;
my $type = shift;
$_ = $map_section{$_};
s/\//_/g;
return "s$_.html" if ($type eq $single_index);
return "$_";
} # }}}
sub SectionsToHTML() { # {{{
my $cur_sec = shift;
my $type = shift;
my $depth = 0;
my $pr_depth = -1;
my $res = '';
my $cur_depth = ($cur_sec =~ s;/;/;g) + 0;
my %override_node_section = ();
my $sec = "";
foreach my $s (split /\/+/, $cur_sec) {
$sec .= $s;
$override_node_section{$sec} = 1;
$sec .= '/';
}
foreach my $s (sort keys %sections) {
$depth = ($s =~ s;/;/;g) + 0;
my $ps = $s;
$ps =~ s/[^\/]+$//;
next unless ($type eq $all_index or $ps eq '' or index($cur_sec .'/', $ps) == 0);
&CleanAndDie('Internal error') if ($depth > $pr_depth + 1);
$res .= "<ul>\n" if ($depth > $pr_depth);
while ($pr_depth > $depth) {
$res .= "</ul>\n";
$pr_depth--;
}
$pr_depth = $depth;
my $ns = $section_names{$s};
$ns =~ s|^.*/||g;
$ns .= " ..." if ($type ne "all"
and exists $node_section{$s}
and not exists $override_node_section{$s});
if ($s eq $cur_sec) {
$res .= "<li><strong>$ns</strong>\n";
} elsif ($type eq $single_index and ($sections{$s} > 0 or index($cur_sec, $s .'/') != 0 )){
$res .= "<li><a href=\"" . &SectionToFileName($s, $type) . "\">$ns</a>\n";
} elsif ($sections{$s} > 0 and $type eq $all_index) {
my $slink = &SectionToFileName($s, $type);
$res .= "<li><a href=\"#" . $slink . "\" name=\"m" . $slink . "\">$ns</a>\n";
} else {
$res .= "<li>$ns\n";
}
}
while ($pr_depth > -1) {
$res .= "</ul>\n";
$pr_depth--;
}
return $res;
} # }}}
sub DocumentToHTML($) { # {{{
my $doc = shift; # reference to hash
my $res = '';
my $pkg = undef;
if (exists $doc->{'document'}) {
# TODO: document could be generated from more than one control file
# we really should support this
$pkg = $dbfile2pkg{ $doc->{'document'} };
}
$res .= "<dt>";
$res .= "<a href=\"" . &GetURL($doc->{'frm_name_0'}, $doc->{'frm_file_0'}) . "\"";
$res .= " name=\"" . &HTMLEncode($doc->{'document'}) . "\""
if defined ($doc->{'document'});
$res .= ">" . &HTMLEncode($doc->{'title'}) . "</a>\n";
if (defined $pkg) {
$res .= " <small><em>(package: <a href=\"" . &GetURL('pkgsearch', $pkg);
$res .= "\">" . &HTMLEncode($pkg) . "</a>)";
$res .= "</em></small>\n";
}
$res .= "</dt>\n";
$res .= "<dd>" . &HTMLEncodeAbstract($doc->{'abstract'});
if ($doc->{'frm_count'} > 1) {
$res .= "\n<br><b>Formats:</b> ";
for (my $i=0; $i < $doc->{'frm_count'}; $i++) {
my $fmt = $doc->{"frm_name_$i"};
my $loc = $doc->{"frm_file_$i"};
$res .= "[<a href=\"" . &GetURL($fmt, $loc);
($fmt = $loc) =~ s/\/.*$// if $fmt eq $doc_base_fmt;
$res .= "\">" . $fmt . "</a>] ";
}
}
$res .= "</dd>\n";
$res .= "<dt><br></dt>\n";
return $res;
} # }}}
sub BuildMenus { # {{{
my $res = '';
my $last_sec = '';
my $need_close = undef;
open ALL_INDEX, ">$out_dir_tmp/all.html" or &CleanAndDie("Can't create all.html file: $!");
print ALL_INDEX &TemplateFile($template_all_start,
{ 'TITLE' => &HTMLEncode($title),
'SECTIONLIST' => &SectionsToHTML('', 'all') });
if (defined $dwww_regdocs_cache) {
open CACHE, ">$dwww_regdocs_cache.tmp.$$" or $dwww_regdocs_cache = undef;
}
foreach my $sec (sort keys %doc_list) {
my $fname = &SectionToFileName($sec, $single_index);
open SINGLE_INDEX, ">$out_dir_tmp/" . $fname
or &CleanAndDie("Can't create $fname: $!");
print SINGLE_INDEX &TemplateFile($template_single_start,
{ 'TITLE' => &HTMLEncode($title),
'SECTION' => &HTMLEncode($section_names{$sec}),
'SECTIONLIST' => &SectionsToHTML($sec, $single_index)});
print SINGLE_INDEX "<dl>\n";
my $alllink = &SectionToFileName($sec, $all_index);
print ALL_INDEX "<h2 class=\"c\"><a name=\"" . $alllink . "\" href=\"#m" . $alllink .
"\"><strong> Section: ". &HTMLEncode($section_names{$sec}) . "</strong></a></h2>\n<dl>";
print SINGLE_INDEX "\n<!-- Section: " . &HTMLEncode($section_names{$sec}) . " -->\n";
foreach my $doc (sort sort_documents @{$doc_list{$sec}}) {
$res = &DocumentToHTML($doc);
print SINGLE_INDEX "<!-- begin entry -->\n";
print SINGLE_INDEX $res;
print SINGLE_INDEX "<!-- end entry -->\n";
print ALL_INDEX $res;
if (defined $dwww_regdocs_cache and exists $doc->{'document'}) {
print CACHE join( "\01", ( $doc->{'document'},
$section_names{$sec},
$fname . '#' . &HTMLEncode($doc->{'document'}),
&GetURL($doc->{'frm_name_0'}, $doc->{'frm_file_0'}),
$doc->{'title'}
) ) . "\n";
}
}
print SINGLE_INDEX "</dl>\n";
print ALL_INDEX "</dl>\n";
print SINGLE_INDEX &TemplateFile($template_single_end, {});
close SINGLE_INDEX;
}
print ALL_INDEX &TemplateFile($template_all_end, {});
close ALL_INDEX;
if (defined $dwww_regdocs_cache) {
close CACHE;
rename("$dwww_regdocs_cache.tmp.$$", "$dwww_regdocs_cache");
chmod (0644, "$dwww_regdocs_cache");
return 0;
}
} # }}}
sub CreateIndex() { # {{{
if (%sections) {
link("$out_dir_tmp/" . &SectionToFileName((sort keys %sections)[0], $single_index),
"$out_dir_tmp/index.html")
or print STDERR "Can't create index.html link: $!\n";
} else {
open INDEX, ">$out_dir_tmp/all.html" or &CleanAndDie("Can't create all.html file: $!");
print INDEX &TemplateFile($template_nosections,
{ 'TITLE' => &HTMLEncode($title) } );
close INDEX;
link("$out_dir_tmp/all.html", "$out_dir_tmp/index.html")
or print STDERR "Can't create index.html link: $!\n";
}
} # }}}
|