natsukantou 0.1.0 → 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: a75166acdb08099edebb39e02f78ca3bf04af192e3233f71067f1178950e964b
4
- data.tar.gz: f5af8a9a4df6a4c82e61b7bef67ec6beb3bcac6b49db2dbe7ae46ce8385084ef
3
+ metadata.gz: c57ca088a6f550986f0d3d4244a5ec85ba4271b8034b76d9c389ebbce5a6219d
4
+ data.tar.gz: 6ef86b82bf6ba37ef22e0235aa4219d3c0e91f6578ea783a53b808ac5753e9ef
5
5
  SHA512:
6
- metadata.gz: 3079ead2000c24c0c6e06156e6e775e56e233c11fe06a851e655d2b2197bb133b855ae901d8749520f9661ff37e89d759e318c2ad1e9516097fd7c14c0a6d125
7
- data.tar.gz: 6612f175de419c560af5e221bbf3f9e074692a5fee2f3907dd800bddc7404db932201ffe69e191fadc87169ae8a6ce7df93700c48510dcbbfc96fc2bdbc28b1f
6
+ metadata.gz: b405772e21b46d959122c0e9e4c1f24eb55bc75b34a3b5b64e21c13f645766a47cfcf8d26a5bb5e307d458c2aca11d34a3f75228464dda7d82e020c277eac31a
7
+ data.tar.gz: 59cc9abd5cacf466de075265b9a30d867cf8cec019d9b272382975ab6dd08f9d5fc4c7e9be37e4cfae68583af90b87a91ae398c1378ca5c15077b7c3cc05892a
data/.rubocop.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.7
3
+ Exclude:
4
+ - 'lib/monkey_patch/*'
3
5
 
4
6
  Style/StringLiterals:
5
7
  Enabled: false
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,14 @@
1
+ module Middleware
2
+ class Builder
3
+ def use(middleware, *args, **kwargs, &block)
4
+ if middleware.kind_of?(Builder)
5
+ # Merge in the other builder's stack into our own
6
+ self.stack.concat(middleware.stack)
7
+ else
8
+ self.stack << [middleware, args, kwargs, block]
9
+ end
10
+
11
+ self
12
+ end
13
+ end
14
+ end
@@ -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
@@ -0,0 +1,32 @@
1
+ module Middleware
2
+ class Runner
3
+ protected
4
+
5
+ def build_call_chain(stack)
6
+ stack.reverse.inject(EMPTY_MIDDLEWARE) do |next_middleware, current_middleware|
7
+ # Unpack the actual item
8
+ klass, args, kwargs, block = current_middleware
9
+
10
+ # Default the arguments to an empty array. Otherwise in Ruby 1.8
11
+ # a `nil` args will actually pass `nil` into the class. Not what
12
+ # we want!
13
+ args ||= []
14
+
15
+ if klass.is_a?(Class)
16
+ # If the klass actually is a class, then instantiate it with
17
+ # the app and any other arguments given.
18
+ klass.new(next_middleware, *args, **kwargs, &block)
19
+ elsif klass.respond_to?(:call)
20
+ # Make it a lambda which calls the item then forwards up
21
+ # the chain.
22
+ lambda do |env|
23
+ klass.call(env)
24
+ next_middleware.call(env)
25
+ end
26
+ else
27
+ raise "Invalid middleware, doesn't respond to `call`: #{klass.inspect}"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ 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.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/natsukantou.rb CHANGED
@@ -2,7 +2,10 @@
2
2
 
3
3
  require 'logger'
4
4
  require 'middleware'
5
+ require_relative "monkey_patch/builder"
5
6
  require 'oga'
7
+ require_relative "monkey_patch/oga_document"
8
+ require_relative "monkey_patch/runner"
6
9
 
7
10
  require_relative "natsukantou/version"
8
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.0
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
@@ -124,6 +124,9 @@ files:
124
124
  - README.md
125
125
  - Rakefile
126
126
  - exe/natsukantou
127
+ - lib/monkey_patch/builder.rb
128
+ - lib/monkey_patch/oga_document.rb
129
+ - lib/monkey_patch/runner.rb
127
130
  - lib/natsukantou.rb
128
131
  - lib/natsukantou/deep_l.rb
129
132
  - lib/natsukantou/handle_ruby_markup.rb