Module: Helper

Extended by:
Helper
Included in:
Helper
Defined in:
lib/helper.rb

Overview

雑多なお助けメソッド群

Defined Under Namespace

Classes: AsyncCommand

Constant Summary collapse

HOST_OS =
RbConfig::CONFIG["host_os"]
ENTITIES =
{ quot: '"', amp: "&", nbsp: " ", lt: "<", gt: ">", copy: "(c)", "#39" => "'" }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getchObject



47
48
49
# File 'lib/helper.rb', line 47

def $stdin.getch
  WinAPI._getch.chr
end

Instance Method Details

#ampersand_to_entity(str) ⇒ Object

アンパサンドをエンティティに変換



160
161
162
# File 'lib/helper.rb', line 160

def ampersand_to_entity(str)
  str.gsub(/&(?!amp;)/mi, "&amp;")
end

#confirm(message, default = false, nontty_default = true) ⇒ Object

キーボード入力による確認をする

:default: エンターを押した場合に返ってくる値 :nontty_default: pipe等から接続された場合に返ってくる値



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/helper.rb', line 60

def confirm(message, default = false, nontty_default = true)
  return nontty_default unless STDIN.tty?
  confirm_msg = "#{message} (y/n)?: "
  STDOUT.print confirm_msg   # Logger でロギングされないように直接標準出力に表示
  while input = $stdin.getch
    STDOUT.puts input
    case input.downcase
    when "y"
      return true
    when "n"
      return false
    else
      return default if input.strip == ""
      STDOUT.print confirm_msg
    end
  end
end

#convert_to_windows_path(path) ⇒ Object

CYGWINのパスからwindowsのパスへと変換(cygpathを呼び出すだけ)



153
154
155
# File 'lib/helper.rb', line 153

def convert_to_windows_path(path)
  `cygpath -aw \"#{path}\"`.strip
end

#determine_osObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/helper.rb', line 28

def determine_os
  case
  when os_windows?
    :windows
  when os_mac?
    :mac
  when os_cygwin?
    :cygwin
  else
    :other
  end
end

#engine_jruby?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/helper.rb', line 41

def engine_jruby?
  @@engine_is_jruby ||= RUBY_ENGINE == "jruby"
end

#extract_illust_chuki(str) ⇒ Object

文章の中から挿絵注記を分離する



167
168
169
170
171
172
173
174
# File 'lib/helper.rb', line 167

def extract_illust_chuki(str)
  illust_chuki_array = []
  extracted_str = str.gsub(/[  \t]*?([#挿絵(.+?)入る])\n?/) do
    illust_chuki_array << $1
    ""
  end
  [extracted_str, illust_chuki_array]
end

#open_browser(url) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/helper.rb', line 102

def open_browser(url)
  case determine_os
  when :windows
    escaped_url = url.gsub("%", "%^").gsub("&", "^&")
    # MEMO: start の引数を "" で囲むと動かない
    system(%!start #{escaped_url}!)
  when :cygwin
    system(%!cygstart #{url}!)
  when :mac
    system(%!open "#{url}"!)
  else
    open_browser_linux(url, "ブラウザが見つかりませんでした")
  end
end

#open_browser_linux(address, error_message) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/helper.rb', line 78

def open_browser_linux(address, error_message)
  %w(xdg-open firefox w3m).each do |browser|
    system(%!#{browser} "#{address}"!)
    return if $?.success?
  end
  error error_message
end

#open_directory(path, confirm_message = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/helper.rb', line 86

def open_directory(path, confirm_message = nil)
  if confirm_message
    return unless confirm(confirm_message, false, false)
  end
  case determine_os
  when :windows
    system(%!explorer "file:///#{path.encode(Encoding::Windows_31J)}"!)
  when :cygwin
    system(%!cygstart "#{path}"!)
  when :mac
    system(%!open "#{path}"!)
  else
    open_browser_linux(path, "フォルダが開けませんでした")
  end
end

#os_cygwin?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/helper.rb', line 24

def os_cygwin?
  @@os_is_cygwin ||= HOST_OS =~ /cygwin/i
end

#os_mac?Boolean

Returns:

  • (Boolean)


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

def os_mac?
  @@os_is_mac ||= HOST_OS =~ /darwin/i
end

#os_windows?Boolean

Returns:

  • (Boolean)


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

def os_windows?
  @@os_is_windows ||= HOST_OS =~ /mswin(?!ce)|mingw|bccwin/i
end

#pretreatment_source(src, encoding = Encoding::UTF_8) ⇒ Object

ダウンロードしてきたデータを使いやすいように処理



134
135
136
# File 'lib/helper.rb', line 134

def pretreatment_source(src, encoding = Encoding::UTF_8)
  restor_entity(src.force_encoding(encoding)).gsub("\r", "")
end


117
118
119
# File 'lib/helper.rb', line 117

def print_horizontal_rule
  puts "" * 35
end

#replace_filename_special_chars(str, invalid_replace = false) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/helper.rb', line 121

def replace_filename_special_chars(str, invalid_replace = false)
  result = str.tr("/:*?\"<>|.", "/:*?”〈〉|.").gsub("\\", "").gsub("\t", "").gsub("\n", "")
  if invalid_replace
    org_encoding = result.encoding
    result = result.encode(Encoding::Windows_31J, invalid: :replace, undef: :replace, replace: "_")
                   .encode(org_encoding)
  end
  result
end

#restor_entity(str) ⇒ Object

エンティティ復号



142
143
144
145
146
147
148
# File 'lib/helper.rb', line 142

def restor_entity(str)
  result = str.encode("UTF-16BE", "UTF-8", :invalid => :replace, :undef => :replace, :replace => "?").encode("UTF-8")
  ENTITIES.each do |key, value|
    result.gsub!("&#{key};", value)
  end
  result
end