dimples 11.2.0 → 12.0.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: 53b0d98c918f5d4860900213f9797177cc07a801464911856127d8f3bb4e6ac0
4
- data.tar.gz: c48d064da98832cfda7d8a878929dcfa1914e584521bc9cd2eab62f2c9245012
3
+ metadata.gz: 0fcf6e00b6b3aa4b7096a9d7e52d403b5bc6bdd0f4564139d1d625fc6a95f7f6
4
+ data.tar.gz: 7f794a7c556717f7096e18cc82b5d4c0fb64ae36c426c0720bf10dff7538c6c5
5
5
  SHA512:
6
- metadata.gz: 99fe1fdcdd089fa248708aace30b3593ca4935e344206bc37118e20c8421992fe0acf8c10c0b32a713c1b71533b2612f59fd1b089491fa2a5fe4dfb57a4b7abf
7
- data.tar.gz: 1d0a41b232f0f6caacaba68ed9ec39013e40a75812564c6391f7b2120f6cc2556b329cc774cdf6816ac9ac0b1df89059ae8229ca592a5bbd21211fd4991b54d9
6
+ metadata.gz: 15291e14e987e06abd9d4ff514eef20129b7f177f7d3b7016238739060b1de8d33f314cbf02a171974cc43fe2fbbbfd85dcdb478b9bdd868d88132dc998a5033
7
+ data.tar.gz: f7e89be6177ecd1464258e7414445caf79e609c55ca74ddc8458165a53d2008b61b1da49a3d6ff25b105229a8685b80d6d9020999df832d4eacb866edea6c457
@@ -47,7 +47,16 @@ module Dimples
47
47
  def expand_paths(root, paths)
48
48
  root = File.expand_path(root)
49
49
 
50
- paths.transform_values! { |value| File.expand_path(File.join(root, value)) }
50
+ paths = paths.to_h do |key, value|
51
+ expanded_path = if value.is_a?(Hash)
52
+ expand_paths(File.join(root, key.to_s), value)
53
+ else
54
+ File.expand_path(File.join(root, value))
55
+ end
56
+
57
+ [key, expanded_path]
58
+ end
59
+
51
60
  paths.tap { |expanded_paths| expanded_paths[:root] = root }
52
61
  end
53
62
  end
data/lib/dimples/entry.rb CHANGED
@@ -9,9 +9,11 @@ module Dimples
9
9
  FRONT_MATTER_PATTERN = /^(-{3}\n.*?\n?)^(-{3}*$\n?)/m
10
10
  DEFAULT_FILENAME = 'index.html'
11
11
 
12
- attr_reader :path, :contents, :metadata
12
+ attr_reader :site, :path, :contents, :metadata
13
+
14
+ def initialize(site:, source:)
15
+ @site = site
13
16
 
14
- def initialize(source:)
15
17
  contents = case source
16
18
  when Pathname
17
19
  @path = File.expand_path(source)
@@ -35,7 +37,46 @@ module Dimples
35
37
  end
36
38
  end
37
39
 
38
- def render(context: nil)
40
+ def template
41
+ return nil if layout.nil?
42
+
43
+ @site.templates[layout.to_sym]
44
+ end
45
+
46
+ def generate(output_path:, payload: nil)
47
+ payload ||= {}
48
+
49
+ unless payload[:url]
50
+ url = output_path.gsub(@site.config.build_paths[:root], '')
51
+ url = File.join(url, filename) unless filename.start_with?('index.')
52
+
53
+ payload[:url] = url
54
+ end
55
+
56
+ type = self.class.to_s.split('::')[-1].downcase.to_sym
57
+
58
+ payload[:site] ||= site
59
+ payload[type] = @metadata
60
+
61
+ body = render(payload: payload)
62
+ template = self.template
63
+
64
+ loop do
65
+ break if template.nil?
66
+
67
+ body = template.render(payload: payload) { body }.strip
68
+ template = template.template
69
+ end
70
+
71
+ full_path = File.join(output_path, filename)
72
+
73
+ FileUtils.mkdir_p(output_path) unless File.directory?(output_path)
74
+ File.write(full_path, body)
75
+
76
+ full_path
77
+ end
78
+
79
+ def render(payload: nil)
39
80
  contents
40
81
  end
41
82
 
data/lib/dimples/pager.rb CHANGED
@@ -9,8 +9,8 @@ module Dimples
9
9
 
10
10
  attr_reader :current_page, :previous_page, :next_page, :page_count
11
11
 
