Skip to content

Commit

Permalink
replace original toc.awk
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre committed Sep 14, 2024
1 parent b333d60 commit 8d39d65
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 76 deletions.
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
toc.html: template.html toc.awk
./toc2.awk $< > $@
./toc.awk $< > $@

index.html: template.html toc.html
m4 $< > $@
65 changes: 20 additions & 45 deletions docs/toc.awk
Original file line number Diff line number Diff line change
@@ -1,55 +1,30 @@
#!/usr/bin/awk -f

BEGIN { print "<ul>" }
END { print "</ul>" }

/head[2345]\(/ {
printf "\t"
function make_id(s, ret) {
ret = tolower(s)
gsub(/ +/, "-", ret)
return ret
}

/head2\(/ {
last_level = 2
if (sublist) {
print "</ul>" # close previous sublist
sublist = 0
}
match($0, /head2\(([^)]+)\)/, arr)
printf "<li><a href=\"#%s\">%s</a></li>\n", make_id(arr[1]), arr[1]
BEGIN {
HEAD = "head([2345])\\(([^)]+)\\)"
}
END { print "</ul>" }

/head3\(/ {
last_level = 3
if (!sublist) {
print "<ul>"
$0 ~ HEAD {
match($0, HEAD, arr)
cur = arr[1] # current heading level
while (p > 0 && cur < buf[p-1]) {
#printf("closing list with cur = %d, last = %d\n", cur, buf[p-1])
print "</ul>"
p--
}
sublist = 1
printf "\t"
match($0, /head3\(([^)]+)\)/, arr)
printf "<li><a href=\"#%s\">%s</a></li>\n", make_id(arr[1]), arr[1]
}

/head4\(/ {
last_level = 4
print "<ul>" # always add a list level for this, assume it's alone
printf "\t\t"
match($0, /head4\(([^)]+)\)/, arr)
printf "<li><a href=\"#%s\">%s</a></li>\n", make_id(arr[1]), arr[1]
print "</ul>"
}

/head5\(/ {
last_level = 5
if (last_level != 4) {
print "<ul>" # always add a list level for this, assume it's alone
if (buf[p-1] < cur) {
#printf "opening new list with cur = %d, last = %d\n", cur, buf[p-1]
print "<ul>"
}
printf "\t\t"
match($0, /head5\(([^)]+)\)/, arr)
printf "<li><a href=\"#%s\">%s</a></li>\n", make_id(arr[1]), arr[1]
print "</ul>"
}
if (buf[p-1] != arr[1])
buf[p++] = arr[1] # heading level stack

function make_id(s, ret) {
ret = tolower(s)
gsub(/ +/, "-", ret)
return ret
printf "<li><a href=\"#%s\">%s</a></li>\n", make_id(arr[2]), arr[2]
}
30 changes: 0 additions & 30 deletions docs/toc2.awk

This file was deleted.

0 comments on commit 8d39d65

Please sign in to comment.