jekyll-chatgpt-translate 0.0.34 → 0.0.36

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: f382405857f236d2b59e129760b41f49efd2815b8ebcffab87bf8d92f89c40a6
4
- data.tar.gz: 65897a2650943260e087213ae42baaee18cf34d6a163d90e7ba5348dd3c46a78
3
+ metadata.gz: 297a8b46d6e777cfc060956768b7807acf69b4fa1f52c65f4fbed3a0084c6268
4
+ data.tar.gz: a81769edc12e05ad6aecb14b31bdec6ee2dcdc797ee27e6e3d8b03f8dc4c975a
5
5
  SHA512:
6
- metadata.gz: ebbc5c28294a0a69b9a0cf3ee08ee7cf84ff9eb609e8efde9549b02f11e653236deebe371e6506fefa78156a229c8deeaab7f0af13e4caa0f1e108205971ec51
7
- data.tar.gz: 7de6d8bd8e966b3b0a4c322092dbaf0e38620e56d89ff1400f48f7a2199475133f095d18bec467bbe01c8fb12b63f13f982cc86d0376fe458631db22a82c6ecf
6
+ metadata.gz: ebb2e3a583e2d2e0282b6d89ad09479aebd33c62b5fa20ae681d89a2b864dfb9f4af6e2d293e8c3817225331e1a47a6196014b9ee4f2b9080f04b3146a1fe738
7
+ data.tar.gz: dcf28aec1603c0b2cece95fe06085e3fe028e288bf1ca60a3388405395906df5a5a123dc0b23e8146bc3689ec82d0b96baece8744f8c003b0f89b87a43ed5e3b
data/Gemfile CHANGED
@@ -25,11 +25,11 @@
25
25
  source 'https://rubygems.org'
26
26
  gemspec
27
27
 
28
- gem 'cucumber', '9.0.1', require: false
28
+ gem 'cucumber', '9.0.2', require: false
29
29
  gem 'kramdown-parser-gfm', '1.1.0', require: false
30
30
  gem 'minitest', '5.19.0', require: false
31
31
  gem 'rake', '13.0.6', require: false
32
- gem 'rubocop', '1.56.2', require: false
33
- gem 'rubocop-rspec', '2.23.2', require: false
32
+ gem 'rubocop', '1.56.4', require: false
33
+ gem 'rubocop-rspec', '2.24.0', require: false
34
34
  gem 'simplecov', '0.22.0', require: false
35
35
  gem 'webmock', '3.19.1', require: false
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
29
29
  s.required_ruby_version = '>= 2.6'
30
30
  s.name = 'jekyll-chatgpt-translate'
31
- s.version = '0.0.34'
31
+ s.version = '0.0.36'
32
32
  s.license = 'MIT'
33
33
  s.summary = 'Translate Jekyll Pages Through ChatGPT'
