Class: Bloggit::Post
- Inherits:
-
Object
- Object
- Bloggit::Post
- Defined in:
- lib/bloggit/post.rb
Overview
Post
A blog post is a YAML file containing two documents. The first document should be a hash of attributes, the second a string document. The format looks a little like the following:
title: To Boldly Go
publish_date: 3/07/2004
allow_comments: true
format: Markdown
tags: [one, two, three and four]
slug: to-boldly-go
--- |
This is the post body... Fun, isn't it? Yes. Yes it is.
And you know, I don't care if anybody knows! :D
The filename is the date and slugline (YYYY.MM.DD_slug.post)
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#source ⇒ Object
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #first ⇒ Object
-
#has_next? ⇒ Boolean
Navigation.
- #has_previous? ⇒ Boolean
- #has_tags? ⇒ Boolean
-
#initialize(site, filename, body, atts = {}) ⇒ Post
constructor
A new instance of Post.
- #is_draft? ⇒ Boolean
- #is_publishable? ⇒ Boolean
- #last ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #next ⇒ Object
- #permalink ⇒ Object
- #previous ⇒ Object
- #render_content(template_binding = nil) ⇒ Object
- #tags ⇒ Object
- #to_json ⇒ Object
- #to_yaml ⇒ Object
Constructor Details
#initialize(site, filename, body, atts = {}) ⇒ Post
Returns a new instance of Post.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bloggit/post.rb', line 25 def initialize(site, filename, body, atts={}) @site = site @filename = filename @data = atts.clone @data.reverse_merge!(site.settings.posts) unless site.nil? @source = body.to_s date_str = if atts.has_key? 'publish_date' atts.publish_date.to_s.gsub('.','-') else @filename.split('_').to_s.gsub('.','-') end begin publish_date = Date.parse(date_str) rescue raise "Date format error for #{@filename}" end @data.publish_date = publish_date @data.post_year = publish_date.year @data.post_month = publish_date.month @data.post_date = publish_date.day @data.slug = @filename.split('_')[-1].gsub('.post', '') unless @data.has_key? 'slug' @data.status = 'publish' unless @data.has_key? 'status' @data. = [] unless @data.has_key? 'tags' @data..uniq! @data..each {|tag| Tag.register_post( tag, self ) } if self.is_publishable? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/bloggit/post.rb', line 121 def method_missing(name,*args) if @data.has_key? name.to_s @data[name.to_s] else super(*args) end end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
23 24 25 |
# File 'lib/bloggit/post.rb', line 23 def filename @filename end |
#source ⇒ Object
Returns the value of attribute source.
23 24 25 |
# File 'lib/bloggit/post.rb', line 23 def source @source end |
Class Method Details
.from_file(path, site = nil) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/bloggit/post.rb', line 144 def from_file(path, site=nil) path = File.(path) raise "File must exist" unless File.exists?( path ) yml_docs = YAML::load_stream( File.open(path, 'r') ) raise "Post is missing a document part" if yml_docs.documents.length != 2 raise "Post format is invalid" unless yml_docs[0].is_a?(Hash) and yml_docs[1].is_a?(String) atts = yml_docs[0] body = yml_docs[1] new( site, File.basename(path), body, atts ) end |
.to_file(title, site = nil, options = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bloggit/post.rb', line 156 def to_file(title, site=nil, ={}) slug = title.to_s.downcase.gsub( /[^-a-z0-9~\s\.:;+=_]/, '').gsub(/[\s\.:;=_+]+/, '-').gsub(/[\-]{2,}/, '-').to_s raise "Slug can't be empty" if slug.empty? path = "#{Date.today.strftime('%Y.%m.%d')}_#{slug}.post" post = new( site, path, "New Post\n\n", 'slug'=>slug, 'title'=>title ) new_post_path = File.join(Dir.getwd, 'posts', path) raise "Path Exception! #{new_post_path} already exists." if File.exists?(new_post_path) File.open( new_post_path, 'w' ) {|f| f.write post.to_yaml } post end |
Instance Method Details
#first ⇒ Object
86 87 88 |
# File 'lib/bloggit/post.rb', line 86 def first @site.posts[0] end |
#has_next? ⇒ Boolean
Navigation
76 77 78 79 |
# File 'lib/bloggit/post.rb', line 76 def has_next? idx = @site.posts.index(self) +1 idx < @site.posts.length end |
#has_previous? ⇒ Boolean
81 82 83 84 |
# File 'lib/bloggit/post.rb', line 81 def has_previous? idx = @site.posts.index(self) idx > 0 end |
#has_tags? ⇒ Boolean
63 64 65 |
# File 'lib/bloggit/post.rb', line 63 def @data.has_key? 'tags' end |
#is_draft? ⇒ Boolean
55 56 57 |
# File 'lib/bloggit/post.rb', line 55 def is_draft? @data.status == 'draft' end |
#is_publishable? ⇒ Boolean
59 60 61 |
# File 'lib/bloggit/post.rb', line 59 def is_publishable? !is_draft? and @data.publish_date <= Date.today end |
#last ⇒ Object
100 101 102 |
# File 'lib/bloggit/post.rb', line 100 def last @site.posts[@site.posts.length() -1] end |
#next ⇒ Object
95 96 97 98 |
# File 'lib/bloggit/post.rb', line 95 def next idx = @site.posts.index(self) +1 @site.posts[idx] end |
#permalink ⇒ Object
71 72 73 |
# File 'lib/bloggit/post.rb', line 71 def permalink @site.build_post_path("#{@data.post_year}/#{@data.post_month}/#{@data.slug}.html") end |
#previous ⇒ Object
90 91 92 93 |
# File 'lib/bloggit/post.rb', line 90 def previous idx = @site.posts.index(self) -1 @site.posts[idx] end |
#render_content(template_binding = nil) ⇒ Object
104 105 106 107 108 |
# File 'lib/bloggit/post.rb', line 104 def render_content(template_binding=nil) src = @source src = Template.from_text(src).template.result(template_binding) unless template_binding.nil? TextFormatter.render(src, @data.fetch('format', 'simple')) end |
#tags ⇒ Object
67 68 69 |
# File 'lib/bloggit/post.rb', line 67 def @data. end |
#to_json ⇒ Object
115 116 117 118 119 |
# File 'lib/bloggit/post.rb', line 115 def to_json data_hash = prepare_data data_hash['source'] = @source data_hash.to_json end |
#to_yaml ⇒ Object
110 111 112 113 |
# File 'lib/bloggit/post.rb', line 110 def to_yaml yml_s = prepare_data.to_yaml yml_s << @source.to_yaml end |