Class: Dimples::Sources::Base
- Inherits:
-
Object
- Object
- Dimples::Sources::Base
show all
- Defined in:
- lib/dimples/sources/base.rb
Overview
A base class representing a source file with frontmatter metadata that can be rendered.
Constant Summary
collapse
- FRONT_MATTER_PATTERN =
/^(-{3}\n.*?\n?)^(-{3}*$\n?)/m
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(site, path) ⇒ Base
Returns a new instance of Base.
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/dimples/sources/base.rb', line 11
def initialize(site, path)
@site = site
@path = File.expand_path(path)
@metadata = default_metadata
@contents = File.read(@path)
matches = @contents.match(FRONT_MATTER_PATTERN)
return unless matches
@metadata.merge!(YAML.safe_load(matches[1], symbolize_names: true, permitted_classes: [Date]))
@contents = matches.post_match.strip
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args) ⇒ Object
76
77
78
|
# File 'lib/dimples/sources/base.rb', line 76
def method_missing(method_name, *_args)
@metadata[method_name] if @metadata.key?(method_name)
end
|
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
9
10
11
|
# File 'lib/dimples/sources/base.rb', line 9
def contents
@contents
end
|
Returns the value of attribute metadata.
9
10
11
|
# File 'lib/dimples/sources/base.rb', line 9
def metadata
@metadata
end
|
#path ⇒ Object
Returns the value of attribute path.
9
10
11
|
# File 'lib/dimples/sources/base.rb', line 9
def path
@path
end
|
Instance Method Details
#context(metadata) ⇒ Object
45
46
47
48
49
|
# File 'lib/dimples/sources/base.rb', line 45
def context(metadata)
Object.new.tap do |context|
metadata.each { |key, variable| context.instance_variable_set("@#{key}", variable) }
end
end
|
51
52
53
54
55
56
|
# File 'lib/dimples/sources/base.rb', line 51
def default_metadata
{
layout: nil,
filename: 'index.html'
}
end
|
#output_directory ⇒ Object
58
59
60
|
# File 'lib/dimples/sources/base.rb', line 58
def output_directory
@site.config[:output][:root]
end
|
#render(metadata = {}, body = nil) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/dimples/sources/base.rb', line 38
def render(metadata = {}, body = nil)
output = template.render(context(metadata)) { body }
return output unless @metadata[:layout] && @site.layouts[@metadata[:layout]]
@site.layouts[@metadata[:layout]].render(metadata, output)
end
|
#template ⇒ Object
66
67
68
|
# File 'lib/dimples/sources/base.rb', line 66
def template
raise NotImplementedError, 'You must set a Tilt template for this class.'
end
|
#url ⇒ Object
62
63
64
|
# File 'lib/dimples/sources/base.rb', line 62
def url
@metadata[:url] || output_directory.gsub(@site.config[:output][:root], '')
end
|
#write(output_path = nil, metadata = {}) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/dimples/sources/base.rb', line 25
def write(output_path = nil, metadata = {})
metadata[:site] ||= @site
metadata[context_key] ||= self
output = render(metadata)
output_path = File.join(output_directory, filename) if output_path.nil?
output_dir = File.dirname(output_path)
FileUtils.mkdir_p(output_dir) unless File.directory?(output_dir)
File.write(output_path, output)
end
|