natsukantou 0.1.1 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40e03ddc9cd827e3047661cab129984d98c4feb300a4ba9a61980dfba440a74a
4
- data.tar.gz: aeff37004a2afb7094f391bb25ffeaa2696979382f6170c28b72a600aaee8cae
3
+ metadata.gz: 1b299497adbec2c0446350fc1085c4f17c6a9ebe165246dbba07fef1914348e5
4
+ data.tar.gz: 8164f3a25f80bc7fde73fd8062563ecbdf34f5abc2f5b1b594e78a219bafe2b8
5
5
  SHA512:
6
- metadata.gz: 3cea30f5d6074a7dfd075d6489bbe8cbbbaf4a053a3aa3645d9540f206e526834ef301710306949676917c94c7aef3b519c07ca8b3ed5ac8fe870606b2131108
7
- data.tar.gz: 8cf2cbd8fdc20cf6c3a24deadbf7f98ebb0cb5dd242c4d013182d264bcd9d482668ba263eb89bb01c8c8245abebc7dc2734941c0d2b32130dbe615f0fce24e67
6
+ metadata.gz: 34b63359b8441b651ffc071c841eae8cb74a29b2c7ea0fb2a717794a16de5dcc2fa8b94c5abf9e59f600cc49c7c9716a668e456790e01bf709d70a39737ef435
7
+ data.tar.gz: 951610545756fbe6b1b8384ba358b64b32f471ec6e5574f270b88f166ec70ef575270ac90b4ec2624f904d3d8bfb6ffe7b977a3aa4930ac82fc6ed04f1d90d70
data/CHANGELOG.md CHANGED
@@ -3,3 +3,17 @@
3
3
  ## [0.1.0] - 2022-07-05
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.1] - 2022-09-04
8
+
9
+ - Fix compatibility to Ruby 3
10
+
11
+ ## [0.1.2] - 2022-10-01
12
+
13
+ - Document interlacing
14
+ - Set lang attribute after translation
15
+ - Fix Oga error
16
+
17
+ ## [0.1.3] - 2023-02-27
18
+
19
+ - Skip interlacing if nodes' text() are identical
data/README.md CHANGED
@@ -18,13 +18,17 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
18
 
19
19
  ## Usage
20
20
 
21
- If you want to just use it as a executable, run `natsukantou [XML_FILE]`.
21
+ To run it as a command:
22
+
23
+ $ natsukantou [XML_FILE]
22
24
 
23
25
  It's a wizard that guides you through setting up a translator configuration.
24
26
 
25
27
  Then it will translate the XML document.
26
28
 
27
- If you choose to save the config (`translator_config.rb`), next time you can reuse it by calling `natsukantou -c [CONFIG_FILE] [XML_FILE]`.
29
+ If you choose to save the config (`translator_config.rb`), next time you can reuse it by calling
30
+
31
+ $ natsukantou -c [CONFIG_FILE] [XML_FILE]
28
32
 
29
33
  ## Feature
30
34
 
@@ -0,0 +1,66 @@
1
+ module Oga
2
+ module XML
3
+ class Element
4
+ def lang
5
+ get('lang')
6
+ end
7
+
8
+ def lang=(lang)
9
+ if lang
10
+ set('lang', lang)
11
+ set('xml:lang', lang) if attribute('xml:lang')
12
+ else
13
+ unset('lang')
14
+ unset('xml:lang')
15
+ end
16
+ end
17
+ end
18
+
19
+ class Document
20
+ def html_node
21
+ at_xpath('html')
22
+ end
23
+
24
+ def lang
25
+ html_node&.lang
26
+ end
27
+
28
+ # TODO: handle non-HTML
29
+ def lang=(lang)
30
+ html_node&.lang = lang
31
+ end
32
+
33
+ # @param other [Oga::XML::Document] other to interlace from.
34
+ # Note this will have side-effects on it, so be aware not to persist it.
35
+ # @param level [String] CSS selector to interlace
36
+ def interlace(other, level:)
37
+ nodes = css(level).to_a
38
+ nodes_other = other.css(level).to_a
39
+
40
+ root_lang = lang
41
+ root_lang_other = other.lang
42
+
43
+ nodes.each.with_index do |node, i|
44
+ node_other = nodes_other[i]
45
+
46
+ node.lang = root_lang
47
+ node_other.lang = root_lang_other
48
+
49
+ next if node.text() == nodes_other[i].text()
50
+
51
+ node.after(node_other)
52
+ end
53
+ end
54
+ end
55
+
56
+ class Generator
57
+ def on_text(node, output)
58
+ if @html_mode && (parent = node.parent) && parent.literal_html_name?
59
+ output << node.text.encode('utf-8')
60
+ else
61
+ output << Entities.encode(node.text).encode('utf-8')
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -47,6 +47,8 @@ module Natsukantou
47
47
 
