hyde-page-css 0.3.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hyde-page-css.rb +214 -5
- metadata +3 -6
- data/lib/hyde/page/css.rb +0 -130
- data/lib/hyde/page/generated_page_css_file.rb +0 -35
- data/lib/hyde/renderer.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6ec003b0a85dea4cbf951dbd077514f5db76261f41e65958b3275a5eba2368c
|
4
|
+
data.tar.gz: c9b997562983ef4957bfaa13dde64c034ec60df290252244773e645b802b8ed4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fd1e02c5900318abe34d80f696a00c02072962086ed5110edcfed657946f7d98b855ac618c0101dc2a2f45988c9f0f7be978d3bfffe46d11bae44b7fc2c761c
|
7
|
+
data.tar.gz: 485f1c4392783641de2a3f3dee6d63d3c364e4c5e99f240a4d470058336916032366e3c104404a0b859beba7977e015dd77178022d6be8fb238b009d823d0843
|
data/lib/hyde-page-css.rb
CHANGED
@@ -1,11 +1,220 @@
|
|
1
|
+
require "jekyll"
|
2
|
+
require "digest"
|
3
|
+
|
4
|
+
Jekyll::Hooks.register :pages, :post_init do |page|
|
5
|
+
Hyde::Page::Css.new(page).run if page.instance_of? Jekyll::Page
|
6
|
+
end
|
7
|
+
|
1
8
|
module Hyde
|
2
9
|
module Page
|
3
10
|
class Css
|
4
|
-
VERSION = "0.
|
11
|
+
VERSION = "0.4.1"
|
12
|
+
end
|
13
|
+
|
14
|
+
class GeneratedCssFile < Jekyll::StaticFile
|
15
|
+
attr_accessor :file_contents
|
16
|
+
attr_reader :generator
|
17
|
+
|
18
|
+
def initialize(site, dir, name)
|
19
|
+
@site = site
|
20
|
+
@dir = dir
|
21
|
+
@name = name
|
22
|
+
@relative_path = File.join(*[@dir, @name].compact)
|
23
|
+
@extname = File.extname(@name)
|
24
|
+
@type = @collection&.label&.to_sym
|
25
|
+
@generator = "hyde-page-css"
|
26
|
+
end
|
27
|
+
|
28
|
+
def write(dest)
|
29
|
+
dest_path = destination(dest)
|
30
|
+
return false if File.exist?(dest_path)
|
31
|
+
|
32
|
+
FileUtils.mkdir_p(File.dirname(dest_path))
|
33
|
+
FileUtils.rm(dest_path) if File.exist?(dest_path)
|
34
|
+
|
35
|
+
File.open(dest_path, "w") do |output_file|
|
36
|
+
output_file << file_contents
|
37
|
+
end
|
38
|
+
|
39
|
+
true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Css
|
44
|
+
@@config = {
|
45
|
+
"source" => "assets/css",
|
46
|
+
"destination" => "assets/css",
|
47
|
+
"minify" => true,
|
48
|
+
"enable" => true,
|
49
|
+
"keep_files" => true,
|
50
|
+
"dev_mode" => false
|
51
|
+
}
|
52
|
+
|
53
|
+
def initialize(page)
|
54
|
+
@page = page
|
55
|
+
@site = page.site
|
56
|
+
@config = fetch_config
|
57
|
+
@site.data["_hyde_pages_cache"] ||= {}
|
58
|
+
|
59
|
+
if keep_files? && !dev_mode?
|
60
|
+
@site.config.fetch("keep_files").push(destination)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def run
|
65
|
+
css = fetch_css(@page)
|
66
|
+
layout = fetch_layout(fetch_layout_name(@page))
|
67
|
+
css_groups = parent_layout_css(layout, css).reverse
|
68
|
+
return if css_groups.empty?
|
69
|
+
|
70
|
+
for group in css_groups
|
71
|
+
lookup_name = names_to_key(group)
|
72
|
+
cache_entry = @site.data["_hyde_pages_cache"].fetch(lookup_name, nil)
|
73
|
+
|
74
|
+
if cache_entry.nil?
|
75
|
+
data = concatenate_files(group)
|
76
|
+
return if data == ""
|
77
|
+
|
78
|
+
data = minify(data)
|
79
|
+
return if data == ""
|
80
|
+
|
81
|
+
generated_file = generate_file(group, data)
|
82
|
+
|
83
|
+
# file already exists, so skip writing out the data to disk
|
84
|
+
return unless @site.static_files.find { |static_file| static_file.name == generated_file.name }.nil?
|
85
|
+
|
86
|
+
# place file data into the new file
|
87
|
+
generated_file.file_contents = data
|
88
|
+
|
89
|
+
# assign static file to list for jekyll to render
|
90
|
+
@site.static_files << generated_file
|
91
|
+
|
92
|
+
# add to cache
|
93
|
+
cache_entry = {
|
94
|
+
url: generated_file.url,
|
95
|
+
data: data
|
96
|
+
}
|
97
|
+
@site.data["_hyde_pages_cache"][lookup_name] = cache_entry
|
98
|
+
end
|
99
|
+
|
100
|
+
# assign to page.data.css_files for liquid output
|
101
|
+
add_to_urls(cache_entry.fetch(:url, nil)).compact
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def names_to_key(names)
|
108
|
+
names.join("-")
|
109
|
+
end
|
110
|
+
|
111
|
+
def add_to_urls(url)
|
112
|
+
@page.data["css_files"] ||= []
|
113
|
+
@page.data["css_files"].push(url)
|
114
|
+
end
|
115
|
+
|
116
|
+
def fetch_config
|
117
|
+
@@config.merge(@site.config.fetch("hyde_page_css", {}))
|
118
|
+
end
|
119
|
+
|
120
|
+
def keep_files?
|
121
|
+
@config.fetch("keep_files") == true
|
122
|
+
end
|
123
|
+
|
124
|
+
def dev_mode?
|
125
|
+
@config.fetch("dev_mode") == true
|
126
|
+
end
|
127
|
+
|
128
|
+
def minify?
|
129
|
+
@config.fetch("minify") == true
|
130
|
+
end
|
131
|
+
|
132
|
+
def destination
|
133
|
+
@config.fetch("destination")
|
134
|
+
end
|
135
|
+
|
136
|
+
def source
|
137
|
+
@config.fetch("source")
|
138
|
+
end
|
139
|
+
|
140
|
+
def qualified_source
|
141
|
+
File.join(*[@site.source, source].compact)
|
142
|
+
end
|
143
|
+
|
144
|
+
def fetch_layout_name(obj_with_data, default = nil)
|
145
|
+
obj_with_data.data.fetch("layout", default)
|
146
|
+
end
|
147
|
+
|
148
|
+
def fetch_css(obj_with_data, default = [])
|
149
|
+
[obj_with_data.data.fetch("css", []).reverse]
|
150
|
+
end
|
151
|
+
|
152
|
+
def fetch_layout(layout_name, default = nil)
|
153
|
+
@site.layouts.fetch(layout_name, default)
|
154
|
+
end
|
155
|
+
|
156
|
+
def parent_layout_css(layout, css)
|
157
|
+
if layout.nil?
|
158
|
+
return css
|
159
|
+
end
|
160
|
+
|
161
|
+
layout_name = fetch_layout_name(layout)
|
162
|
+
parent_layout = fetch_layout(layout_name)
|
163
|
+
css = css.concat(fetch_css(layout))
|
164
|
+
|
165
|
+
parent_layout_css(parent_layout, css)
|
166
|
+
end
|
167
|
+
|
168
|
+
def concatenate_files(files, data = [])
|
169
|
+
files.each do |file_name|
|
170
|
+
# tmp page required to handle anything with frontmatter/yaml header
|
171
|
+
tmp_page = Jekyll::PageWithoutAFile.new(@site, nil, source, file_name)
|
172
|
+
path = File.join([qualified_source, file_name])
|
173
|
+
|
174
|
+
begin
|
175
|
+
tmp_page.content = File.read(path)
|
176
|
+
data.push(Jekyll::Renderer.new(@site, tmp_page).run)
|
177
|
+
rescue
|
178
|
+
Jekyll.logger.warn("Page CSS Warning:", "Unable to find #{path}")
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
data.join("\n")
|
183
|
+
end
|
184
|
+
|
185
|
+
def style
|
186
|
+
if dev_mode?
|
187
|
+
"expanded"
|
188
|
+
elsif !minify?
|
189
|
+
"expanded"
|
190
|
+
else
|
191
|
+
"compressed"
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def minify(data)
|
196
|
+
return data unless minify?
|
197
|
+
|
198
|
+
converter_config = {"sass" => {"style" => style}}
|
199
|
+
Jekyll::Converters::Scss.new(converter_config).convert(data)
|
200
|
+
end
|
201
|
+
|
202
|
+
def generate_file(files, data)
|
203
|
+
file_name = generate_file_name(files, data)
|
204
|
+
Hyde::Page::GeneratedCssFile.new(@site, source, file_name)
|
205
|
+
end
|
206
|
+
|
207
|
+
def generate_file_name(files, data, prefix: nil)
|
208
|
+
file_names = [prefix]
|
209
|
+
|
210
|
+
if dev_mode?
|
211
|
+
files.each { |file| file_names.push(file.gsub(".css", "")) }
|
212
|
+
end
|
213
|
+
|
214
|
+
file_names.push(Digest::MD5.hexdigest(data)[0, 6])
|
215
|
+
|
216
|
+
file_names.compact.join("-") + ".css"
|
217
|
+
end
|
5
218
|
end
|
6
219
|
end
|
7
220
|
end
|
8
|
-
|
9
|
-
require_relative "hyde/renderer.rb"
|
10
|
-
require_relative "hyde/page/css.rb"
|
11
|
-
require_relative "hyde/page/generated_page_css_file.rb"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hyde-page-css
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregory Daynes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|
@@ -38,9 +38,6 @@ extensions: []
|
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
40
|
- lib/hyde-page-css.rb
|
41
|
-
- lib/hyde/page/css.rb
|
42
|
-
- lib/hyde/page/generated_page_css_file.rb
|
43
|
-
- lib/hyde/renderer.rb
|
44
41
|
homepage: https://github.com/gregdaynes/hyde-page-css
|
45
42
|
licenses:
|
46
43
|
- MIT
|
@@ -60,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
57
|
- !ruby/object:Gem::Version
|
61
58
|
version: '0'
|
62
59
|
requirements: []
|
63
|
-
rubygems_version: 3.
|
60
|
+
rubygems_version: 3.5.3
|
64
61
|
signing_key:
|
65
62
|
specification_version: 4
|
66
63
|
summary: Plugin for jekyll to enable per page css files
|
data/lib/hyde/page/css.rb
DELETED
@@ -1,130 +0,0 @@
|
|
1
|
-
require 'jekyll'
|
2
|
-
require_relative "generated_page_css_file"
|
3
|
-
|
4
|
-
module Hyde
|
5
|
-
module Page
|
6
|
-
class Css
|
7
|
-
@@config = {
|
8
|
-
"asset_path" => "assets/css",
|
9
|
-
"file_output_path" => "assets/css",
|
10
|
-
"css_minify" => true,
|
11
|
-
"enable" => true,
|
12
|
-
"keep_files" => true,
|
13
|
-
"dev_mode" => false
|
14
|
-
}
|
15
|
-
|
16
|
-
def initialize(layout, info)
|
17
|
-
@site = info[:registers][:site]
|
18
|
-
@page = info[:registers][:page]
|
19
|
-
@data = layout.data
|
20
|
-
@config = @@config.merge(@site.config.dig("hyde_page_css") || {})
|
21
|
-
|
22
|
-
@qualified_asset_path = File.join(*[@site.source, @config["asset_path"]].compact)
|
23
|
-
|
24
|
-
if config("keep_files") == true && config("dev_mode") == false
|
25
|
-
@site.config["keep_files"].push(config("file_output_path"))
|
26
|
-
end
|
27
|
-
|
28
|
-
@page["css"] = if @page["css"].nil?
|
29
|
-
[]
|
30
|
-
else
|
31
|
-
[@page["css"]]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def generate
|
36
|
-
return unless config("enable") == true
|
37
|
-
@page["css"].unshift(@data["css"])
|
38
|
-
|
39
|
-
file_groups = flatten_group(@page["css"])
|
40
|
-
|
41
|
-
css_files = []
|
42
|
-
|
43
|
-
for files in file_groups do
|
44
|
-
break if !files&.length
|
45
|
-
|
46
|
-
data = concatenate_files(files.compact)
|
47
|
-
data = minify(data)
|
48
|
-
next if data == ""
|
49
|
-
|
50
|
-
file = generate_file(files, data)
|
51
|
-
css_files << file.url
|
52
|
-
|
53
|
-
# file already exists, so skip writing out the data to disk
|
54
|
-
next if @site.static_files.find { |x| x.name == file.name }
|
55
|
-
|
56
|
-
# place file data into the new file
|
57
|
-
file.file_contents = data
|
58
|
-
|
59
|
-
# assign static file to list for jekyll to render
|
60
|
-
@site.static_files << file
|
61
|
-
end
|
62
|
-
|
63
|
-
# the recursive nature of this will sometimes have duplicate css files
|
64
|
-
@page["css_files"] = css_files.uniq
|
65
|
-
end
|
66
|
-
|
67
|
-
private
|
68
|
-
|
69
|
-
def config(*)
|
70
|
-
@config.dig(*)
|
71
|
-
end
|
72
|
-
|
73
|
-
def flatten_group(arr, acc = [])
|
74
|
-
return [arr] if !arr.last.is_a?(Array)
|
75
|
-
|
76
|
-
acc += [arr.first]
|
77
|
-
acc += flatten_group(arr.last)
|
78
|
-
end
|
79
|
-
|
80
|
-
def concatenate_files(files, data = "")
|
81
|
-
for file in files do
|
82
|
-
# tmp page required to handle anything with frontmatter/yaml header
|
83
|
-
tmp_page = Jekyll::PageWithoutAFile.new(@site, nil, config("asset_path"), file)
|
84
|
-
path = File.join([@qualified_asset_path, file])
|
85
|
-
|
86
|
-
begin
|
87
|
-
file_contents = File.read(path)
|
88
|
-
tmp_page.content = file_contents
|
89
|
-
data << Jekyll::Renderer.new(@site, tmp_page).run
|
90
|
-
rescue
|
91
|
-
Jekyll.logger.warn("Page CSS Warning:", "Unable to find #{path}")
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
data
|
96
|
-
end
|
97
|
-
|
98
|
-
def minify(data)
|
99
|
-
style = if config("dev_mode") == true
|
100
|
-
"expanded"
|
101
|
-
elsif config("css_minify") == false
|
102
|
-
"expanded"
|
103
|
-
else
|
104
|
-
"compressed"
|
105
|
-
end
|
106
|
-
|
107
|
-
converter_config = {"sass" => {"style" => style}}
|
108
|
-
Jekyll::Converters::Scss.new(converter_config).convert(data)
|
109
|
-
end
|
110
|
-
|
111
|
-
def generate_file(files, data)
|
112
|
-
file_name = generate_file_name(files, data)
|
113
|
-
|
114
|
-
Hyde::Page::GeneratedPageCssFile.new(@site, config("asset_path"), file_name)
|
115
|
-
end
|
116
|
-
|
117
|
-
def generate_file_name(files, data, prefix: nil)
|
118
|
-
file_names = [prefix]
|
119
|
-
|
120
|
-
if config("dev_mode")
|
121
|
-
files.each { |file| file_names.push(file.gsub(".css", "")) }
|
122
|
-
end
|
123
|
-
|
124
|
-
file_names.push(Digest::MD5.hexdigest(data)[0, 6])
|
125
|
-
|
126
|
-
file_names.compact.join("-") + ".css"
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Hyde
|
2
|
-
module Page
|
3
|
-
# Alternative class for jekyll's static files
|
4
|
-
# this allows the creation of files without a source file
|
5
|
-
|
6
|
-
class GeneratedPageCssFile < Jekyll::StaticFile
|
7
|
-
attr_accessor :file_contents
|
8
|
-
attr_reader :generator
|
9
|
-
|
10
|
-
def initialize(site, dir, name)
|
11
|
-
@site = site
|
12
|
-
@dir = dir
|
13
|
-
@name = name
|
14
|
-
@relative_path = File.join(*[@dir, @name].compact)
|
15
|
-
@extname = File.extname(@name)
|
16
|
-
@type = @collection&.label&.to_sym
|
17
|
-
@generator = "hyde-page-css"
|
18
|
-
end
|
19
|
-
|
20
|
-
def write(dest)
|
21
|
-
dest_path = destination(dest)
|
22
|
-
return false if File.exist?(dest_path)
|
23
|
-
|
24
|
-
FileUtils.mkdir_p(File.dirname(dest_path))
|
25
|
-
FileUtils.rm(dest_path) if File.exist?(dest_path)
|
26
|
-
|
27
|
-
File.open(dest_path, "w") do |output_file|
|
28
|
-
output_file << file_contents
|
29
|
-
end
|
30
|
-
|
31
|
-
true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/lib/hyde/renderer.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Jekyll
|
2
|
-
class Renderer
|
3
|
-
def render_layout(output, layout, info)
|
4
|
-
Hyde::Page::Css.new(layout, info).generate
|
5
|
-
|
6
|
-
# TODO Would be nice to call super here instead of cloned logic from Jekyll internals
|
7
|
-
payload["content"] = output
|
8
|
-
payload["layout"] = Utils.deep_merge_hashes(layout.data, payload["layout"] || {})
|
9
|
-
|
10
|
-
render_liquid(
|
11
|
-
layout.content,
|
12
|
-
payload,
|
13
|
-
info,
|
14
|
-
layout.path
|
15
|
-
)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|