File: mkcontents.pl

package info (click to toggle)
fte 0.50.0-1.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,536 kB
  • ctags: 6,167
  • sloc: cpp: 45,854; ansic: 2,586; perl: 808; makefile: 125; sh: 104
file content (183 lines) | stat: -rwxr-xr-x 4,099 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -w
# mkcontents

#use strict qw(refs subs);

sub openlevel;
sub closelevel;
sub newindex;
sub doneindex;
sub html;
sub tag;
sub out;

$output_level = 0;
$output = 0;

# read input files
open(INDEX, "<INDEX") || die "open INDEX: $!";
while (defined($filename = <INDEX>)) {
    chomp $filename;
    open(FILE, "<$filename") || die "open $filename: $!";
    {
        local $/ = undef;
        $filetext = <FILE>;
        print STDERR "$filename:\n";
        $output = 0;
        html($filetext);
    }
    close(FILE);
}
close(INDEX);
closelevel(0);

sub html {
    my $text = $_[0];
    my $cpos = 0;

    TAG: while ($opos = $cpos, ($cpos = index($text, "<", $cpos) + 1) != 0) {
        pos($text) = $cpos;  # match regexp there

        out(substr($text, $opos, pos($text) - $opos - 1));
        # output text

        $TAG = undef;
        
        if ($text =~ /\G!(.*?)(--.*?--\s*)*(.*?)>/sgo) { # comment
            $cpos = pos $text;
            #&comment($2);
            next TAG;
        }
        
        pos ($text) = $cpos;
        if ($text =~ /\G\s*([\/]?[A-Za-z0-9]*)/go) {
            $cpos = pos($text);
            $TAG = uc $1;
            
            #print "<|", $TAG, "\n";
        }
        
        undef %ARGS;
        
        ARG: while (1) {
            pos($text) = $cpos;
            
            if ($text =~ /\G\s*/go) { $cpos = pos ($text); } # skip whitespace
            
            last ARG unless $text =~ /\G([A-Za-z0-9]+)\s*/go; # param name
            $cpos = pos $text;
            
            $pname = uc $1;
            if ($text =~ /\G=\s*/go) {
                $cpos = pos $text;
                
                if ($text =~ /\G"([^"]*)"\s*/go) {
                    $cpos = pos $text;
                    #print "+|$pname=\"$1\"\n";
                    $ARGS{$pname} = $1;
                    next ARG;
                };
                pos($text) = $cpos;
                if ($text =~ /\G'([^']*)'\s*/go) {
                    $cpos = pos $text;
                    #print "+|$pname='$1'\n";
                    $ARGS{$pname} = $1;
                    next ARG;
                };
                pos($text) = $cpos;
                if ($text =~ /\G([^ <>"']+)\s*/go) {
                    $cpos = pos $text;
                    #print "+|$pname=$1\n";
                    $ARGS{$pname} = $1;
                    next ARG;
                };
                $ARGS{$pname} = "";
                die "no value for tag";
            }
            #print "+|$pname\n";
        }
        pos($text) = $cpos;
        ($cpos = index($text, ">", $cpos) + 1) != 0 or die "tag without end";
        
        tag($TAG, \%ARGS);
    }
    out(substr($text, $opos, length($text) - $opos));
}

sub closelevel {
    my $level = $_[0];
    
    while ($output_level > $level) {
        print "\n</DL>\n";
        $output_level--;
    }
}

sub openlevel {
    my $level = $_[0];

    while ($output_level < $level) {
        print "<DL>\n";
        $output_level++;
    }
}

sub newindex {
    my $level = $_[0];

    closelevel($level);
    openlevel($level);
    print "\n<LI>";
    $output = 1;
}

sub doneindex {
    my $level = $_[0];

    warn "$filename:level:$level mismatch" if ($output_level != $level);
    print "</LI>\n";
    $output = 0;
}

sub tag {
    my $TAG = $_[0];
    my %ARGS = %{$_[1]};

    $TAG eq 'TITLE' && do {
        newindex(1);
        print "<A HREF=\"$filename\" TARGET=\"main\">\n";
    }
    or $TAG eq "/TITLE" && do {
        print "</A>";
        doneindex(1);
    }
    or $TAG =~ /^H([1-6])$/o && do {
        newindex(1 + $1);
    }
    or $TAG =~ /^\/H([1-6])$/o && do {
        doneindex(1 + $1);
    }
    or $TAG eq 'A' && do {
        if (defined $ARGS{"NAME"}) {
            $aname = $filename . '#' . $ARGS{"NAME"};
            print "<A HREF=\"$aname\" TARGET=\"main\">" if $output;
        } 
    }
    or $TAG eq '/A' && do {
        print "</A>" if $output;
    }
}

sub out {
    my $lin = $_[0];
    my $first = 1;
    my $i;

    print $lin if $output;
}

sub comment {
}

sub badtag {
}