module WolfTrans::Util
Public Class Methods
escape_path(path)
click to toggle source
Escape a string for use as a path on the filesystem stackoverflow.com/questions/2270635/invalid-chars-filter-for-file-folder-name-ruby
# File lib/wolftrans/util.rb, line 26 def self.escape_path(path) full_strip(path).gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') end
full_strip(str)
click to toggle source
Strip all leading/trailing whitespace, including fullwidth spaces
# 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)
click to toggle source
Get the name of a path case-insensitively
# 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)
click to toggle source
Read all lines of a txt file as UTF-8, rejecting anything with a BOM
# 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)
click to toggle source
Sanitize a path; i.e., standardize path separators remove trailing separator
# 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)
click to toggle source
Determine if a string should be added to the translation map. All non-nill, non-empty, and “black square” character strings are good.
# File lib/wolftrans/util.rb, line 32 def self.translatable?(string) string && !string.empty? && string != "\u25A0" end