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 +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/konjak +16 -0
- data/konjak.gemspec +29 -0
- data/lib/konjak.rb +48 -0
- data/lib/konjak/begin_paired_tag.rb +13 -0
- data/lib/konjak/body.rb +14 -0
- data/lib/konjak/code_data.rb +4 -0
- data/lib/konjak/element.rb +5 -0
- data/lib/konjak/end_paired_tag.rb +10 -0
- data/lib/konjak/header.rb +39 -0
- data/lib/konjak/highlight.rb +14 -0
- data/lib/konjak/inline_element.rb +5 -0
- data/lib/konjak/isolated_tag.rb +13 -0
- data/lib/konjak/map.rb +23 -0
- data/lib/konjak/note.rb +19 -0
- data/lib/konjak/parser.rb +14 -0
- data/lib/konjak/placeholder.rb +10 -0
- data/lib/konjak/property.rb +29 -0
- data/lib/konjak/segment.rb +15 -0
- data/lib/konjak/structural_element.rb +5 -0
- data/lib/konjak/sub_flow.rb +15 -0
- data/lib/konjak/text.rb +15 -0
- data/lib/konjak/tmx.rb +27 -0
- data/lib/konjak/translation_unit.rb +40 -0
- data/lib/konjak/translation_unit_variant.rb +40 -0
- data/lib/konjak/translator.rb +55 -0
- data/lib/konjak/unknown_tag.rb +11 -0
- data/lib/konjak/user_defined_encoding.rb +24 -0
- data/lib/konjak/version.rb +3 -0
- data/spec/fixtures/gtt.tmx +92 -0
- data/spec/fixtures/sample.tmx +68 -0
- data/spec/konjak_parse_spec.rb +179 -0
- data/spec/konjak_spec.rb +5 -0
- data/spec/konjak_translate_spec.rb +27 -0
- data/spec/spec_helper.rb +3 -0
- metadata +205 -0
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
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
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
|
data/lib/konjak/body.rb
ADDED
@@ -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,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
|
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
|
data/lib/konjak/note.rb
ADDED
@@ -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(/&(#\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,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
|