Class: Dimples::Page

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

Overview

A single page on a site.

Direct Known Subclasses

Feed, Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, path = nil) ⇒ Page

Returns a new instance of Page.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dimples/page.rb', line 10

def initialize(site, path = nil)
  @site = site
  @path = path

  if @path
    data = File.read(@path)
    @contents, @metadata = FrontMatter.parse(data)
  else
    @contents = ''
    @metadata = {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



55
56
57
58
59
60
61
62
63
# File 'lib/dimples/page.rb', line 55

def method_missing(method_name, *args, &block)
  if @metadata.key?(method_name)
    @metadata[method_name]
  elsif (matches = method_name.match(/([a-z_]+)=/))
    @metadata[matches[1].to_sym] = args[0]
  else
    super
  end
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



6
7
8
# File 'lib/dimples/page.rb', line 6

def contents
  @contents
end

#metadataObject

Returns the value of attribute metadata.



7
8
9
# File 'lib/dimples/page.rb', line 7

def 
  @metadata
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/dimples/page.rb', line 8

def path
  @path
end

Instance Method Details

#extensionObject



27
28
29
# File 'lib/dimples/page.rb', line 27

def extension
  @metadata[:extension] || 'html'
end

#filenameObject



23
24
25
# File 'lib/dimples/page.rb', line 23

def filename
  @metadata[:filename] || 'index'
end

#render(context = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/dimples/page.rb', line 31

def render(context = {})
   = @metadata.dup
  .merge!(context[:page]) if context[:page]

  context[:page] = Hashie::Mash.new()

  renderer.render(context)
end

#write(output_directory, context = {}) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/dimples/page.rb', line 40

def write(output_directory, context = {})
  FileUtils.mkdir_p(output_directory) unless Dir.exist?(output_directory)
  output_path = File.join(output_directory, "#{filename}.#{extension}")

  File.write(output_path, render(context))
rescue SystemCallError => e
  raise PublishingError, "Failed to publish file at #{output_path} (#{e})"
end