natsukantou 0.1.0 → 0.1.2
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 +4 -4
- data/.rubocop.yml +2 -0
- data/README.md +6 -2
- data/lib/monkey_patch/builder.rb +14 -0
- data/lib/monkey_patch/oga_document.rb +64 -0
- data/lib/monkey_patch/runner.rb +32 -0
- data/lib/natsukantou/deep_l.rb +2 -0
- data/lib/natsukantou/minhon.rb +2 -0
- data/lib/natsukantou/setup/config_load_or_prompt.rb +2 -0
- data/lib/natsukantou/utility/env.rb +4 -1
- data/lib/natsukantou/utility/language_code.rb +5 -3
- data/lib/natsukantou/version.rb +1 -1
- data/lib/natsukantou.rb +3 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c57ca088a6f550986f0d3d4244a5ec85ba4271b8034b76d9c389ebbce5a6219d
|
4
|
+
data.tar.gz: 6ef86b82bf6ba37ef22e0235aa4219d3c0e91f6578ea783a53b808ac5753e9ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b405772e21b46d959122c0e9e4c1f24eb55bc75b34a3b5b64e21c13f645766a47cfcf8d26a5bb5e307d458c2aca11d34a3f75228464dda7d82e020c277eac31a
|
7
|
+
data.tar.gz: 59cc9abd5cacf466de075265b9a30d867cf8cec019d9b272382975ab6dd08f9d5fc4c7e9be37e4cfae68583af90b87a91ae398c1378ca5c15077b7c3cc05892a
|
data/.rubocop.yml
CHANGED
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
|
-
|
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
|
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
|
data/lib/natsukantou/deep_l.rb
CHANGED
data/lib/natsukantou/minhon.rb
CHANGED
@@ -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
|
-
|
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
|
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?(
|
17
|
-
other
|
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
|
|
data/lib/natsukantou/version.rb
CHANGED
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.
|
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-
|
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
|