dimples 2.6.2 → 2.7.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
  SHA1:
3
- metadata.gz: 5e5f03c50fe8926f6f855a323f979cbbb6f1d229
4
- data.tar.gz: d7787365993d7c243af27c594378f3600d42fdbd
3
+ metadata.gz: 65b3390259e961fbd65ac4deee280a236440ae2f
4
+ data.tar.gz: ff08dd71317105a3020fe348cab53187a7cbff89
5
5
  SHA512:
6
- metadata.gz: 030acdc4c08d8a860a6f593951434da2ab9ffbc4cc3bd678efca07ccf994251b0c33edc30b8a42631656a14e0516709f10bf20712c75b47f998a294352fe42ab
7
- data.tar.gz: e7e446e86609c455a65ec55ac08fac9e21a577b3940662e1df7b6b4963c8ec7f369d5598550c864b181d2a1870d6189b3909389d3b9c4fed406a26cd8e418be1
6
+ metadata.gz: 873f3072be0e2908e6a07f8fe68c4c304602f37f2397e8714f05ded6affb932b4741adfebe4e56dd5885194c4a744e1ff5848bc21b0f13a837038783b88233d0
7
+ data.tar.gz: 8fad5b09ff01ffcb3cfc7beced962a523a53abc81f3058d265bfd633f3872d4b38b70757814875cf64e1e0550e410c771bafe2fad1a211f369ace6a50bf50b79
data/lib/dimples.rb CHANGED
@@ -12,13 +12,12 @@ require 'dimples/errors'
12
12
  require 'dimples/logger'
13
13
 
14
14
  require 'dimples/frontable'
15
- require 'dimples/writeable'
16
- require 'dimples/renderable'
17
15
 
18
16
  require 'dimples/category'
19
17
  require 'dimples/configuration'
20
18
  require 'dimples/page'
21
19
  require 'dimples/post'
20
+ require 'dimples/renderer'
22
21
  require 'dimples/site'
23
22
  require 'dimples/template'
24
23
 
data/lib/dimples/page.rb CHANGED
@@ -4,12 +4,9 @@ module Dimples
4
4
  # A class that models a single site page.
5
5
  class Page
6
6
  include Frontable
7
- include Writeable
8
- include Renderable
9
7
 
10
8
  attr_accessor :path
11
9
  attr_accessor :title
12
- attr_accessor :template
13
10
  attr_accessor :filename
14
11
  attr_accessor :extension
15
12
  attr_accessor :layout
@@ -41,5 +38,27 @@ module Dimples
41
38
 
42
39
  File.join(parts)
43
40
  end
41
+
42
+ def render(context = {})
43
+ renderer.render(context)
44
+ end
45
+
46
+ def renderer
47
+ @renderer ||= Renderer.new(@site, self)
48
+ end
49
+
50
+ def write(path, context = {})
51
+ output = context ? render(context) : contents
52
+ parent_path = File.dirname(path)
53
+
54
+ FileUtils.mkdir_p(parent_path) unless Dir.exist?(parent_path)
55
+
56
+ File.open(path, 'w+') do |file|
57
+ file.write(output)
58
+ end
59
+ rescue SystemCallError => e
60
+ error_message = "Failed to write #{path} (#{e.message})"
61
+ raise Errors::PublishingError, error_message
62
+ end
44
63
  end
45
64
  end
data/lib/dimples/post.rb CHANGED
@@ -2,24 +2,12 @@
2
2
 
3
3
  module Dimples
4
4
  # A class that models a single site post.
5
- class Post
6
- include Frontable
7
- include Writeable
8
- include Renderable
9
-
10
- attr_accessor :path
11
- attr_accessor :title
5
+ class Post < Page
12
6
  attr_accessor :categories
13
- attr_accessor :template
14
- attr_accessor :filename
15
- attr_accessor :extension
16
- attr_accessor :layout
17
- attr_accessor :contents
18
7
  attr_accessor :slug
19
8
  attr_accessor :year
20
9
  attr_accessor :month
21
10
  attr_accessor :day
22
- attr_accessor :rendered_contents
23
11
  attr_accessor :previous_post
24
12
  attr_accessor :next_post
25
13
  attr_reader :date
@@ -27,18 +15,15 @@ module Dimples
27
15
  FILENAME_DATE = /(\d{4})-(\d{2})-(\d{2})-(.+)/
28
16
 
29
17
  def initialize(site, path)
30
- @site = site
31
- @path = path
32
- @filename = 'index'
33
- @extension = 'html'
18
+ super(site, path)
34
19
 
35
20
  parts = File.basename(path, File.extname(path)).match(FILENAME_DATE)
36
21
 
22
+ @filename = 'index'
37
23
  @slug = parts[4]
38
- self.date = Time.mktime(parts[1], parts[2], parts[3])
39
-
40
24
  @layout = @site.config['layouts']['post']
41
- @contents = read_with_front_matter(path)
25
+
26
+ self.date = Time.mktime(parts[1], parts[2], parts[3])
42
27
  end
43
28
 
44
29
  def date=(date)
@@ -50,7 +35,7 @@ module Dimples
50
35
  end
51
36
 
52
37
  def output_path(parent_path)
53
- parent_path = @date.strftime(parent_path) if parent_path =~ /%/
38
+ parent_path = @date.strftime(parent_path) if parent_path.match?(/%/)
54
39
  File.join([parent_path, @slug.to_s, "#{@filename}.#{@extension}"])
55
40
  end
