summaryrefslogtreecommitdiff
path: root/web/noweb/src/awk/noindex.nw
blob: ba57f6e234755e528836eeb777505daa7ac22bf2 (plain)
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
184
185
186
187
This program is similar to [[makeindex]] in that it grovels through [[.aux]] 
files looking for index information, which it writes to a [[.nwi]] file.
It's used when [[noweave -indexfrom]] is used on many files separately;
it combines the separate indices into a single, correctly sorted index.
That index file is read by [[\nowebindex*]].
<<noindex>>=
#!/bin/sh

if [ $# -ne 1 ]; then
  echo "Usage: `basename $0` file[.tex]" 1>&2
  exit 1
fi

nawk 'BEGIN { infile="'"$1"'"
              <<main code>> 
              exit
            }
<<functions>>' 1>&2
<<main code>>=
if (infile ~ /\.tex$/) {
    infile = substr(infile, 1, length(infile)-4) ".aux"
} else if (infile !~ /\.aux$/) {
    infile = infile ".aux"
}
idx[0] = ""
delete idx[0]
gobble(infile)
alphasort(idx)
outname = substr(infile, 1, length(infile)-4) ".nwi"
last = ""
for (i = 0; i < count; i++) {
  out = stripcount(sorted[i])
  if (last != out) {
    print out > outname
    last = out
#    <show sort key [[i]]>
  }
}
<<show sort key [[i]]>>=
key = sortkeys[i]
sub(/^\n+/, "", key)
sub(/\n.*$/, "", key)
print "% " key > outname
<<functions>>=
function gobble(name, infile, rc, tag) {
  for (rc = (getline line < name); rc > 0; rc = (getline line < name)) {
    if (line ~ /^\\@input{[^}]*}$/)
        gobble(substr(line, 9, length(line)-9))
    else if (line ~ /^\\nwixadds{/) {
        count++
        tag = "000000" count
        tag = substr(tag, length(tag)-6+1)
        idx[count] = tag " " substr(line, 11)
    }
  }
  if (rc < 0) print "No file " name "."
  else close(name)
  return
}
<<functions>>=
function stripcount(s) {
  sub(/^[0-9]+/, "", s)
  sub(/ +/, "", s)
  return "\\nwixaddsx{" s
}
<<functions>>=
function alphasort(a, x, n) {
  n = 0
  for (x in a) 
    n = insertitem(a[x], n)
  finish_sorting(n)
  return n
}
function insertitem(x, n, i, tmp) {
  sorted[n] = x
  sortkeys[n] = sortkey(x)
  return n+1
}
<<functions>>=
function finish_sorting(n) {
  firstwork = nextwork = 0
  addquick(0, n)
  while(nextwork > firstwork)
    qsort()
}
<<functions>>=
function addquick(l, r) {
  workq[nextwork++] = l
  workq[nextwork++] = r
}
<<get [[l]] and [[r]] out of work queue>>=
l = workq[firstwork]
delete workq[firstwork]
firstwork++
r = workq[firstwork]
delete workq[firstwork]
firstwork++
<<functions>>=
function qsort(l, r,    mid, i, last) {
  <<get [[l]] and [[r]] out of work queue>>
  if (r - l < 10) 
    isort(l, r)
  else {
    mid = l + int((r - l) * rand())
    swap(l, mid)
    last = l
    for (i = l+1; i < r; i++)
       if (sortkeys[i] <  sortkeys[l] ||
           sortkeys[i] == sortkeys[l] && sorted[i] < sorted[l])
	      swap(++last, i)
    swap(l, last)
    addquick(l, last)
    addquick(last+1, r)    
  }
}
<<functions>>=
function isort(l, r,    n) {
  for (n = l + 1; n < r; n++)
    for (i = n; i > l && (sortkeys[i] <  sortkeys[i-1] ||
                          sortkeys[i] == sortkeys[i-1] && sorted[i] < sorted[i-1]); i--)
      swap(i, i-1)
}
<<functions>>=
function swap(i, j, tmp) {
    tmp = sortkeys [i]; sortkeys [i] = sortkeys [j]; sortkeys [j] = tmp
    tmp = sorted[i]; sorted[i] = sorted[j]; sorted[j] = tmp
}
<<functions>>=
function sortkey(s, count) {
  match(s, /^[0-9]+/)
  count = substr(s, RSTART, RLENGTH)
  sub(/^[0-9]+ */, "", s)
  if (s ~ /c}/) {
      return firstkey(substr(s, 3)) "\n" count
  } else if (s ~ /i}/) {
      return firstkey(substr(s, 3)) "\n" count
  } else {
      print "sortkey handed non-chunk and non-index: " s
      exit 1
  }
}
<<functions>>=
function firstkey(s, r, openbrace) {
  if (s !~ /^{{/) {
     <<complain about format and exit>>
  }
  sub (/^{{/, "", s)
  gsub(/\\([a-zA-Z]+|.) */, "", s)	# kill control sequences
  openbrace = 1
  r = ""
  while (openbrace > 0)
    if (match(s, /^[^{}]*/) <= 0) 
      openbrace--
    else {
      r = r substr(s, RSTART, RLENGTH)
      c = substr(s, RSTART+RLENGTH, 1)
      s = substr(s, RSTART+RLENGTH+1)
      if (c == "}") openbrace--
      else openbrace++
      if (openbrace > 0) r = r c
    }
  return alphabet(r) "\n" r
}
<<complain about format and exit>>=
print "key \"" substr(s, 1, 6) "...\" is ill-formatted"
exit 1
<<functions>>=
function alphabet(s, r) {
  r = ""
  while (match(s, /[a-zA-Z \t]/) > 0) {
    s = substr(s, RSTART)
    c = substr(s, 1, 1)
    if (c == " " || c == "\t") {
      r = r " "
      sub(/^[ \t]+/, "", s)
    } else {
      match(s, /^[a-zA-Z]+/)
      r = r substr(s, RSTART, RLENGTH)
      s = substr(s, RSTART+RLENGTH)
    }
  }
  sub(/^ +/, "", r)
  return r
}
@
\section{Index}
\nowebindex