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
18
19
20
21
22
23
# File 'lib/dimples/sources/base.rb', line 11

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

   = 
  @contents = File.read(@path)

  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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object (private)



76
77
78
# File 'lib/dimples/sources/base.rb', line 76

def method_missing(method_name, *_args)
  [method_name] if .key?(method_name)
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

Instance Method Details

#context(metadata) ⇒ Object



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

def context()
  Object.new.tap do |context|
    .each { |key, variable| context.instance_variable_set("@#{key}", variable) }
  end
end

#default_metadataObject



51
52
53
54
55
56
# File 'lib/dimples/sources/base.rb', line 51

def 
  {
    layout: nil,
    filename: 'index.html'
  }
end

#output_directoryObject



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

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

#render(metadata = {}, body = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/dimples/sources/base.rb', line 38

def render( = {}, body = nil)
  output = template.render(context()) { body }
  return output unless [:layout] && @site.layouts[[:layout]]

  @site.layouts[[:layout]].render(, output)
end

#templateObject

Raises:

  • (NotImplementedError)


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

#urlObject



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

def url
  [:url] || output_directory.gsub(@site.config[:output][:root], '')
end

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dimples/sources/base.rb', line 25

def write(output_path = nil,  = {})
  [:site] ||= @site
  [context_key] ||= self

  output = render()

  output_path = File.join(output_directory, filename) if output_path.nil?
  output_dir = File.dirname(output_path)

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