Module: Honyomi::Util

Defined in:
lib/honyomi/util.rb

Class Method Summary collapse

Class Method Details

.count_digit(num) ⇒ Object



106
107
108
# File 'lib/honyomi/util.rb', line 106

def count_digit(num)
  num.to_s.length
end

.default_homeObject



102
103
104
# File 'lib/honyomi/util.rb', line 102

def default_home
  File.expand_path '~'
end

.exist_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/honyomi/util.rb', line 115

def exist_command?(command)
  !(RubyWhich.new.which(command).empty?)
end

.extract_keywords(query) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_dirObject



98
99
100
# File 'lib/honyomi/util.rb', line 98

def home_dir
  ENV['HONYOMI_DATABASE_DIR'] || File.join(default_home, '.honyomi')
end

.image_path(page) ⇒ Object



110
111
112
113
# 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?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/honyomi/util.rb', line 20

def platform_osx?
  RUBY_PLATFORM =~ /darwin/
end

.platform_win?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/honyomi/util.rb', line 16

def platform_win?
  RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin|bccwin/
end

.render_bookmark_comment_to_html(bookmark) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# 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?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/honyomi/util.rb', line 12

def ruby19?
  RUBY_VERSION >= '1.9.0'
end

.ruby20?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/honyomi/util.rb', line 8

def ruby20?
  RUBY_VERSION >= '2.0.0'
end

.strip_page(page) ⇒ Object



38
39
40
# File 'lib/honyomi/util.rb', line 38

def strip_page(page)
  page.gsub(/[ \t]/, "")
end