jekyll-highres-link 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 76de01644f80d95feda4b06aaa48d2b60cfe68671bbcd8145b0ae894263d903c
4
+ data.tar.gz: 9cd6348a14ef8a92a2d6c34fc11ebe27271e09fe9f254698f205681ef115016c
5
+ SHA512:
6
+ metadata.gz: 256b728c8219f6ecfb31c903df100b13dd85553a657e247b27335f9b4273b0ea5af41d65a3ec99f5306311b979d0674f260907faf1f60f74b13f7e23abe55976
7
+ data.tar.gz: 3d3699152e36c56e27bf6f592ba960977c1c933ba7aa5b991188fc02ac523f7afc0689dd81ac03c1e853550a183d3253aa774a59ddbbd1cf107209d158083b30
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Jekyll High-res Link
2
+
3
+ A jekyll plugin that wraps <img> tags with links to uncompressed originals, saving bandwidth
4
+
5
+ ## Usage
6
+
7
+ 1. Add the pulgin to your Gemfile
8
+ ```
9
+ gem 'jekyll-highres-link'
10
+ ```
11
+
12
+ 2. Add the plugin to your _config file:
13
+ ```
14
+ plugins:
15
+ - jekyll-highres-link
16
+ - OTHER_PLUGINS...
17
+ ```
18
+
19
+ 3. Configure the path for the uncompressed images. You can either go with relative or absolute paths (from the root of the project). For absolute paths add in the _config.yaml file the directory with a starting "/":
20
+ ```
21
+ highres-link:
22
+ - uncompressed_dir: /absolute/path
23
+ ```
24
+ OR, if the uncompressed images depend on the image location, don't add a starting "/"
25
+ ```
26
+ highres-link:
27
+ - uncompressed_dir: relative/path
28
+ ```
29
+ 4. That's it!
30
+
31
+ ## To be added
32
+ - [ ] Automatically compress the images (for now, you can run the following fish script: [compress.fish](https://gist.github.com/Perseus333/8a161ffa160b0d2f6c2fd3c9756b1568))
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+
5
+ module Jekyll
6
+ module HighresLink
7
+ class WrapImages
8
+ DEFAULT_UNCOMPRESSED_DIR = "uncompressed"
9
+
10
+ def self.process(doc)
11
+ return unless doc.output_ext == ".html"
12
+
13
+ config = doc.site.config["highres-link"] || {}
14
+ uncompressed_dir = config["uncompressed_dir"] || DEFAULT_UNCOMPRESSED_DIR
15
+
16
+ html = Nokogiri::HTML::DocumentFragment.parse(doc.output)
17
+ changed = false
18
+
19
+ html.css("img").each do |img|
20
+ src = img["src"]
21
+
22
+ unless src
23
+ next
24
+ end
25
+
26
+ rel_path = src.sub(%r{^/?}, "")
27
+ original_path = find_original_file(rel_path, uncompressed_dir)
28
+
29
+ unless original_path
30
+ Jekyll.logger.debug "HighresLink:", "\tNo original found for: #{rel_path}"
31
+ next
32
+ end
33
+
34
+ href = "/#{original_path}"
35
+ Jekyll.logger.info "HighresLink:", "\tWrapping with link to: #{href}"
36
+
37
+ link = Nokogiri::XML::Node.new("a", html)
38
+ link["href"] = href
39
+ link["target"] = "_blank"
40
+
41
+ img.replace(link)
42
+ link.add_child(img)
43
+ changed = true
44
+ end
45
+
46
+ doc.output = html.to_html if changed
47
+ end
48
+
49
+ def self.find_original_file(rel_path, uncompressed_dir)
50
+ basename = File.basename(rel_path, File.extname(rel_path))
51
+ dir = File.dirname(rel_path)
52
+
53
+ base_dir = if uncompressed_dir.start_with?("/")
54
+ File.join(uncompressed_dir, basename)
55
+ else
56
+ File.join(dir, uncompressed_dir)
57
+ end
58
+
59
+ %w[png jpg jpeg webp gif svg].each do |ext|
60
+ candidate = File.join(base_dir, "#{basename}.#{ext}")
61
+
62
+ if File.exist?(candidate)
63
+ return candidate
64
+ end
65
+ end
66
+
67
+ nil
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module HighresLink
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+ require "nokogiri"
5
+ require_relative "jekyll/highres-link"
6
+
7
+ Jekyll::Hooks.register [:pages, :documents], :post_render, priority: :low do |page|
8
+ Jekyll::HighresLink::WrapImages.process(page)
9
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-highres-link
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Perseus (Lynx)
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: jekyll
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 4.0.0
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '7.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: 4.0.0
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '7.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: nokogiri
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 1.10.0
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.0'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 1.10.0
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '2.0'
52
+ description: A Jekyll plugin that automatically wraps <img> tags with links to their
53
+ high-res versions
54
+ email:
55
+ - perseuslynx333@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - README.md
61
+ - lib/jekyll-highres-link.rb
62
+ - lib/jekyll-highres-link/version.rb
63
+ - lib/jekyll/highres-link.rb
64
+ homepage: https://github.com/perseus333/jekyll-highres-link
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ - - "<"
77
+ - !ruby/object:Gem::Version
78
+ version: '5.0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.6.7
86
+ specification_version: 4
87
+ summary: Wraps images with links to high-resolution originals
88
+ test_files: []