Module: Milkode::Util
- Defined in:
- lib/milkode/common/util.rb
Defined Under Namespace
Classes: ZipfileNotFound
Class Method Summary collapse
-
.divide_shortpath(shortpath) ⇒ Object
‘package/to/a.txt’ #=> ‘package’, ‘to/a.txt’ ‘package’ #=> ‘package’, nil.
- .downcase?(str) ⇒ Boolean
-
.exist_command?(command) ⇒ Boolean
指定したコマンドが存在するか?.
- .filename_to_utf8(str_from_file) ⇒ Object
- .fuzzy_gotoline_keyword?(keyword) ⇒ Boolean
-
.gem_version_more?(name, version) ⇒ Boolean
gem_version_more?(‘rroonga’, ‘2.1.0’) #=> rroonga >= ‘2.1.0’.
- .git_url?(src) ⇒ Boolean
- .github_repo(src) ⇒ Object
- .gotoline_keyword?(keyword) ⇒ Boolean
- .gotoline_multi?(words) ⇒ Boolean
-
.groonga_table_sort(table, keys, options = {}) ⇒ Object
互換性保持のため.
- .highlight_keywords(src, keywords, css_class) ⇒ Object
- .ignore_case?(pattens, is_sensitive) ⇒ Boolean
- .larger_than_oneline(content) ⇒ Object
- .load_content(out, filename) ⇒ Object
- .normalize_filename(str) ⇒ Object
-
.parse_gotoline(words) ⇒ Object
parse_gotoline([‘a’, ‘123’, ‘b’]) #=> [[‘a’, ‘b’], 123]] parse_gotoline([‘a’, ‘123’, ‘b’, 55]) #=> [[‘a’, ‘b’, ‘123’], 55]] parse_gotoline() #=> [[‘a’], 55]].
- .parse_gotoline_multi(words) ⇒ Object
- .parse_gotoline_single(words) ⇒ Object
-
.pipe?(io) ⇒ Boolean
StringIO patch.
- .platform_osx? ⇒ Boolean
- .platform_win? ⇒ Boolean
- .relative_path(path, basedir) ⇒ Object
- .root_entrylist(filename) ⇒ Object
- .ruby19? ⇒ Boolean
- .ruby20? ⇒ Boolean
- .shell_kcode ⇒ Object
- .str2kcode(str) ⇒ Object
- .svn_url?(src) ⇒ Boolean
-
.truncate_nsec(t) ⇒ Object
Timeからnsecを切り捨てる rroongaのTimeカラムはマイクロ秒までしか格納出来ない.
- .warning_alert(out, msg) ⇒ Object
-
.zip_extract(filename, dst_dir) ⇒ Object
zipファイルを展開し、展開フォルダ名を返す ファイルが見つからなかった時はnilを返す.
Class Method Details
.divide_shortpath(shortpath) ⇒ Object
‘package/to/a.txt’ #=> ‘package’, ‘to/a.txt’ ‘package’ #=> ‘package’, nil
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/milkode/common/util.rb', line 177 def divide_shortpath(shortpath) shortpath = shortpath[1..-1] if shortpath[0..0] == '/' # 先頭の'/'はカット a = shortpath.split('/') if (a.size >= 2) return a[0], a[1..-1].join('/') else return a[0], nil end end |
.downcase?(str) ⇒ Boolean
113 114 115 |
# File 'lib/milkode/common/util.rb', line 113 def downcase?(str) str == str.downcase end |
.exist_command?(command) ⇒ Boolean
指定したコマンドが存在するか?
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/milkode/common/util.rb', line 236 def exist_command?(command) # open3 : Not working on Pure Windows # begin # Open3.capture3('type', command)[2].exited? # rescue Errno::ENOENT # false # end # whichr !(RubyWhich.new.which(command).empty?) end |
.filename_to_utf8(str_from_file) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/milkode/common/util.rb', line 85 def filename_to_utf8(str_from_file) if platform_osx? if (ruby19?) str_from_file.encode('UTF-8', 'UTF8-MAC') else str_from_file end else Kconv.kconv(str_from_file, Kconv::UTF8) end end |
.fuzzy_gotoline_keyword?(keyword) ⇒ Boolean
125 126 127 |
# File 'lib/milkode/common/util.rb', line 125 def fuzzy_gotoline_keyword?(keyword) keyword =~ /\A.*:\d+\Z/ end |
.gem_version_more?(name, version) ⇒ Boolean
gem_version_more?(‘rroonga’, ‘2.1.0’) #=> rroonga >= ‘2.1.0’
222 223 224 |
# File 'lib/milkode/common/util.rb', line 222 def gem_version_more?(name, version) Gem.loaded_specs[name].version >= Gem::Version.new(version) end |
.git_url?(src) ⇒ Boolean
188 189 190 |
# File 'lib/milkode/common/util.rb', line 188 def git_url?(src) (src =~ /^(?:git[:@])|(?:ssh:)|(?:\.git\Z)/) != nil end |
.github_repo(src) ⇒ Object
302 303 304 305 306 307 308 309 310 |
# File 'lib/milkode/common/util.rb', line 302 def github_repo(src) if src.match(/\Agit@github\.com:(.*)\.git\Z/) $1 elsif src.match(/\A\w+:\/\/github\.com\/(.*)\.git\Z/) $1 else nil end end |
.gotoline_keyword?(keyword) ⇒ Boolean
121 122 123 |
# File 'lib/milkode/common/util.rb', line 121 def gotoline_keyword?(keyword) keyword =~ /\A\/.*:\d+\Z/ end |
.gotoline_multi?(words) ⇒ Boolean
167 168 169 170 171 172 173 |
# File 'lib/milkode/common/util.rb', line 167 def gotoline_multi?(words) if (words.join(" ") =~ /:\d+/) true else false end end |
.groonga_table_sort(table, keys, options = {}) ⇒ Object
互換性保持のため
227 228 229 230 231 232 233 |
# File 'lib/milkode/common/util.rb', line 227 def groonga_table_sort(table, keys, = {}) if gem_version_more?('rroonga', '2.1.0') table.sort(keys, ).map{|r| r.value} else table.sort(keys, ) end end |
.highlight_keywords(src, keywords, css_class) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/milkode/common/util.rb', line 248 def highlight_keywords(src, keywords, css_class) # Init highlight_map hightlight_map = Array.new(src.length, nil) keywords.each do |keyword| pos = 0 loop do r = src.match(/#{Regexp.escape(keyword)}/i, pos) do |m| s = m.begin(0) l = keyword.length e = s+l (s...e).each {|i| hightlight_map[i] = 1 } pos = e end break if r.nil? end end # Delete html tag index = 0 in_tag = false src.each_char do |char| in_tag = true if char == '<' hightlight_map[index] = nil if in_tag in_tag = false if char == '>' index += 1 end # Output result = "" index = 0 prev = nil src.each_char do |char| current = hightlight_map[index] if prev.nil? && current result += "<span class='#{css_class}'>" elsif prev && current.nil? result += "</span>" end result += char index += 1 prev = current end result += "</span>" if prev result end |
.ignore_case?(pattens, is_sensitive) ⇒ Boolean
117 118 119 |
# File 'lib/milkode/common/util.rb', line 117 def ignore_case?(pattens, is_sensitive) !is_sensitive && (pattens.all? {|v| Util.downcase? v}) end |
.larger_than_oneline(content) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/milkode/common/util.rb', line 97 def larger_than_oneline(content) begin content && content.count($/) > 1 rescue ArgumentError true end end |
.load_content(out, filename) ⇒ Object
205 206 207 208 209 210 211 212 213 |
# File 'lib/milkode/common/util.rb', line 205 def load_content(out, filename) str = File.read(filename) begin Kconv.kconv(str, Kconv::UTF8) rescue ArgumentError warning_alert(out, "skip kconv. file size too big (or negative string size) : #{filename}.") str end end |
.normalize_filename(str) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/milkode/common/util.rb', line 105 def normalize_filename(str) if platform_win? str.gsub(/\A([a-z]):/) { "#{$1.upcase}:" } else str end end |
.parse_gotoline(words) ⇒ Object
parse_gotoline([‘a’, ‘123’, ‘b’]) #=> [[‘a’, ‘b’], 123]] parse_gotoline([‘a’, ‘123’, ‘b’, 55]) #=> [[‘a’, ‘b’, ‘123’], 55]] parse_gotoline() #=> [[‘a’], 55]]
132 133 134 135 136 137 138 |
# File 'lib/milkode/common/util.rb', line 132 def parse_gotoline(words) if gotoline_multi?(words) parse_gotoline_multi(words) else [parse_gotoline_single(words)] end end |
.parse_gotoline_multi(words) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/milkode/common/util.rb', line 160 def parse_gotoline_multi(words) words.map do |v| a = v.split(':') [[a[0..-2].join(':')], a[-1].to_i] end end |
.parse_gotoline_single(words) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/milkode/common/util.rb', line 140 def parse_gotoline_single(words) lineno = -1 index = -1 words.each_with_index do |v, idx| n = v.to_i if (n != 0) lineno = n index = idx end end if (lineno == -1) [words, 1] # 行番号らしきものは見つからなかった else words.delete_at(index) [words, lineno] end end |
.pipe?(io) ⇒ Boolean
StringIO patch
197 198 199 |
# File 'lib/milkode/common/util.rb', line 197 def pipe?(io) !io.instance_of?(IO) || !File.pipe?(io) end |
.platform_osx? ⇒ Boolean
73 74 75 |
# File 'lib/milkode/common/util.rb', line 73 def platform_osx? RUBY_PLATFORM =~ /darwin/ end |
.platform_win? ⇒ Boolean
69 70 71 |
# File 'lib/milkode/common/util.rb', line 69 def platform_win? RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin|bccwin/ end |
.relative_path(path, basedir) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/milkode/common/util.rb', line 51 def relative_path(path, basedir) path = Pathname.new(normalize_filename path) basedir = Pathname.new(normalize_filename basedir) begin path.relative_path_from(basedir) rescue ArgumentError path end end |
.root_entrylist(filename) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/milkode/common/util.rb', line 39 def root_entrylist(filename) list = [] Archive::Zip.open(filename) do |archive| archive.each do |entry| list << entry.zip_path if entry.zip_path.split('/').size == 1 end end list end |
.ruby19? ⇒ Boolean
65 66 67 |
# File 'lib/milkode/common/util.rb', line 65 def ruby19? RUBY_VERSION >= '1.9.0' end |
.ruby20? ⇒ Boolean
61 62 63 |
# File 'lib/milkode/common/util.rb', line 61 def ruby20? RUBY_VERSION >= '2.0.0' end |
.shell_kcode ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/milkode/common/util.rb', line 77 def shell_kcode if platform_win? Kconv::SJIS # win7? cygwin utf8? else Kconv::UTF8 end end |
.str2kcode(str) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/milkode/common/util.rb', line 312 def str2kcode(str) case str.downcase when 'none' Kconv::NOCONV when 'jis' Kconv::JIS when 'sjis' Kconv::SJIS when 'euc' Kconv::EUC when 'ascii' Kconv::ASCII when 'utf8' Kconv::UTF8 when 'utf16' Kconv::UTF16 else nil end end |
.svn_url?(src) ⇒ Boolean
192 193 194 |
# File 'lib/milkode/common/util.rb', line 192 def svn_url?(src) (src =~ /^(?:svn|svn\+ssh):\/\//) != nil end |
.truncate_nsec(t) ⇒ Object
Timeからnsecを切り捨てる
rroongaのTimeカラムはマイクロ秒までしか格納出来ない
217 218 219 |
# File 'lib/milkode/common/util.rb', line 217 def truncate_nsec(t) Time.at(t.to_i, t.usec) end |
.warning_alert(out, msg) ⇒ Object
201 202 203 |
# File 'lib/milkode/common/util.rb', line 201 def warning_alert(out, msg) out.puts "[warning] #{msg}" end |
.zip_extract(filename, dst_dir) ⇒ Object
zipファイルを展開し、展開フォルダ名を返す ファイルが見つからなかった時はnilを返す
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/milkode/common/util.rb', line 19 def zip_extract(filename, dst_dir) require 'archive/zip' raise ZipfileNotFound unless File.exist?(filename) root_list = root_entrylist(filename) if (root_list.size == 1) # そのまま展開 Archive::Zip.extract filename, dst_dir return root_list[0].gsub("/", "") else # ディレクトリを作ってその先で展開 dir = File.basename(filename).sub(/#{File.extname(filename)}$/, "") FileUtils.mkdir_p File.join(dst_dir, dir) Archive::Zip.extract filename, File.join(dst_dir, dir) return dir end end |