jekyll-fetch 0.1.2 → 0.2.1

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: c20c1e9f5f1e7adfb901ebed2f8b8a46433c5873e8900d520c1c7c62c1b6eba7
4
- data.tar.gz: dc9d30295267e25d84e64849c412179a909d5d32918114471066ab201da5e493
3
+ metadata.gz: c5454f6d5abbbd61428289ac94ae2283a5668ace9ec37941d6a333897e70d140
4
+ data.tar.gz: 295243d723f1b8e149a3ef010d40bff5c7468569937144720a3756a998114612
5
5
  SHA512:
6
- metadata.gz: 5afa64c5c08176b909052577953722cd5273cf921828c8515fef41813865ce2b7f9844728553f39ae60a6828240a352318395bb9887e2a5bbdc5694cd700bb91
7
- data.tar.gz: a2cca41b29546782c888c11ed2a2414e3177d7cc5a6d94e51acb038836091f1088fcef7758859b73b7c58638439d5081c0a2806aa987bab59f5f8f4bbdafdb11
6
+ metadata.gz: 9c8ca44f9b14fb48ff1662e7ca8e0a901dab63ee028fdccee0dae934af36548a05ec7611945084d44c039be8747313f0632397bbfaf836cf310de128015cc669
7
+ data.tar.gz: 57fb86f18102e24be963427de16c7a00136da3a90920ec3eafe2cefbb1cccf7110b090fce3c9cff69d3b6e5d61e4937e773e9c9455507dfd869f11f4a852a5b7
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,38 @@ 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
+
45
+ ### Specify GitHub branches
46
+
47
+ You can change the default branch used for retrieving GitHub files from main to the value of your choice by updating `_config.yml` :
48
+
49
+ ```yaml
50
+ fetch: # Config section for the plugin
51
+ default_github_branch: main # This is the default value but you can use whatever you want
52
+ ```
53
+
54
+ The `github_readme` and `github_file` accept an optional argument for one-time overrides of the branch :
55
+
56
+ ```
57
+ {{ "gh_user/repo_name" | github_readme: 'branch_name' }}
58
+ Will render to the content of the `README.md` file from the repository
59
+
60
+ {{ "gh_user/repo_name" | github_file: "path/to/file/in/repo", "branch_name" }}
61
+ Will render to the content of the specified file from the repository
62
+ ```
63
+
32
64
  ## Development
33
65
 
34
66
  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.1'
5
5
  end
data/lib/jekyll-fetch.rb CHANGED
@@ -1,29 +1,91 @@
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
10
+ CONFIG = {
11
+ 'default_github_branch' => 'main'
12
+ }.freeze
9
13
  class Error < StandardError; end
10
14
 
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!
15
+ # Helper functions for HTTP requests
16
+ class Utils
17
+ class << self
18
+ def fetch(uri_str, limit = 10)
19
+ raise ArgumentError, 'Max retries reached' if limit.zero?
20
+
21
+ begin
22
+ unsafe_fetch(uri_str, limit)
23
+ rescue Errno::ECONNREFUSED
24
+ Jekyll.logger.warn "Connection refused for #{uri_str}, retrying in 2s..."
25
+ sleep 2
26
+ Jekyll.logger.warn "Retrying (#{limit - 1} tries left)"
27
+ fetch(uri_str, limit - 1)
28
+ end
29
+ end
30
+
31
+ def config
32
+ @config ||= CONFIG.merge(JEKYLL_SITE.config['fetch'] || {})
33
+ end
34
+
35
+ private
36
+
37
+ def unsafe_fetch(uri_str, limit)
38
+ raise ArgumentError, 'Max retries reached' if limit.zero?
39
+
40
+ response_to_body(
41
+ uri_to_response(uri_str), limit - 1
42
+ )
43
+ end
44
+
45
+ def uri_to_response(uri_str)
46
+ url = URI.parse(uri_str)
47
+ req = Net::HTTP::Get.new(url.path, { 'User-Agent' => 'Mozilla/5.0 (etc...)' })
48
+ Net::HTTP.start(url.host, url.port, use_ssl: true) { |http| http.request(req) }
49
+ end
50
+
51
+ def response_to_body(response, limit)
52
+ case response
53
+ when Net::HTTPSuccess then response.body.force_encoding('UTF-8')
54
+ when Net::HTTPRedirection then fetch(response['location'], limit)
55
+ else
56
+ response.error!
57
+ end
58
+ end
24
59
  end
25
60
  end
61
+
62
+ def fetch(uri_str, limit = 10)
63
+ raise ArgumentError, 'HTTP redirect too deep' if limit.zero?
64
+
65
+ Jekyll.logger.debug "fetch(#{uri_str})"
66
+ Utils.fetch(uri_str, limit)
67
+ end
68
+
69
+ def github_url(repo)
70
+ "https://github.com/#{repo}"
71
+ end
72
+
73
+ def github_readme(repo, branch = nil)
74
+ branch ||= Utils.config['default_github_branch']
75
+ Jekyll.logger.debug "github_readme(#{repo}, #{branch})"
76
+ github_file repo, 'README.md', branch
77
+ end
78
+
79
+ def github_file(repo, file, branch = nil)
80
+ branch ||= Utils.config['default_github_branch']
81
+ Jekyll.logger.debug "github_file(#{repo}, #{file}, #{branch})"
82
+ fetch "#{github_url(repo)}/raw/#{branch}/#{file}"
83
+ end
26
84
  end
27
85
  end
28
86
 
29
87
  Liquid::Template.register_filter(Jekyll::JekyllFetch)
88
+
89
+ Jekyll::Hooks.register :site, :after_init do |site|
90
+ Jekyll::JekyllFetch::JEKYLL_SITE = site
91
+ end
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.1
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