12
- def self.paginate(url:, posts:, options: {}, context: {}, &block)
13
- new(url: url, posts: posts, options: options).paginate(context: context, &block)
12
+ def self.paginate(url:, posts:, options: {}, payload: {}, &block)
13
+ new(url: url, posts: posts, options: options).paginate(payload: payload, &block)
14
14
  end
15
15
 
16
16
  def initialize(url:, posts:, options: {})
@@ -24,11 +24,11 @@ module Dimples
24
24
  step_to(1)
25
25
  end
26
26
 
27
- def paginate(context: {})
27
+ def paginate(payload: {})
28
28
  (1..@page_count).each do |index|
29
29
  step_to(index)
30
30
 
31
- yield(current_page_path, context.merge(metadata)) if block_given?
31
+ yield(current_page_path, payload.merge(metadata)) if block_given?
32
32
  end
33
33
  end
34
34
 
data/lib/dimples/post.rb CHANGED
@@ -11,18 +11,18 @@ module Dimples
11
11
 
12
12
  attr_reader :rendered_contents
13
13
 
14
- def initialize(path:)
15
- super(source: Pathname.new(path))
14
+ def initialize(site:, path:)
15
+ super(site:, source: Pathname.new(path))
16
16
  end
17
17
 
18
18
  def slug
19
19
  File.basename(path, File.extname(path))&.downcase
20
20
  end
21
21
 
22
- def render(context: nil)
22
+ def render(payload: nil)
23
23
  @rendered_contents ||= Redcarpet::Render::SmartyPants.render(
24
24
  Dimples::Post.markdown.render(contents)
25
- )
25
+ ).strip
26
26
  end
27
27
 
28
28
  private
data/lib/dimples/site.rb CHANGED
@@ -30,7 +30,7 @@ module Dimples
30
30
 
31
31
  def posts
32
32
  @posts ||= source_files(path: :posts, extension: 'markdown').map do |path|
33
- Dimples::Post.new(path: path)
33
+ Dimples::Post.new(site: self, path: path)
34
34
  end
35
35
  .sort_by!(&:date)
36
36
  .reverse!
@@ -38,13 +38,18 @@ module Dimples
38
38
 
39
39
  def pages
40
40
  @pages ||= source_files(path: :pages, extension: 'erb').map do |path|
41
- Dimples::Template.new(path: path)
41
+ Dimples::Template.new(site: self, path: path)
42
42
  end
43
43
  end
44
44
 
45
45
  def templates
46
46
  @templates ||= source_files(path: :templates, extension: 'erb').to_h do |path|
47
- [File.basename(path, '.erb').to_sym, Dimples::Template.new(path: path)]
47
+ key = File.basename(
48
+ path.gsub(@config.source_paths[:templates] + File::SEPARATOR, '').tr(File::SEPARATOR, '_'),
49
+ '.erb'
50
+ )
51
+
52
+ [key.to_sym, Dimples::Template.new(site: self, path: path)]
48
53
  end
49
54
  end
50
55
 
@@ -70,15 +75,16 @@ module Dimples
70
75
 
71
76
  url = @config.build_paths[:posts].gsub(@config.build_paths[:root], '').concat('/')
72
77
 
73
- Pager.paginate(url: url, posts: posts, options: @config.pagination) do |url, page_context|
74
- generate_entry(
75
- entry: templates[:posts],
78
+ Pager.paginate(url: url, posts: posts, options: @config.pagination) do |url, payload|
79
+ templates[:posts].generate(
76
80
  output_path: File.join(@config.build_paths[:root], url),
77
- context: page_context
81
+ payload: payload
78
82
  )
79
83
  end
80
84
 
81
- generate_feed(output_path: @config.build_paths[:root], posts: posts) if @config.generation[:main_feed]
85
+ return unless @config.generation[:main_feed]
86
+
87
+ generate_feed(output_path: @config.build_paths[:root], posts: posts)
82
88
  end
83
89
 
84
90
  def generate_post(post)
@@ -87,7 +93,7 @@ module Dimples
87
93
  @config.build_paths[:posts]
88
94
  ).concat("/#{post.slug}/")
89
95
 
90
- generate_entry(entry: post, output_path: path)
96
+ post.generate(output_path: path)
91
97
  end
92
98
 
93
99
  def generate_pages
@@ -100,75 +106,60 @@ module Dimples
100
106
  @config.build_paths[:root]
101
107
  ).concat('/')
102
108
 
103
- generate_entry(entry: page, output_path: path)
109
+ page.generate(output_path: path)
104
110
  end
