hyde-page-js 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ad8c15a48cdb354d8445e0cc30b9d4ba2544b6afb510cd79426a42a76a8d883c
4
- data.tar.gz: b367b416ff3bf8925cfe2c0eeec958111a0144a306aeab1fd030cbb330759bf1
3
+ metadata.gz: 867063daf19d510599c6474020f8f133c2564628a1eeb72b02a4aeb23a33d4ed
4
+ data.tar.gz: ba3b8d908e8e3af80e8d31757bc9a39d28e10fb2408de974ca0fd31978197fad
5
5
  SHA512:
6
- metadata.gz: ec5049e2ab3676ecbe9b1b606a3eaf1806aeac25d8543671219a6eb7c422b5e12fdfa732d11e730efd7e31c0afa09c396c1e04bd6c0e06f485961ced4c78ca19
7
- data.tar.gz: fca64106f4a05093fc2fc3d0787b4ec115beab21482c7d4e4a4898e1a6393f78224d3b785599794eb823969f6f028978728bcee24569adeb153bede1929d564d
6
+ metadata.gz: e8b40488499185989b1155c71c37e18bbb418f9b5017e2dd8b55e0b96600148bb86eab059d6289aaf93ec1eaab0a91052606194c4314f71a0b03b779f8a9d421
7
+ data.tar.gz: d716cd8a8d0fb70ec94e3dec9cf4cbf12e66e6fda60f4338967fe52f8834e3d75ae1fd10feb1dbe3e0d3f97c6bdc672bd8194e7e91eae7ebb6088da329288a58
data/lib/hyde-page-js.rb CHANGED
@@ -1,11 +1,198 @@
1
+ require 'terser'
2
+ require 'digest'
3
+ require 'jekyll'
4
+
5
+ Jekyll::Hooks.register :pages, :pre_render do |page, payload|
6
+ Hyde::Page::Js.new(page).run
7
+ end
8
+
1
9
  module Hyde
2
10
  module Page
3
11
  class Js
4
- VERSION = "0.1.0"
12
+ VERSION = "0.2.0"
13
+ end
14
+
15
+ class GeneratedJsFile < Jekyll::StaticFile
16
+ attr_accessor :file_contents
17
+ attr_reader :generator
18
+
19
+ def initialize(site, dir, name)
20
+ @site = site
21
+ @dir = dir
22
+ @name = name
23
+ @relative_path = File.join(*[@dir, @name].compact)
24
+ @extname = File.extname(@name)
25
+ @type = @collection&.label&.to_sym
26
+ @generator = "hyde-page-js"
27
+ end
28
+
29
+ def write(dest)
30
+ dest_path = destination(dest)
31
+ return false if File.exist?(dest_path)
32
+
33
+ FileUtils.mkdir_p(File.dirname(dest_path))
34
+ FileUtils.rm(dest_path) if File.exist?(dest_path)
35
+
36
+ File.open(dest_path, "w") do |output_file|
37
+ output_file << file_contents
38
+ end
39
+
40
+ true
41
+ end
42
+ end
43
+
44
+ class Js
45
+ @@config = {
46
+ "source" => "assets/js",
47
+ "destination" => "assets/js",
48
+ "minify" => true,
49
+ "enable" => true,
50
+ "keep_files" => true,
51
+ "dev_mode" => false
52
+ }
53
+
54
+ def initialize(page)
55
+ @page = page
56
+ @site = page.site
57
+ @config = fetch_config
58
+
59
+ if keep_files? && !dev_mode?
60
+ @site.config.fetch("keep_files").push(destination)
61
+ end
62
+ end
63
+
64
+ def run
65
+ js = fetch_js(@page)
66
+ layout = fetch_layout(fetch_layout_name(@page))
67
+ results = parent_layout_js(layout, js).reverse
68
+
69
+ data = concatenate_files(results)
70
+ return if data == ""
71
+
72
+ data = minify(data)
73
+ return if data == ""
74
+
75
+ generated_file = generate_file(results, data)
76
+
77
+ # file already exists, so skip writing out the data to disk
78
+ return unless @site.static_files.find { |static_file| static_file.name == generated_file.name }.nil?
79
+
80
+ # place file data into the new file
81
+ generated_file.file_contents = data
82
+
83
+ # assign static file to list for jekyll to render
84
+ @site.static_files << generated_file
85
+
86
+ # assign to site.data.js_files for liquid output
87
+ add_to_urls(generated_file.url)
88
+ end
89
+
90
+ private
91
+
92
+ def add_to_urls(url)
93
+ @site.data['js_files'] ||= []
94
+ @site.data['js_files'].push(url)
95
+ end
96
+
97
+ def fetch_config
98
+ @@config.merge(@site.config.fetch("hyde_page_js", {}))
99
+ end
100
+
101
+ def keep_files?
102
+ @config.fetch("keep_files") == true
103
+ end
104
+
105
+ def dev_mode?
106
+ @config.fetch("dev_mode") == true
107
+ end
108
+
109
+ def minify?
110
+ @config.fetch("minify") == true
111
+ end
112
+
113
+ def destination
114
+ @config.fetch("destination")
115
+ end
116
+
117
+ def source
118
+ @config.fetch("source")
119
+ end
120
+
121
+ def qualified_source
122
+ File.join(*[@site.source, source].compact)
123
+ end
124
+
125
+ def fetch_layout_name(obj_with_data, default = nil)
126
+ obj_with_data.data.fetch('layout', default)
127
+ end
128
+
129
+ def fetch_js(obj_with_data, default = [])
130
+ obj_with_data.data.fetch('js', []).reverse
131
+ end
132
+
133
+ def fetch_layout(layout_name, default = nil)
134
+ @site.layouts.fetch(layout_name, default)
135
+ end
136
+
137
+ def mangle?
138
+ if dev_mode?
139
+ false
140
+ elsif minify?
141
+ true
142
+ end
143
+ end
144
+
145
+ def parent_layout_js(layout, js)
146
+ if layout.nil?
147
+ return js.uniq.compact
148
+ end
149
+
150
+ layout_name = fetch_layout_name(layout)
151
+ parent_layout = fetch_layout(layout_name)
152
+ js = js.concat(fetch_js(layout))
153
+
154
+ parent_layout_js(parent_layout, js)
155
+ end
156
+
157
+ def concatenate_files(files, data = [])
158
+ files.each do |file_name|
159
+ # tmp page required to handle anything with frontmatter/yaml header
160
+ tmp_page = Jekyll::PageWithoutAFile.new(@site, nil, source, file_name)
161
+ path = File.join([qualified_source, file_name])
162
+
163
+ begin
164
+ tmp_page.content = File.read(path)
165
+ data.push(Jekyll::Renderer.new(@site, tmp_page).run)
166
+ rescue
167
+ Jekyll.logger.warn("Page JS Warning:", "Unable to find #{path}")
168
+ end
169
+ end
170
+
171
+ data.join("\n")
172
+ end
173
+
174
+ def minify(data)
175
+ converter_config = {mangle: mangle?, output: {comments: :copyright}}
176
+ js_converter = Terser.new(converter_config)
177
+ js_converter.compile(data)
178
+ end
179
+
180
+ def generate_file(files, data)
181
+ file_name = generate_file_name(files, data)
182
+ Hyde::Page::GeneratedJsFile.new(@site, source, file_name)
183
+ end
184
+
185
+ def generate_file_name(files, data, prefix: nil)
186
+ file_names = [prefix]
187
+
188
+ if dev_mode?
189
+ files.each { |file| file_names.push(file.gsub(".js", "")) }
190
+ end
191
+
192
+ file_names.push(Digest::MD5.hexdigest(data)[0, 6])
193
+
194
+ file_names.compact.join("-") + ".js"
195
+ end
5
196
  end