34
34
  s.description = [
@@ -42,6 +42,7 @@ Gem::Specification.new do |s|
42
42
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
43
43
  s.rdoc_options = ['--charset=UTF-8']
44
44
  s.extra_rdoc_files = %w[README.md LICENSE.txt]
45
+ s.add_runtime_dependency 'humanize', '>= 2'
45
46
  s.add_runtime_dependency 'iri', '>= 0'
46
47
  s.add_runtime_dependency 'iso-639', '>= 0'
47
48
  s.add_runtime_dependency 'jekyll', '>= 3'
@@ -52,34 +52,55 @@ class GptTranslate::ChatGPT
52
52
  end
53
53
 
54
54
  def translate(markdown, min: 32)
55
- GptTranslate::Pars.new(markdown).to_a.map do |par|
55
+ pars = GptTranslate::Pars.new(markdown).to_a
56
+ ready = []
57
+ later = []
58
+ pars.each_with_index do |pa, i|
59
+ par = pa.dup
56
60
  par.strip!
57
61
  if @source == @target
58
62
  Jekyll.logger.debug("No need to translate from #{@source.inspect} to #{@target.inspect}: #{par.inspect}")
59
- par
63
+ ready[i] = par
60
64
  elsif par.length < min
61
65
  Jekyll.logger.debug("Not translating this, b/c too short: #{par.inspect}")
62
- par
66
+ ready[i] = par
63
67
  elsif par.start_with?('```')
64
68
  Jekyll.logger.debug("Not translating this code block: #{par.inspect}")
65
- par
66
- elsif par =~ /^[^\p{Alnum}\*'"\[]/
67
- Jekyll.logger.debug("Not translating this, b/c it's not a plain text: #{par.inspect}")
68
- par
69
+ ready[i] = par
69
70
  elsif @key.empty?
70
- par
71
+ ready[i] = par
72
+ elsif par.start_with?('> ')
73
+ ready[i] = "> #{translate_par(par[2..])}"
71
74
  elsif par.start_with?('* ')
72
- "* #{translate_par(par[2..])}"
75
+ ready[i] = "* #{translate_par(par[2..])}"
73
76
  elsif par =~ /^[0-9]+\. /
74
- "1. #{translate_par(par.split('.', 2)[1])}"
77
+ ready[i] = "1. #{translate_par(par.split('.', 2)[1])}"
78
+ elsif par =~ /^[^\p{Alnum}\*'"\[]/
79
+ Jekyll.logger.debug("Not translating this, b/c it's not a plain text: #{par.inspect}")
80
+ ready[i] = par
75
81
  else
76
- translate_par(par)
82
+ later[i] = par
77
83
  end
78
- end.join("\n\n")
84
+ end
85
+ accum = []
86
+ (0..pars.length).each do |i|
87
+ if !ready[i].nil? && !accum.empty? && later[i].nil?
88
+ ready[i - 1] = translate_pars(accum)
89
+ accum = []
90
+ next
91
+ end
92
+ accum << later[i] unless later[i].nil?
93
+ end
94
+ ready[pars.length - 1] = translate_pars(accum) unless accum.empty?
95
+ ready.join("\n\n")
79
96
  end
80
97
 
81
98
  private
82
99
 
100
+ def translate_pars(accum)
101
+ translate_par(accum.join("\n\n"))
102
+ end
103
+
83
104
  def translate_par(par)
84
105
  client = OpenAI::Client.new(access_token: @key)
85
106
  if @@models_printed
@@ -63,7 +63,7 @@ class GptTranslate::Generator < Jekyll::Generator
63
63
  site.posts.docs.shuffle.each_with_index do |doc, pos|
64
64
  plain = GptTranslate::Plain.new(doc.content).to_s
65
65
  config['targets'].each do |target|
66
- start = Time.now
66
+ pstart = Time.now
67
67
  link = GptTranslate::Permalink.new(doc, target['permalink']).to_path
68
68
  lang = target['language']
69
69
  raise 'Language must be defined for each target' if target.nil?
@@ -129,7 +129,7 @@ class GptTranslate::Generator < Jekyll::Generator
129
129
  added = true
130
130
  translated += 1
131
131
  Jekyll.logger.info("Translated via ChatGPT \
132
- in #{(Time.now - start).round(2)}s: #{path} (#{File.size(path)} bytes)")
132
+ in #{(Time.now - pstart).round(2)}s: #{path} (#{File.size(path)} bytes)")
133
133
  end
134
134
  next unless added
135
135
  doc.data['chatgpt-translate'] ||= {}
@@ -55,8 +55,6 @@ class GptTranslate::Plain
55
55
  # Motivated by https://github.com/vmg/redcarpet/blob/master/lib/redcarpet/render_strip.rb
56
56
  class Strip < Redcarpet::Render::Base
57
57
  %i[
58
- block_quote
59
- block_html
60
58
  autolink
61
59
  underline
62
60
  triple_emphasis
@@ -78,6 +76,10 @@ class GptTranslate::Plain
78
76
  code
79
77
  end
80
78
 
79
+ def block_quote(txt)
80
+ "> #{txt}"
81
+ end
82
+
81
83
  def emphasis(txt)
82
84
  "*#{txt}*"
83
85
  end
@@ -100,6 +102,10 @@ class GptTranslate::Plain
100
102
  "![#{alt}](#{link} \"#{title}\")"
101
103
  end
102
104
 
105
+ def block_html(html)
106
+ "#{html}\n"
107
+ end
108
+
103
109
  def raw_html(html)
104
110
  html
105
111
  end
@@ -23,6 +23,7 @@
23
23
  # SOFTWARE.
24
24
 
25
25
  require 'iso-639'
26
+ require 'humanize'
26
27
 
27
28
  # The module we are in.
28
29
  module GptTranslate; end
@@ -47,8 +48,11 @@ class GptTranslate::Prompt
47
48
  raise "Unknown source language ISO-639 code: #{@source.inspect}" if from.nil?
48
49
  to = ISO_639.find_by_code(@target)
49
50
  raise "Unknown source language ISO-639 code: #{@target.inspect}" if to.nil?
51
+ md = @par
52
+ parts = md.split("\n\n")
53
+ label = parts.size > 1 ? "#{parts.size.humanize(locale: :en)} Markdown paragraphs" : 'Markdown paragraph'
50
54
  head = [
51
- 'Please, translate the following Markdown paragraph from ',
55
+ "Please, translate the following #{label} from ",
52
56
  from[3],
53
57
  ' to ',
54
58
  to[3],
@@ -23,5 +23,5 @@
23
23
  # SOFTWARE.
24
24
 
25
25
  module GptTranslate
26
- VERSION = '0.0.34'
26
+ VERSION = '0.0.36'
27
27
  end
data/test/test_chatgpt.rb CHANGED
@@ -64,9 +64,9 @@ class GptTranslate::ChatGPTTest < Minitest::Test
64
64
  chat = GptTranslate::ChatGPT.new('', 'foo', 'xx', 'xx')
65
65
  chat.translate(
66
66
  "
67
- How are you, my friend?
67
+ How are you, my friend? This text must be translated through ChatGPT.
68
68
 
69
- Read this Java code:
69
+ Read this Java code (this paragraph must also be translated through ChatGPT):
70
70
 
71
71
  ```
72
72
  System.out.println(\"Hello, dude!\");
data/test/test_pars.rb CHANGED
@@ -37,6 +37,10 @@ class GptTranslate::ParsTest < Minitest::Test
37
37
  assert_equal(2, GptTranslate::Pars.new("\n\n\nHello,\n\n**world**\n!\n\n").to_a.size)
38
38
  end
39
39
 
40
+ def test_returns_unfrozen_strings
41
+ GptTranslate::Pars.new("Hi, world!\n\n```\ntest\n```\n\nBye\n").to_a.map(&:strip!)
42
+ end
43
+
40
44
  def test_understands_code_block
41
45
  pars = GptTranslate::Pars.new("Hello:\n\n```java\na\n\nb\n\nc\n```\n\nz").to_a
42
46
  assert_equal(3, pars.size)
data/test/test_plain.rb CHANGED
@@ -85,6 +85,13 @@ class GptTranslate::PlainTest < Minitest::Test
85
85
  )
86
86
  end
87
87
 
88
+ def test_quote
89
+ assert_equal(
90
+ "He said this:\n\n> Life is great!",
91
+ GptTranslate::Plain.new("He said this:\n\n\n> Life is great!\n\n").to_s
92
+ )
93
+ end
94
+
88
95
  def test_code
89
96
  assert_equal(
90
97
  'Hello, `Java`!',
@@ -136,6 +143,11 @@ class GptTranslate::PlainTest < Minitest::Test
136
143
  assert_equal('<img src="a"/>', GptTranslate::Plain.new('<img src="a"/>').to_s)
137
144
  end
138
145
 
146
+ def test_html_hr
147
+ md = "First\n\n<hr/>\n\nsecond!"
148
+ assert_equal(md, GptTranslate::Plain.new(md).to_s)
149
+ end
150
+
139
151
  def test_liquid_tags
140
152
  assert_equal(
141
153
  'Hello, !',
data/test/test_prompt.rb CHANGED
@@ -52,4 +52,10 @@ class GptTranslate::PromptTest < Minitest::Test
52
52
  GptTranslate::Prompt.new('Hello, Jeff!', 'en', 'zh').to_s
53
53
  )
54
54
  end
55
+
56
+ def test_multiple_paragraphs
57
+ assert(
58
+ GptTranslate::Prompt.new("Hello,\n\nJeff!", 'en', 'zh').to_s.include?("\"Hello,\n\nJeff!\"")
59
+ )
60
+ end
55
61
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-chatgpt-translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.34
4
+ version: 0.0.36
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-06 00:00:00.000000000 Z
11
+ date: 2023-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: humanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: iri
15
29
  requirement: !ruby/object:Gem::Requirement