Class: Jenner::Item
- Inherits:
-
Object
- Object
- Jenner::Item
- Defined in:
- lib/jenner/item.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#template_path ⇒ Object
readonly
Returns the value of attribute template_path.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #body ⇒ Object
- #formatted_url ⇒ Object
- #generate! ⇒ Object
- #haml? ⇒ Boolean
-
#initialize(path, site) ⇒ Item
constructor
A new instance of Item.
- #input_filename ⇒ Object
- #liquid_body ⇒ Object
- #local_path ⇒ Object
- #markdown? ⇒ Boolean
- #output_filename ⇒ Object
- #path ⇒ Object
- #public_path ⇒ Object
- #relative_path ⇒ Object
- #render ⇒ Object
- #template ⇒ Object
- #to_liquid ⇒ Object
- #to_liquid_without_body ⇒ Object
- #underscored_title ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(path, site) ⇒ Item
Returns a new instance of Item.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/jenner/item.rb', line 5 def initialize(path, site) @path = path @site = site @body = File.read(File.join(@site.root,'_site',@path)) if @body =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m @body = $' @header = YAML.load($1) end begin @title = @header.delete("title") @date = @header.delete("date") @template_path = @header.delete("template") @tags = @header.delete("tags") || [] @url_format = @header.delete("url_format") @data = @header rescue Exception => e raise "Invalid header data on item at path: #{@path}" end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/jenner/item.rb', line 4 def data @data end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
4 5 6 |
# File 'lib/jenner/item.rb', line 4 def date @date end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
4 5 6 |
# File 'lib/jenner/item.rb', line 4 def @tags end |
#template_path ⇒ Object (readonly)
Returns the value of attribute template_path.
4 5 6 |
# File 'lib/jenner/item.rb', line 4 def template_path @template_path end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
4 5 6 |
# File 'lib/jenner/item.rb', line 4 def title @title end |
Instance Method Details
#body ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/jenner/item.rb', line 105 def body if haml? Haml::Engine.new(liquid_body).render(Jenner.deep_struct(self.to_liquid_without_body), :site => Jenner.deep_struct(@site.to_liquid)) elsif markdown? Maruku.new(liquid_body).to_html else liquid_body end end |
#formatted_url ⇒ Object
67 68 69 |
# File 'lib/jenner/item.rb', line 67 def formatted_url "/#{relative_path}#{@date.strftime(@url_format).gsub(":title", underscored_title)}" end |
#generate! ⇒ Object
125 126 127 128 129 130 |
# File 'lib/jenner/item.rb', line 125 def generate! return if File.basename(public_path)[0] == "_" FileUtils.mkdir_p(File.dirname(public_path)) File.write(public_path,render) end |
#haml? ⇒ Boolean
97 98 99 |
# File 'lib/jenner/item.rb', line 97 def haml? File.extname(@path) == ".haml" end |
#input_filename ⇒ Object
55 56 57 |
# File 'lib/jenner/item.rb', line 55 def input_filename File.extname(@path) end |
#liquid_body ⇒ Object
101 102 103 |
# File 'lib/jenner/item.rb', line 101 def liquid_body Liquid::Template.parse(@body).render({'self' => self.to_liquid_without_body}, registers: { site: @site }) end |
#local_path ⇒ Object
35 36 37 |
# File 'lib/jenner/item.rb', line 35 def local_path @path end |
#markdown? ⇒ Boolean
93 94 95 |
# File 'lib/jenner/item.rb', line 93 def markdown? File.extname(@path) == ".markdown" end |
#output_filename ⇒ Object
49 50 51 52 53 |
# File 'lib/jenner/item.rb', line 49 def output_filename return "index.html" if File.extname(url) == "" File.basename(@path).sub(".markdown",".html") end |
#path ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/jenner/item.rb', line 39 def path return @path if @url_format.nil? if File.extname(url) == "" "#{url}/index.html" else url end end |
#public_path ⇒ Object
121 122 123 |
# File 'lib/jenner/item.rb', line 121 def public_path File.join(@site.root,'public',path.sub('.markdown','.html').sub(".haml",".html")) end |
#relative_path ⇒ Object
28 29 30 31 32 33 |
# File 'lib/jenner/item.rb', line 28 def relative_path path = File.dirname(@path) return "" if path == "." "#{path}/" end |
#render ⇒ Object
115 116 117 118 119 |
# File 'lib/jenner/item.rb', line 115 def render template.render( 'item' => self.to_liquid ) end |
#template ⇒ Object
71 72 73 |
# File 'lib/jenner/item.rb', line 71 def template Jenner::Template.from_file(File.join(@site.root,'_templates',"#{@template_path}"), @site) end |
#to_liquid ⇒ Object
87 88 89 90 91 |
# File 'lib/jenner/item.rb', line 87 def to_liquid to_liquid_without_body.merge( 'body' => body ) end |
#to_liquid_without_body ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jenner/item.rb', line 75 def to_liquid_without_body { 'title' => @title, 'date' => @date, 'template_path' => @template_path, 'tags' => @tags, 'data' => @data, 'url' => url, 'site' => @site } end |
#underscored_title ⇒ Object
63 64 65 |
# File 'lib/jenner/item.rb', line 63 def underscored_title @title.gsub(/[^\w]+/,"-").downcase end |
#url ⇒ Object
59 60 61 |
# File 'lib/jenner/item.rb', line 59 def url @url_format.nil? ? "/#{@path.sub(".markdown",".html")}" : formatted_url end |