module Honyomi::Util
Public Instance Methods
count_digit(num)
click to toggle source
# File lib/honyomi/util.rb, line 106 def count_digit(num) num.to_s.length end
default_home()
click to toggle source
# File lib/honyomi/util.rb, line 102 def default_home File.expand_path '~' end
exist_command?(command)
click to toggle source
# File lib/honyomi/util.rb, line 115 def exist_command?(command) !(RubyWhich.new.which(command).empty?) end
extract_keywords(query)
click to toggle source
# File lib/honyomi/util.rb, line 62 def extract_keywords(query) return [] if query.nil? query.split.reduce([]) do |a, e| e = e.gsub(/^\(|\)|AND|OR$/, "") if e =~ /^"(.+)"$/ a + [$1] elsif e =~ /^-/ a elsif e =~ /:/ a else if e.empty? a else a + [e] end end end end
filename_to_utf8(src)
click to toggle source
# File lib/honyomi/util.rb, line 24 def filename_to_utf8(src) if platform_osx? if ruby19? src.encode('UTF-8', 'UTF8-MAC') else src end elsif platform_win? Kconv.kconv(src, Kconv::UTF8) else src end end
highlight_keywords(src, keywords, css_class)
click to toggle source
# File lib/honyomi/util.rb, line 42 def highlight_keywords(src, keywords, css_class) return "" if src.nil? return src if keywords.nil? words = nil if Groonga::VERSION[0] >= 3 words = Groonga::PatriciaTrie.create(key_type: "ShortText", normalizer: "NormalizerAuto") else words = Groonga::PatriciaTrie.create(key_type: "ShortText", key_normalize: true) end keywords.each { |keword| words.add(keword) } other_text_handler = Proc.new { |string| ERB::Util.h(string) } options = { other_text_handler: other_text_handler } words.tag_keys(src, options) do |record, word| "<span class='#{css_class}'>#{ERB::Util.h(word)}</span>" end end
home_dir()
click to toggle source
# File lib/honyomi/util.rb, line 98 def home_dir ENV['HONYOMI_DATABASE_DIR'] || File.join(default_home, '.honyomi') end
image_path(page)
click to toggle source
# File lib/honyomi/util.rb, line 110 def image_path(page) zerofill = format("%0#{count_digit(page.book.page_num)}d", page.page_no) "#{home_dir}/image/#{page.book.id}/book-#{zerofill}.jpg" end
platform_osx?()
click to toggle source
# File lib/honyomi/util.rb, line 20 def platform_osx? RUBY_PLATFORM =~ /darwin/ end
platform_win?()
click to toggle source
# File lib/honyomi/util.rb, line 16 def platform_win? RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin|bccwin/ end
render_bookmark_comment_to_html(bookmark)
click to toggle source
# File lib/honyomi/util.rb, line 84 def render_bookmark_comment_to_html(bookmark) comment = CGI.escape_html(bookmark.comment || "") URI.extract(comment, %w{http https}).uniq.each do |uri| unless uri.match(/(\.jpg|\.jpeg|\.png)/) comment.gsub!(uri, %Q{<a href="#{uri}">#{uri}</a>}) end end comment.gsub!(/P([1-9][0-9]*)/, %Q(<a href="/v/#{bookmark.page.book.id}?page=\\1">P\\1</a>)) comment.gsub("\n", "<br/>") end
ruby19?()
click to toggle source
# File lib/honyomi/util.rb, line 12 def ruby19? RUBY_VERSION >= '1.9.0' end
ruby20?()
click to toggle source
# File lib/honyomi/util.rb, line 8 def ruby20? RUBY_VERSION >= '2.0.0' end
strip_page(page)
click to toggle source
# File lib/honyomi/util.rb, line 38 def strip_page(page) page.gsub(/[ \t]/, "") end