File: genmenu.pl

package info (click to toggle)
remstats 1.00a4-8woody1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,576 kB
  • ctags: 1,020
  • sloc: perl: 11,706; ansic: 2,776; makefile: 944; sh: 869
file content (137 lines) | stat: -rwxr-xr-x 3,225 bytes parent folder | download
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
#!@@PERL@@ @@PERLOPTS@@

# Copyright 1999, 2000, 2001 (c) Thomas Erskine <@@AUTHOR@@>
# See the COPYRIGHT file with the distribution.

# genmenu - make the menu-bar for the documentation
# $Id: genmenu.pl,v 1.5 2001/08/28 15:22:24 remstats Exp $

# - - -   Configuration   - - -

# What is this program called, for error-messages and file-names
$main::prog = 'genmenu';
# What style to apply to the current page?
$main::style = 'background-color: 0xffffff';
# What does an indent look like?
$main::indenter = '&nbsp;&nbsp;&nbsp;&nbsp;';

# - - -   Version History   - - -

(undef, $main::version) = split(' ', '$Revision: 1.5 $');

# - - -   Setup   - - -

use Getopt::Std;

# Parse the command-line
# STRICT use vars qw( $opt_d $opt_h );
getopts('d:h');

if (defined $main::opt_h) { &usage; } # no return
if (defined $main::opt_d) { $main::debug = $main::opt_d; } else { $main::debug = 0; }

if ($#ARGV != 1) { &usage; } # no return
my $thispage = shift @ARGV;
my $menufile = shift @ARGV;
open (MENU, "<$menufile") or &abort("can't open $menufile: $!");

# - - -   Mainline   - - -

# Build the tree
my (%up, %pagename, %indent, %path, @pages, @path, $lastindent, $prev,
	$indent, $page, $pagename, %expand);
%up = %pagename = %indent = %path = @pages = ();
$up{'index'} = undef;
$up = 'index';
@path = ('index');
$lastindent = 0;
$prev = 'index';
while (<MENU>) {
	chomp;
	next if (/^#/ or /^\s*$/);

	if (/^(\t*)(\S+)(\s+(\S.*))?/) {
		$indent = length($1);
		$page = $2;
		$pagename = $4;
		unless (defined $pagename) { $pagename = $page; }
		$pagename =~ s/ /&nbsp;/g;

		push @pages, $page;
		$pagename{$page} = $pagename;
		$indent{$page} = $indent;
		$path{$page} = [@path, $page];

		if ($indent > $lastindent) {
			push @path, $prev;
			$up = $prev;
		}
		elsif ($indent < $lastindent) {
			$#path = $indent;
			$up = $path[$#path];
		}
		$up{$page} = $up;
		&debug("page=$page, indent=$indent, up=$up, prev=$prev\n\t".join(', ',@path)) if ($main::debug);;

		$lastindent = $indent;
		$prev = $page;
	}
	else {
		&error("bad line: $_");
	}
}
close (MENU);

# Where is this page?
%expand = ();
my $it = $thispage;
while (defined $it) {
	$expand{$it} = 1;
	last if (defined $up{$it} and $it eq $up{$it});
	$it = $up{$it};
}
	
foreach $page (@pages) {
	$pagename = $pagename{$page};
	$indent = $indent{$page};
	@path = @{$path{$page}};
	$up = $up{$page};

	next unless ($expand{$up});

	$indent_text = $indenter x $indent;
	if ($page eq $thispage) {
		print "&nbsp;&nbsp;$indent_text<A HREF=\"$page.html\"><SPAN STYLE=\"$style\">$pagename</SPAN></A><BR>\n";
	}
	else {
		print "&nbsp;&nbsp;$indent_text<A HREF=\"$page.html\">$pagename</A><BR>\n";
	}

}

exit 0;

#----------------------------------------------------------------- usage ---
sub usage {
	print STDERR <<"EOD_USAGE";
$main::prog version $main::version
usage: $0 [options] pagename menufile
where options are:
	-d	enable debugging output
	-h	show this help
EOD_USAGE
	exit 0;
}

#----------------------------------------------------------------- debug ---
sub debug {
	my ($msg) = @_;

	if ($debug) { print STDERR "DEBUG: $msg\n"; }
0;
}

#----------------------------------------------- keep_strict_happy ---
sub keep_strict_happy {
	$main::opt_h = 0;
}