diff --git a/.gitignore b/.gitignore index 09715c5..f90ab01 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ /dist/ /pkg/ /*.txt +/*.nrset .bundle vendor diff --git a/Gemfile b/Gemfile index a9d692c..291fc37 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,4 @@ source 'https://rubygems.org' gemspec gem 'rspec' gem 'rake' +gem 'rexml' diff --git a/Gemfile.lock b/Gemfile.lock index 6665261..de2e2f7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -19,6 +19,8 @@ GEM concurrent-ruby (~> 1.0) minitest (5.14.3) rake (13.2.1) + rexml (3.3.5) + strscan rspec (3.13.0) rspec-core (~> 3.13.0) rspec-expectations (~> 3.13.0) @@ -32,6 +34,7 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) rspec-support (3.13.1) + strscan (3.1.0) tzinfo (2.0.4) concurrent-ruby (~> 1.0) zeitwerk (2.4.2) @@ -42,6 +45,7 @@ PLATFORMS DEPENDENCIES anpan! rake + rexml rspec BUNDLED WITH diff --git a/Rakefile b/Rakefile index cf74feb..52985ab 100644 --- a/Rakefile +++ b/Rakefile @@ -8,12 +8,17 @@ RSpec::Core::RakeTask.new(:spec) task default: :package CONFS = { - 'anpan.txt': 'anpan.yaml', - 'google_japanese_input.txt': 'google_japanese_input.yaml', - 'dvorakjp_prime.txt': 'dvorakjp.yaml', + anpan: 'anpan.yaml', + google_japanese_input: 'google_japanese_input.yaml', + dvorakjp_prime: 'dvorakjp.yaml', } -TABLE_PATHS = CONFS.keys.map(&:to_s) +TARGETS = { + google_japanese_input: ".txt", kawasemi: ".nrset" +} + +TABLE_PATHS = CONFS.keys.map {|k| TARGETS.map {|t, ext| "#{k}#{ext}" } }.flatten + package_task = Rake::PackageTask.new("tables", Anpan::VERSION) do |p| p.need_zip = true p.package_files.include TABLE_PATHS @@ -21,16 +26,20 @@ end file package_task.package_dir_path => TABLE_PATHS -def output_to_file(filename, conf) +def output_to_file(filename, conf, target) puts filename File.open(filename.to_s, 'w') do |file| - file.write Anpan.new(conf).render + file.write Anpan.new(conf).render(target) end end -CONFS.each do |filename, conf| - file filename do - output_to_file filename, conf +CONFS.each do |key, conf| + TARGETS.each do |target, ext| + filename = "#{key}#{ext}" + + file filename do + output_to_file filename, conf, target + end end end diff --git a/lib/anpan.rb b/lib/anpan.rb index e83a7d2..d8ae18a 100644 --- a/lib/anpan.rb +++ b/lib/anpan.rb @@ -1,4 +1,5 @@ require 'yaml' +require 'rexml/document' require 'anpan/vowel' require 'anpan/consonant' require 'anpan/an' @@ -18,7 +19,26 @@ def table(args = {}) @an.table(args) end - def render - @an.patterns.map(&:render).join("\n") + def render(target) + case target + when :google_japanese_input + @an.patterns.map { |p| p.render(target) }.join("\n") + when :kawasemi + doc = REXML::Document.new + doc << REXML::XMLDecl.new('1.0', 'UTF-8') + plist = REXML::Element.new('plist') + plist.add_attribute('version', '1.0') + dict = REXML::Element.new('dict') + + @an.patterns.each do |p| + p.render(target).each do |e| + dict << e + end + end + plist << dict + doc << plist + + doc.to_s + end end end diff --git a/lib/anpan/pattern.rb b/lib/anpan/pattern.rb index c4c8501..b73339b 100644 --- a/lib/anpan/pattern.rb +++ b/lib/anpan/pattern.rb @@ -1,4 +1,5 @@ require 'anpan/pattern/table' +require 'rexml/document' class Anpan class Pattern @@ -10,9 +11,22 @@ def initialize(input, output = input, addition = nil, as_is = false) @as_is = as_is end - def render + def render(target = :google_japanese_input) output = @as_is ? @output : output_jp - [@input, output, @addition].join("\t").gsub(/\t+$/, '') + + case target + when :google_japanese_input + [@input, output, @addition].join("\t").gsub(/\t+$/, '') + when :kawasemi + # かわせみは自動で tt -> っt に変換してくれるのでそれを阻害しないようにする + return [] unless @addition.empty? + + key = REXML::Element.new('key') + key.text = @input + value = REXML::Element.new('string') + value.text = output + [key, value] + end end def to_h