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
88
89
90
91
92
|
# File 'lib/dimples/entry.rb', line 88
def method_missing(method_name, *_args)
method_sym = method_name.to_sym
@metadata.send(method_sym)
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, filename) if output_path.nil?
write(output_path: output_path, body: render(context: context))
end
|
#output_directory ⇒ Object
80
81
82
|
# File 'lib/dimples/entry.rb', line 80
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
73
74
75
76
77
78
|
# File 'lib/dimples/entry.rb', line 62
def render(context: {}, body: nil)
context[:site] ||= @site
context[:page] ||= self
@rendered_contents = template.render(
Object.new.tap do |render_context|
context.each do |key, value|
render_context.instance_variable_set("@#{key}", value)
end
end
) { body }
template = @site.layouts[layout.to_sym] unless layout.nil?
return @rendered_contents if template.nil?
template.render(context: context, body: @rendered_contents)
end
|
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
94
95
96
|
# File 'lib/dimples/entry.rb', line 94
def respond_to_missing?(method_name, _include_private = false)
@metadata.respond_to?(method_name.to_sym) || super
end
|
#template ⇒ Object
84
85
86
|
# File 'lib/dimples/entry.rb', line 84
def template
@template ||= Tilt::ERBTemplate.new { @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(filename) unless 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
|