Class: Dimples::Page

Inherits:
Object
  • Object
show all
Includes:
Frontable, Renderable
Defined in:
lib/dimples/page.rb

Overview

A class that models a single site page.

Direct Known Subclasses

Post

Constant Summary

Constants included from Frontable

Frontable::SKIPPED_METADATA_KEYS

Instance Attribute Summary collapse

Attributes included from Renderable

#rendered_contents

Instance Method Summary collapse

Methods included from Renderable

#render, #rendering_engine

Methods included from Frontable

#read_with_front_matter

Constructor Details

#initialize(site, path = nil) ⇒ Page

Returns a new instance of Page.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dimples/page.rb', line 17

def initialize(site, path = nil)
  @site = site
  @extension = 'html'
  @path = path

  if @path
    @filename = File.basename(@path, File.extname(@path))
    @output_directory = File.dirname(@path).gsub(
      @site.source_paths[:pages],
      @site.output_paths[:site]
    )

    read_with_front_matter
  else
    @filename = 'index'
    @contents = ''
    @output_directory = @site.output_paths[:site]
  end
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



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

def contents
  @contents
end

#extensionObject

Returns the value of attribute extension.



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

def extension
  @extension
end

#filenameObject

Returns the value of attribute filename.



11
12
13
# File 'lib/dimples/page.rb', line 11

def filename
  @filename
end

#layoutObject

Returns the value of attribute layout.



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

def layout
  @layout
end

#output_directoryObject

Returns the value of attribute output_directory.



15
16
17
# File 'lib/dimples/page.rb', line 15

def output_directory
  @output_directory
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'lib/dimples/page.rb', line 10

def title
  @title
end

Instance Method Details

#inspectObject



52
53
54
# File 'lib/dimples/page.rb', line 52

def inspect
  "#<Dimples::Page @output_path=#{output_path}>"
end

#output_pathObject



37
38
39
# File 'lib/dimples/page.rb', line 37

def output_path
  File.join(@output_directory, "#{@filename}.#{@extension}")
end

#write(context = {}) ⇒ Object



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

def write(context = {})
  FileUtils.mkdir_p(@output_directory) unless Dir.exist?(@output_directory)

  File.open(output_path, 'w+') do |file|
    file.write(context ? render(context) : contents)
  end
rescue SystemCallError => e
  error_message = "Failed to write #{path} (#{e.message})"
  raise Errors::PublishingError, error_message
end