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
- #assign_metadata ⇒ Object
-
#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_for(path) ⇒ 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 20 |
# File 'lib/dimples/sources/base.rb', line 11 def initialize(site, path) @site = site @path = File.(path) @metadata = @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 @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
#assign_metadata ⇒ Object
30 31 32 33 34 |
# File 'lib/dimples/sources/base.rb', line 30 def @metadata.each_key do |key| self.class.send(:define_method, key.to_sym) { @metadata[key] } end end |
#output_directory ⇒ Object
58 59 60 |
# File 'lib/dimples/sources/base.rb', line 58 def output_directory @site.config[:output][:root] end |
#parse_metadata(contents) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/dimples/sources/base.rb', line 22 def (contents) 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 |
#render(render_metadata = {}, body = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/dimples/sources/base.rb', line 48 def render( = {}, body = nil) [:site] ||= @site. [:page] ||= output = template.render(Metadata.new()) { body } return output unless @metadata[:layout] && @site.layouts[@metadata[:layout]] @site.layouts[@metadata[:layout]].render(, 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_for(path) ⇒ Object
62 63 64 |
# File 'lib/dimples/sources/base.rb', line 62 def url_for(path) path.gsub(@site.config[:output][:root], '').concat('/') end |
#write(output_path = nil, metadata = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dimples/sources/base.rb', line 36 def write(output_path = nil, = {}) output_path = File.join(output_directory, filename) if output_path.nil? output_dir = File.dirname(output_path) @metadata[:url] = url_for(output_dir) output = render() FileUtils.mkdir_p(output_dir) unless File.directory?(output_dir) File.write(output_path, output) end |