gryphon_nest 1.2.0 → 1.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6d453e21e5e415707640be17773b49c68e9cc89051e7f4414b1339a6ffc8ced
4
- data.tar.gz: 62c65f5a605cee0c62b1abba8f638afe6fa7cc8f7e3f118787d2f0dcb6aa379c
3
+ metadata.gz: 72b699d93837e7f59c74c2782f38526c2f6bc869d6af875bc174d13e29951560
4
+ data.tar.gz: 6150b1914e70d54e352d181c15750a57c524ae8271ce25de5bb976da4cb0bbf8
5
5
  SHA512:
6
- metadata.gz: 56bf077e811a4ab6bf7a6836f42a929fea615a1e89078d1e6c3df63d753e08ff3e9f8d82d91b1588ac26648543702aa7543d95b212b0455f55cf78a44e95c4ec
7
- data.tar.gz: 73ffb7df7f94d890f6c46e82687010722e7989e3cd5e0c8dad7c5c4a878bb288cfe0b03fd4e5d449a006cd32012153675ec665442fd37508dd25e8f681b7bf46
6
+ metadata.gz: b60a684cf031ca5d37f3fa111e5b552def7d2041b723a8cca940a497db385111d2b54751501d60e0c22c7ef9f74cd14cdd5effef11b713c92fe4c0e590624eeb
7
+ data.tar.gz: 20e53c701a4bf0fa32d265d7fff95ece1a3c52eeb77439c347efd5d55a1401c2ee37ee1e2427cf575c70548220ff8640d193ef9ee5571178c06797233e3ae72b
data/bin/nest CHANGED
File without changes
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'pathname'
5
+
6
+ module GryphonNest
7
+ class FileUtil
8
+ # @params path [String]
9
+ # @return [Array]
10
+ def self.glob(path)
11
+ files = Dir.glob(path).reject do |p|
12
+ File.directory?(p)
13
+ end
14
+
15
+ files.map do |f|
16
+ Pathname.new(f)
17
+ end
18
+ end
19
+
20
+ # @param junk_files [Array]
21
+ def self.delete(junk_files)
22
+ junk_files.each do |f|
23
+ puts "Deleting #{f}"
24
+ FileUtils.remove_file(f)
25
+ end
26
+ end
27
+
28
+ # @param src [Pathname]
29
+ # @param dest [Pathname]
30
+ # @return [Boolean]
31
+ def self.file_newer?(src, dest)
32
+ return true unless dest.exist?
33
+
34
+ src.mtime > dest.mtime
35
+ end
36
+
37
+ # @param path [Pathname]
38
+ # @param content [String]
39
+ def self.write_file(path, content)
40
+ dir = path.dirname
41
+
42
+ unless dir.exist?
43
+ puts "Creating #{dir}"
44
+ dir.mkdir
45
+ end
46
+
47
+ puts "Creating #{path}"
48
+ path.write(content)
49
+ end
50
+ end
51
+ end
@@ -5,51 +5,35 @@ require 'mustache'
5
5
 
6
6
  module GryphonNest
7
7
  # Renders mustache templates to html
8
- class Renderer
9
- # @param layouts [Array]
10
- def initialize
8
+ class Renderer < Mustache
9
+ # @param options [Hash]
10
+ def initialize(options = {})
11
11
  @layouts = {}
12
+ super
12
13
  end
13
14
 
14
15
  # @param template [String]
15
- # @param layout [String]
16
16
  # @param context [Hash]
17
17
  # @return [String]
18
- def render(template, layout, context)
19
- File.open(template) do |f|
20
- content = render_template(f.read, context)
21
- layout = get_template_layout(layout)
18
+ def render_file(template, context = {})
19
+ content = super
22
20
 
23
- unless layout.nil?
24
- context[:yield] = content
25
- content = render_template(layout, context)
26
- end
27
-
28
- HtmlBeautifier.beautify(content)
21
+ if context.key?('layout')
22
+ context['yield'] = content
23
+ content = super(context['layout'], context)
29
24
  end
30
- end
31
25
 
