Class: Potion::Renderable

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/potion/renderable.rb

Direct Known Subclasses

Page, Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#category, #link_to, #photo

Constructor Details

#initialize(path, site) ⇒ Renderable

Returns a new instance of Renderable.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/potion/renderable.rb', line 6

def initialize(path, site)
  @path         = path
  @site         = site
                  
  
  @layout = @site.find_layout_by_name(@metadata["layout"])
  
  @relative_output_path ||= @path
  @relative_output_path = @relative_output_path.gsub(site.base_path, "")
  @relative_output_path = @relative_output_path.gsub(File.extname(path), "") unless self.is_html?

  @output_path = File.join(@site.destination_path, @relative_output_path)
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/potion/renderable.rb', line 4

def content
  @content
end

#layoutObject

Returns the value of attribute layout.



4
5
6
# File 'lib/potion/renderable.rb', line 4

def layout
  @layout
end

#metadataObject

Returns the value of attribute metadata.



4
5
6
# File 'lib/potion/renderable.rb', line 4

def 
  @metadata
end

#output_pathObject

Returns the value of attribute output_path.



4
5
6
# File 'lib/potion/renderable.rb', line 4

def output_path
  @output_path
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/potion/renderable.rb', line 4

def path
  @path
end

#relative_output_pathObject

Returns the value of attribute relative_output_path.



4
5
6
# File 'lib/potion/renderable.rb', line 4

def relative_output_path
  @relative_output_path
end

#siteObject

Returns the value of attribute site.



4
5
6
# File 'lib/potion/renderable.rb', line 4

def site
  @site
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/potion/renderable.rb', line 55

def ==(other)
  return false unless self.class == other.class
  self.instance_variables.each do |name|
    return false unless self.instance_variable_get(name) == other.instance_variable_get(name)
  end
  
  true
end

#is_html?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/potion/renderable.rb', line 75

def is_html?
  File.extname(@path) == ".html"
end

#load_content_and_metadataObject



20
21
22
23
24
25
26
27
# File 'lib/potion/renderable.rb', line 20

def 
  @content  = File.open(path) {|file| file.read}
  begin
    @metadata = YAML.load(@content.slice!(/\A(---\s*\n.*?\n?)^(---\s*$\n?)/m, 0))
  rescue Psych::SyntaxError
    raise "\n\nERROR: Invalid YAML frontmatter in file: #{@path}\n\n"
  end
end

#renderObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/potion/renderable.rb', line 29

def render
  @site.class.extensions.each do |extension|
    extension.new.process(self)
  end
  
  layout = Tilt.new(@layout.path) { @layout.content}
  
  if self.is_html?
    layout.render(self) do
      @content
    end
  else
    item = Tilt.new(@path) { @content }
    layout.render(self) do
      item.render(self)
    end
  end
end

#titleObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/potion/renderable.rb', line 64

def title
  return self.["title"] unless self.["title"].nil?
  
  filename = File.split(@path)[1]
  filename.gsub!(/\d+-\d+-\d+-/, "")
  filename.gsub!(File.extname(filename), "")
  filename.gsub!(File.extname(filename), "") unless self.is_html?
  filename.gsub!("-", " ")
  filename[0].upcase + filename[1..-1]
end

#writeObject



48
49
50
51
52
53
# File 'lib/potion/renderable.rb', line 48

def write    
  FileUtils.mkdir_p(File.split(@output_path)[0])
  File.open(@output_path, "w+") do |stream|
    stream.puts self.render
  end
end