Class: Potion::Renderable
- Inherits:
-
Object
- Object
- Potion::Renderable
- Includes:
- Helpers
- Defined in:
- lib/potion/renderable.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#layout ⇒ Object
Returns the value of attribute layout.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#output_path ⇒ Object
Returns the value of attribute output_path.
-
#path ⇒ Object
Returns the value of attribute path.
-
#relative_output_path ⇒ Object
Returns the value of attribute relative_output_path.
-
#site ⇒ Object
Returns the value of attribute site.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(path, site) ⇒ Renderable
constructor
A new instance of Renderable.
- #is_html? ⇒ Boolean
- #load_content_and_metadata ⇒ Object
- #render ⇒ Object
- #title ⇒ Object
- #write ⇒ Object
Methods included from Helpers
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
#content ⇒ Object
Returns the value of attribute content.
4 5 6 |
# File 'lib/potion/renderable.rb', line 4 def content @content end |
#layout ⇒ Object
Returns the value of attribute layout.
4 5 6 |
# File 'lib/potion/renderable.rb', line 4 def layout @layout end |
#metadata ⇒ Object
Returns the value of attribute metadata.
4 5 6 |
# File 'lib/potion/renderable.rb', line 4 def @metadata end |
#output_path ⇒ Object
Returns the value of attribute output_path.
4 5 6 |
# File 'lib/potion/renderable.rb', line 4 def output_path @output_path end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/potion/renderable.rb', line 4 def path @path end |
#relative_output_path ⇒ Object
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 |
#site ⇒ Object
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
75 76 77 |
# File 'lib/potion/renderable.rb', line 75 def is_html? File.extname(@path) == ".html" end |
#load_content_and_metadata ⇒ Object
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 |
#render ⇒ Object
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 |
#title ⇒ Object
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 |
#write ⇒ Object
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 |