jekyll-fetch 0.1.2 → 0.2.0

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: c20c1e9f5f1e7adfb901ebed2f8b8a46433c5873e8900d520c1c7c62c1b6eba7
4
- data.tar.gz: dc9d30295267e25d84e64849c412179a909d5d32918114471066ab201da5e493
3
+ metadata.gz: f4434d6eccdd7106706ea9bc830e1112398ad65a2aeba36784b2329e910a2ff1
4
+ data.tar.gz: bfc916463dc0571bc8796bb40c7f561cc0ac1fe14726517a8b158d7c21ecd824
5
5
  SHA512:
6
- metadata.gz: 5afa64c5c08176b909052577953722cd5273cf921828c8515fef41813865ce2b7f9844728553f39ae60a6828240a352318395bb9887e2a5bbdc5694cd700bb91
7
- data.tar.gz: a2cca41b29546782c888c11ed2a2414e3177d7cc5a6d94e51acb038836091f1088fcef7758859b73b7c58638439d5081c0a2806aa987bab59f5f8f4bbdafdb11
6
+ metadata.gz: 224200bbc378ba973f547545ec1a6cb382184ac79aec98d03b17da34b5bbabf8b90051cf3c003a233b37ce47a21ab487c06c557d38f1a1afc3ad50e4454223c5
7
+ data.tar.gz: 67f3dd59fa6215fcf957715fd451f9bd6385e7adf98b2569f04178f43716388e015e31526a6273021d2053a8f14ba96537f9433547cf43157ad1e37b7e918594
data/.rubocop.yml CHANGED
@@ -0,0 +1,5 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Naming/FileName:
5
+ Enabled: false
data/README.md CHANGED
@@ -29,6 +29,19 @@ The example above will render to the raw markdown of this readme. You can chain
29
29
  {{ "https://raw.githubusercontent.com/pcouy/jekyll-fetch/main/README.md" | fetch | markdownify }}
30
30
  ```
31
31
 
32
+ Additionally, this plugin contains helper filters that help you work with files from GitHub repositories :
33
+
34
+ ```
35
+ {{ "gh_user/repo_name" | github_url }}
36
+ Will render to : https://github.com/gh_user/repo_name
37
+
38
+ {{ "gh_user/repo_name" | github_readme }}
39
+ Will render to the content of the `README.md` file from the repository
40
+
41
+ {{ "gh_user/repo_name" | github_file: "path/to/file/in/repo" }}
42
+ Will render to the content of the specified file from the repository
43
+ ```
44
+
32
45
  ## Development
33
46
 
34
47
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllFetch
4
- VERSION = "0.1.2"
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/jekyll-fetch.rb CHANGED
@@ -1,28 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "jekyll-fetch/version"
4
- require "liquid"
5
- require "net/https"
3
+ require_relative 'jekyll-fetch/version'
4
+ require 'liquid'
5
+ require 'net/https'
6
6
 
7
7
  module Jekyll
8
+ # Filters for transforming URL strings into the content at the URL
8
9
  module JekyllFetch
9
10
  class Error < StandardError; end
10
11
 
11
- def fetch(uri_str, limit = 10)
12
- # You should choose better exception.
13
- raise ArgumentError, 'HTTP redirect too deep' if limit == 0
14
-
15
- puts uri_str
16
- url = URI.parse(uri_str)
17
- req = Net::HTTP::Get.new(url.path, { 'User-Agent' => 'Mozilla/5.0 (etc...)' })
18
- response = Net::HTTP.start(url.host, url.port, use_ssl: true) { |http| http.request(req) }
19
- case response
20
- when Net::HTTPSuccess then response.body.force_encoding("UTF-8")
21
- when Net::HTTPRedirection then fetch(response['location'], limit - 1)
22
- else
23
- response.error!
12
+ # Helper functions for HTTP requests
13
+ class Utils
14
+ class << self
15
+ def fetch(uri_str, limit = 10)
16
+ raise ArgumentError, 'Max retries reached' if limit.zero?
17
+
18
+ begin
19
+ unsafe_fetch(uri_str, limit)
20
+ rescue Errno::ECONNREFUSED
21
+ Jekyll.logger.warn "Connection refused for #{uri_str}, retrying in 2s..."
22
+ sleep 2
23
+ Jekyll.logger.warn "Retrying (#{limit - 1} tries left)"
24
+ fetch(uri_str, limit - 1)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def unsafe_fetch(uri_str, limit)
31
+ raise ArgumentError, 'Max retries reached' if limit.zero?
32
+
33
+ response_to_body(
34
+ uri_to_response(uri_str), limit - 1
35
+ )
36
+ end
37
+
38
+ def uri_to_response(uri_str)
39
+ url = URI.parse(uri_str)
40
+ req = Net::HTTP::Get.new(url.path, { 'User-Agent' => 'Mozilla/5.0 (etc...)' })
41
+ Net::HTTP.start(url.host, url.port, use_ssl: true) { |http| http.request(req) }
42
+ end
43
+
44
+ def response_to_body(response, limit)
45
+ case response
46
+ when Net::HTTPSuccess then response.body.force_encoding('UTF-8')
47
+ when Net::HTTPRedirection then fetch(response['location'], limit)
48
+ else
49
+ response.error!
50
+ end
51
+ end
24
52
  end
25
53
  end
54
+
55
+ def fetch(uri_str, limit = 10)
56
+ raise ArgumentError, 'HTTP redirect too deep' if limit.zero?
57
+
58
+ Jekyll.logger.debug "fetch(#{uri_str})"
59
+ Utils.fetch(uri_str, limit)
60
+ end
61
+
62
+ def github_url(repo)
63
+ "https://github.com/#{repo}"
64
+ end
65
+
66
+ def github_readme(repo)
67
+ Jekyll.logger.debug "github_readme(#{repo})"
68
+ github_file repo, 'README.md'
69
+ end
70
+
71
+ def github_file(repo, file)
72
+ Jekyll.logger.debug "github_file(#{repo}, #{file})"
73
+ fetch "#{github_url(repo)}/raw/main/#{file}"
74
+ end
26
75
  end
27
76
  end
28
77
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-fetch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pcouy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-14 00:00:00.000000000 Z
11
+ date: 2023-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liquid