natsukantou 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40e03ddc9cd827e3047661cab129984d98c4feb300a4ba9a61980dfba440a74a
4
- data.tar.gz: aeff37004a2afb7094f391bb25ffeaa2696979382f6170c28b72a600aaee8cae
3
+ metadata.gz: c57ca088a6f550986f0d3d4244a5ec85ba4271b8034b76d9c389ebbce5a6219d
4
+ data.tar.gz: 6ef86b82bf6ba37ef22e0235aa4219d3c0e91f6578ea783a53b808ac5753e9ef
5
5
  SHA512:
6
- metadata.gz: 3cea30f5d6074a7dfd075d6489bbe8cbbbaf4a053a3aa3645d9540f206e526834ef301710306949676917c94c7aef3b519c07ca8b3ed5ac8fe870606b2131108
7
- data.tar.gz: 8cf2cbd8fdc20cf6c3a24deadbf7f98ebb0cb5dd242c4d013182d264bcd9d482668ba263eb89bb01c8c8245abebc7dc2734941c0d2b32130dbe615f0fce24e67
6
+ metadata.gz: b405772e21b46d959122c0e9e4c1f24eb55bc75b34a3b5b64e21c13f645766a47cfcf8d26a5bb5e307d458c2aca11d34a3f75228464dda7d82e020c277eac31a
7
+ data.tar.gz: 59cc9abd5cacf466de075265b9a30d867cf8cec019d9b272382975ab6dd08f9d5fc4c7e9be37e4cfae68583af90b87a91ae398c1378ca5c15077b7c3cc05892a
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,64 @@
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
+ node.after(node_other)
50
+ end
51
+ end
52
+ end
53
+
54
+ class Generator
55
+ def on_text(node, output)
56
+ if @html_mode && (parent = node.parent) && parent.literal_html_name?
57
+ output << node.text.encode('utf-8')
58
+ else
59
+ output << Entities.encode(node.text).encode('utf-8')
60
+ end
61
+ end
62
+ end
63
+ end
64
+ 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.2"
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.2
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: 2022-10-01 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