32
- private
33
-
34
- # @param content [String]
35
- # @param context [Hash]
36
- # @return [String]
37
- def render_template(content, context)
38
- Mustache.render(content, context)
26
+ HtmlBeautifier.beautify(content)
39
27
  end
40
28
 
41
- # @param path [String]
42
- # @return [String|null]
43
- def get_template_layout(path)
44
- return @layouts[path] if @layouts.key?(path)
45
-
46
- return unless File.exist?(path)
29
+ # @param name [String]
30
+ # @return [String]
31
+ def partial(name)
32
+ return @layouts[name] if @layouts.key?(name)
47
33
 
48
- File.open(path) do |file|
49
- content = file.read
50
- @layouts[path] = content
51
- return content
52
- end
34
+ content = File.read(name)
35
+ @layouts[name] = content
36
+ content
53
37
  end
54
38
  end
55
39
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GryphonNest
4
- VERSION = '1.2.0'
4
+ VERSION = '1.3.0'
5
5
  end
data/lib/gryphon_nest.rb CHANGED
@@ -8,6 +8,7 @@ require 'yaml'
8
8
  module GryphonNest
9
9
  autoload :NotFoundError, 'gryphon_nest/not_found_error'
10
10
  autoload :Renderer, 'gryphon_nest/renderer'
11
+ autoload :FileUtil, 'gryphon_nest/file_util'
11
12
  autoload :VERSION, 'gryphon_nest/version'
12
13
 
13
14
  class << self
@@ -17,20 +18,22 @@ module GryphonNest
17
18
  ASSETS_DIR = 'assets'
18
19
  LAYOUT_DIR = 'layouts'
19
20
  TEMPLATE_EXT = '.mustache'
21
+ DEFAULT_LAYOUT = Pathname.new("#{LAYOUT_DIR}/main.mustache")
20
22
 
23
+ # @raise [NotFoundError]
21
24
  def build_website
22
25
  raise NotFoundError, "Content directory doesn't exist" unless Dir.exist?(CONTENT_DIR)
23
26
 
24
27
  existing_files = []
25
28
  if Dir.exist?(BUILD_DIR)
26
- existing_files = filter_glob("#{BUILD_DIR}/**/*")
29
+ existing_files = FileUtil.glob("#{BUILD_DIR}/**/*")
27
30
  else
28
31
  Dir.mkdir(BUILD_DIR)
29
32
  end
30
33
 
31
34
  existing_files = existing_files.difference(process_content)
32
35
  existing_files = existing_files.difference(copy_assets)
33
- cleanup(existing_files)
36
+ FileUtil.delete(existing_files)
34
37
  end
35
38
 
36
39
  # @param port [Integer]
@@ -44,72 +47,35 @@ module GryphonNest
44
47
 
45
48
  private
46
49
 
47
- # @param template_name [String]
48
- # @return [String]
49
- def get_output_name(template_name)
50
- dir = File.dirname(template_name)
51
- basename = File.basename(template_name, TEMPLATE_EXT)
50
+ # @param source_file [Pathname]
51
+ # @return [Pathname]
52
+ def get_output_name(source_file)
53
+ dir = source_file.dirname
54
+ basename = source_file.basename(TEMPLATE_EXT)
55
+ path = dir.sub(CONTENT_DIR, BUILD_DIR)
52
56
 
53
- path = Pathname.new(dir)
54
- path = path.sub(CONTENT_DIR, BUILD_DIR)
57
+ path = path.join(basename) if basename.to_s != 'index'
55
58
 
56
- path = path.join(basename) if basename != 'index'
57
-
58
- path = path.join('index.html')
59
- path.to_s
60
- end
61
-
62
- # @params path [String]
63
- # @return [Array]
64
- def filter_glob(path)
65
- Dir.glob(path).reject do |p|
66
- File.directory?(p)
67
- end
68
- end
69
-
70
- # @param path [String]
71
- # @param content [String]
72
- def save_html_file(path, content)
73
- dir = File.dirname(path)
74
-
75
- unless Dir.exist?(dir)
76
- puts "Creating #{dir}"
77
- Dir.mkdir(dir)
78
- end
79
-
80
- puts "Creating #{path}"
81
- File.write(path, content)
59
+ path.join('index.html')
82
60
  end