6
197
  end
7
198
  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-js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
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-25 00:00:00.000000000 Z
11
+ date: 2023-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -37,7 +37,7 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '1.1'
40
- type: :development
40
+ type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
@@ -52,9 +52,6 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - lib/hyde-page-js.rb
55
- - lib/hyde/page/css.rb
56
- - lib/hyde/page/generated_page_css_file.rb
57
- - lib/hyde/renderer.rb
58
55
  homepage: https://github.com/gregdaynes/hyde-page-js
59
56
  licenses:
60
57
  - MIT
@@ -74,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
71
  - !ruby/object:Gem::Version
75
72
  version: '0'
76
73
  requirements: []
77
- rubygems_version: 3.4.10
74
+ rubygems_version: 3.5.3
78
75
  signing_key:
79
76
  specification_version: 4
80
77
  summary: Plugin for jekyll to enable per page js files
data/lib/hyde/page/css.rb DELETED
@@ -1,131 +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
- # original jekyll renderer, not our modified version
90
- data << Jekyll::Renderer.new(@site, tmp_page).run
91
- rescue
92
- Jekyll.logger.warn("Page CSS Warning:", "Unable to find #{path}")
93
- end
94
- end
95
-
96
- data
97
- end
98
-
99
- def minify(data)
100
- style = if config("dev_mode") == true
101
- "expanded"
102
- elsif config("css_minify") == false
103
- "expanded"
104
- else
105
- "compressed"
106
- end
107
-
108
- converter_config = {"sass" => {"style" => style}}
109
- Jekyll::Converters::Scss.new(converter_config).convert(data)
110
- end
111
-
112
- def generate_file(files, data)
113
- file_name = generate_file_name(files, data)
114
-
115
- Hyde::Page::GeneratedPageCssFile.new(@site, config("asset_path"), file_name)
116
- end
117
-
118
- def generate_file_name(files, data, prefix: nil)
119
- file_names = [prefix]
120
-
121
- if config("dev_mode")
122
- files.each { |file| file_names.push(file.gsub(".css", "")) }
123
- end
124
-
125
- file_names.push(Digest::MD5.hexdigest(data)[0, 6])
126
-
127
- file_names.compact.join("-") + ".css"
128
- end
129
- end
130
- end
131
- 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