jekyll-fetch 0.1.1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c48088f55e630fa5865535fdcf4eb47bfef25c876d965f1e254199d0977a5160
4
- data.tar.gz: 96f9f13e4d5577f53d81c5697b2bf3298a5a8e5c8bb479ab61dcc9d57a3a053f
3
+ metadata.gz: f4434d6eccdd7106706ea9bc830e1112398ad65a2aeba36784b2329e910a2ff1
4
+ data.tar.gz: bfc916463dc0571bc8796bb40c7f561cc0ac1fe14726517a8b158d7c21ecd824
5
5
  SHA512:
6
- metadata.gz: d3b8b051a198b602949f1100341291754f02ec9c479ffdb9c2936d081622fe9a0bcc4055bfe413a61bc80514c35fabe7b75da68066d94853abe69691c210cc8b
7
- data.tar.gz: 518db64573f46664c8c39dc20ce9a60ec282b7d67ef2a23a24704f2a2032c1f2b39714ce763eac6810758b3eb69a36774fba49f2d47e5942309092e3ff51f76f
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.1"
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,17 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-fetch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
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-11 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
- name: jekyll
14
+ name: liquid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-http
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
@@ -26,7 +40,7 @@ dependencies:
26
40
  version: '0'
27
41
  description:
28
42
  email:
29
- - couy.pierre@gmail.com
43
+ - contact@pierre-couy.dev
30
44
  executables: []
31
45
  extensions: []
32
46
  extra_rdoc_files: []
@@ -39,12 +53,12 @@ files:
39
53
  - lib/jekyll-fetch.rb
40
54
  - lib/jekyll-fetch/version.rb
41
55
  - sig/jekyll_http.rbs
42
- homepage: https://github.com/pcouy/jekyll-fetch
56
+ homepage: https://pierre-couy.dev/projects/jekyll-fetch.html
43
57
  licenses:
44
58
  - MIT
45
59
  metadata:
46
60
  allowed_push_host: https://rubygems.org
47
- homepage_uri: https://github.com/pcouy/jekyll-fetch
61
+ homepage_uri: https://pierre-couy.dev/projects/jekyll-fetch.html
48
62
  source_code_uri: https://github.com/pcouy/jekyll-fetch
49
63
  post_install_message:
50
64
  rdoc_options: []