83
61
 
84
- # @param src [String]
85
- # @param dest [String]
86
- # @param layout_file [String]
87
- # @param context_file [String]
62
+ # @param source_file [Pathname]
63
+ # @param dest_file [Pathname]
64
+ # @param context_file [Pathname, nil]
65
+ # @param layout_file [Pathname, nil]
88
66
  # @return [Boolean]
89
- def can_create_html_file?(src, dest, layout_file, context_file)
90
- return true unless File.exist?(dest)
91
-
92
- src_mtime = File.mtime(src)
93
- dest_mtime = File.mtime(dest)
94
-
95
- return true if src_mtime > dest_mtime
67
+ def resources_updated?(source_file, dest_file, context_file, layout_file)
68
+ return true if FileUtil.file_newer?(source_file, dest_file)
96
69
 
97
- if File.exist?(layout_file)
98
- layout_mtime = File.mtime(layout_file)
99
- return true if layout_mtime > dest_mtime
100
- end
101
-
102
- if File.exist?(context_file)
103
- context_mtime = File.mtime(context_file)
104
- return true if context_mtime > dest_mtime
105
- end
70
+ return true if !context_file.nil? && FileUtil.file_newer?(context_file, dest_file)
106
71
 
107
- false
72
+ !layout_file.nil? && FileUtil.file_newer?(layout_file, dest_file)
108
73
  end
109
74
 
110
75
  # @param name [String]
111
76
  # @param context [Hash]
112
- # @return [String]
77
+ # @return [Pathname, nil]
78
+ # @raise [NotFoundError]
113
79
  def get_layout_file(name, context)
114
80
  path = Pathname.new(LAYOUT_DIR)
115
81
 
@@ -117,100 +83,85 @@ module GryphonNest
117
83
  layout = context['layout']
118
84
  path = path.join(layout)
119
85
 
120
- raise NotFoundError, "#{name} requires layout file #{layout} but it doesn't exist or can't be read" unless File.exist?(path)
86
+ raise NotFoundError, "#{name} requires layout file #{layout} but it doesn't exist or can't be read" unless path.exist?
121
87
 
122
- return path.to_s
88
+ return path
123
89
  end
124
90
 
125
- path.join('main.mustache').to_s
91
+ DEFAULT_LAYOUT if DEFAULT_LAYOUT.exist?
126
92
  end
127
93
 
128
- # @param path [String]
94
+ # @param path [Pathname, nil]
129
95
  # @return [Hash]
130
- def read_context_file(path)
131
- return {} if path == ''
132
-
133
- return {} unless File.exist?(path)
96
+ def get_context(path)
97
+ return {} if path.nil?
134
98
 
135
99
  File.open(path) do |yaml|
136
100
  YAML.safe_load(yaml)
137
101
  end
138
102
  end
139
103
 
140
- # @param name [String]
141
- # @return [String]
104
+ # @param name [Pathname]
105
+ # @return [Pathname, nil]
142
106
  def get_context_file(name)
143
- basename = File.basename(name, TEMPLATE_EXT)
107
+ basename = name.basename(TEMPLATE_EXT)
108
+ FileUtil.glob("#{DATA_DIR}/#{basename}.{yaml,yml}")[0]
109
+ end
144
110
 
145
- Dir.glob("#{DATA_DIR}/#{basename}.{yaml,yml}") do |f|
146
- return f
147
- end
111
+ # @param renderer [Renderer]
112
+ # @param source_file [Pathname]
113
+ # @param dest_file [Pathname]
114
+ # @raise [NotFoundError]
115
+ def process_file(renderer, source_file, dest_file)
116
+ context_file = get_context_file(source_file)
117
+ context = get_context(context_file)
118
+ layout_file = get_layout_file(source_file.basename, context)
119
+ context['layout'] = layout_file unless layout_file.nil?
120
+
121
+ return unless resources_updated?(source_file, dest_file, context_file, layout_file)
148
122
 
149
- ''
123
+ content = renderer.render_file(source_file, context)
124
+ FileUtil.write_file(dest_file, content)
150
125
  end
151
126
 
152
127
  # @return [Array]
