Skip to content

Commit

Permalink
Use new output from seznam
Browse files Browse the repository at this point in the history
  • Loading branch information
jphager2 committed Nov 9, 2019
1 parent 72081b0 commit 8469b89
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.5.3
ruby-2.6.5
61 changes: 41 additions & 20 deletions lib/seznam_slovnik/translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,63 @@ def call(**options)

doc = Nokogiri::HTML(html)

title = doc.css('#results h1').text
quick_definitions = doc
.css('#results #fastMeanings table tr td:last-child')
.map(&method(:clean_quick_meaning))
.reject(&:empty?)
title = doc.css('h1').text
parts = doc
.css('.TranslatePage-results .Box--partOfSpeech')
.map(&method(:build_part_of_speech))

_, columns = IO.console.winsize
out = <<~OUT
Results for: #{title.colorize(color: :light_blue, mode: :bold)}
#{"=" * (title.length + 13)}
Quick Definitions
#{"=" * 17}
Definitions
===========
OUT

quick_definitions.each do |definition|
out << " * #{definition}\n"
parts.each do |part|
out << part[:name]
out << "\n"
out << "-" * part[:name].length
out << "\n"
part[:definitions].each_with_index do |definition, i|
out << " #{i + 1})"
needs_padding = false
definition[:lines].each do |line|
out << " " * part[:definition_pad] if needs_padding
out << " * #{line}\n"
needs_padding = true
end
end
out << "\n"
end
out << "\n"
out << "-" * columns.to_i

puts(color ? out : out.uncolorize)
end

private def clean_quick_meaning(meaning)
meaning
.children
.map { |node| node.name == 'br' ? ', ' : node.text }
.join
.strip
.gsub(/\s+/, " ")
.gsub(/\s,/, ",")
.split(ABBREVIATION)
.map { |part| part.match?(ABBREVIATION) ? part.colorize(mode: :italic) : part.colorize(color: :light_blue, mode: :bold) }
.join
private def build_part_of_speech(part)
pos = {}
pos[:name] = part.at_css('.Box-header-title').text
pos[:definitions] = part.css('li').map(&method(:build_definition))
pos[:definition_pad] = pos[:definitions].length.to_s.length + 2 # " 1)"
pos
end

private def build_definition(definition)
df = {}
df[:lines] = definition.css('.Box-content-line').map(&method(:clean_line))
df
end

private def clean_line(line)
line.children.map do |part|
next '->' if part['class'].to_s == "Box-content-pointer"
part.text
end.map(&:strip).reject(&:empty?).join(' ')
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/seznam_slovnik/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SeznamSlovnik
VERSION = "0.2.0"
VERSION = "0.3.0"
end

0 comments on commit 8469b89

Please sign in to comment.