105
111
 
106
112
  def generate_categories
107
113
  categories.each do |category, posts|
108
- context = { title: category.capitalize, category: category }
109
-
110
114
  Pager.paginate(
111
115
  url: "/categories/#{category}/",
112
116
  posts: posts,
113
- context: context,
117
+ payload: { title: category.capitalize, category: category },
114
118
  options: @config.pagination
115
- ) do |url, page_context|
116
- generate_entry(
117
- entry: templates[:posts],
119
+ ) do |url, payload|
120
+ templates[:posts].generate(
118
121
  output_path: File.join(@config.build_paths[:root], url),
119
- context: page_context
122
+ payload: payload
120
123
  )
121
124
  end
122
125
 
123
- if @config.generation[:category_feeds]
124
- generate_feed(output_path: File.join(@config.build_paths[:root], 'categories', category), posts: posts)
125
- end
126
+ return unless @config.generation[:category_feeds]
127
+
128
+ generate_feed(
129
+ output_path: File.join(@config.build_paths[:root], 'categories', category),
130
+ posts: posts
131
+ )
126
132
  end
127
133
  end
128
134
 
129
135
  def generate_feed(output_path:, posts:)
130
136
  return if templates[:feed].nil?
131
137
 
132
- generate_entry(
133
- entry: templates[:feed],
138
+ templates[:feed].generate(
134
139
  output_path: output_path,
135
- context: { posts: posts.slice(0, 10) }
140
+ payload: { posts: posts.slice(0, 10) }
136
141
  )
137
142
  end
138
143
 
139
- def generate_entry(entry:, output_path:, context: {})
140
- unless context[:url]
141
- url = output_path.gsub(@config.build_paths[:root], '')
142
- url = File.join(url, entry.filename) unless entry.filename == Dimples::Entry::DEFAULT_FILENAME
143
-
144
- context[:url] = url
145
- end
146
-
147
- context = { page: Context.from_hash(entry.metadata.merge(context)), site: self }
148
- full_path = File.join(output_path, entry.filename)
149
-
150
- FileUtils.mkdir_p(output_path) unless File.directory?(output_path)
151
- File.write(full_path, render_entry(entry: entry, context: context, body: entry.contents))
152
- end
153
-
154
- def render_entry(entry:, context: {}, body: nil)
155
- rendered_body = entry.render(context: context) { body }.strip
156
-
157
- template = templates[entry.layout&.to_sym]
158
- return rendered_body if template.nil?
159
-
160
- render_entry(entry: template, context:, body: rendered_body)
161
- end
162
-
163
144
  def prepare_output_directory
164
145
  if Dir.exist?(@config.build_paths[:root])
165
146
  unless @config.generation[:overwrite_existing_site]
166
147
  raise "The site directory (#{@config.build_paths[:root]}) already exists."
167
148
  end
168
149
 
169
- FileUtils.rm_rf(File.join(@config.build_paths[:root], '**', '*'), secure: true)
170
- else
171
- Dir.mkdir(@config.build_paths[:root])
150
+ FileUtils.rm_rf(@config.build_paths[:root], secure: true)
151
+ end
152
+
153
+ create_build_paths(@config.build_paths)
154
+ end
155
+
156
+ def create_build_paths(paths)
157
+ paths.except(:root).each_value do |path|
158
+ if path.is_a?(Hash)
159
+ create_build_paths(path)
160
+ else
161
+ FileUtils.mkdir_p(path) unless Dir.exist?(path)
162
+ end
172
163
  end
173
164
  end
174
165
 
@@ -5,15 +5,14 @@ require 'erb'
5
5
  module Dimples
6
6
  # A class for a single ERB template file used on a site.
7
7
  class Template < Entry
8
- def initialize(path:)
9
- super(source: Pathname.new(path))
8
+ def initialize(site:, path:)
9
+ super(site:, source: Pathname.new(path))
10
10
  end
11
11
 
12
- def render(context:)
13
- context.merge(metadata).each do |key, value|
14
- instance_variable_set("@#{key}", value)
15
- end
12
+ def render(payload: nil)
13
+ payload ||= {}
16
14
 
15
+ instance_variable_set("@this", Context.from_hash(payload.merge(metadata)))
17
16
  ERB.new(contents).result(binding)
18
17
  end
19
18
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dimples
4
- VERSION = '11.2.0'
4
+ VERSION = '12.0.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dimples
3
3
  version: !ruby/object:Gem::Version
4
- version: 11.2.0
4
+ version: 12.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bogan