Class: Dimples::Sources::Base

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

Overview

A base class representing a source file with frontmatter metadata that can be rendered.

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:, path:) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
# File 'lib/dimples/sources/base.rb', line 11

def initialize(site:, path:)
  @site = site
  @path = File.expand_path(path)
  @contents = File.read(@path)

  (@contents)
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



9
10
11
# File 'lib/dimples/sources/base.rb', line 9

def contents
  @contents
end

#metadataObject

Returns the value of attribute metadata.



9
10
11
# File 'lib/dimples/sources/base.rb', line 9

def 
  
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/dimples/sources/base.rb', line 9

def path
  @path
end

#rendered_contentsObject

Returns the value of attribute rendered_contents.



9
10
11
# File 'lib/dimples/sources/base.rb', line 9

def rendered_contents
  @rendered_contents
end

Instance Method Details

#output_directoryObject



55
56
57
# File 'lib/dimples/sources/base.rb', line 55

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

#parse_metadata(contents) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dimples/sources/base.rb', line 19

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(context: {}, body: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/dimples/sources/base.rb', line 45

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

  @rendered_contents = template.render(.new(context)) { body }
  return @rendered_contents unless [:layout] && @site.layouts[[:layout]]

  @site.layouts[[:layout]].render(context:, body: @rendered_contents)
end

#templateObject

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/dimples/sources/base.rb', line 63

def template
  raise NotImplementedError, 'You must set a Tilt template for this class.'
end

#url_for(path) ⇒ Object



59
60
61
# File 'lib/dimples/sources/base.rb', line 59

def url_for(path)
  path.gsub(@site.config.build_paths[:root], '').concat('/')
end

#write(output_path: nil, metadata: {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dimples/sources/base.rb', line 33

def write(output_path: nil, metadata: {})
  output_path = File.join(output_directory, filename) if output_path.nil?
  output_dir = File.dirname(output_path)

  [:url] = url_for(output_dir)

  output = render(context: )

  FileUtils.mkdir_p(output_dir) unless File.directory?(output_dir)
  File.write(output_path, output)
end