128
+ # @raise [NotFoundError]
153
129
  def process_content
154
130
  created_files = []
155
- renderer = Renderer.new
156
131
 
157
- filter_glob("#{CONTENT_DIR}/**/*").each do |template|
158
- if File.extname(template) != TEMPLATE_EXT
159
- puts "Skipping non template file #{template}"
132
+ renderer = Renderer.new
133
+ FileUtil.glob("#{CONTENT_DIR}/**/*").each do |source_file|
134
+ if source_file.extname != TEMPLATE_EXT
135
+ warn "Skipping non template file #{source_file}"
160
136
  next
161
137
  end
162
138
 
163
- dest_file = get_output_name(template)
164
- context_file = get_context_file(template)
165
- context = read_context_file(context_file)
166
- layout_file = get_layout_file(template, context)
167
-
139
+ dest_file = get_output_name(source_file)
168
140
  created_files << dest_file
169
- next unless can_create_html_file?(template, dest_file, layout_file, context_file)
170
-
171
- content = renderer.render(template, layout_file, context)
172
- save_html_file(dest_file, content)
141
+ process_file(renderer, source_file, dest_file)
173
142
  end
174
143
 
175
144
  created_files
176
145
  end
177
146
 
178
- # @param src [String]
179
- # @param dest [String]
180
- # @return [Boolean]
181
- def can_copy_asset?(src, dest)
182
- return true unless File.exist?(dest)
183
-
184
- File.mtime(src) > File.mtime(dest)
185
- end
186
-
187
147
  # @return [Array]
188
148
  def copy_assets
189
149
  return [] unless Dir.exist?(ASSETS_DIR)
190
150
 
191
151
  copied_files = []
192
- filter_glob("#{ASSETS_DIR}/**/*").each do |asset|
193
- dest = Pathname.new(asset)
194
- dest = dest.sub(ASSETS_DIR, BUILD_DIR)
195
- copied_files << dest.to_s
152
+ FileUtil.glob("#{ASSETS_DIR}/**/*").each do |asset|
153
+ dest = asset.sub(ASSETS_DIR, BUILD_DIR)
154
+ copied_files << dest
196
155
 
197
- next unless can_copy_asset?(asset, dest)
156
+ next unless FileUtil.file_newer?(asset, dest)
198
157
 
199
158
  puts "Copying #{asset} to #{dest}"
200
- dest_dir = File.dirname(dest)
159
+ dest_dir = dest.dirname
201
160
  FileUtils.makedirs(dest_dir)
202
161
  FileUtils.copy_file(asset, dest)
203
162
  end
204
163
 
205
164
  copied_files
206
165
  end
207
-
208
- # @param junk_files [Array]
209
- def cleanup(junk_files)
210
- junk_files.each do |f|
211
- puts "Deleting #{f}"
212
- FileUtils.remove_file(f)
213
- end
214
- end
215
166
  end
216
167
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gryphon_nest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Birmingham
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-28 00:00:00.000000000 Z
11
+ date: 2023-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: htmlbeautifier
@@ -106,6 +106,7 @@ files:
106
106
  - LICENSE
107
107
  - bin/nest
108
108
  - lib/gryphon_nest.rb
109
+ - lib/gryphon_nest/file_util.rb
109
110
  - lib/gryphon_nest/not_found_error.rb
110
111
  - lib/gryphon_nest/renderer.rb
111
112
  - lib/gryphon_nest/version.rb
@@ -115,7 +116,7 @@ licenses:
115
116
  metadata:
116
117
  homepage_uri: https://github.com/chrisBirmingham/gryphon_nest
117
118
  source_code_uri: https://github.com/chrisBirmingham/gryphon_nest
118
- post_install_message:
119
+ post_install_message:
119
120
  rdoc_options: []
120
121
  require_paths:
121
122
  - lib
@@ -130,8 +131,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  - !ruby/object:Gem::Version
131
132
  version: '0'
132
133
  requirements: []
133
- rubygems_version: 3.1.2
134
- signing_key:
134
+ rubygems_version: 3.3.7
135
+ signing_key:
135
136
  specification_version: 4
136
137
  summary: Yet another static site generator
137
138
  test_files: []