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
|
#!/bin/awk -f
# This script relies ON START-TOC and END-TOC being used to delimit portions of the
# input that should be scanned for sectioin headers, command definitions, and
# inclusions.
#
# Things it knows about:
# * Asciidoc section headers
# * Asciidoc file inclusions (each one becomes a topic)
# * Asciidoc hanging-definition syntax (each one becomes a topic)
# * START-TOC/END-TOC begfins or ends a span in which topic should be s anned for
# * TOPIC marks a following topic that is not a reposurgeon command.
function flush_chapter_toc() {
if (intoc) {
if (counters[3] == "") { # put a blank line after every chapter
print ""
}
if (maxcommand == 1) { # if there are no commands, the generated command list will be nil
temp = "nil"
} else { # otherwise, it'll be a comma-separated list of strings
temp = "[]string{"
for (i = 1; i < maxcommand; i++) {
if (i > 1) {
temp = temp ", "
}
temp = temp "\"" commands[i] "\""
}
temp = temp "}"
}
if (counters[3] == "" || (counters[3] != "" && maxcommand > 1)) { # only print chapter headings or sections with commands
print "\thelp{\"" indentation counters[depth] "." title "\", " temp "},"
}
}
}
BEGIN {
intoc = 0
print "package main"
print ""
print "type help struct {"
print " title string"
print " commands []string"
print "}"
print ""
print "var _Helps = []help{"
}
/^=+/ {
flush_chapter_toc()
maxcommand = 1
delete commands
depth = length($1)
title = ""
for (i = 2; i <= NF; i++) {
title = title " " $i
}
delete oldcounters
for (i in counters) {
oldcounters[i] = counters[i]
}
delete counters
for (i = 1; i < depth; i++) {
counters[i] = oldcounters[i]
}
counters[depth] = oldcounters[depth] + 1
indentation = ""
for (i = 2; i < depth; i++) {
indentation = indentation " "
}
#topicsuffix = ""
}
/TOPIC/ {
topicsuffix = "*"
}
/[^:]::$/ {
name = ($1 ~ /SELECTION/) ? $2 : $1
sub(/[^a-z]+/, "", name)
commands[maxcommand] = name topicsuffix
maxcommand += 1
topicsuffix = ""
}
/include::/ {
split($1, parts, "/")
split(parts[2], parts, ".")
commands[maxcommand] = parts[1] topicsuffix
maxcommand += 1
topicsuffix = ""
}
/START-TOC/ {
maxcommand = 1
intoc = 1
topicsuffix = ""
}
/END-TOC/ {
flush_chapter_toc()
intoc = 0
topicsuffix = ""
}
END {
print " help{\"Starred topics are not commands.\", nil},"
print "}"
}
|