blob: 9cb1942e5cf3e19f6c339626f99e479784545a56 (
plain) (
blame)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
#!/usr/local/bin/ruby
# HTML reference generator
# by A.Ito 1999/3/30
require 'kconv'
###########################################################################
class URL
attr 'scheme'
attr 'host'
attr 'port'
attr 'file'
attr 'label'
def initialize(str)
if /([a-zA-Z+\-]+):(.*)/ =~ str then
@scheme = $1
str = $2
else
@scheme = 'unknown'
end
hostpart = ''
if %r'//([^/]*)(/.*)' =~ str then
hostpart = $1
str = $2
elsif %r'//([^/]*)$' =~ str then
hostpart = str
str = ''
end
if hostpart != '' then
if /(.*):(\d+)/ =~ hostpart then
@host = $1
@port = $2
else
@host = hostpart
@port = ''
end
else
@host = @port = ''
end
if /(.*)#(.*)/ =~ str then
@file = $1
@label = $2
else
@file = str
@label = ''
end
end
def to_s
s = "#{@scheme}:"
if s == 'news' or s == 'mailto' then
return s+@file
end
s += "//"+@host
s += ":"+@port if @port.size > 0
s += @file
s += "#"+@label if @label.size > 0
s
end
def complete(current)
@scheme = current.scheme if @scheme == 'unknown'
@port = current.port if @host == '' and @port == ''
@host = current.host if @host == ''
unless @file =~ %r'^/' then
@file = File.expand_path(File.dirname(current.file)+'/'+@file)
end
self
end
end
class Tag
def initialize(str)
if str =~ /<(.+)>/ then
str = $1
end
tags = str.split
@tagname = tags.shift.downcase
@vals = {}
tags.each do |t|
if t =~ /=/ then
tn,tv = t.split(/\s*=\s*/,2)
tv.sub!(/^"/,"")
tv.sub!(/"$/,"")
@vals[tn.downcase] = tv
else
@vals[t.downcase] = TRUE
end
end
end
def tagname
return @tagname
end
def each
@vals.each do |k,v|
yield k,v
end
end
def switch(k)
return @vals[k]
end
def to_s
if tagname =~ /!--/ then
return ''
end
t = "<"+tagname
if @vals.size == 0 then
return t+">"
end
each do |a,v|
if v == true then
t += " #{a}"
else
t += " #{a}=\"#{v}\""
end
end
t+">"
end
end
class TokenStream
TAG_START = ?<
TAG_END = ?>
AMP_START = ?&
AMP_END = ?;
def initialize(file)
if file.kind_of?(IO) then
@f = file
else
@f = File.new(file)
end
@buf = nil
@bpos = 0
end
def read_until(endsym)
complete = FALSE
tag = []
begin
while @bpos < @buf.size
c = @buf[@bpos]
if c == endsym then
tag.push(c.chr)
complete = TRUE
@bpos += 1
break
end
if c == 10 || c == 13 then
tag.push(' ')
else
tag.push(c.chr)
end
@bpos += 1
end
unless complete
@buf = @f.gets
@bpos = 0
break if @f.eof?
end
end until complete
return tag.join('')
end
def get
while TRUE
if @buf.nil? then
@buf = @f.gets
if @f.eof? then
return nil
end
@buf = Kconv.toeuc(@buf)
@bpos = 0
end
if @buf[@bpos] == TAG_START then
return Tag.new(read_until(TAG_END))
elsif @buf[@bpos] == AMP_START then
return read_until(AMP_END)
else
i = @bpos
while i < @buf.size && @buf[i] != TAG_START && @buf[i] != AMP_START
i += 1
end
r = @buf[@bpos,i-@bpos]
if i == @buf.size then
@buf = nil
else
@bpos = i
end
redo if r =~ /^\s+$/
return r
end
end
end
public :eof?
def eof?
@f.eof?
end
end
################################ MAIN ####################################
refs = []
refnum = 0
body_finished = false
html_finished = false
currentURL = nil
immediate_ref = false
while ARGV[0] =~ /^-/
case ARGV.shift
when '-url'
currentURL = URL.new(ARGV.shift)
when '-u'
immediate_ref = true
end
end
if ARGV.size > 0 then
f = TokenStream.new(ARGV[0])
else
f = TokenStream.new(STDIN)
end
until f.eof?
tok = f.get
if tok.kind_of?(Tag) then
if tok.tagname == 'a' and !tok.switch('href').nil? then
refs[refnum] = tok.switch('href')
refnum += 1
elsif tok.tagname == '/a' then
if immediate_ref then
r = refs[refnum-1]
if !currentURL.nil? then
r = URL.new(r).complete(currentURL).to_s
end
print "[#{r}]"
else
print "[#{refnum}]"
end
elsif tok.tagname == '/body' then
body_finished = true
break
elsif tok.tagname == '/html' then
html_finished = true
break
end
print tok.to_s
elsif !tok.nil? then
print tok
end
end
if !immediate_ref and refs.size > 0 then
print "<hr><h2>References</h2>\n"
for i in 0..refs.size-1
if currentURL.nil? then
r = refs[i]
else
r = URL.new(refs[i])
r.complete(currentURL)
r = r.to_s
end
print "[#{i+1}] #{r}<br>\n"
end
end
print "</body>\n" unless body_finished
print "</html>\n" unless html_finished
|