Class: Dimples::Entry
- Inherits:
-
Object
- Object
- Dimples::Entry
- Defined in:
- lib/dimples/entry.rb
Overview
A class representing a dynamic source entry that can generate rendered content
Constant Summary collapse
- FRONT_MATTER_PATTERN =
/^(-{3}\n.*?\n?)^(-{3}*$\n?)/m- DEFAULT_FILENAME =
'index.html'
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
- #generate(output_path:, context: nil) ⇒ Object
-
#initialize(site:, source:) ⇒ Entry
constructor
A new instance of Entry.
- #method_missing(method_name, *_args) ⇒ Object
- #parse_metadata(contents) ⇒ Object
- #render(context: nil) ⇒ Object
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
- #template ⇒ Object
Constructor Details
#initialize(site:, source:) ⇒ Entry
Returns a new instance of Entry.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dimples/entry.rb', line 14 def initialize(site:, source:) @site = site contents = case source when Pathname @path = File.(source) File.read(@path) when String source end (contents) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args) ⇒ Object
79 80 81 |
# File 'lib/dimples/entry.rb', line 79 def method_missing(method_name, *_args) [method_name.to_sym] end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def contents @contents end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def @metadata end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def path @path end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def site @site end |
Instance Method Details
#generate(output_path:, context: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dimples/entry.rb', line 46 def generate(output_path:, context: nil) context ||= {} unless context[:url] url = output_path.gsub(@site.config.build_paths[:root], '') url = File.join(url, filename) unless filename == DEFAULT_FILENAME context[:url] = url end context = { page: Context.from_hash(@metadata.merge(context)), site: @site } body = render(context:) template = self.template loop do break if template.nil? body = template.render(context:) { body }.strip template = template.template end full_path = File.join(output_path, filename) FileUtils.mkdir_p(output_path) unless File.directory?(output_path) File.write(full_path, body) full_path end |
#parse_metadata(contents) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dimples/entry.rb', line 28 def (contents) @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 end |
#render(context: nil) ⇒ Object
75 76 77 |
# File 'lib/dimples/entry.rb', line 75 def render(context: nil) contents end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
83 84 85 |
# File 'lib/dimples/entry.rb', line 83 def respond_to_missing?(method_name, _include_private = false) .key?(method_name.to_sym) || super end |
#template ⇒ Object
40 41 42 43 44 |
# File 'lib/dimples/entry.rb', line 40 def template return nil if layout.nil? @site.templates[layout.to_sym] end |