Class: Dimples::Sources::Base
- Inherits:
-
Object
- Object
- Dimples::Sources::Base
- 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
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(site, path) ⇒ Base
constructor
A new instance of Base.
- #output_directory ⇒ Object
- #parse_metadata(contents) ⇒ Object
- #render(render_metadata = {}, body = nil) ⇒ Object
- #template ⇒ Object
- #url ⇒ Object
- #write(output_path = nil, metadata = {}) ⇒ Object
Constructor Details
#initialize(site, path) ⇒ Base
Returns a new instance of Base.
11 12 13 14 15 16 17 18 19 |
# File 'lib/dimples/sources/base.rb', line 11 def initialize(site, path) @site = site @path = File.(path) = @contents = File.read(@path) (@contents) 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 |
#metadata ⇒ Object
Returns the value of attribute metadata.
9 10 11 |
# File 'lib/dimples/sources/base.rb', line 9 def 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
#output_directory ⇒ Object
53 54 55 |
# File 'lib/dimples/sources/base.rb', line 53 def output_directory @site.config[:output][:root] end |
#parse_metadata(contents) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dimples/sources/base.rb', line 21 def (contents) matches = contents.match(FRONT_MATTER_PATTERN) return unless matches .merge!(YAML.safe_load(matches[1], symbolize_names: true, permitted_classes: [Date])) @contents = matches.post_match.strip .each_key do |key| self.class.send(:define_method, key.to_sym) { [key] } end end |
#render(render_metadata = {}, body = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/dimples/sources/base.rb', line 43 def render( = {}, body = nil) [:site] ||= @site. [:page] ||= output = template.render(Metadata.new()) { body } return output unless [:layout] && @site.layouts[[:layout]] @site.layouts[[:layout]].render(, output) end |
#template ⇒ Object
61 62 63 |
# File 'lib/dimples/sources/base.rb', line 61 def template raise NotImplementedError, 'You must set a Tilt template for this class.' end |
#url ⇒ Object
57 58 59 |
# File 'lib/dimples/sources/base.rb', line 57 def url [:url] || output_directory.gsub(@site.config[:output][:root], '') end |
#write(output_path = nil, metadata = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/dimples/sources/base.rb', line 33 def write(output_path = nil, = {}) output = render() 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 |