jekyll-gallery-plugin 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # Jekyll-Gallery
2
+
3
+ This is a Jekyll plugin that provides a Liquid tag to render one of the named named galleries declared
4
+ in the Front Matter inside the content, using a customizable template which, out of the bo, supports the
5
+ Javascript [Glightbox library][glightbox].
6
+
7
+ It also takes care of the gallery integrity at the compile time:
8
+ - checking for the *file* field presence;
9
+ - checking for the files presence within the related path.
10
+
11
+ Everytime one of these constraints is broken, an error message is raised, blocking any further action.
12
+
13
+ As an extra feature, the filesystem organization-and-check supports the *image* field declared for the
14
+ [Jekyll-Seo-Tag plugin][seotag] implementing the subkey *cover*.
15
+
16
+ [![Ruby](https://github.com/fabiomux/jekyll-gallery-plugin/actions/workflows/main.yml/badge.svg)][wf_main]
17
+ [![Gem Version](https://badge.fury.io/rb/jekyll-gallery-plugin.svg)][gem_version]
18
+
19
+ ## Installation
20
+
21
+ Can install the gem either manually or using *Bundler*.
22
+
23
+ ### Using Bundler
24
+
25
+ Install the gem and add to the application's Gemfile by executing:
26
+
27
+ $ bundle add jekyll-gallery-plugin --group jekyll_plugins
28
+
29
+ ### Manually
30
+
31
+ If bundler is not being used to manage dependencies, install the gem by executing:
32
+
33
+ $ gem install jekyll-gallery-plugin
34
+
35
+ Then, add the following code within the Gemfile of your Jekyll project:
36
+
37
+ ```ruby
38
+ group :jekyll_plugins do
39
+ ...
40
+ gem 'jekyll-gallery-plugin'
41
+ end
42
+ ```
43
+
44
+ ## Configuration
45
+
46
+ Inside the `_config.yml` file can set up the following fields:
47
+ ```yaml
48
+ jekyll-gallery:
49
+ assets_path: assets/images
50
+ collections_dir:
51
+ posts: posts
52
+ downloads: downloads
53
+ ...
54
+ ```
55
+
56
+ The assigned values are the default values.
57
+
58
+ *assets_path*
59
+ : This is the general path where assets lie, these files will be directly served by Jekyll so you will
60
+ have this same prefix path in your images as well.
61
+
62
+ *collections_dir*
63
+ : This is where can specify a folder name for each collection, by default it takes the slug of the
64
+ collection itself.
65
+
66
+ Within the default template must add the code to recall the Glightbox CSS and Javascript scripts, the
67
+ instructions are pretty standard:
68
+ 1. The CSS file goes in the *head* section
69
+ 2. The Javascript library file in before the *body* closing tag
70
+ 3. The Javascript code before the *body* tag and after the previous mentioned Javascript library
71
+
72
+ Here an example using the CDN resources:
73
+ ```html
74
+ <html>
75
+ <head>
76
+ <meta charset="utf-8" >
77
+ ...
78
+ <!-- (1) -->
79
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/glightbox/dist/css/glightbox.min.css" />
80
+ ...
81
+ </head>
82
+
83
+ ...
84
+
85
+ <!-- (2) -->
86
+ <script src="https://cdn.jsdelivr.net/gh/mcstudios/glightbox/dist/js/glightbox.min.js"></script>
87
+
88
+ <!-- (3) -->
89
+ <script type="text/javascript">
90
+ const lightbox = GLightbox();
91
+ </script>
92
+ </html>
93
+ ```
94
+
95
+ ## Usage
96
+
97
+ The usage of this plugin is splitted in two parts:
98
+ 1. The Front Matter to declare the galleries;
99
+ 2. the content, placing the *gallery* tag to render the gallery declared.
100
+
101
+ So to declaration will be something like:
102
+ ```yaml
103
+ gallery:
104
+ gallery_name_1:
105
+ - file: subfolder_1/image_name_1.jpg
106
+ title: "Title 1"
107
+ description: "Description 1"
108
+ - file: subfolder_1/image_name_2.jpg
109
+ title: "Title 2"
110
+ description: "Description 2"
111
+ - file: subfolder_1/image_name_3.jpg
112
+ title: "Title 3"
113
+ description: "Description 3"
114
+ gallery_name_2:
115
+ - file: subfolder_2/image_name_1.jpg
116
+ title: "Title 1"
117
+ description: "Description 1"
118
+ ```
119
+
120
+ Each gallery has a *name* that introduces the list of items (*gallery_name_1*, *gallery_name_2*), each
121
+ items must provide the *file* field with the relative path, a *title* and a *description*
122
+
123
+ Supposing we are filling a *posts* content, and using all the default values, the images will take place
124
+ under the *assets/images/posts/subfolder_1/...* and *assets/images/posts/subfolder_2/...* following the
125
+ scheme:
126
+
127
+ <assets_path>/<collections_dir>/<image_folder_and_filename>
128
+
129
+ To render the gallery in whatever point within the content:
130
+
131
+ {% gallery name='<gallery_name>' %}
132
+
133
+ For example to render the second gallery:
134
+ ```liquid
135
+ {% gallery name='gallery_name_2' %}
136
+ ```
137
+
138
+ ## More Help
139
+
140
+ More info is available at:
141
+ - the [project page on the Freeaptitude blog][project_page];
142
+ - the [Jekyll-Gallery Github wiki][jekyll_gallery_wiki];
143
+ - the [Glightbox README page][glightbox_readme].
144
+
145
+ [glightbox]: https://biati-digital.github.io/glightbox/ "Glightbox Javascript plugin Home page"
146
+ [glightbox_readme]: https://github.com/biati-digital/glightbox#usage "Glightbox README page - Usage"
147
+ [seotag]: https://github.com/jekyll/jekyll-seo-tag "Jekyll-Seo-Tag Github repository"
148
+ [wf_main]: https://github.com/fabiomux/jekyll-gallery-plugin/actions/workflows/main.yml
149
+ [gem_version]: https://badge.fury.io/rb/jekyll-gallery-plugin
150
+ [project_page]: https://freeaptitude.altervista.org/projects/jekyll-gallery.html "Project page on the Freeaptitude blog"
151
+ [jekyll_gallery_wiki]: https://github.com/fabiomux/jekyll-gallery-plugin/wiki "Jekyll-Gallery wiki page on GitHub"
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/jekyll/gallery/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jekyll-gallery-plugin"
7
+ spec.version = Jekyll::Gallery::VERSION
8
+ spec.authors = ["Fabio Mucciante"]
9
+ spec.email = ["fabio.mucciante@gmail.com"]
10
+
11
+ spec.summary = "Customizable Jekyll plugin that renders a gallery using the GLightbox Javascript library."
12
+ spec.description = "It applies a gallery template and checks for the files listed presence."
13
+ spec.homepage = "https://freeaptitude.altervista.org/projects/jekyll-gallery.html"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/fabiomux/jekyll-gallery-plugin"
20
+ spec.metadata["changelog_uri"] = "https://freeaptitude.altervista.org/projects/jekyll-gallery.html#changelog"
21
+ spec.metadata["wiki_uri"] = "https://github.com/fabiomux/jekyll-gallery-plugin/wiki"
22
+ spec.metadata["rubygems_mfa_required"] = "true"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(__dir__) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ # spec.add_dependency "example-gem", "~> 1.0"
37
+ spec.add_dependency "jekyll", ">= 3.7", "< 5.0"
38
+
39
+ # For more information and examples about making a new gem, check out our
40
+ # guide at: https://bundler.io/guides/creating_gem.html
41
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Gallery
5
+ #
6
+ # Configurations required across the gem.
7
+ #
8
+ class Config
9
+ def self.gconfig
10
+ @@gconfig ||= Jekyll.configuration.freeze
11
+ end
12
+
13
+ def self.base_url
14
+ @@base_url ||= gconfig["base_url"].freeze
15
+ end
16
+
17
+ def self.url
18
+ @@url ||= gconfig["url"].freeze
19
+ end
20
+
21
+ def self.source
22
+ @@source ||= gconfig["source"].freeze
23
+ end
24
+
25
+ def self.config
26
+ @@config ||= gconfig["jekyll-gallery"].freeze
27
+ end
28
+
29
+ def self.selector
30
+ @@selector ||= (config["post_selector"] || "include.post").freeze
31
+ end
32
+
33
+ def self.a_path
34
+ @@a_path ||= (config["assets_path"] || "assets/images").freeze
35
+ end
36
+
37
+ def self.c_paths
38
+ @@c_paths ||= (config["collections_dir"] || {}).freeze
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Gallery
5
+ #
6
+ # Contains the methods that assist the code creation.
7
+ #
8
+ module Helper
9
+ def template(tpl, gcfg)
10
+ file = File.join(Dir.pwd, gcfg["includes_dir"], "jekyll-gallery-#{tpl}.html")
11
+ return file if File.exist? file
12
+
13
+ File.expand_path(File.join(__dir__, "..", "..", "jekyll-gallery-#{tpl}.html"))
14
+ end
15
+
16
+ def self.collection_path(collection)
17
+ Config.c_paths[collection.to_s] || collection.to_s
18
+ end
19
+
20
+ def self.expand_remote_file(file, collection)
21
+ base_path = Config.base_url
22
+ a_path = Config.a_path
23
+
24
+ if file =~ /\|/
25
+ c, f = file.split("|")
26
+ return [base_path, a_path, collection_path(c), f].join("/")
27
+ end
28
+
29
+ [base_path, a_path, collection_path(collection), file].join("/")
30
+ end
31
+
32
+ def self.expand_local_file(file, collection)
33
+ a_path = Config.a_path
34
+ source = Config.source
35
+
36
+ if file =~ /\|/
37
+ c, f = file.split("|")
38
+ return File.join(source, a_path, collection_path(c), f)
39
+ end
40
+
41
+ File.join(source, a_path, collection_path(collection), file)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Gallery
5
+ #
6
+ # Expand the image:cover paths to accomplish the
7
+ # Jekyll-Gallery filesystem standards and return
8
+ # a path compatible with the Jekyll-Seo-Tag plugin.
9
+ #
10
+ class SeoImage
11
+ def self.setup
12
+ Jekyll::Hooks.register :documents, :pre_render do |document|
13
+ image = document["image"]
14
+ collection = document.type
15
+
16
+ update_frontmatter(image, collection)
17
+ end
18
+ end
19
+
20
+ def self.update_frontmatter(image, collection)
21
+ return unless image.is_a? Hash
22
+ return if image["cover"].nil?
23
+
24
+ file = Helper.expand_local_file(image["cover"], collection)
25
+ raise MissingFile, file unless File.exist?(file)
26
+
27
+ image["path"] = Helper.expand_remote_file(image["cover"], collection)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Gallery
5
+ #
6
+ # Gallery view rendering.
7
+ #
8
+ class Gallery < Liquid::Tag
9
+ include Helper
10
+
11
+ def initialize(tag_name, text, tokens)
12
+ super
13
+ @@tpl_i ||= File.read(template("image", Config.gconfig)).freeze
14
+ @@tpl_g ||= File.read(template("gallery", Config.gconfig)).freeze
15
+ @text = text
16
+ end
17
+
18
+ def render(context)
19
+ init_params(@text, context)
20
+ site = context.registers[:site]
21
+ tpl = []
22
+
23
+ @files.each_with_index do |f, i|
24
+ raise MissingFileField, { name: @name, idx: i.next } if f["file"].nil?
25
+
26
+ file = Helper.expand_local_file(f["file"], @collection)
27
+ raise MissingFile, file unless File.exist?(file)
28
+
29
+ file = Helper.expand_remote_file(f["file"], @collection)
30
+ vars = {
31
+ "filename" => file,
32
+ "name" => @name,
33
+ "desc_position" => @desc_position,
34
+ "title" => f["title"],
35
+ "description" => f["description"],
36
+ "a_class" => @a_class,
37
+ "img_class" => @img_class,
38
+ "counter" => i.next
39
+ }
40
+ tpl << (Liquid::Template.parse @@tpl_i).render(site.site_payload.merge!(vars))
41
+ end
42
+
43
+ images = tpl.join("\n")
44
+ vars = {
45
+ "images" => images,
46
+ "g_class" => @g_class
47
+ }
48
+ (Liquid::Template.parse @@tpl_g).render site.site_payload.merge!(vars)
49
+ end
50
+
51
+ private
52
+
53
+ def init_params(text, context)
54
+ params = text.split_params(context)
55
+ @name = params["name"]
56
+ raise MissingGallery if @name.nil? || @name.empty?
57
+
58
+ @a_class = params["a_class"]
59
+ @img_class = params["img_class"]
60
+ @g_class = params["g_class"] || ""
61
+
62
+ @collection = context["#{Config.selector}.collection"] || context["page.collection"]
63
+
64
+ @desc_position = params["desc_position"] || Config.config["desc_position"] || "bottom"
65
+
66
+ @files = context["#{Config.selector}.gallery.#{@name}"] || context["page.gallery.#{@name}"]
67
+ raise UnknownGallery, @name if @files.nil?
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Gallery
5
+ #
6
+ # Convert the params to hash
7
+ #
8
+ class ::String
9
+ def split_params(context)
10
+ scan(/(\w+)=(?:(["'])(.+?)\2|([^ ]+))/)
11
+ .to_h do |x|
12
+ x[3] = context[x[3]] if x[1].nil?
13
+ x.delete_at 1
14
+ x.compact
15
+ end
16
+ end
17
+ end
18
+
19
+ #
20
+ # Remove nil or empty items.
21
+ #
22
+ class ::Array
23
+ def cleanup
24
+ delete_if { |x| x.nil? || x.to_s.empty? }
25
+ end
26
+ end
27
+
28
+ #
29
+ # Invalid group name error
30
+ #
31
+ class InvalidGroup < StandardError
32
+ def initialize(name)
33
+ super "The group '#{name}' is not valid!"
34
+ end
35
+ end
36
+
37
+ #
38
+ # No gallery name specified.
39
+ #
40
+ class MissingGallery < StandardError
41
+ def initialize
42
+ super "No gallery name has been specified!"
43
+ end
44
+ end
45
+
46
+ #
47
+ # The specified gallery name doesn't exist.
48
+ #
49
+ class UnknownGallery < StandardError
50
+ def initialize(name)
51
+ super "The gallery '#{name}' doesn't exist!"
52
+ end
53
+ end
54
+
55
+ #
56
+ # Not existing file.
57
+ #
58
+ class MissingFile < StandardError
59
+ def initialize(name)
60
+ super "The file '#{name}' doesn't exist"
61
+ end
62
+ end
63
+
64
+ #
65
+ # Not existing file field
66
+ #
67
+ class MissingFileField < StandardError
68
+ def initialize(data)
69
+ super "No 'file' field in the gallery '#{data[:name]}' item n.#{data[:idx]}!"
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Gallery
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+
5
+ require_relative "gallery/config"
6
+ require_relative "gallery/helper"
7
+ require_relative "gallery/tags"
8
+ require_relative "gallery/utils"
9
+ require_relative "gallery/seo"
10
+
11
+ Liquid::Template.register_tag("gallery", Jekyll::Gallery::Gallery)
12
+ Jekyll::Gallery::SeoImage.setup
@@ -0,0 +1,3 @@
1
+ <div class="gallery {{g_class}}">
2
+ {{images}}
3
+ </div>
@@ -0,0 +1,12 @@
1
+ <figure>
2
+ <a href="{{filename}}"
3
+ class="glightbox {{a_class}}"
4
+ data-gallery="gallery-{{name}}"
5
+ data-type="image"
6
+ data-title="{{title}}"
7
+ data-description="{{description}}"
8
+ data-desc-position="{{desc_position}}">
9
+ <img src="{{filename}}" alt="{{title}}" class="{{img_class}}">
10
+ </a>
11
+ <figcaption>{{title}}</figcaption>
12
+ </figure>
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "jekyll/gallery"
@@ -0,0 +1,8 @@
1
+ module Jekyll
2
+ module Gallery
3
+ module Plugin
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-gallery-plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Fabio Mucciante
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.7'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ description: It applies a gallery template and checks for the files listed presence.
34
+ email:
35
+ - fabio.mucciante@gmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - ".rspec"
41
+ - ".rubocop.yml"
42
+ - CODE_OF_CONDUCT.md
43
+ - Gemfile
44
+ - Gemfile.lock
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - jekyll-gallery-plugin.gemspec
49
+ - lib/jekyll-gallery-gallery.html
50
+ - lib/jekyll-gallery-image.html
51
+ - lib/jekyll-gallery-plugin.rb
52
+ - lib/jekyll/gallery.rb
53
+ - lib/jekyll/gallery/config.rb
54
+ - lib/jekyll/gallery/helper.rb
55
+ - lib/jekyll/gallery/seo.rb
56
+ - lib/jekyll/gallery/tags.rb
57
+ - lib/jekyll/gallery/utils.rb
58
+ - lib/jekyll/gallery/version.rb
59
+ - sig/jekyll/gallery/plugin.rbs
60
+ homepage: https://freeaptitude.altervista.org/projects/jekyll-gallery.html
61
+ licenses: []
62
+ metadata:
63
+ homepage_uri: https://freeaptitude.altervista.org/projects/jekyll-gallery.html
64
+ source_code_uri: https://github.com/fabiomux/jekyll-gallery-plugin
65
+ changelog_uri: https://freeaptitude.altervista.org/projects/jekyll-gallery.html#changelog
66
+ wiki_uri: https://github.com/fabiomux/jekyll-gallery-plugin/wiki
67
+ rubygems_mfa_required: 'true'
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.4.3
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Customizable Jekyll plugin that renders a gallery using the GLightbox Javascript
87
+ library.
88
+ test_files: []