jekyll-theme-centos 2.52.0.beta.33 → 2.52.0.beta.34
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 +4 -4
- data/_plugins/converter_link.rb +260 -0
- data/_plugins/optimizer_compressor.rb +316 -0
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: eb03b8524faeb2a8ced08783f43b9f19019d76923a1b2136966bd51678fac8ff
|
|
4
|
+
data.tar.gz: 1975121e2011726602a952cf41043d2d3531ae5d764b4b20084c76bbb52389d8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 761dc927d30ba7b818c11a6478e549d8cad29a4e4579211b70d28015b38866890d2314cda159a10c6161b7bd0e3cbaa98f494daa6fb57f3b23f00446fd62c02a
|
|
7
|
+
data.tar.gz: 11b84e8e917a6c99bcf6e0cb3457176c1386441f63d7e39ce12f80eff8e11e936bf52992416cd26bf405c19b53a345b98b3270b05ccd33043f097d39d8b42741
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# This Jekyll plugin, `converter_link`, automatically enhances `<a>` tags
|
|
4
|
+
# within Markdown content by adding specific Bootstrap-related CSS classes and
|
|
5
|
+
# external-link icons. This functionality is conditional, applying different
|
|
6
|
+
# sets of classes based on whether a link is located inside a Bootstrap alert
|
|
7
|
+
# box or is a regular link. It explicitly avoids modifying elements that are
|
|
8
|
+
# designated as buttons by having the `.btn` class.
|
|
9
|
+
#
|
|
10
|
+
# === External Link Icon Support ===
|
|
11
|
+
#
|
|
12
|
+
# For links pointing to external domains (different from site.url), the plugin
|
|
13
|
+
# automatically adds a Font Awesome external-link icon (fa-solid fa-external-link)
|
|
14
|
+
# at the end of the link text, similar to the link component behavior.
|
|
15
|
+
#
|
|
16
|
+
# === External Link Detection & Target Management ===
|
|
17
|
+
#
|
|
18
|
+
# The plugin automatically detects external links and manages their behavior:
|
|
19
|
+
# - Relative URLs (starting with /, ../) and same-domain absolute URLs open in the same window
|
|
20
|
+
# - Absolute URLs pointing to different domains (http:// or https://) automatically get target="_blank"
|
|
21
|
+
# - Links with target="_blank" automatically receive rel="noopener noreferrer" protection
|
|
22
|
+
# against security vulnerabilities (window.opener access, referrer leakage)
|
|
23
|
+
#
|
|
24
|
+
# === Configuration ===
|
|
25
|
+
#
|
|
26
|
+
# You can customize the classes applied by this plugin by adding a
|
|
27
|
+
# `converter_link` section to your `_config.yml` file.
|
|
28
|
+
#
|
|
29
|
+
# * `enabled`: Boolean (default: true) that controls whether the plugin runs.
|
|
30
|
+
# Set to false to disable automatic link styling and icon insertion.
|
|
31
|
+
#
|
|
32
|
+
# * `default_link_classes`: Defines the classes to be applied to all links that are not
|
|
33
|
+
# inside an .alert. Example: `default_link_classes: "text-decoration-none"`
|
|
34
|
+
#
|
|
35
|
+
# * `alert_link_classes`: Defines the classes for links specifically located within an
|
|
36
|
+
# .alert box. Example: `alert_link_classes: "text-decoration-underline"`
|
|
37
|
+
#
|
|
38
|
+
# * `external_link_icon`: Boolean (default: true) that controls whether external-link
|
|
39
|
+
# icons are added to cross-domain external links. Set to false to disable.
|
|
40
|
+
#
|
|
41
|
+
# If no configuration is provided, the plugin will use default fallback classes
|
|
42
|
+
# and enable external link icons.
|
|
43
|
+
#
|
|
44
|
+
# Here is an example `_config.yml` setup:
|
|
45
|
+
#
|
|
46
|
+
# ```yaml
|
|
47
|
+
# converter_link:
|
|
48
|
+
# enabled: true
|
|
49
|
+
# default_link_classes: "link link-offset-3 link-offset-3-hover link-underline-primary link-underline-opacity-0 link-underline-opacity-100-hover"
|
|
50
|
+
# alert_link_classes: "alert-link"
|
|
51
|
+
# external_link_icon: true
|
|
52
|
+
# ```
|
|
53
|
+
|
|
54
|
+
require 'jekyll'
|
|
55
|
+
require 'nokogiri'
|
|
56
|
+
|
|
57
|
+
module Jekyll
|
|
58
|
+
module Converters
|
|
59
|
+
# This custom converter applies specific CSS classes to <a> tags
|
|
60
|
+
# based on their context (e.g., inside an alert box) and excludes
|
|
61
|
+
# buttons, all done via Nokogiri's powerful CSS selectors.
|
|
62
|
+
# It also adds external-link icons for cross-domain external links.
|
|
63
|
+
class LinkConverter < Jekyll::Converters::Markdown
|
|
64
|
+
def self.name
|
|
65
|
+
'LinkConverter'
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Extract domain from a URL by removing protocol and path components
|
|
69
|
+
# Example: "https://example.com/path?query=1#anchor" -> "example.com"
|
|
70
|
+
def extract_domain(url)
|
|
71
|
+
return '' if url.nil? || url.empty?
|
|
72
|
+
|
|
73
|
+
# Remove protocol (http:// or https://)
|
|
74
|
+
domain = url.sub(%r{^https?://}, '')
|
|
75
|
+
|
|
76
|
+
# Extract just the domain part (before /, ?, or #)
|
|
77
|
+
domain = domain.split(%r{[/?#]}).first
|
|
78
|
+
|
|
79
|
+
# Convert to lowercase for case-insensitive comparison
|
|
80
|
+
domain.downcase
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Check if a link is an external link pointing to a different domain
|
|
84
|
+
# Returns true if:
|
|
85
|
+
# - Link starts with http:// or https://
|
|
86
|
+
# - Link domain is different from site.url domain
|
|
87
|
+
# - Link doesn't already have an external-link icon child
|
|
88
|
+
def is_external_different_domain_link?(link, site_domain)
|
|
89
|
+
href = link['href'].to_s
|
|
90
|
+
|
|
91
|
+
# Check if it's an absolute URL with http/https
|
|
92
|
+
return false unless href =~ %r{^https?://}
|
|
93
|
+
|
|
94
|
+
# Extract domain from the link
|
|
95
|
+
link_domain = extract_domain(href)
|
|
96
|
+
|
|
97
|
+
# Check if domains are different
|
|
98
|
+
return false if link_domain == site_domain || link_domain.empty?
|
|
99
|
+
|
|
100
|
+
# Check if an icon has already been added (avoid duplicates)
|
|
101
|
+
# Check for both new icons (fa-external-link) and old icons
|
|
102
|
+
# (fa-external-link-alt, fa-up-right-from-square, etc.)
|
|
103
|
+
return false if link.css('i[class*="fa-external-link"]').any?
|
|
104
|
+
return false if link.css('i[class*="up-right-from-square"]').any?
|
|
105
|
+
return false if link.css('svg[data-icon="external-link"]').any?
|
|
106
|
+
return false if link.css('svg[data-icon="up-right-from-square"]').any?
|
|
107
|
+
return false if link.css('svg[data-icon="arrow-up-right-from-square"]').any?
|
|
108
|
+
|
|
109
|
+
true
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Determine if a link points to an external domain
|
|
113
|
+
# Returns true if:
|
|
114
|
+
# - Link is absolute (starts with http:// or https://)
|
|
115
|
+
# - Link domain is different from site.url domain
|
|
116
|
+
# Returns false if:
|
|
117
|
+
# - Link is relative (starts with /, ../)
|
|
118
|
+
# - Link domain matches site.url domain
|
|
119
|
+
def cross_domain_external_link?(link, site_domain)
|
|
120
|
+
href = link['href'].to_s
|
|
121
|
+
|
|
122
|
+
# Relative URLs (starting with / or ../) are internal links
|
|
123
|
+
return false if href =~ %r{^(/|\.\.)}
|
|
124
|
+
|
|
125
|
+
# Check if it's an absolute URL with http/https
|
|
126
|
+
return false unless href =~ %r{^https?://}
|
|
127
|
+
|
|
128
|
+
# Extract domain from the link
|
|
129
|
+
link_domain = extract_domain(href)
|
|
130
|
+
|
|
131
|
+
# Check if domains are different
|
|
132
|
+
return false if link_domain == site_domain || link_domain.empty?
|
|
133
|
+
|
|
134
|
+
true
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Add target="_blank" to external cross-domain links
|
|
138
|
+
# Only adds if target is not already set
|
|
139
|
+
def add_external_target_blank(link, site_domain)
|
|
140
|
+
# Skip if link already has a target attribute
|
|
141
|
+
return if link['target'].to_s.strip != ''
|
|
142
|
+
|
|
143
|
+
# Add target="_blank" if this is a cross-domain external link
|
|
144
|
+
return unless cross_domain_external_link?(link, site_domain)
|
|
145
|
+
|
|
146
|
+
link['target'] = '_blank'
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Add security attributes to links with target="_blank"
|
|
150
|
+
# Adds rel="noopener noreferrer" to prevent security vulnerabilities
|
|
151
|
+
# Preserves any existing rel attribute values
|
|
152
|
+
def add_external_target_protection(link)
|
|
153
|
+
target = link['target'].to_s
|
|
154
|
+
|
|
155
|
+
# Only add protection for target="_blank"
|
|
156
|
+
return unless target == '_blank'
|
|
157
|
+
|
|
158
|
+
# Get existing rel attribute (if any)
|
|
159
|
+
existing_rel = link['rel'].to_s.strip
|
|
160
|
+
rel_values = existing_rel.empty? ? [] : existing_rel.split(/\s+/)
|
|
161
|
+
|
|
162
|
+
# Add required security values if not already present
|
|
163
|
+
rel_values << 'noopener' unless rel_values.include?('noopener')
|
|
164
|
+
rel_values << 'noreferrer' unless rel_values.include?('noreferrer')
|
|
165
|
+
|
|
166
|
+
# Set the updated rel attribute
|
|
167
|
+
link['rel'] = rel_values.join(' ')
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def matches(ext)
|
|
171
|
+
ext =~ /^\.md$/i
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def output_ext(_ext)
|
|
175
|
+
'.html'
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def convert(content)
|
|
179
|
+
html = super(content)
|
|
180
|
+
config = @config['converter_link'] || {}
|
|
181
|
+
|
|
182
|
+
return html unless plugin_enabled?(config)
|
|
183
|
+
|
|
184
|
+
doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
|
185
|
+
|
|
186
|
+
apply_link_styles(doc, config)
|
|
187
|
+
add_external_link_features(doc, config)
|
|
188
|
+
|
|
189
|
+
doc.to_html
|
|
190
|
+
rescue StandardError => e
|
|
191
|
+
Jekyll.logger.error 'LinkConverter Error:', "An error occurred during link conversion: #{e.message}"
|
|
192
|
+
Jekyll.logger.error 'Backtrace:', e.backtrace.join("\n")
|
|
193
|
+
|
|
194
|
+
html
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Check if the plugin is enabled in configuration
|
|
198
|
+
# Defaults to true if not explicitly configured
|
|
199
|
+
def plugin_enabled?(config)
|
|
200
|
+
enabled = config.key?('enabled') ? config['enabled'] : true
|
|
201
|
+
unless enabled
|
|
202
|
+
Jekyll.logger.info 'LinkConverter:', 'Disabled via configuration. Skipping custom link modification.'
|
|
203
|
+
end
|
|
204
|
+
enabled
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# Apply CSS classes to links based on their context (alert or default)
|
|
208
|
+
def apply_link_styles(doc, config)
|
|
209
|
+
default_classes = config.fetch('default_link_classes',
|
|
210
|
+
'link link-offset-3 link-offset-3-hover link-underline-primary link-underline-opacity-0 link-underline-opacity-100-hover')
|
|
211
|
+
alert_classes = config.fetch('alert_link_classes', 'alert-link')
|
|
212
|
+
|
|
213
|
+
# Apply alert classes to links inside p.alert
|
|
214
|
+
doc.css('p.alert a:not(.btn)').each do |link|
|
|
215
|
+
add_classes(link, alert_classes)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Apply default classes to all other non-alert links
|
|
219
|
+
doc.css('a:not(.btn)').each do |link|
|
|
220
|
+
add_classes(link, default_classes) unless link.ancestors('p.alert').any?
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Add CSS classes to a link element
|
|
225
|
+
def add_classes(link, classes_string)
|
|
226
|
+
classes_string.split(' ').each { |cls| link.add_class(cls) }
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Add external link features: target="_blank", icons, and security attributes
|
|
230
|
+
def add_external_link_features(doc, config)
|
|
231
|
+
site_domain = extract_site_domain
|
|
232
|
+
return if site_domain.empty?
|
|
233
|
+
|
|
234
|
+
doc.css('a:not(.btn)').each do |link|
|
|
235
|
+
add_external_target_blank(link, site_domain)
|
|
236
|
+
add_external_icon(link, site_domain, config, doc)
|
|
237
|
+
add_external_target_protection(link)
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Extract site domain from Jekyll configuration
|
|
242
|
+
def extract_site_domain
|
|
243
|
+
site_url = @config['url'] || @config['baseurl'] || ''
|
|
244
|
+
extract_domain(site_url)
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# Add external-link icon to cross-domain external links
|
|
248
|
+
def add_external_icon(link, site_domain, config, doc)
|
|
249
|
+
external_link_icon = config.fetch('external_link_icon', true)
|
|
250
|
+
|
|
251
|
+
return unless external_link_icon
|
|
252
|
+
return unless is_external_different_domain_link?(link, site_domain)
|
|
253
|
+
|
|
254
|
+
icon = Nokogiri::XML::Node.new('i', doc)
|
|
255
|
+
icon['class'] = 'fa-solid fa-external-link ms-1'
|
|
256
|
+
link.add_child(icon)
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# This Jekyll plugin, `html_compressor`, automatically compresses HTML, CSS, and
|
|
4
|
+
# JavaScript files generated by Jekyll to reduce their file size and improve page
|
|
5
|
+
# load performance. It runs after the entire site is built and processes all output
|
|
6
|
+
# files in the destination directory.
|
|
7
|
+
#
|
|
8
|
+
# === How It Works ===
|
|
9
|
+
#
|
|
10
|
+
# The plugin uses a post-write hook that runs after Jekyll finishes writing all
|
|
11
|
+
# files to the output directory (public/). It then traverses the directory tree
|
|
12
|
+
# and compresses files using the minify-html gem (Rust-based minifier):
|
|
13
|
+
#
|
|
14
|
+
# - **HTML Compression:** Removes comments, whitespace, and optional tags using
|
|
15
|
+
# proper HTML parsing. Expected reduction: 10-25%
|
|
16
|
+
#
|
|
17
|
+
# - **CSS Compression:** Removes comments and whitespace from inline and standalone
|
|
18
|
+
# CSS files. Skips pre-minified files (*.min.css).
|
|
19
|
+
# Expected reduction: 10-20%
|
|
20
|
+
#
|
|
21
|
+
# - **JavaScript Compression:** Removes comments and unnecessary whitespace from
|
|
22
|
+
# inline and standalone JavaScript files using proper parsing. Skips pre-minified
|
|
23
|
+
# files (*.min.js). Renames compressed files to *.min.js. Expected reduction: 20-30%
|
|
24
|
+
#
|
|
25
|
+
# === Error Handling ===
|
|
26
|
+
#
|
|
27
|
+
# The plugin is designed to fail gracefully:
|
|
28
|
+
# - If compression fails for any file, the original file is preserved
|
|
29
|
+
# - Errors are logged as warnings but do not break the build
|
|
30
|
+
# - Statistics are still displayed for successfully compressed files
|
|
31
|
+
#
|
|
32
|
+
# === Performance ===
|
|
33
|
+
#
|
|
34
|
+
# Processing typically adds less than 1 second to the total build time. The
|
|
35
|
+
# minify-html gem (Rust-based) provides efficient processing.
|
|
36
|
+
#
|
|
37
|
+
# === Requirements ===
|
|
38
|
+
#
|
|
39
|
+
# This plugin requires the 'minify_html' gem. Install with:
|
|
40
|
+
# gem install minify_html
|
|
41
|
+
#
|
|
42
|
+
# === Configuration ===
|
|
43
|
+
#
|
|
44
|
+
# This plugin is always enabled and has no configuration options. It is designed
|
|
45
|
+
# to work automatically with no setup required. To verify it's working, build
|
|
46
|
+
# your site and check that files in public/ are smaller and have minimal whitespace.
|
|
47
|
+
#
|
|
48
|
+
# === W3C HTML Validation Compatibility ===
|
|
49
|
+
#
|
|
50
|
+
# This plugin uses minify-html's built-in `ensure_spec_compliant_unquoted_attribute_values`
|
|
51
|
+
# option to maintain W3C validation compliance. This preserves attribute quotes on values
|
|
52
|
+
# containing special characters, preventing unquoting issues with attributes like:
|
|
53
|
+
# prefix="og: https://ogp.me/ns#"
|
|
54
|
+
#
|
|
55
|
+
# === CSS/JavaScript Minification Note ===
|
|
56
|
+
#
|
|
57
|
+
# The minify-html Ruby gem only exposes HTML minification through its public API.
|
|
58
|
+
# CSS and JavaScript minification are supported internally through wrapper tags:
|
|
59
|
+
# CSS is wrapped in `<style>` tags and JS in `<script>` tags for minification,
|
|
60
|
+
# then extracted. This is the only approach supported by the gem's design.
|
|
61
|
+
|
|
62
|
+
require 'jekyll'
|
|
63
|
+
require 'pathname'
|
|
64
|
+
|
|
65
|
+
begin
|
|
66
|
+
require 'minify_html'
|
|
67
|
+
rescue LoadError
|
|
68
|
+
raise Jekyll::Errors::FatalException, "Missing required gem 'minify_html'. " \
|
|
69
|
+
'Install with: gem install minify_html'
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
module Jekyll
|
|
73
|
+
# Main HTML/CSS/JS compression module
|
|
74
|
+
class HtmlCompressor
|
|
75
|
+
# Default minify-html options for HTML compression
|
|
76
|
+
HTML_MINIFY_OPTIONS = {
|
|
77
|
+
keep_html_and_head_opening_tags: true,
|
|
78
|
+
keep_closing_tags: true,
|
|
79
|
+
minify_css: true,
|
|
80
|
+
minify_js: true,
|
|
81
|
+
remove_bangs: true,
|
|
82
|
+
remove_processing_instructions: true,
|
|
83
|
+
ensure_spec_compliant_unquoted_attribute_values: true
|
|
84
|
+
}.freeze
|
|
85
|
+
|
|
86
|
+
# Compression types with display names for logging
|
|
87
|
+
COMPRESSION_TYPES = [
|
|
88
|
+
{ type: :html, display_name: 'HTML' },
|
|
89
|
+
{ type: :css, display_name: 'CSS' },
|
|
90
|
+
{ type: :js, display_name: 'JS' }
|
|
91
|
+
].freeze
|
|
92
|
+
|
|
93
|
+
def initialize
|
|
94
|
+
@stats = {
|
|
95
|
+
html: { count: 0, before: 0, after: 0 },
|
|
96
|
+
css: { count: 0, before: 0, after: 0 },
|
|
97
|
+
js: { count: 0, before: 0, after: 0 },
|
|
98
|
+
errors: []
|
|
99
|
+
}
|
|
100
|
+
@start_time = Time.now
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Main entry point: compress all files in the site destination
|
|
104
|
+
def self.compress_site(site)
|
|
105
|
+
compressor = new
|
|
106
|
+
compressor.process_site(site)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def process_site(site)
|
|
110
|
+
dest = Pathname.new(site.dest)
|
|
111
|
+
return unless dest.directory?
|
|
112
|
+
|
|
113
|
+
Jekyll.logger.info 'HtmlCompressor:', 'Starting compression...'
|
|
114
|
+
|
|
115
|
+
# Find and compress all files
|
|
116
|
+
find_and_compress_files(dest)
|
|
117
|
+
|
|
118
|
+
# Log final statistics
|
|
119
|
+
log_statistics
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
# Find all compressible files in destination directory
|
|
125
|
+
def find_and_compress_files(dest)
|
|
126
|
+
dest.glob('**/*.{html,css,js}').each do |file_path|
|
|
127
|
+
compress_file(file_path)
|
|
128
|
+
end
|
|
129
|
+
rescue StandardError => e
|
|
130
|
+
Jekyll.logger.error 'HtmlCompressor:', "Error during file discovery: #{e.message}"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Process a single file based on its extension
|
|
134
|
+
def compress_file(file_path)
|
|
135
|
+
# Skip pre-minified files
|
|
136
|
+
return if file_path.to_s.end_with?('.min.css', '.min.js')
|
|
137
|
+
|
|
138
|
+
case file_path.extname.downcase
|
|
139
|
+
when '.html'
|
|
140
|
+
safe_compress(file_path, :html)
|
|
141
|
+
when '.css'
|
|
142
|
+
safe_compress(file_path, :css)
|
|
143
|
+
when '.js'
|
|
144
|
+
safe_compress(file_path, :js, rename_to_min: true)
|
|
145
|
+
end
|
|
146
|
+
rescue StandardError => e
|
|
147
|
+
Jekyll.logger.warn 'HtmlCompressor:', "Unexpected error processing #{file_path}: #{e.message}"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Safely compress a file, preserving original on error
|
|
151
|
+
def safe_compress(file_path, type, rename_to_min: false)
|
|
152
|
+
original_content = file_path.read(encoding: 'UTF-8')
|
|
153
|
+
original_size = original_content.bytesize
|
|
154
|
+
|
|
155
|
+
# Skip empty files
|
|
156
|
+
return if original_size.zero?
|
|
157
|
+
|
|
158
|
+
begin
|
|
159
|
+
compressed = compress_by_type(original_content, type)
|
|
160
|
+
|
|
161
|
+
# Sanity check: ensure result is not empty
|
|
162
|
+
raise 'Compression resulted in empty content' if compressed.nil? || compressed.strip.empty?
|
|
163
|
+
|
|
164
|
+
compressed_size = compressed.bytesize
|
|
165
|
+
|
|
166
|
+
# Write compressed file and rename if needed
|
|
167
|
+
write_compressed_file(file_path, compressed, original_content, rename_to_min: rename_to_min)
|
|
168
|
+
|
|
169
|
+
# Update statistics
|
|
170
|
+
@stats[type][:count] += 1
|
|
171
|
+
@stats[type][:before] += original_size
|
|
172
|
+
@stats[type][:after] += compressed_size
|
|
173
|
+
rescue StandardError => e
|
|
174
|
+
# Log error but preserve original file
|
|
175
|
+
error_msg = "Failed to compress #{file_path}: #{e.message}"
|
|
176
|
+
Jekyll.logger.warn 'HtmlCompressor:', error_msg
|
|
177
|
+
@stats[:errors] << error_msg
|
|
178
|
+
|
|
179
|
+
# Ensure original file is preserved
|
|
180
|
+
file_path.write(original_content, encoding: 'UTF-8')
|
|
181
|
+
end
|
|
182
|
+
rescue StandardError => e
|
|
183
|
+
Jekyll.logger.warn 'HtmlCompressor:', "Error reading file #{file_path}: #{e.message}"
|
|
184
|
+
@stats[:errors] << "Read error: #{file_path}"
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Write compressed file to disk and rename if needed (e.g., .js to .min.js)
|
|
188
|
+
def write_compressed_file(file_path, compressed_content, original_content, rename_to_min: false)
|
|
189
|
+
file_path.write(compressed_content, encoding: 'UTF-8')
|
|
190
|
+
|
|
191
|
+
# Rename .js to .min.js if requested
|
|
192
|
+
if rename_to_min && file_path.extname == '.js'
|
|
193
|
+
new_path = file_path.sub_ext('.min.js')
|
|
194
|
+
file_path.rename(new_path)
|
|
195
|
+
end
|
|
196
|
+
rescue StandardError => e
|
|
197
|
+
# Restore original if write/rename fails
|
|
198
|
+
file_path.write(original_content, encoding: 'UTF-8')
|
|
199
|
+
raise "Failed to write compressed file: #{e.message}"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# Dispatch to appropriate compression method
|
|
203
|
+
def compress_by_type(content, type)
|
|
204
|
+
case type
|
|
205
|
+
when :html then compress_html(content)
|
|
206
|
+
when :css then compress_css(content)
|
|
207
|
+
when :js then compress_js(content)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# =========================================================================
|
|
212
|
+
# Compression using minify-html gem
|
|
213
|
+
# =========================================================================
|
|
214
|
+
|
|
215
|
+
# Compress HTML content using minify-html gem
|
|
216
|
+
def compress_html(content)
|
|
217
|
+
minify_html(content, HTML_MINIFY_OPTIONS)
|
|
218
|
+
rescue StandardError => e
|
|
219
|
+
raise "HTML minification failed: #{e.message}"
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Compress CSS/JS content wrapped in temporary tags, then extract
|
|
223
|
+
def compress_wrapped(content, wrapper_tag, minify_option)
|
|
224
|
+
wrapped = "<#{wrapper_tag}>#{content}</#{wrapper_tag}>"
|
|
225
|
+
minified = minify_html(wrapped, { minify_option => true })
|
|
226
|
+
match = minified.match(%r{<#{wrapper_tag}>(.*)</#{wrapper_tag}>}m)
|
|
227
|
+
raise "#{wrapper_tag.upcase} minification resulted in empty content" unless match
|
|
228
|
+
|
|
229
|
+
match[1]
|
|
230
|
+
rescue StandardError => e
|
|
231
|
+
raise "#{wrapper_tag.upcase} minification failed: #{e.message}"
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Compress CSS content using minify-html gem
|
|
235
|
+
def compress_css(content)
|
|
236
|
+
compress_wrapped(content, 'style', :minify_css)
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Compress JavaScript content using minify-html gem
|
|
240
|
+
def compress_js(content)
|
|
241
|
+
compress_wrapped(content, 'script', :minify_js)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# =========================================================================
|
|
245
|
+
# Logging and Statistics
|
|
246
|
+
# =========================================================================
|
|
247
|
+
|
|
248
|
+
# Format file size for display
|
|
249
|
+
def format_size(bytes)
|
|
250
|
+
if bytes < 1024
|
|
251
|
+
"#{bytes}B"
|
|
252
|
+
elsif bytes < 1_048_576
|
|
253
|
+
"#{(bytes / 1024.0).round(1)}KB"
|
|
254
|
+
else
|
|
255
|
+
"#{(bytes / 1_048_576.0).round(1)}MB"
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Calculate compression percentage
|
|
260
|
+
def compression_percent(before, after)
|
|
261
|
+
return 0 if before.zero?
|
|
262
|
+
|
|
263
|
+
(((before - after) / before.to_f) * 100).round(1)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Log compression statistics
|
|
267
|
+
def log_statistics
|
|
268
|
+
elapsed = Time.now - @start_time
|
|
269
|
+
|
|
270
|
+
Jekyll.logger.info 'HtmlCompressor:', 'Compression complete using minify-html gem'
|
|
271
|
+
|
|
272
|
+
# Log per-type statistics and calculate totals
|
|
273
|
+
total_before = 0
|
|
274
|
+
total_after = 0
|
|
275
|
+
|
|
276
|
+
COMPRESSION_TYPES.each do |type_config|
|
|
277
|
+
type = type_config[:type]
|
|
278
|
+
display_name = type_config[:display_name]
|
|
279
|
+
data = @stats[type]
|
|
280
|
+
|
|
281
|
+
next if data[:count].zero?
|
|
282
|
+
|
|
283
|
+
percent = compression_percent(data[:before], data[:after])
|
|
284
|
+
before_fmt = format_size(data[:before])
|
|
285
|
+
after_fmt = format_size(data[:after])
|
|
286
|
+
|
|
287
|
+
Jekyll.logger.info 'HtmlCompressor:',
|
|
288
|
+
"#{display_name}: #{data[:count]} files (#{before_fmt} → #{after_fmt}, #{percent}% reduction)"
|
|
289
|
+
|
|
290
|
+
total_before += data[:before]
|
|
291
|
+
total_after += data[:after]
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# Log overall statistics
|
|
295
|
+
if total_before > 0
|
|
296
|
+
total_percent = compression_percent(total_before, total_after)
|
|
297
|
+
total_before_fmt = format_size(total_before)
|
|
298
|
+
total_after_fmt = format_size(total_after)
|
|
299
|
+
|
|
300
|
+
Jekyll.logger.info 'HtmlCompressor:',
|
|
301
|
+
"Total: #{total_before_fmt} → #{total_after_fmt} (#{total_percent}% reduction) in #{elapsed.round(2)}s"
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Log errors if any
|
|
305
|
+
return if @stats[:errors].empty?
|
|
306
|
+
|
|
307
|
+
Jekyll.logger.warn 'HtmlCompressor:', "#{@stats[:errors].length} compression errors (files preserved):"
|
|
308
|
+
@stats[:errors].each { |error| Jekyll.logger.warn 'HtmlCompressor:', " - #{error}" }
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# Register the post_write hook
|
|
314
|
+
Jekyll::Hooks.register :site, :post_write do |site|
|
|
315
|
+
Jekyll::HtmlCompressor.compress_site(site)
|
|
316
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jekyll-theme-centos
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.52.0.beta.
|
|
4
|
+
version: 2.52.0.beta.34
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- ReleaseBot
|
|
@@ -251,6 +251,8 @@ files:
|
|
|
251
251
|
- _layouts/download/cards.html
|
|
252
252
|
- _layouts/download/default.html
|
|
253
253
|
- _layouts/people/default.html
|
|
254
|
+
- _plugins/converter_link.rb
|
|
255
|
+
- _plugins/optimizer_compressor.rb
|
|
254
256
|
- _sass/base/_customization.scss
|
|
255
257
|
- _sass/base/_light-dark.scss
|
|
256
258
|
- _sass/base/_maps.scss
|