Class: Dimples::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/dimples/entry.rb

Overview

A class representing a dynamic source entry that can generate rendered content

Direct Known Subclasses

Post, Template

Constant Summary collapse

FRONT_MATTER_PATTERN =
/^(-{3}\n.*?\n?)^(-{3}*$\n?)/m
DEFAULT_FILENAME =
'index.html'

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path(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



87
88
89
# File 'lib/dimples/entry.rb', line 87

def method_missing(method_name, *_args)
  [method_name.to_sym]
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



12
13
14
# File 'lib/dimples/entry.rb', line 12

def contents
  @contents
end

#metadataObject (readonly)

Returns the value of attribute metadata.



12
13
14
# File 'lib/dimples/entry.rb', line 12

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/dimples/entry.rb', line 12

def path
  @path
end

#siteObject (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:, payload: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dimples/entry.rb', line 50

def generate(output_path:, payload: nil)
  payload ||= {}

  unless payload[:url]
    url = output_path.gsub(@site.config.build_paths[:root], '')
    url = File.join(url, filename) unless filename.start_with?('index.')

    payload[:url] = url
  end

  type = self.class.to_s.split('::')[-1].downcase.to_sym

  payload[:site] ||= site
  payload[type] = self

  body = render(payload: payload)
  template = self.template

  loop do
    break if template.nil?

    body = template.render(payload: payload) { 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
39
40
41
42
# 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]))
    @metadata.transform_values! do |value|
      value.is_a?(Hash) ? Context.from_hash(value) : value
    end

    @contents = matches.post_match.strip
  else
    @contents = contents
  end
end

#render(payload: nil) ⇒ Object



83
84
85
# File 'lib/dimples/entry.rb', line 83

def render(payload: nil)
  contents
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/dimples/entry.rb', line 91

def respond_to_missing?(method_name, _include_private = false)
  .key?(method_name.to_sym) || super
end

#templateObject



44
45
46
47
48
# File 'lib/dimples/entry.rb', line 44

def template
  return nil if layout.nil?

  @site.templates[layout.to_sym]
end