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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
#!/usr/local/bin/ruby
#
# HTML to LaTeX converter
# by A. Ito, 16 June, 1997
#
require 'kconv'
# configuration
def gif2eps(giffile,epsfile)
cmd = "convert #{giffile} #{epsfile}"
STDERR.print cmd,"\n"
system cmd
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
end
class TokenStream
TAG_START = ?<
TAG_END = ?>
AMP_START = ?&
AMP_END = ?;
AMP_REPLACE_TABLE = {
'&' => '\\&',
'>' => '$>$',
'<' => '$<$',
' ' => '~',
'"' => '"',
}
def initialize(file)
if file.kind_of?(File) 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 = Kconv.toeuc(@f.gets)
if @f.eof? then
return nil
end
@bpos = 0
end
if @buf[@bpos] == TAG_START then
return Tag.new(read_until(TAG_END))
elsif @buf[@bpos] == AMP_START then
return replace_amp(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
def replace_amp(s)
if AMP_REPLACE_TABLE.key?(s) then
return AMP_REPLACE_TABLE[s]
else
return s
end
end
end
def print_header
print '
\documentstyle[epsf]{jarticle}
\def\hr{\par\hbox to \textwidth{\hrulefill}}
\def\pre{\begin{quote}\def\baselinestretch{0.8}\tt\obeylines}
\def\endpre{\end{quote}}
\makeatletter
\@ifundefined{gt}{\let\gt=\dg}{}
\makeatother
'
end
class Environ_stack
def initialize(*envs)
@stack = envs
end
def action(tag)
if tag =~ /^!/ then # comment
return ["",nil]
end
i = @stack.size-1
while i >= 0
a = @stack[i].action(tag)
unless a.nil? then
return a
end
i -= 1
end
return nil
end
def pop
@stack.pop
end
def push(env)
@stack.push(env)
end
def top
@stack[@stack.size-1]
end
def dup
@stack.push(top.clone)
end
end
class Environment
def initialize(interp)
@silent = FALSE
@in_table = FALSE
@interp = interp;
@align = nil;
end
def action(tag)
return @interp[tag]
end
def flush(tok)
if tok.kind_of?(String) then
tok = tok.gsub(/&/,"\\&");
tok = tok.gsub(/%/,"\\%");
tok = tok.gsub(/#/,"\\#");
tok = tok.gsub(/\$/,"\\$");
tok = tok.gsub(/_/,"\\verb+_+");
tok = tok.gsub(/\^/,"\\verb+^+");
tok = tok.gsub(/~/,"\\verb+~+");
end
if @in_table then
@table[@table_rows][@table_cols] += tok
elsif !@silent then
if !@align.nil? && tok =~ /\n$/ then
print tok.chop,"\\\\\n"
else
print tok
end
end
end
def set_interp(interp)
@interp = interp
end
# tag processing methods
# <TITLE>
def do_silent(tag)
@silent = TRUE
end
# </TITLE>
def undo_silent(tag)
@silent = FALSE
end
# <IMG>
def img_proc(tag)
src = tag.switch('src')
newfile = src.sub(/\.GIF/i,".eps")
gif2eps(src,newfile)
flush "\\epsfile{file=#{newfile}}\n"
end
# <TABLE>
def starttable(tag)
@table = []
@tablespan = []
@table_rows = -1
@table_cols_max = 0
@in_table = TRUE
unless tag.switch('border').nil? then
@table_border = TRUE
else
@table_border = FALSE
end
end
# <TR>
def start_row(tag)
@table_rows += 1
@table[@table_rows] = []
@tablespan[@table_rows] = []
@table_cols = -1
@colspan = 1
end
# <TD>
def start_col(tag)
@colspan = tag.switch('colspan')
if @colspan.nil? then
@colspan = 1
else
@colspan = @colspan.to_i
end
@tablespan[@table_rows][@table_cols+1] = @colspan
@table_cols += @colspan
if @table_cols > @table_cols_max then
@table_cols_max = @table_cols
end
end
# </TABLE>
def endtable(tag)
@in_table = FALSE
flush "\\begin{tabular}{*{"
flush @table_cols_max+1
if @table_border then
flush "}{|l}|}\n\\hline\n"
else
flush "}{l}}\n"
end
for i in 0..@table_rows
j = 0
while j <= @table_cols
span = @tablespan[i][j]
if span == 1 then
flush @table[i][j]
elsif @table_border then
form = "|l"
if j+span > @table_cols then
form = "|l|"
end
flush "\\multicolumn{"+span.to_s+"}{"+form+"}{"
flush @table[i][j+span-1]
flush "}"
else
flush "\\multicolumn{"+span.to_s+"}{l}{"
flush @table[i][j+span-1]
flush "}"
end
j += span
if j <= @table_cols then
flush "&"
end
end
flush "\\\\\n"
flush "\\hline\n" if @table_border
end
flush "\\end{tabular}\n"
end
# <CENTER>
def startcenter(tag)
if @in_table then
flush "\\hfil"
else
flush "\\begin{center}\n"
end
end
# </CENTER>
def endcenter(tag)
if @in_table then
flush "\\hfil"
else
flush "\\end{center}\n"
end
end
# <P>
def paragraph(tag)
align = tag.switch('align')
if align.nil? then
flush "\\par\n"
@endparagraph = ""
else
align = align.downcase
case align
when "left" then
flush "\\begin{flushleft}\n"
@endparagraph = "\\end{flushleft}\n"
when "center" then
flush "\\begin{center}\n"
@endparagraph = "\\end{center}\n"
when "right" then
flush "\\begin{flushright}\n"
@endparagraph = "\\end{flushright}\n"
end
end
@align = align
end
# </P>
def endparagraph(tag)
unless @align.nil? then
@align = nil
flush @endparagraph
end
end
end
enum_interp = {
'li' => ["\\item ",nil]
}
item_interp = {
'li' => ["\\item ",nil]
}
desc_interp = {
'dt' => ["\\item[",nil],
'dd' => ["]\n",nil]
}
table_interp = {
'tr' => [:start_row,nil],
'td' => [:start_col,nil],
'/tr' => ["",nil],
'/td' => ["",nil],
}
para_interp = {
'/p' => [:endparagraph ,"pop",TRUE],
}
main_interp = {
'body' => ["\\begin{document}\n",nil,FALSE],
'/body' => ["\\end{document}\n",nil,FALSE],
'head' => ["",nil,FALSE],
'/head' => ["",nil,FALSE],
'html' => ["",nil,FALSE],
'/html' => ["",nil,FALSE],
'title' => [:do_silent,nil,FALSE],
'/title' => [:undo_silent,nil,FALSE],
'!' => ["",nil,FALSE],
'h1' => ["\\section{",nil,TRUE],
'h2' => ["\\subsection{",nil,TRUE],
'h3' => ["\\subsubsection{",nil,TRUE],
'h4' => ["\\paragraph{",nil,TRUE],
'/h1' => ["}\n",nil,TRUE],
'/h2' => ["}\n",nil,TRUE],
'/h3' => ["}\n",nil,TRUE],
'/h4' => ["}\n",nil,TRUE],
'a' => ["",nil,TRUE],
'/a' => ["",nil,TRUE],
'center' => [:startcenter,nil,TRUE],
'/center' => [:endcenter,nil,TRUE],
'ol' => ["\\begin{enumerate}\n",enum_interp,TRUE],
'/ol' => ["\\end{enumerate}\n","pop",TRUE],
'ul' => ["\\begin{itemize}\n",item_interp,TRUE],
'/ul' => ["\\end{itemize}\n","pop",TRUE],
'dl' => ["\\begin{description}\n",desc_interp,TRUE],
'/dl' => ["\\end{description}\n","pop",TRUE],
'pre' => ["\\begin{pre}\n",nil,TRUE],
'/pre' => ["\\end{pre}\n",nil,TRUE],
'p' => [:paragraph ,para_interp,TRUE],
'br' => ["\\par ",nil,TRUE],
'img' => [:img_proc,nil,TRUE],
'hr' => ["\\hr ",nil,TRUE],
'b' => ["{\\bf\\gt ",nil,TRUE],
'/b' => ["}",nil,TRUE],
'strong' => ["{\\bf\\gt ",nil,TRUE],
'/strong' => ["}",nil,TRUE],
'dfn' => ["{\\bf\\gt ",nil,TRUE],
'/dfn' => ["}",nil,TRUE],
'i' => ["{\\it",nil,TRUE],
'/i' => ["}",nil,TRUE],
'address' => ["{\\it",nil,TRUE],
'/address'=> ["}",nil,TRUE],
'cite' => ["{\\it",nil,TRUE],
'/cite' => ["}",nil,TRUE],
'code' => ["{\\tt",nil,TRUE],
'/code' => ["}",nil,TRUE],
'kbd' => ["{\\tt",nil,TRUE],
'/kbd' => ["}",nil,TRUE],
'tt' => ["{\\tt",nil,TRUE],
'/tt' => ["}",nil,TRUE],
'samp' => ["{\\tt",nil,TRUE],
'/samp' => ["}",nil,TRUE],
'em' => ["{\\em",nil,TRUE],
'/em' => ["}",nil,TRUE],
'u' => ["$\\underline{\\mbox{",nil,TRUE],
'/u' => ["}}$",nil,TRUE],
'sub' => ["${}_\mbox{",nil,TRUE],
'/sub' => ["}$",nil,TRUE],
'sup' => ["${}^\mbox{",nil,TRUE],
'/sup' => ["}$",nil,TRUE],
'table' => [:starttable, table_interp,TRUE],
'/table' => [:endtable, "pop",TRUE],
'font' => ["",nil,TRUE],
'/font' => ["",nil,TRUE],
}
################################ MAIN ####################################
$in_document = FALSE
print_header
intp = Environ_stack.new(Environment.new(main_interp))
f = TokenStream.new(ARGV[0])
until f.eof?
tok = f.get
if tok.kind_of?(Tag) then
case tok.tagname
when "body"
$in_document = TRUE
when "/body"
$in_document = FALSE
end
act = intp.action(tok.tagname)
if act.nil? then
STDERR.print "tag ",tok.tagname," ignored\n"
else
if act[2] && !$in_document then
print "\\begin{document}\n"
$in_document = TRUE
end
# enviconment push
if act[1].kind_of?(Hash) &&
(tok.tagname != "p" || tok.switch('align') != nil) then
intp.dup
intp.top.set_interp(act[1])
end
if act[0].kind_of?(String) then
intp.top.flush act[0]
elsif act[0].kind_of?(Fixnum) then # interned symbol
intp.top.send(act[0],tok)
end
# environment pop
if act[1] == "pop" then
intp.pop
end
end
elsif !tok.nil? then
intp.top.flush tok
end
end
if $in_document then
print "\\end{document}\n"
end
|