루비와 gtk로 만든 미니 사전입니다.
찾을 단어를 마우스로 더블 클릭하여(또는 드래그하여) 선택하면 마우스 위치에 사전이 뜹니다.
사전 소스는 구글 사전입니다.(파싱하기 제일 편한 것으로 ;;
[attachment=0:2r62it8x]minidict.png[/attachment:2r62it8x]
[code:2r62it8x]
#!/usr/bin/ruby1.9.1
coding: utf-8
version: 20100911
colyright: GPL v3
author: cogniti AT gmail.com
버그
@window.present 가 올라오지(raise) 못하는 경우가 있다.
require ‘gtk2’
require ‘gtkhtml2’
require ‘net/http’
Gtk::Window
@window = Gtk::Window.new("사전")
@window.signal_connect( "destroy" ) { Gtk.main_quit }
@window.set_default_size(300, 200)
@window.opacity=0.9
@window.decorated = false
@window.accept_focus = false
#@window.keep_above = true
@window.signal_connect(‘enter-notify-event’) do
@window.decorated = true
end
Gtk::ScrolledWindow
sw = Gtk::ScrolledWindow.new(nil, nil)
sw.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC)
Gtk::HtmlView
@htmlview = Gtk::HtmlView.new
@htmlview.show
add
sw.add(@htmlview)
@window.add(sw)
show
@window.show_all
Gtk::HtmlDocument
@doc = Gtk::HtmlDocument.new
@doc.open_stream(‘text/html’)
@doc.write_stream(‘<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><html><body><h3>구글 사전을 이용한 영한사전입니다.<br/> 마우스로 단어를 더블 클릭(또는 드래그)하면 그 위치에 창이 뜨고 단어가 표시됩니다.</h3></body></html>’)
@doc.close_stream()
@htmlview.document = @doc
CLIPBOARD
@clipboard = Gtk::Clipboard.get(Gdk::Selection::PRIMARY) # 선택한 문자열
@clipboard.signal_connect(‘owner-change’) do
@q = @clipboard.wait_for_text # 선택한 문자열 읽어온다.
unless @q.nil?
# 마우스 포인터 위치 읽어 온다.
x = Gdk::Window.default_root_window.pointer[1]
y = Gdk::Window.default_root_window.pointer[2]
# 마우스 작업에 방해되지 않도록 +10 해준다.
@window.move(x+10, y+10)
dict @q
@window.decorated = false
@window.present
end
end
def convert_to_html s
s.gsub!(/<?xml version="1.0" encoding="UTF-8" ?>/, "")
s.gsub!(/<DictionaryPage>/, "<html>")
s.gsub!(/</DictionaryPage>/, "</html>")
s.gsub!(/<AvailableLanguages \/>/, "")
s.gsub!(/<result>/, "<body>")
s.gsub!(/<\/result>/, "<\/body>")
s.gsub!(/<result \/>/, "")
s.gsub!(/<primary source_language=\"0\">/, "<dl>")
s.gsub!(/<\/primary>/, "<\/dl>")
s.gsub!(/<related_phrase source_language=\"0\">/, "<!-- IGNORE")
s.gsub!(/<\/related_phrase>/, "IGNORE-- >")
s.gsub!(/<term language=\"0\" type=\"0\">/, "")
s.gsub!(/<term language=\"9\" type=\"1\">/, "")
s.gsub!(/<\/term>/, "")
s.gsub!(/<text>/, "<dd>")
s.gsub!(/<\/text>/, "<\/dd>")
s.gsub!(/<PronunciationGroup type=\"2\">/, "")
s.gsub!(/<\/PronunciationGroup>/, "")
s.gsub!(/<dd>#{@q}<\/dd>/, "<dt>#{@q}</dt>")
if s.strip! == "<html>\n \n \n</html>"
"찾는 단어가 없습니다."
else
s
end
end
def dict q
# | ==> %7C
uri = "http://www.google.com/dictionary/feeds?client=ig&langpair=en%7Cko&q=#{q}&hl=ko"
begin
s = Net::HTTP.get URI.parse(uri)
@result = convert_to_html(s)
@doc.open_stream(‘text/html’)
@doc.write_stream(‘<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />’)
@doc.write_stream(@result)
@doc.close_stream
rescue => e
puts e.message
end
end
Gtk.main
[/code:2r62it8x]