class TwitterCldr::Resources::TransformsImporter
Private Instance Methods
each_transform_file(&block)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 138 def each_transform_file(&block) Dir.glob(File.join(transforms_path, '*.xml')).each(&block) end
execute()
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 20 def execute transform_id_map = {} FileUtils.mkdir_p(output_path) each_transform_file do |transform_file| transform_data = parse_transform_data(File.read(transform_file)) output_file = File.join(output_path, "#{File.basename(transform_file).chomp('.xml')}.yml") transform_id_map.merge!(map_aliases(transform_data, output_file)) write_transform_data(transform_data, output_file) end write_transform_id_map(transform_id_map) end
fix_rule(rule)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 131 def fix_rule(rule) rule. gsub("←", '<'). gsub("→", '>'). gsub("↔", '<>') end
fix_rule_wrapping(rules)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 117 def fix_rule_wrapping(rules) wrap = false rules.each_with_object([]) do |rule, ret| if wrap ret.last.sub!(/\\\z/, rule) else ret << rule end wrap = rule.end_with?('\\') end end
get_aliases(node)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 93 def get_aliases(node) if attrib = node.attribute('alias') attrib.value.split(' ') end end
get_variant(node)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 99 def get_variant(node) if attrib = node.attribute('variant') attrib.value end end
join_transform_id(source, target, variant)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 53 def join_transform_id(source, target, variant) TwitterCldr::Transforms::TransformId.join(source, target, variant) end
map_aliases(transform_data, path)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 35 def map_aliases(transform_data, path) filename = File.basename(path).chomp('.yml') aliases = transform_data.flat_map do |transform_datum| (transform_datum[:aliases] || []) + [ join_transform_id( transform_datum[:source], transform_datum[:target], transform_datum[:variant] ) ] end aliases.uniq.each_with_object({}) do |aliass, ret| ret[aliass] = filename end end
normalize_transform_id(id_str)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 57 def normalize_transform_id(id_str) TwitterCldr::Transforms::TransformId.parse(id_str).to_s end
output_path()
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 146 def output_path params.fetch(:output_path) end
parse_transform_data(transform_data)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 78 def parse_transform_data(transform_data) doc = Nokogiri.XML(transform_data) doc.xpath('supplementalData/transforms/transform').map do |transform_node| { source: transform_node.attribute('source').value, target: transform_node.attribute('target').value, aliases: get_aliases(transform_node), variant: get_variant(transform_node), direction: transform_node.attribute('direction').value, rules: rules(transform_node) } end end
rules(transform_node)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 105 def rules(transform_node) rules = fix_rule_wrapping( transform_node.xpath('tRule').flat_map do |rule_node| fix_rule(rule_node.content).split("\n").map(&:strip) end ) rules.reject do |rule| rule.strip.empty? || rule.strip.start_with?('#') end end
transforms_path()
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 142 def transforms_path File.join(requirements[:cldr].common_path, 'transforms') end
write_transform_data(transform_data, path)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 61 def write_transform_data(transform_data, path) File.open(path, 'w:utf-8') do |output| output.write( TwitterCldr::Utils::YAML.dump( TwitterCldr::Utils.deep_symbolize_keys(transforms: transform_data), use_natural_symbols: true ) ) end end
write_transform_id_map(transform_id_map)
click to toggle source
# File lib/twitter_cldr/resources/transforms_importer.rb, line 72 def write_transform_id_map(transform_id_map) File.open(File.join(output_path, 'transform_id_map.yml'), 'w+') do |output| output.write(YAML.dump(transform_id_map)) end end