Class: Dimples::Entry
- Inherits:
-
Object
show all
- Includes:
- Forwardable
- Defined in:
- lib/dimples/entry.rb
Overview
A class representing a dynamic source entry
Constant Summary
collapse
- FRONT_MATTER_PATTERN =
/^(-{3}\n.*?\n?)^(-{3}*$\n?)/m
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(site:, source:) ⇒ Entry
Returns a new instance of Entry.
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/dimples/entry.rb', line 16
def initialize(site:, source:)
@site = site
contents = case source
when Pathname
@path = File.expand_path(source)
File.read(@path)
when String
source
end
parse_metadata(contents)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args) ⇒ Object
86
87
88
|
# File 'lib/dimples/entry.rb', line 86
def method_missing(method_name, *_args)
@metadata.send(method_name)
end
|
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
13
14
15
|
# File 'lib/dimples/entry.rb', line 13
def contents
@contents
end
|
Returns the value of attribute metadata.
14
15
16
|
# File 'lib/dimples/entry.rb', line 14
def metadata
@metadata
end
|
#path ⇒ Object
Returns the value of attribute path.
13
14
15
|
# File 'lib/dimples/entry.rb', line 13
def path
@path
end
|
#rendered_contents ⇒ Object
Returns the value of attribute rendered_contents.
13
14
15
|
# File 'lib/dimples/entry.rb', line 13
def rendered_contents
@rendered_contents
end
|
Instance Method Details
#generate(output_path: nil, context: {}) ⇒ Object
44
45
46
47
|
# File 'lib/dimples/entry.rb', line 44
def generate(output_path: nil, context: {})
output_path = File.join(output_directory, @metadata[:filename]) if output_path.nil?
write(output_path: output_path, body: render(context: context))
end
|
#output_directory ⇒ Object
74
75
76
|
# File 'lib/dimples/entry.rb', line 74
def output_directory
@output_directory ||= @site.config.build_paths[:root]
end
|
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/dimples/entry.rb', line 30
def parse_metadata(contents)
metadata = default_metadata
matches = contents.match(FRONT_MATTER_PATTERN)
if matches
metadata.merge!(YAML.safe_load(matches[1], symbolize_names: true, permitted_classes: [Date]))
@contents = matches.post_match.strip
else
@contents = contents
end
@metadata = Metadata.new(metadata)
end
|
#render(context: {}, body: nil) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/dimples/entry.rb', line 62
def render(context: {}, body: nil)
context[:site] ||= @site.metadata
context[:page] ||= @metadata
@rendered_contents = template.render(Metadata.new(context)) { body }
layout = @site.layouts[@metadata.layout.to_sym] if @metadata.layout
return @rendered_contents if layout.nil?
layout.render(context: context, body: @rendered_contents)
end
|
#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean
90
91
92
|
# File 'lib/dimples/entry.rb', line 90
def respond_to_missing?(_method_name, _include_private = false)
true
end
|
#template ⇒ Object
78
79
80
|
# File 'lib/dimples/entry.rb', line 78
def template
@template ||= Tilt::ERBTemplate.new { @contents }
end
|
#to_h ⇒ Object
82
83
84
|
# File 'lib/dimples/entry.rb', line 82
def to_h
@metadata.to_h.merge({ contents: contents })
end
|
#url ⇒ Object
56
57
58
59
60
|
# File 'lib/dimples/entry.rb', line 56
def url
@url ||= output_directory.gsub(@site.config.build_paths[:root], '').tap do |url|
url.concat(@metadata[:filename]) unless @metadata[:filename] == 'index.html'
end
end
|
#write(output_path:, body:) ⇒ Object
49
50
51
52
53
54
|
# File 'lib/dimples/entry.rb', line 49
def write(output_path:, body:)
parent_directory = File.dirname(output_path)
FileUtils.mkdir_p(parent_directory) unless File.directory?(parent_directory)
File.write(output_path, body)
end
|