#!/bin/sh
# Copyright 1994 by Norman Ramsey. All rights reserved.
# See file COPYRIGHT for more information.
# Do not try to understand this file! Look at lib/tohtml.nw in the noweb source!
delay=0 raw=0 localindex=0 noindex=0
for i do
case $i in
-delay) delay=1 ;;
-raw) raw=1 ;;
-localindex) if [ $noindex -eq 0 ]; then localindex=1; fi;;
-noindex) localindex=0; noindex=1 ;;
esac
done
nawk 'function writechunk(label, ref, tag, name, suffix) {
printf "%s",
linklabelto(label, ref, sgmlwrap(tag, "<" convquotes(name) ">" suffix))
}
function linklabelto(label, ref, contents, s) {
s = label != "" || ref != "" ? "" : "")
s = s contents
s = s (label != "" || ref != "" ? "" : "")
return s
}
function linkto(ref, contents) {
return linklabelto("", ref, contents)
}
function linklabel(label, contents) {
return linklabelto(label, "", contents)
}
function sgmlwrap(tag, s) {
return "<" tag ">" s "" tag ">"
}
function image(s) {
gsub(/"/, "\\\"", s)
return "\"" s "\""
}
function escapeSpecials (l) {
gsub(/&/, "\\&", l)
gsub(/, "\\<", l)
gsub(/>/, "\\>", l)
gsub(/"/, "\\"", l)
return l
}
function convquotes(s, r, i, line) {
r = ""
while (i = index(s, "[[")) {
r = r substr(s, 1, i-1) ""
s = substr(s, i+2)
if (i = match(s, "\\]\\]+")) {
line = substr(s, 1, i-1+RLENGTH-2)
# line = escapeSpecials(line) # destroys internal markup --- do not call
r = r line "
"
s = substr(s, i+RLENGTH)
} else {
r = r s ""
s = ""
}
}
return r s
}
BEGIN { defns[0] = 0
defns_above[0] = 0
useitemstab[0] = 0 }
!doneraw { # do not do in BEGIN because not all awks assign variables yet
if (raw) { braw = "\\begin{rawhtml}"; eraw = "\\end{rawhtml}" }
else braw = eraw = ""
doneraw = 1
}
/^@begin code / { code = 1; printf "%s
", braw; ecode = "" } /^@end code / { code = 0; previscode = 1; useitemscount = split(useitemstab[thischunk], a) if (pendingprev != "" || pendingnext != "" || useitemscount > 0) { if (ecode == "") { printf "
" ecode = "" } useprefix = "Used " for (j = 1; j <= useitemscount; j++) { if (defns_above[a[j]] > 0) usedir = "above" else usedir = "below" printf "%s%s", useprefix, linkto(a[j], usedir (useitemscount > 1 ? " (" j ")" : "")) useprefix = ", " } if (useitemscount > 0 && (pendingprev != "" || pendingnext != "")) printf "; " p = useitemscount > 0 ? "previous" : "Previous" n = useitemscount > 0 ? "next" : "Next" if (pendingprev != "") if (pendingnext != "") printf "%s and %s definitions", linkto(pendingprev, p), linkto(pendingnext, "next") else printf "%s definition", linkto(pendingprev, p) else if (pendingnext != "") printf "%s definition", linkto(pendingnext, n) pendingprev = pendingnext = "" useitems = "" print ".
" } printf "%s%s", ecode, eraw } /^@begin docs / { if (previscode) printf "%s", (raw ? "\\par" : "
")
previscode = text = 0
}
/^@end docs / { if (lastxreflabel != "")
printf "%s%s%s\n", braw, linklabel(lastxreflabel, "*"), eraw
lastxreflabel = ""
}
/^@text / { line = substr($0, 7); text += length(line)
if (code) {
if (lastindexref != "" && line ~ /[^ \t]/) {
printf "%s", linkto(lastindexref, line)
lastindexref = ""
} else {
printf "%s", escapeSpecials(line)
}
} else if (quoting) {
if (line ~ /[^ \t]/) {
printf "%s", linklabelto(lastxreflabel, lastindexref,
escapeSpecials(line))
lastindexref = lastxreflabel = ""
} else {
printf "%s", escapeSpecials(line)
}
} else {
if (lastxreflabel != "" && line ~ /[^ \t]/) {
match(line, /^[ \t]*/)
blanks = substr(line, RSTART, RLENGTH)
line = substr(line, RSTART+RLENGTH)
if (line ~ /^[{}\\<&]/) {
char = "*"
} else {
char = substr(line, 1, 1)
line = substr(line, 2)
}
printf "%s%s%s%s%s", braw, blanks, linklabel(lastxreflabel, char), eraw, line
if (lastxreflabel != "") defns_above[lastxreflabel] = 1
lastxreflabel = ""
} else {
printf "%s", line
}
}
}
/^@nl$/ { print "" }
/^@defn / { thischunk = name = substr($0, 7)
if (lastxreflabel != "") defns_above[lastxreflabel] = 1
writechunk(lastxreflabel, lastxrefref, "dfn", name, defns[name] "=")
lastxreflabel = lastxrefref = ""
defns[name] = "+"
}
/^@use / { writechunk(lastxreflabel, lastxrefref, "i", substr($0, 6), "") }
/^@quote$/ { quoting = 1 ; printf "%s", braw }
/^@endquote$/ { quoting = 0 ; printf "
%s", eraw }
/^@file / { filename = substr($0, 7); lastxreflabel = lastxrefref = "" }
/^@literal / { printf "%s", substr($0, 10) }
/^@header html / { printf "
" ecode = "" } printf "This code is written to a file (or else not used).
" } /^@xref (begindefs|defitem|enddefs)/ { } /^@xref beginchunks$/ { printf "%s
" ecode = "" }; printf "Defines"; comma = " " } } /^@index isused / { } /^@index defitem / { if (localindex) { arg = substr($0, 16) printf "%s%s", comma, linkto("NWI-" escapeSpecials(arg), sgmlwrap("code", escapeSpecials(arg))) comma = ", " } } /^@index enddefs/ { if (localindex) { printf " (links are to index).
\n" } } /^@index (beginuses|isdefined|useitem|enduses)/ { } # use local links END { print "" }' \ delay=$delay raw=$raw localindex=$localindex noindex=$noindex