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, #scope

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).sub(
      @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



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

def inspect
  "#<#{self.class} @output_path=#{output_path}>"
end

#output_filenameObject



41
42
43
# File 'lib/dimples/page.rb', line 41

def output_filename
  "#{@filename}.#{@extension}"
end

#output_pathObject



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

def output_path
  File.join(@output_directory, output_filename)
end

#urlObject



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

def url
  absolute_output_directory = File.absolute_path(@output_directory)

  absolute_output_directory.sub(@site.output_paths[:site], '').tap do |url|
    url[0] = '/' unless url[0] == '/'
    url.concat('/') unless url[-1] == '/'
    url.concat(output_filename) if filename != 'index'
  end
end

#write(context = {}) ⇒ Object



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

def write(context = {})
  FileUtils.mkdir_p(@output_directory) unless Dir.exist?(@output_directory)
  File.write(output_path, render(context))
rescue SystemCallError => e
  error_message = "Failed to write #{path} (#{e.message})"
  raise Errors::PublishingError, error_message
end