Class: Dimples::Entry

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

Overview

A class representing a dynamic source entry

Direct Known Subclasses

Layout, Page, Post

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site:, source:) ⇒ Entry

Returns a new instance of Entry.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dimples/entry.rb', line 16

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



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

def method_missing(method_name, *_args)
  .send(method_name)
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



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

def contents
  @contents
end

#metadataObject (readonly)

Returns the value of attribute metadata.



14
15
16
# File 'lib/dimples/entry.rb', line 14

def 
  
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#rendered_contentsObject

Returns the value of attribute rendered_contents.



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

def rendered_contents
  @rendered_contents
end

Instance Method Details

#generate(output_path: nil, context: {}) ⇒ Object



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

def generate(output_path: nil, context: {})
  output_path = File.join(output_directory, [:filename]) if output_path.nil?
  write(output_path: output_path, body: render(context: context))
end

#output_directoryObject



74
75
76
# File 'lib/dimples/entry.rb', line 74

def output_directory
  @output_directory ||= @site.config.build_paths[:root]
end

#parse_metadata(contents) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dimples/entry.rb', line 30

def (contents)
   = 

  matches = contents.match(FRONT_MATTER_PATTERN)
  if matches
    .merge!(YAML.safe_load(matches[1], symbolize_names: true, permitted_classes: [Date]))
    @contents = matches.post_match.strip
  else
    @contents = contents
  end

   = .new()
end

#render(context: {}, body: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dimples/entry.rb', line 62

def render(context: {}, body: nil)
  context[:site] ||= @site.
  context[:page] ||= 

  @rendered_contents = template.render(.new(context)) { body }

  layout = @site.layouts[.layout.to_sym] if .layout
  return @rendered_contents if layout.nil?

  layout.render(context: context, body: @rendered_contents)
end

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

Returns:

  • (Boolean)


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

def respond_to_missing?(_method_name, _include_private = false)
  true
end

#templateObject



78
79
80
# File 'lib/dimples/entry.rb', line 78

def template
  @template ||= Tilt::ERBTemplate.new { @contents }
end

#to_hObject



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

def to_h
  .to_h.merge({ contents: contents })
end

#urlObject



56
57
58
59
60
# File 'lib/dimples/entry.rb', line 56

def url
  @url ||= output_directory.gsub(@site.config.build_paths[:root], '').tap do |url|
    url.concat([:filename]) unless [:filename] == 'index.html'
  end
end

#write(output_path:, body:) ⇒ Object



49
50
51
52
53
54
# File 'lib/dimples/entry.rb', line 49

def write(output_path:, body:)
  parent_directory = File.dirname(output_path)

  FileUtils.mkdir_p(parent_directory) unless File.directory?(parent_directory)
  File.write(output_path, body)
end