#!/usr/bin/ruby =begin @description inofficial (illegal?) client for dict_leo_org @author alexis reigel, mail@koffeinfrei.org @date march 05 @license gpl version >= 2 =end # TODO: display forum matches too require 'open-uri' require 'cgi' OPTION_SEARCH_TYPE = 'fuzzy' # strict, relaxed, fuzzy OPTION_LANG = 'de' # de, en, ... TODO: match words below not working with other than de COL_WIDTH = 80 SEARCH_STR_WORD = '' SEARCH_STR_HEAD = ' 0 and !ARGV.empty? then word = ARGV.join(" ") else puts "Usage: #{$0} "; exit end puts "Looking up \"#{word}\"...\n\n" open "http://dict.leo.org/?lp=ende&lang=#{OPTION_LANG}"+ "&searchLoc=0&cmpType=fuzzy#{OPTION_SEARCH_TYPE}"+ "&relink=off§Hdr=on&spellToler=on&search=#{CGI::escape(word)}" do |f| f.each_line do |l| #find line on which the translations are line = l[/.*<\/table>/] if l =~ /DEUTSCH/ next if line.nil? puts "ENGLISCH#{' ' * (COL_WIDTH - 8)}DEUTSCH" puts line[/\s*(\d+\s?\w+)\s*<\/strong>/, 1] ih = line.index(SEARCH_STR_HEAD) i1 = count = 0 until (i1 = line.index(SEARCH_STR_WORD, i1 + 1)).nil? count = count + 1 # write headings unless ih.nil? if i1 > ih heading = line[ih, 500][/([^>]+)<\/B>/, 1].gsub(/ /, '').strip puts "\n#{' ' * (COL_WIDTH - heading.length - 2)}[#{heading}]" ih = i1 end ih = line.index(SEARCH_STR_HEAD, ih) end i2 = line.index('', i1) curr = line[i1 + SEARCH_STR_WORD.length, i2 - i1 - SEARCH_STR_WORD.length] print curr.gsub(/<\/?\w+>/,'') if count % 2 == 0 then print "\n" else print " " * [COL_WIDTH - curr.length, 1].max end end break # skip next lines, as the one we're looking for has been found end end