jekyll-chatgpt-translate 0.0.35 → 0.0.37

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: 841ab37ebdb5e4049f301883bd5d3c411dbf7719824c52f8fa3f3604aebb0109
4
- data.tar.gz: e553ef7cdf4564adba5128bf9bb7eea90c59f7724fa72986427a5e31e7bf2bb2
3
+ metadata.gz: c0f1eb7be508b40853f8fc04d94cd0c6f30ddfe2be81b6d3fc7f6a45101d10b1
4
+ data.tar.gz: cb4d6e55f8d93e00307135f52cfba873a7ece62a01cb785f0d0e4ada293aaa20
5
5
  SHA512:
6
- metadata.gz: a9a6c85190c260ba4770691f4810cbe775f6ae3b2aafba67f5f0081fb540e2a86dc817ae65fccb857643e39d446e4c62b2a98dabe8ce43a46df8a6786ac59b1b
7
- data.tar.gz: dba3c7a5bd214a8f970fa623d7dc46779928ddf334fa10f3ca42cfafb61f9a7fae580cd86ed0f11d55f1a08082e0f04eead95ab098481231940e94216cdacaa0
6
+ metadata.gz: 053d40f293680a7b4ec3f78f5b09b1ddef17183b1dbfce93ab10fffca3cbb1b1384644e4da3377c5c1abc0ff96fdb1b55d8f58d7e30788aa7ddeba44dc6e0d65
7
+ data.tar.gz: 0cc7dd560f374a400b8c8ff2b7782daca793a2db48eac1fc698806506f0341713751241a2feb739d485145efbe57ba6c5d2d43d188208fa0e8142993447a6928
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.35'
31
+ s.version = '0.0.37'
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'
@@ -51,36 +51,62 @@ class GptTranslate::ChatGPT
51
51
  @target = target
52
52
  end
53
53
 
54
- def translate(markdown, min: 32)
55
- GptTranslate::Pars.new(markdown).to_a.map do |pa|
54
+ def translate(markdown, min: 32, window_length: 2000)
55
+ pars = GptTranslate::Pars.new(markdown).to_a
56
+ ready = []
57
+ later = []
58
+ pars.each_with_index do |pa, i|
56
59
  par = pa.dup
57
60
  par.strip!
58
61
  if @source == @target
59
62
  Jekyll.logger.debug("No need to translate from #{@source.inspect} to #{@target.inspect}: #{par.inspect}")
60
- par
63
+ ready[i] = par
61
64
  elsif par.length < min
62
65
  Jekyll.logger.debug("Not translating this, b/c too short: #{par.inspect}")
63
- par
66
+ ready[i] = par
64
67
  elsif par.start_with?('```')
65
68
  Jekyll.logger.debug("Not translating this code block: #{par.inspect}")
66
- par
67
- elsif par =~ /^[^\p{Alnum}\*'"\[]/
68
- Jekyll.logger.debug("Not translating this, b/c it's not a plain text: #{par.inspect}")
69
- par
69
+ ready[i] = par
70
70
  elsif @key.empty?
71
- par
71
+ ready[i] = par
72
+ elsif par.start_with?('> ')
73
+ ready[i] = "> #{translate_par(par[2..])}"
72
74
  elsif par.start_with?('* ')
73
- "* #{translate_par(par[2..])}"
75
+ ready[i] = "* #{translate_par(par[2..])}"
74
76
  elsif par =~ /^[0-9]+\. /
75
- "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
76
81
  else
77
- translate_par(par)
82
+ later[i] = par
83
+ end
84
+ end
85
+ out = []
86
+ i = 0
87
+ while i < pars.length
88
+ unless ready[i].nil?
89
+ out << ready[i]
90
+ i += 1
91
+ next
92
+ end
93
+ accum = []
94
+ until later[i].nil?
95
+ accum << later[i]
96
+ break if accum.join.split.count > window_length
97
+ i += 1
78
98
  end
79
- end.join("\n\n")
99
+ out << translate_pars(accum)
100
+ end
101
+ out.join("\n\n")
80
102
  end
81
103
 
82
104
  private
83
105
 
106
+ def translate_pars(accum)
107
+ translate_par(accum.join("\n\n"))
108
+ end
109
+
84
110
  def translate_par(par)
85
111
  client = OpenAI::Client.new(access_token: @key)
86
112
  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.35'
26
+ VERSION = '0.0.37'
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!\");
@@ -76,7 +76,8 @@ class GptTranslate::ChatGPTTest < Minitest::Test
76
76
 
77
77
  This is it.
78
78
  ",
79
- min: 40
79
+ min: 40,
80
+ window_length: 10
80
81
  )
81
82
  end
82
83
 
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.35
4
+ version: 0.0.37
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-10-02 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