require 'tk'
require 'tkextlib/tkHTML'
root = TkRoot.new(:title=>'HTML File Viewer', :iconname=>'HV')
file = ARGV[0]
biggray = TkPhotoImage.new(:data=><<'EOD')
R0lGODdhPAA+APAAALi4uAAAACwAAAAAPAA+AAACQISPqcvtD6OctNqLs968+w+G4kiW5omm
6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNFgsAO///
EOD
smgray = TkPhotoImage.new(:data=><<'EOD')
R0lGODdhOAAYAPAAALi4uAAAACwAAAAAOAAYAAACI4SPqcvtD6OctNqLs968+w+G4kiW5omm
6sq27gvH8kzX9m0VADv/
EOD
nogifbig = TkPhotoImage.new(:data=><<'EOD')
R0lGODdhJAAkAPEAAACQkADQ0PgAAAAAACwAAAAAJAAkAAACmISPqcsQD6OcdJqKM71PeK15
AsSJH0iZY1CqqKSurfsGsex08XuTuU7L9HywHWZILAaVJssvgoREk5PolFo1XrHZ29IZ8oo0
HKEYVDYbyc/jFhz2otvdcyZdF68qeKh2DZd3AtS0QWcDSDgWKJXY+MXS9qY4+JA2+Vho+YPp
FzSjiTIEWslDQ1rDhPOY2sXVOgeb2kBbu1AAADv/
EOD
nogifsm = TkPhotoImage.new(:data=><<'EOD')
R0lGODdhEAAQAPEAAACQkADQ0PgAAAAAACwAAAAAEAAQAAACNISPacHtD4IQz80QJ60as25d
3idKZdR0IIOm2ta0Lhw/Lz2S1JqvK8ozbTKlEIVYceWSjwIAO///
EOD
ul_hyper = TkVariable.new(0)
show_tbl = TkVariable.new(0)
show_img = TkVariable.new(1)
pick_font = proc{|size, attrs|
puts "FontCmd: #{size} #{attrs}"
[ ((attrs =~ /fixed/)? 'courier': 'charter'),
(12 * (1.2**(size.to_f - 4.0))).to_i,
((attrs =~ /italic/)? 'italic': 'roman'),
((attrs =~ /bold/)? 'bold': 'normal') ].join(' ')
}
form_cmd = proc{|n, cmd, style, *args|
case cmd
when 'select', 'textarea', 'input'
TkLabel.new(:widgetname=>args[0], :image=>nogifsm)
end
}
images = {}
old_imgs = {}
big_imgs = {}
hotkey = {}
move_big_image = proc{|b|
if big_imgs.key?(b)
b.copy(big_imgs[b])
big_imgs[b].delete
big_imgs.delete(b)
Tk.update
end
}
image_cmd = proc{|*args|
if show_img.bool
smgray
else
fn = args[0]
if old_imgs.key?(fn)
images[fn] = old_imgs[fn]
old_imgs.delete(fn)
images[fn]
else
begin
img = TkPhotoImage.new(:file=>fn)
rescue
smgray
else
if img.width * img.height > 20000
b = TkPhotoImage.new(:width=>img.width, :height=>img.height)
big_imgs[b] = img
img = b
Tk.after_idle(proc{ move_big_image.call(b) })
end
images[fn] = img
img
end
end
end
}
script_cmd = proc{|*args|
}
applet_cmd = proc{|w, arglist|
TkLabel.new(w, :text=>"The Applet #{w}", :bd=>2, :relief=>raised)
}
html = Tk::HTML_Widget.new(:padx=>5, :pady=>9,
:formcommand=>form_cmd,
:imagecommand=>image_cmd,
:scriptcommand=>script_cmd,
:appletcommand=>applet_cmd,
:underlinehyperlinks=>0,
:bg=>'white', :tablerelief=>:raised)
vscr = html.yscrollbar(TkScrollbar.new)
hscr = html.xscrollbar(TkScrollbar.new)
Tk.grid(html, vscr, :sticky=>:news)
Tk.grid(hscr, :sticky=>:ew)
Tk.root.grid_columnconfigure(0, :weight=>1)
Tk.root.grid_columnconfigure(1, :weight=>0)
Tk.root.grid_rowconfigure(0, :weight=>1)
Tk.root.grid_rowconfigure(1, :weight=>0)
priv = {}
last_file = ''
read_file = proc{|name|
begin
fp = open(name, 'r')
ret = fp.read(File.size(name))
rescue
ret = nil
fp = nil
Tk.messageBox(:icon=>'error', :message=>"fail to open '#{name}'",
:type=>:ok)
ensure
fp.close if fp
end
ret
}
clear_screen = proc{
html.clear
old_imgs.clear
big_imgs.clear
hotkey.clear
images.each{|k, v| old_imgs[k] = v }
images.clear
}
load_file = proc{|name|
if (doc = read_file.call(name))
clear_screen.call
last_file = name
html.configure(:base=>name)
html.parse(doc)
old_imgs.clear
end
}
href_binding = proc{|x, y|
html.selection_clear
priv['mark'] = "@#{x},#{y}"
lst = html.href(x, y)
unless lst.size.zero?
lnk, target = lst
if lnk != ""
if lnk =~ /^#{last_file} html.yview($1)
else
load_file.call(lnk)
end
end
end
}
html.clipping_window.bind('1', href_binding, '%x %y')
html.clipping_window.bind('B1-Motion', proc{|w, x, y|
w.selection_set(priv['mark'], "@#{x},#{y}")
TkClipboard.clear
begin
TkClipboard.append(TkSelection.get)
rescue
end
}, '%W %x %y')
last_dir = Dir.pwd
sel_load = proc{
filetypes = [
['Html Files', ['.html', '.htm']],
['All Files', '*']
]
f = Tk.getOpenFile(:initialdir=>last_dir, :filetypes=>filetypes)
if f != ''
load_file.call(f)
last_dir = File.dirname(f)
end
}
refresh = proc{|*args|
load_file.call(last_file)
}
Tk::HTML_Widget::ClippingWindow.bind('Motion', proc{|w, x, y|
parent = w.winfo_parent
url = parent.href(x, y)
unless url.empty?
parent[:cursor] = 'hand2'
else
parent[:cursor] = ''
end
}, '%W %x %y')
menu_spec = [
[['File', 0],
['Open', sel_load, 0],
['Refresh', refresh, 0],
'---',
['Exit', proc{exit}, 1]],
[['View', 0],
['Underline Hyperlinks', ul_hyper],
['Show Table Structure', show_tbl],
['Show Images', show_img]]
]
mbar = Tk.root.add_menubar(menu_spec)
ul_hyper.trace('w', proc{
html[:underlinehyperlinks] = ul_hyper.value
refresh.call
})
show_tbl.trace('w', proc{
if show_tbl.bool
html[:tablerelief] = :flat
else
html[:tablerelief] = :raised
end
refresh.call
})
show_img.trace('w', refresh)
Tk.update
if file && file != ""
load_file.call(file)
end
Tk.mainloop