48
48
  env[:dom] = dom(result)
49
49
 
50
+ env[:dom].lang = env[:lang_to].code
51
+
50
52
  @app.call(env)
51
53
  end
52
54
  end
@@ -54,6 +54,8 @@ module Natsukantou
54
54
  env[:dom] = dom(translated_xml)
55
55
  end
56
56
 
57
+ env[:dom].lang = env[:lang_to].code
58
+
57
59
  @app.call(env)
58
60
  end
59
61
 
@@ -20,6 +20,8 @@ module Natsukantou
20
20
 
21
21
  Kernel.eval(config_content)
22
22
  end
23
+
24
+ NatsukantouTranslator
23
25
  end
24
26
  end
25
27
  end
@@ -14,7 +14,10 @@ module Natsukantou
14
14
  def initialize(env)
15
15
  # Convert languages
16
16
  [:lang_from, :lang_to].each do |key|
17
- env[key] = LanguageCode.new(env.fetch(key))
17
+ lang = env.fetch(key)
18
+ next if lang.is_a?(LanguageCode)
19
+
20
+ env[key] = LanguageCode.new(lang)
18
21
  end
19
22
 
20
23
  # Required keys
@@ -7,14 +7,16 @@ module Natsukantou
7
7
  @main, @sub = @code.split('-')
8
8
 
9
9
  if @main.length != 2
10
- raise 'Language code shoul be in the form of xx or xx-yy'
10
+ raise 'Language code should be in the form of xx or xx-yy'
11
11
  end
12
12
  end
13
13
 
14
14
  attr_reader :code, :main, :sub
15
15
 
16
- def is?(other_code)
17
- other = self.class.new(other_code)
16
+ def is?(other)
17
+ if !other.is_a?(self.class)
18
+ other = self.class.new(other)
19
+ end
18
20
 
19
21
  return false if other.main != main
20
22
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Natsukantou
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/natsukantou.rb CHANGED
@@ -3,8 +3,9 @@
3
3
  require 'logger'
4
4
  require 'middleware'
5
5
  require_relative "monkey_patch/builder"
6
- require_relative "monkey_patch/runner"
7
6
  require 'oga'
7
+ require_relative "monkey_patch/oga_document"
8
+ require_relative "monkey_patch/runner"
8
9
 
9
10
  require_relative "natsukantou/version"
10
11
  require_relative "natsukantou/setup/registry"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: natsukantou
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - lulalala
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-04 00:00:00.000000000 Z
11
+ date: 2023-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleware
@@ -125,6 +125,7 @@ files:
125
125
  - Rakefile
126
126
  - exe/natsukantou
127
127
  - lib/monkey_patch/builder.rb
128
+ - lib/monkey_patch/oga_document.rb
128
129
  - lib/monkey_patch/runner.rb
129
130
  - lib/natsukantou.rb
130
131
  - lib/natsukantou/deep_l.rb
@@ -166,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
167
  - !ruby/object:Gem::Version
167
168
  version: '0'
168
169
  requirements: []
169
- rubygems_version: 3.1.6
170
+ rubygems_version: 3.4.6
170
171
  signing_key:
171
172
  specification_version: 4
172
173
  summary: human language translation library for XML documents