-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-toc
executable file
·78 lines (65 loc) · 1.71 KB
/
make-toc
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
#!/usr/bin/perl
# make-toc file
# Create a simple table-of-contents for a module
# See make-module for the file format.
# +-------+----------------------------------------------------------
# | Setup |
# +-------+
my $file = $ARGV[0];
# +------+-----------------------------------------------------------
# | Main |
# +------+
open my $PORT, "< $file" or die "Could not open file '$file' because $!";
my $title = <$PORT>;
my $curlevel = 0;
while (my $line = <$PORT>) {
my $newline = $line;
chomp($line);
$line =~ s/^( *)//;
my $spaces = " $1";
my $newlevel = length($spaces)/2;
while ($curlevel < $newlevel) {
for (my $i = 0; $i < $curlevel; $i++) { print " "; }
print "<ul>\n";
++$curlevel;
} # while
while ($curlevel > $newlevel) {
--$curlevel;
for (my $i = 0; $i < $curlevel; $i++) { print " "; }
print "</ul>\n";
}
# Content
print "$spaces<li>";
if ($line =~ m/^{/) {
$line =~ s/^{//;
$line =~ s/}$//;
my ($type,$title,$content_id,$due,$other) = split(/;/,$line);
if ($content_id eq "_") {
print "$title";
}
elsif ($type eq "Assignment") {
print "<a href='../assignments/$content_id'>$title</a>";
} # assignment
elsif ($type eq "Discussion") {
print "<a href='../discussion_topics/$content_id'>$title</a>";
} # discussion
elsif ($type eq "Page") {
print "<a href='../pages/$content_id'>$title</a>";
} # page
else {
print STDERR "Entry $title has unknown type: $type\n";
exit 1;
}
} # if content
else {
print "$line";
}
print "<\/li>\n";
}
while ($curlevel > 0) {
--$curlevel;
for (my $i = 0; $i < $curlevel; $i++) { print " "; }
print "</ul>\n";
}
close $PORT;
exit 0;