konjak 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 620fb76e02f4a35a2e085fb25e50d2cf5924d4a3
4
+ data.tar.gz: 13a5d704eceda0ab92f7bc73a7af9f42024f5fcb
5
+ SHA512:
6
+ metadata.gz: f792f56294bcd16a38a8f71ad876d3fef54d5f1b00b53a93eb21f6456a7abce9229c541b8e16d81296d2bf4c580861fccc9d17c39421b418864c13aab50f90ce
7
+ data.tar.gz: 45cbd09c3b26c03d9f8236a9ea31bfc0d3116d1a54a124d7dbb7c82ab5ababfcae58e3b4556c17d884b13c315ac2e3e23bbbef070dbfadc921edc715fcf614b3
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -rrspec/its
2
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in konjak.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Seiei Higa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Konjak
2
+
3
+ TMX(Translation Memory eXchange) tools for ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'konjak'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install konjak
18
+
19
+ ## Usage
20
+
21
+ $ konjak translate file.tmx file.txt src target
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/hanachin/konjak/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/konjak ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'konjak'
4
+
5
+ if ARGV.size != 5 || ARGV[0] != 'translate'
6
+ puts <<USAGE
7
+ usage: konjak translate src target file.tmx file.txt
8
+ USAGE
9
+ exit 1
10
+ end
11
+
12
+ _command, src, target, tmx_path, target_path = ARGV
13
+
14
+ tmx = File.read(tmx_path)
15
+ doc = File.read(target_path)
16
+ puts Konjak.translate(doc, Konjak.parse(tmx, gtt: true), src, target)
data/konjak.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'konjak/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "konjak"
8
+ spec.version = Konjak::VERSION
9
+ spec.authors = ["Seiei Higa"]
10
+ spec.email = ["hanachin@gmail.com"]
11
+ spec.summary = %q{TMX(Translation Memory exChange) tools for ruby}
12
+ spec.description = %q{TMX(Translation Memory exChange) tools for ruby}
13
+ spec.homepage = "https://github.com/hanachin/konjak"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "mem"
22
+ spec.add_dependency "nokogiri"
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-doc"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec", "~> 3.2.0"
28
+ spec.add_development_dependency "rspec-its", "~> 1.0.1"
29
+ end
data/lib/konjak.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'konjak/version'
2
+ require 'konjak/parser'
3
+
4
+ # not elements
5
+ require 'konjak/code_data'
6
+ require 'konjak/text'
7
+
8
+ # elements
9
+ require 'konjak/element'
10
+ require 'konjak/inline_element'
11
+ require 'konjak/structural_element'
12
+
13
+ # structural elements
14
+ require 'konjak/body'
15
+ require 'konjak/header'
16
+ require 'konjak/map'
17
+ require 'konjak/note'
18
+ require 'konjak/property'
19
+ require 'konjak/segment'
20
+ require 'konjak/tmx'
21
+ require 'konjak/translation_unit'
22
+ require 'konjak/translation_unit_variant'
23
+ require 'konjak/user_defined_encoding'
24
+
25
+ # inline elements
26
+ require 'konjak/begin_paired_tag'
27
+ require 'konjak/end_paired_tag'
28
+ require 'konjak/highlight'
29
+ require 'konjak/isolated_tag'
30
+ require 'konjak/placeholder'
31
+ require 'konjak/sub_flow'
32
+ require 'konjak/unknown_tag'
33
+
34
+ # translator
35
+ require 'konjak/translator'
36
+
37
+ module Konjak
38
+ class << self
39
+ def parse(xml, **kw)
40
+ Parser.new.parse(xml, **kw)
41
+ end
42
+
43
+ def translate(doc, xml_or_tmx, src_lang, target_lang)
44
+ tmx = xml_or_tmx.kind_of?(Tmx) ? xml_or_tmx : parse(xml_or_tmx)
45
+ Translator.new(tmx, src_lang, target_lang).translate(doc)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ module Konjak
2
+ class BeginPairedTag < InlineElement
3
+ # required attrs
4
+ attr_accessor :i
5
+
6
+ # optional attrs
7
+ attr_accessor :x, :type
8
+
9
+ def can_contain?(element)
10
+ CodeData === element || SubFlow === element
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Konjak
2
+ class Body < StructuralElement
3
+ # children
4
+ attr_accessor :translation_units
5
+
6
+ def initialize(body)
7
+ @translation_units = body.children.select {|c| c.name == 'tu' }.map {|tu| TranslationUnit.new tu }
8
+ end
9
+
10
+ def can_contain?(element)
11
+ TranslationUnit === element
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module Konjak
2
+ class CodeData
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module Konjak
2
+ class Element
3
+ attr_accessor :child_elements
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ module Konjak
2
+ class EndPairedTag < InlineElement
3
+ # required attrs
4
+ attr_accessor :i
5
+
6
+ def can_contain?(element)
7
+ CodeData === element || SubFlow === element
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ module Konjak
2
+ class Header < StructuralElement
3
+ # required attrs
4
+ attr_accessor :creation_tool, :creation_tool_version, :seg_type, :o_tmf, :admin_lang, :src_lang, :data_type
5
+
6
+ # optional attrs
7
+ attr_accessor :o_encoding, :creation_date, :creation_id, :change_date, :change_id
8
+
9
+ # children
10
+ attr_accessor :notes, :user_defined_encodings, :properties
11
+
12
+ def initialize(header)
13
+ # required attrs
14
+ @creation_tool = header[:creationtool]
15
+ @creation_tool_version = header[:creationtoolversion]
16
+ @seg_type = header[:segtype]
17
+ @o_tmf = header[:"o-tmf"]
18
+ @admin_lang = header[:adminlang]
19
+ @src_lang = header[:srclang]
20
+ @data_type = header[:datatype]
21
+
22
+ # optional attrs
23
+ @o_encoding = header[:"o-encoding"]
24
+ @creation_date = header[:creationdate]
25
+ @creation_id = header[:creationid]
26
+ @change_date = header[:changedate]
27
+ @change_id = header[:changeid]
28
+
29
+ # children
30
+ @notes = header.children.select {|c| c.name == 'note' }.map {|n| Note.new n }
31
+ @user_defined_encodings = header.children.select {|c| c.name == 'ude' }.map {|n| UserDefinedEncoding.new n }
32
+ @properties = header.children.select {|c| c.name == 'prop' }.map {|n| Property.new n }
33
+ end
34
+
35
+ def can_contain?(element)
36
+ [Note, UserDefinedEncoding, Property].any? {|c| c === element }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ module Konjak
2
+ class Hilight < InlineElement
3
+ # optional attrs
4
+ attr_accessor :x, :type
5
+
6
+ # FIXME
7
+ # Text data,
8
+ # Zero, one or more of the following elements: <bpt>, <ept>, <it>, <ph>, and <hi>.
9
+ # They can be in any order, except that each <bpt> element must have a subsequent corresponding <ept> element.
10
+ def can_contain?(element)
11
+ [Text, BeginPairedTag, EndPairedTag, IsolatedTag, Placeholder, Hilight].any? {|c| c === element }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module Konjak
2
+ # content markup
3
+ class InlineElement < Element
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ module Konjak
2
+ class IsolatedTag < InlineElement
3
+ # required attrs
4
+ attr_accessor :pos
5
+
6
+ # optional attrs
7
+ attr_accessor :x, :type
8
+
9
+ def can_contain?(element)
10
+ CodeData === element || SubFlow === element
11
+ end
12
+ end
13
+ end
data/lib/konjak/map.rb ADDED
@@ -0,0 +1,23 @@
1
+ module Konjak
2
+ class Map < StructuralElement
3
+ # required attrs
4
+ attr_accessor :unicode
5
+
6
+ # optional attrs
7
+ attr_accessor :code, :entity, :substitution
8
+
9
+ def initialize(map)
10
+ @unicode = map[:unicode]
11
+ @code = map[:code]
12
+ @entity = map[:ent]
13
+ @substitution = map[:subst]
14
+ end
15
+
16
+ # FIXME:
17
+ # code, ent and subst. At least one of these attributes should be specified.
18
+ # If the code attribute is specified, the parent <ude> element must specify a base attribute.
19
+ def can_contain?(element)
20
+ false
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ module Konjak
2
+ class Note < StructuralElement
3
+ # optional attrs
4
+ attr_accessor :xml_lang, :o_encoding
5
+
6
+ # text
7
+ attr_accessor :text
8
+
9
+ def initialize(note)
10
+ @xml_lang = note["xml:lang"]
11
+ @o_encoding = note["o-encoding"]
12
+ @text = Text.new(note.text)
13
+ end
14
+
15
+ def can_contain?(element)
16
+ Text === element
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ require 'nokogiri'
2
+
3
+ module Konjak
4
+ class Parser
5
+ def parse(xml, gtt: false)
6
+ if gtt
7
+ # FIXME
8
+ xml = xml.gsub(/&amp;(#\d+|#x[0-9a-fA-F]+|[0-9a-zA-Z]+);/) { "&#{$1};" }
9
+ end
10
+ doc = Nokogiri::XML.parse(xml) {|c| c.noblanks }
11
+ Tmx.new doc.root
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ module Konjak
2
+ class Placeholder < InlineElement
3
+ # optional attrs
4
+ attr_accessor :x, :type, :assoc
5
+
6
+ def can_contain?(element)
7
+ CodeData === element || SubFlow === element
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ module Konjak
2
+ class Property < StructuralElement
3
+ # required attrs
4
+ attr_accessor :type
5
+
6
+ # optional attrs
7
+ attr_accessor :xml_lang, :o_encoding
8
+
9
+ # child
10
+ attr_accessor :text
11
+
12
+ def initialize(property)
13
+ @type = property[:type]
14
+ @xml_lang = property['xml:lang']
15
+ @o_encoding = property['o-encoding']
16
+ @text = Text.new(property.text)
17
+ end
18
+
19
+ def can_contain?(element)
20
+ # FIXME
21
+ # Tool-specific data or text.
22
+ Text === element
23
+ end
24
+
25
+ def unpublished?
26
+ type.start_with? 'x-'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ module Konjak
2
+ # container
3
+ class Segment < StructuralElement
4
+ # children
5
+ attr_accessor :text
6
+
7
+ def initialize(seg)
8
+ @text = Text.new(seg.text)
9
+ end
10
+
11
+ def can_contain?(element)
12
+ [Text, BeginPairedTag, EndPairedTag, IsolatedTag, Placeholder, Highlight].any? {|c| c === element }
13
+ end
14
+ end
15
+ end