kakasi-1.0.2/0000755000004100000410000000000012435702365013014 5ustar www-datawww-datakakasi-1.0.2/Rakefile0000644000004100000410000000112712435702365014462 0ustar www-datawww-datarequire "bundler/gem_tasks" gemspec = Bundler::GemHelper.gemspec require 'rake/testtask' Rake::TestTask.new(:test) do |test| test.libs << 'test' test.test_files = gemspec.test_files test.verbose = true end require 'rdoc/task' Rake::RDocTask.new do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = "#{gemspec.name} #{gemspec.version}" rdoc.rdoc_files.include(gemspec.extra_rdoc_files) rdoc.rdoc_files.include('lib/**/*.rb') end task :default => :test task :compile do gemspec.extensions.each { |ext| dir, extconf = File.split(ext) chdir(dir) { ruby extconf } } end kakasi-1.0.2/Gemfile0000644000004100000410000000013312435702365014304 0ustar www-datawww-datasource 'https://rubygems.org' # Specify your gem's dependencies in kakasi.gemspec gemspec kakasi-1.0.2/kakasi.gemspec0000644000004100000410000000220612435702365015624 0ustar www-datawww-datalib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'kakasi/version' Gem::Specification.new do |spec| spec.name = "kakasi" spec.version = Kakasi::VERSION spec.authors = ["Akinori MUSHA"] spec.email = ["knu@idaemons.org"] spec.description = %q{A Ruby binding for KAKASI implemented with Fiddle/DL/FFI} spec.summary = %q{A Ruby binding for KAKASI implemented with FFI} spec.homepage = "https://github.com/knu/kakasi_ffi" spec.license = "BSD" spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] case RUBY_ENGINE when 'jruby' spec.platform = 'java' else spec.extensions = ["ext/kakasi-config/extconf.rb"] end spec.required_ruby_version = '>= 1.9.3' spec.add_development_dependency "rake" spec.add_development_dependency "rdoc", ["> 2.4.2"] spec.add_development_dependency "bundler", [">= 1.2"] spec.add_development_dependency "test-unit" end kakasi-1.0.2/LICENSE.txt0000644000004100000410000000240712435702365014642 0ustar www-datawww-dataCopyright (c) 2013, 2014 Akinori MUSHA All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. kakasi-1.0.2/.travis.yml0000644000004100000410000000047112435702365015127 0ustar www-datawww-data--- language: ruby rvm: - 1.9.3 - 2.0.0 - 2.1.3 - ruby-head - jruby-19mode - jruby-head - rbx-2 script: rake test matrix: allow_failures: - rvm: ruby-head - rvm: jruby-head before_install: - sudo apt-get update -qq - sudo apt-get install -y libkakasi2 libkakasi2-dev before_script: - rake compile kakasi-1.0.2/lib/0000755000004100000410000000000012435702365013562 5ustar www-datawww-datakakasi-1.0.2/lib/kakasi.rb0000644000004100000410000000066212435702365015356 0ustar www-datawww-datarequire 'kakasi/version' module Kakasi case RUBY_ENGINE when 'jruby' LIBPATH = [].freeze IMPL = :ffi unless defined?(::Kakasi::IMPL) else begin require 'kakasi/config' rescue LoadError LIBPATH = [].freeze IMPL = :ffi unless defined?(::Kakasi::IMPL) end end require 'kakasi/%s' % IMPL def kakasi(options, string) Lib::kakasi(options, string) end module_function :kakasi end kakasi-1.0.2/lib/kakasi/0000755000004100000410000000000012435702365015025 5ustar www-datawww-datakakasi-1.0.2/lib/kakasi/version.rb0000644000004100000410000000010612435702365017034 0ustar www-datawww-datamodule Kakasi KAKASI_VERSION = '2002-09-28' VERSION = '1.0.2' end kakasi-1.0.2/lib/kakasi/ffi.rb0000644000004100000410000000270412435702365016121 0ustar www-datawww-datamodule Kakasi module Lib if defined?(Rubinius) FFI = Rubinius::FFI else require 'ffi' end extend FFI::Library require 'kakasi/platform' try_load { |lib| ffi_lib lib } attach_function 'kakasi_getopt_argv', [:int, :pointer], :int attach_function 'kakasi_do', [:string], :pointer attach_function 'kakasi_close_kanwadict', [], :int attach_function 'kakasi_free', [:pointer], :int INTERNAL_ENCODING = Encoding::CP932 @mutex = Mutex.new @options = nil def kakasi(options, string) @mutex.synchronize { if options != @options kakasi_close_kanwadict() if @options args = ['kakasi', *options.split] argc = args.size argv = FFI::MemoryPointer.new(:pointer, argc). write_array_of_pointer(args.map { |arg| FFI::MemoryPointer.from_string(arg) }) kakasi_getopt_argv(argc, argv).zero? or raise "failed to initialize kakasi" @options = options.dup end encoding = string.encoding result = ''.force_encoding(INTERNAL_ENCODING) string.encode(INTERNAL_ENCODING).split(/(\0+)/).each { |str, nul| buf = kakasi_do(str) result << buf.read_string.force_encoding(INTERNAL_ENCODING) kakasi_free(buf) result << nul if nul } result.encode(encoding) } end module_function :kakasi end end kakasi-1.0.2/lib/kakasi/platform.rb0000644000004100000410000000270712435702365017204 0ustar www-datawww-datamodule Kakasi module Lib if defined?(::FFI::Platform) LIBPREFIX = ::FFI::Platform::LIBPREFIX LIBSUFFIX = ::FFI::Platform::LIBSUFFIX else LIBPREFIX = case RbConfig::CONFIG['host_os'] when /mingw|mswin/i ''.freeze when /cygwin/i 'cyg'.freeze else 'lib'.freeze end LIBSUFFIX = case RbConfig::CONFIG['host_os'] when /darwin/i 'dylib'.freeze when /mingw|mswin|cygwin/i 'dll'.freeze else 'so'.freeze end end DEFLIBPATH = begin [RbConfig::CONFIG['libdir']] | case RbConfig::CONFIG['host_os'] when /mingw|mswin/i [] else %w[/lib /usr/lib] end end.freeze class << self def try_load(libpath = LIBPATH) basename = map_library_name('kakasi') (libpath | DEFLIBPATH).each { |dir| filename = File.join(dir, basename) begin yield filename rescue LoadError next rescue next end return filename } begin yield basename rescue LoadError raise rescue raise LoadError, $!.message end return basename end private def map_library_name(name) "#{LIBPREFIX}#{name}.#{LIBSUFFIX}" end end end end kakasi-1.0.2/lib/kakasi/fiddle.rb0000644000004100000410000000303212435702365016577 0ustar www-datawww-datamodule Kakasi module Lib require 'fiddle/import' unless defined?(Fiddle::Importer) extend Fiddle::Importer include Fiddle case SIZEOF_VOIDP when 4 PackPointers = 'L*' when 8 PackPointers = 'Q*' else raise 'Unsupported pointer size: %u' % SIZEOF_VOIDP end require 'kakasi/platform' try_load { |lib| dlload lib } extern 'int kakasi_getopt_argv(int, char **)' extern 'char *kakasi_do(char *)' extern 'int kakasi_close_kanwadict()' extern 'int kakasi_free(char *)' INTERNAL_ENCODING = Encoding::CP932 @mutex = Mutex.new @options = nil def kakasi(options, string) @mutex.synchronize { if options != @options kakasi_close_kanwadict() if @options args = ['kakasi', *options.split] argc = args.size argv = Pointer.malloc(argc * SIZEOF_VOIDP) argv[0, argv.size] = args.map { |x| Pointer[x].to_i }.pack(PackPointers) kakasi_getopt_argv(argc, argv).zero? or raise "failed to initialize kakasi" @options = options.dup end encoding = string.encoding result = ''.force_encoding(INTERNAL_ENCODING) string.encode(INTERNAL_ENCODING).split(/(\0+)/).each { |str, nul| buf = kakasi_do(str) result << buf.to_s.force_encoding(INTERNAL_ENCODING) kakasi_free(buf) result << nul if nul } result.encode(encoding) } end module_function :kakasi end end kakasi-1.0.2/lib/kakasi/dl.rb0000644000004100000410000000017512435702365015754 0ustar www-datawww-datamodule Kakasi module Lib require 'dl/import' Fiddle = DL Pointer = DL::CPtr end end require 'kakasi/fiddle' kakasi-1.0.2/metadata.yml0000644000004100000410000000531712435702365015325 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: kakasi version: !ruby/object:Gem::Version version: 1.0.2 platform: ruby authors: - Akinori MUSHA autorequire: bindir: bin cert_chain: [] date: 2014-10-13 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: rdoc requirement: !ruby/object:Gem::Requirement requirements: - - ">" - !ruby/object:Gem::Version version: 2.4.2 type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">" - !ruby/object:Gem::Version version: 2.4.2 - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '1.2' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '1.2' - !ruby/object:Gem::Dependency name: test-unit requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' description: A Ruby binding for KAKASI implemented with Fiddle/DL/FFI email: - knu@idaemons.org executables: [] extensions: - ext/kakasi-config/extconf.rb extra_rdoc_files: [] files: - ".gitignore" - ".travis.yml" - Gemfile - LICENSE.txt - README.md - Rakefile - ext/kakasi-config/extconf.rb - kakasi.gemspec - lib/kakasi.rb - lib/kakasi/dl.rb - lib/kakasi/ffi.rb - lib/kakasi/fiddle.rb - lib/kakasi/platform.rb - lib/kakasi/version.rb - test/helper.rb - test/test_kakasi.rb homepage: https://github.com/knu/kakasi_ffi licenses: - BSD metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 1.9.3 required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.2.2 signing_key: specification_version: 4 summary: A Ruby binding for KAKASI implemented with FFI test_files: - test/helper.rb - test/test_kakasi.rb has_rdoc: kakasi-1.0.2/test/0000755000004100000410000000000012435702365013773 5ustar www-datawww-datakakasi-1.0.2/test/helper.rb0000644000004100000410000000063112435702365015577 0ustar www-datawww-datarequire 'rubygems' require 'bundler' begin Bundler.setup(:default, :development) rescue Bundler::BundlerError => e $stderr.puts e.message $stderr.puts "Run `bundle install` to install missing gems" exit e.status_code end require 'test/unit' $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'kakasi' class Test::Unit::TestCase end kakasi-1.0.2/test/test_kakasi.rb0000644000004100000410000000112212435702365016616 0ustar www-datawww-data# -*- coding: utf-8 -*- require 'helper' include Kakasi class TestKakasi < Test::Unit::TestCase def test_kakasi assert_equal 'KAKASHI nanodesu', eval(<<'', TOPLEVEL_BINDING) kakasi('-s -U -Ja -Ha', '案山子なのです') text = '本日は晴れのち曇りです。' assert_equal '本日 は 晴れ のち 曇り です 。', Kakasi.kakasi('-w', text) assert_equal 'honjitsu ha hare nochi kumori desu .', Kakasi.kakasi('-s -Ja -Ha -Ka -Ea -ka', text) assert_equal 'ほんじつ は はれ のち くもり です 。', Kakasi.kakasi('-s -JH -kK', text) end end kakasi-1.0.2/.gitignore0000644000004100000410000000031212435702365015000 0ustar www-datawww-data*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp ext/kakasi-config/Makefile lib/kakasi/config.rb kakasi-1.0.2/ext/0000755000004100000410000000000012435702365013614 5ustar www-datawww-datakakasi-1.0.2/ext/kakasi-config/0000755000004100000410000000000012435702365016322 5ustar www-datawww-datakakasi-1.0.2/ext/kakasi-config/extconf.rb0000644000004100000410000000305512435702365020320 0ustar www-datawww-datarequire 'mkmf' require 'rbconfig' $stdout.sync = true dir_config('kakasi') module KakasiExtConf default_impl = { fiddle: ->{ require 'fiddle/import' extend Fiddle::Importer }, dl: ->{ require 'dl/import' DL::CPtr.method_defined?(:[]=) or raise "DL::CPtr#[]= is missing" extend DL::Importer }, ffi: ->{ if defined?(Rubinius) FFI = Rubinius::FFI else require 'ffi' end extend FFI::Library }, }.find { |impl, code| print "checking #{impl}... " begin code.call rescue LoadError, StandardError => e puts "no (#{e.message})" next false end puts "yes" break impl } or exit false print 'checking for kakasi... ' require '../../lib/kakasi/platform.rb' begin libkakasi = Kakasi::Lib.try_load($LIBPATH) { |lib| case default_impl when :fiddle, :dl dlload lib when :ffi ffi_lib lib else raise LoadError, 'NOTREACHED' end } puts libkakasi rescue LoadError puts 'failed -- libkakasi is not found' exit false end '../../lib/kakasi/config.rb'.tap { |rb| puts 'creating %s' % rb File.open(File.expand_path(rb, File.dirname(__FILE__)), 'wt') { |f| f.print <<-'EOF' % [$LIBPATH.inspect, default_impl.inspect] module Kakasi LIBPATH = %s unless defined?(LIBPATH) IMPL = %s unless defined?(IMPL) end EOF } } end 'Makefile'.tap { |mf| puts 'creating %s' % mf File.open(mf, 'w') { |f| f.print <<-'EOF' all: install: EOF } } kakasi-1.0.2/README.md0000644000004100000410000000221012435702365014266 0ustar www-datawww-data# Kakasi for Ruby A Ruby binding for KAKASI implemented with Fiddle/DL/FFI ## Description This library is a port of Ruby/KAKASI to Ruby >=1.9. Ruby/KAKASI was an extention library written by GOTO Kentaro for ancient CRuby, which did not work with Ruby >=1.9 or JRuby, This implementation uses Fiddle/DL/FFI as a bridge to libkakasi, so it should work with any ruby that supports Fiddle/DL/FFI module, such as: - Ruby 1.9.3 (DL) - Ruby 2.0.0+ (Fiddle) - Rubinius (Rubinius::FFI) - JRuby (FFI) ## Installation Add this line to your application's Gemfile: gem 'kakasi' And then execute: $ bundle Or install it yourself as: $ gem install kakasi ## Usage require 'kakasi' Kakasi.kakasi('-w', 'Rubyから案山子を呼び出せます。') #=> "Ruby から 案山子 を 呼び出せ ます 。" ## Links - [KAKASI - Kanji Kana Simple Inverter](http://kakasi.namazu.org/index.html.en) ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request