Module: WolfTrans::Util

Defined in:
lib/wolftrans/util.rb

Class Method Summary collapse

Class Method Details

.escape_path(path) ⇒ Object



26
27
28
# File 'lib/wolftrans/util.rb', line 26

def self.escape_path(path)
  full_strip(path).gsub(/[\x00\/\\:\*\?\"<>\|]/, '_')
end

.full_strip(str) ⇒ Object

Strip all leading/trailing whitespace, including fullwidth spaces



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

def self.full_strip(str)
  str.strip.sub(/^\u{3000}*/, '').sub(/\u{3000}*$/, '')
end

.join_path_nocase(parent, child) ⇒ Object

Get the name of a path case-insensitively



12
13
14
15
16
17
# File 'lib/wolftrans/util.rb', line 12

def self.join_path_nocase(parent, child)
  child_downcase = child.downcase
  child_case = Dir.entries(parent).select { |e| e.downcase == child_downcase }.first
  return nil unless child_case
  return "#{parent}/#{child_case}"
end

.read_txt(filename) ⇒ Object

Read all lines of a txt file as UTF-8, rejecting anything with a BOM



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wolftrans/util.rb', line 38

def self.read_txt(filename)
  text = nil
  File.open(filename, 'rb:UTF-8') do |file|
    if text = file.read(3)
      if text == "\xEF\xBB\xBF"
        raise "UTF-8 BOM detected; refusing to read file"
      end
      text << file.read
    else
      STDERR.puts "warning: empty patch file '#{filename}'"
      return ''
    end
  end
  # Convert Windows newlines and return
  text.gsub(/\r\n?/, "\n")
end

.sanitize_path(path) ⇒ Object

Sanitize a path; i.e., standardize path separators remove trailing separator



4
5
6
7
8
9
# File 'lib/wolftrans/util.rb', line 4

def self.sanitize_path(path)
  if File::ALT_SEPARATOR
    path = path.gsub(File::ALT_SEPARATOR, '/')
  end
  path.sub(/\/$/, '')
end

.translatable?(string) ⇒ Boolean

Determine if a string should be added to the translation map. All non-nill, non-empty, and “black square” character strings are good.

Returns:

  • (Boolean)


32
33
34
# File 'lib/wolftrans/util.rb', line 32

def self.translatable?(string)
  string && !string.empty? && string != "\u25A0"
end