Module: Helper
- Defined in:
- lib/helper.rb,
lib/web/helper4web.rb
Overview
Defined Under Namespace
Classes: AsyncCommand, InvalidVariableName, InvalidVariableType, UnknownVariableType
Constant Summary
collapse
- HOST_OS =
RbConfig::CONFIG["host_os"]
- ENTITIES =
{ quot: '"', amp: "&", nbsp: " ", lt: "<", gt: ">", copy: "(c)", "#39" => "'" }
- TYPE_OF_VALUE =
{
TrueClass => :boolean, FalseClass => :boolean, Fixnum => :integer,
Float => :float, String => :string
}
Class Method Summary
collapse
Class Method Details
.ampersand_to_entity(str) ⇒ Object
136
137
138
|
# File 'lib/helper.rb', line 136
def ampersand_to_entity(str)
str.gsub(/&(?!amp;)/mi, "&")
end
|
.convert_to_windows_path(path) ⇒ Object
CYGWINのパスからwindowsのパスへと変換(cygpathを呼び出すだけ)
129
130
131
|
# File 'lib/helper.rb', line 129
def convert_to_windows_path(path)
`cygpath -aw \"#{path}\"`.strip
end
|
.determine_os ⇒ Object
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
41
42
43
|
# File 'lib/helper.rb', line 41
def engine_jruby?
@@engine_is_jruby ||= RUBY_ENGINE == "jruby"
end
|
143
144
145
146
147
148
149
150
|
# File 'lib/helper.rb', line 143
def (str)
illust_chuki_array = []
= str.gsub(/[ \t]*?([#挿絵(.+?)入る])\n?/) do
illust_chuki_array << $1
""
end
[, illust_chuki_array]
end
|
.getch ⇒ Object
47
48
49
|
# File 'lib/helper.rb', line 47
def $stdin.getch
WinAPI._getch.chr
end
|
.open_browser(url) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/helper.rb', line 78
def open_browser(url)
case determine_os
when :windows
escaped_url = url.gsub("%", "%^").gsub("&", "^&")
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
54
55
56
57
58
59
60
|
# File 'lib/helper.rb', line 54
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/helper.rb', line 62
def open_directory(path, confirm_message = nil)
if confirm_message
return unless Narou::Input.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
24
25
26
|
# File 'lib/helper.rb', line 24
def os_cygwin?
@@os_is_cygwin ||= HOST_OS =~ /cygwin/i
end
|
.os_mac? ⇒ Boolean
20
21
22
|
# File 'lib/helper.rb', line 20
def os_mac?
@@os_is_mac ||= HOST_OS =~ /darwin/i
end
|
.os_windows? ⇒ 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
110
111
112
|
# File 'lib/helper.rb', line 110
def pretreatment_source(src, encoding = Encoding::UTF_8)
restor_entity(src.force_encoding(encoding)).gsub("\r", "")
end
|
.print_horizontal_rule ⇒ Object
93
94
95
|
# File 'lib/helper.rb', line 93
def print_horizontal_rule
puts "―" * 35
end
|
.replace_filename_special_chars(str, invalid_replace = false) ⇒ Object
97
98
99
100
101
102
103
104
105
|
# File 'lib/helper.rb', line 97
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
118
119
120
121
122
123
124
|
# File 'lib/helper.rb', line 118
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
|
.string_cast_to_type(value, type) ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/helper.rb', line 191
def string_cast_to_type(value, type)
result = nil
case type
when :boolean
case value.strip.downcase
when "true"
result = true
when "false"
result = false
else
raise InvalidVariableType, type
end
when :integer
begin
result = Integer(value)
rescue
raise InvalidVariableType, type
end
when :float
begin
result = Float(value)
rescue
raise InvalidVariableType, type
end
when :directory, :file
if File.method("#{type}?").call(value)
result = File.expand_path(value)
else
raise InvalidVariableType, type
end
when :string
result = value
else
raise UnknownVariableType, type
end
result
end
|
.type_of_value(value) ⇒ Object
237
238
239
|
# File 'lib/helper.rb', line 237
def type_of_value(value)
TYPE_OF_VALUE[value.class]
end
|
.variable_type_to_description(type) ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/helper.rb', line 169
def variable_type_to_description(type)
case type
when :boolean
"true/false "
when :integer
"整数 "
when :float
"小数点数 "
when :string
"文字列 "
when :directory
"フォルダパス"
when :file
"ファイルパス"
else
raise UnknownVariableType, type
end
end
|