56
41
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dimples
4
+ # A class that renders the contents of a document into markup.
5
+ class Renderer
6
+ def initialize(site, source)
7
+ @site = site
8
+ @source = source
9
+
10
+ callback = proc { @source.contents }
11
+
12
+ @engine = if @source.path
13
+ Tilt.new(@source.path, {}, &callback)
14
+ else
15
+ Tilt::StringTemplate.new(&callback)
16
+ end
17
+ end
18
+
19
+ def render(context = {}, body = nil)
20
+ output = @engine.render(scope(context)) { body }.strip
21
+ @source.rendered_contents = output
22
+
23
+ if template = @site.templates[@source.layout]
24
+ output = template.render(context, output)
25
+ end
26
+
27
+ output
28
+ end
29
+
30
+ def scope(context = {})
31
+ context[:site] ||= @site
32
+ context[:this] ||= @source
33
+ context[:type] ||= @source.class.name.split('::').last.downcase.to_sym
34
+
35
+ Object.new.tap do |scope|
36
+ context.each_pair do |key, value|
37
+ scope.instance_variable_set("@#{key}".to_sym, value)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
data/lib/dimples/site.rb CHANGED
@@ -35,6 +35,23 @@ module Dimples
35
35
  set_output_paths
36
36
  end
37
37
 
38
+ def generate
39
+ prepare_output_directory
40
+ scan_files
41
+ generate_files
42
+ copy_assets
43
+ rescue Errors::RenderingError,
44
+ Errors::PublishingError,
45
+ Errors::GenerationError => e
46
+ @errors << e.message
47
+ end
48
+
49
+ def generated?
50
+ @errors.count.zero?
51
+ end
52
+
53
+ private
54
+
38
55
  def set_source_paths
39
56
  @source_paths = {
40
57
  root: File.expand_path(@config['source_path'])
@@ -56,23 +73,6 @@ module Dimples
56
73
  end
57
74
  end
58
75
 
59
- def generate
60
- prepare_output_directory
61
- scan_files
62
- generate_files
63
- copy_assets
64
- rescue Errors::RenderingError,
65
- Errors::PublishingError,
66
- Errors::GenerationError => e
67
- @errors << e.message
68
- end
69
-
70
- def generated?
71
- @errors.count.zero?
72
- end
73
-
74
- private
75
-
76
76
  def prepare_output_directory
77
77
  if Dir.exist?(@output_paths[:site])
78
78
  FileUtils.remove_dir(@output_paths[:site])
@@ -136,11 +136,9 @@ module Dimples
136
136
 
137
137
  def scan_post(path)
138
138
  @post_class.new(self, path).tap do |post|
139
- unless post.categories.nil?
140
- post.categories.each do |slug|
141
- @categories[slug] ||= Dimples::Category.new(self, slug)
142
- @categories[slug].posts << post
143
- end
139
+ post.categories&.each do |slug|
140
+ @categories[slug] ||= Dimples::Category.new(self, slug)
141
+ @categories[slug].posts << post
144
142
  end
145
143
 
146
144
  add_post_to_archives(post)
@@ -337,7 +335,7 @@ module Dimples
337
335
  url: url
338
336
  }
339
337
 
340
- if (index - 1) > 0
338
+ if (index - 1).positive?
341
339
  pagination[:previous_page] = index - 1
342
340
  pagination[:previous_page_url] = url
343
341
 
@@ -4,11 +4,11 @@ module Dimples
4
4
  # A class that models a single template.
5
5
  class Template
6
6
  include Frontable
7
- include Renderable
8
7
 
9
- attr_accessor :slug
10
- attr_accessor :title
11
8
  attr_accessor :path
9
+ attr_accessor :title
10
+ attr_accessor :slug
11
+ attr_accessor :layout
12
12
  attr_accessor :contents
13
13
  attr_accessor :rendered_contents
14
14
 
@@ -19,5 +19,13 @@ module Dimples
19
19
 
20
20
  @contents = read_with_front_matter(path)
21
21
  end
22
+
23
+ def render(context = {}, body = nil)
24
+ renderer.render(context, body)
25
+ end
26
+
27
+ def renderer
28
+ @renderer ||= Renderer.new(@site, self)
29
+ end
22
30
  end
23
31
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dimples
4
- VERSION = '2.6.2'.freeze
4
+ VERSION = '2.7.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dimples
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.2
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bogan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-21 00:00:00.000000000 Z
11
+ date: 2017-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tilt
@@ -146,10 +146,10 @@ files:
146
146
  - lib/dimples/page.rb
147
147
  - lib/dimples/post.rb
148
148
  - lib/dimples/renderable.rb
149
+ - lib/dimples/renderer.rb
149
150
  - lib/dimples/site.rb
150
151
  - lib/dimples/template.rb
151
152
  - lib/dimples/version.rb
152
- - lib/dimples/writeable.rb
153
153
  homepage: http://github.com/waferbaby/dimples
154
154
  licenses:
155
155
  - MIT
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Dimples
4
- # A mixin class that neatly handles writing out a file.
5
- module Writeable
6
- def write(path, context = {})
7
- output = context ? render(context) : contents
8
- parent_path = File.dirname(path)
9
-
10
- FileUtils.mkdir_p(parent_path) unless Dir.exist?(parent_path)
11
-
12
- File.open(path, 'w+') do |file|
13
- file.write(output)
14
- end
15
- rescue SystemCallError => e
16
- error_message = "Failed to write #{path} (#{e.message})"
17
- raise Errors::PublishingError, error_message
18
- end
19
- end
20
- end