jekyll-chatgpt-translate 0.0.8 → 0.0.10

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: ae5a29abd8e4cb757fa40d7b50c999a4453901b1be9a597f23e560fa3900416d
4
- data.tar.gz: e1769e8544c657ad086c462a1d32652edd434ac26509b064d1441f8beb35ea28
3
+ metadata.gz: 38924a750a0276a65b25263ec22b74f92a2ccd652636a65ec0cf90a46bb45983
4
+ data.tar.gz: '058007955befb70a3323a3c68863b09621e63d52da65424383644c7751b47861'
5
5
  SHA512:
6
- metadata.gz: 97ac19fd067d197faf5fb5c2b541a0641b56c0ca0fbbef3f9fbfe02c4efacb89657fc9d9011bd10081ce6221807eba792be5ed8f12e9617a197c36574f668df4
7
- data.tar.gz: 2f5251871b78f3052f33b7c1778318d89945b9874295fc36da9ae8f8d7fb34a720bb475210f0e1546ccf18c7a136ee2ed5e24d70aaa4630d5fe7e1f707d95bac
6
+ metadata.gz: 33e22f1a3f0055308860c966a3ea824bd336f2ef122902bc10e533692649bb55c13d422e8b0bcde148064646745865e403830458c61767ce072ba7cd4329f07f
7
+ data.tar.gz: 546dfbd3042bbf630ec7647096a879fc82d32368714057189b543abb6154ac6dca0fcf504220259ad9ca27182f52e4c432fcb7eeffbb535a93513004109f6c2e
data/features/cli.feature CHANGED
@@ -8,6 +8,7 @@ Feature: Simple site building
8
8
  plugins:
9
9
  - jekyll-chatgpt-translate
10
10
  chatgpt-translate:
11
+ api_key_file: the-file-is-absent.txt
11
12
  source: en
12
13
  layout: translated
13
14
  targets:
@@ -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.8'
31
+ s.version = '0.0.10'
32
32
  s.license = 'MIT'
33
33
  s.summary = 'Translate Jekyll Pages Through ChatGPT'
34
34
  s.description = [
@@ -57,7 +57,7 @@ class GptTranslate::Generator < Jekyll::Generator
57
57
  plain = GptTranslate::Plain.new(doc.content).to_s
58
58
  config['targets'].each do |target|
59
59
  link = GptTranslate::Permalink.new(doc, target['permalink']).to_path
60
- next if GptTranslate::Ping.new(site, link).exists?
60
+ next if GptTranslate::Ping.new(site, link).found?(doc.path)
61
61
  lang = target['language']
62
62
  raise 'Language must be defined for each target' if target.nil?
63
63
  model = config['model'] || 'gpt-3.5-turbo'
@@ -106,9 +106,14 @@ class GptTranslate::Generator < Jekyll::Generator
106
106
  def api_key(config)
107
107
  file = config['api_key_file']
108
108
  key = if file.nil?
109
- ENV.fetch('OPENAI_API_KEY', nil)
110
- else
109
+ k = ENV.fetch('OPENAI_API_KEY', nil)
110
+ Jekyll.logger.info('The key is found in the OPENAI_API_KEY env variable') unless k.nil?
111
+ k
112
+ elsif File.exist?(file)
113
+ Jekyll.logger.info("Reading OpenAI key from the file: #{file}")
111
114
  File.read(file).strip
115
+ else
116
+ Jekyll.logger.info("The file is not found: #{file}")
112
117
  end
113
118
  if key.nil? && Jekyll.env == 'development'
114
119
  Jekyll.logger.info("OPENAI_API_KEY environment variable is not set, \
@@ -46,14 +46,23 @@ class GptTranslate::Ping
46
46
  @path = path
47
47
  end
48
48
 
49
- def exists?
49
+ def found?(file)
50
50
  home = @site.config['url']
51
51
  return false if home.nil?
52
52
  uri = Iri.new(home).path(@path)
53
53
  before = Net::HTTP.get_response(URI(uri.to_s))
54
- if before.is_a?(Net::HTTPSuccess) && before.body.include?("/#{GptTranslate::VERSION}")
55
- Jekyll.logger.info("No need to translate, page exists at #{uri} (#{before.body.split.count} words)")
56
- return true
54
+ if before.is_a?(Net::HTTPSuccess)
55
+ html = before.body
56
+ if html.include?("/#{GptTranslate::VERSION}")
57
+ Jekyll.logger.info("No need to translate, the page exists at \
58
+ #{uri} (#{html.split.count} words), saved to #{file}")
59
+ File.mkdir_p(File.dirname(file))
60
+ File.write(file, html)
61
+ return true
62
+ end
63
+ Jekyll.logger.info("Re-translation required for #{uri}")
64
+ else
65
+ Jekyll.logger.info("The page is absent, will translate: #{uri}")
57
66
  end
58
67
  Jekyll.logger.debug("GET #{uri}: #{before.code}")
59
68
  false
data/test/test_ping.rb CHANGED
@@ -25,6 +25,7 @@
25
25
  require 'minitest/autorun'
26
26
  require 'webmock/minitest'
27
27
  require 'jekyll'
28
+ require 'tempfile'
28
29
  require_relative '../lib/jekyll-chatgpt-translate/ping'
29
30
 
30
31
  # Ping test.
@@ -44,14 +45,18 @@ class GptTranslate::PingTest < Minitest::Test
44
45
  stub_request(:any, 'https://www.yegor256.com/about-me.html')
45
46
  site = FakeSite.new({ 'url' => 'https://www.yegor256.com/' })
46
47
  ping = GptTranslate::Ping.new(site, '/about-me.html')
47
- assert(!ping.exists?)
48
+ Tempfile.open do |f|
49
+ assert(!ping.found?(f))
50
+ end
48
51
  end
49
52
 
50
53
  def test_when_not_exists
51
54
  stub_request(:any, 'https://www.yegor256.com/absent.html').to_return(status: 404)
52
55
  site = FakeSite.new({ 'url' => 'https://www.yegor256.com/' })
53
56
  ping = GptTranslate::Ping.new(site, '/absent.html')
54
- assert(!ping.exists?)
57
+ Tempfile.open do |f|
58
+ assert(!ping.found?(f))
59
+ end
55
60
  end
56
61
 
57
62
  def test_relative_path
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-chatgpt-translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko