Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/dist/
/pkg/
/*.txt
/*.nrset
.bundle
vendor
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ source 'https://rubygems.org'
gemspec
gem 'rspec'
gem 'rake'
gem 'rexml'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -42,6 +45,7 @@ PLATFORMS
DEPENDENCIES
anpan!
rake
rexml
rspec

BUNDLED WITH
Expand Down
27 changes: 18 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,38 @@ 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
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

Expand Down
24 changes: 22 additions & 2 deletions lib/anpan.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'yaml'
require 'rexml/document'
require 'anpan/vowel'
require 'anpan/consonant'
require 'anpan/an'
Expand All @@ -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
18 changes: 16 additions & 2 deletions lib/anpan/pattern.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'anpan/pattern/table'
require 'rexml/document'

class Anpan
class Pattern
Expand All @@ -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
Expand Down