Class: SimplePosts::Post
- Inherits:
-
Object
- Object
- SimplePosts::Post
- Defined in:
- lib/simple_posts/posts.rb
Constant Summary collapse
- DATE_REGEX =
/\d{4}-\d{2}-\d{2}/- SHORT_DATE_FORMAT =
"%Y-%m-%d"- DATE_FORMAT =
"%Y-%m-%d %H:%M:%S"
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#docid ⇒ Object
Returns the value of attribute docid.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#original_body ⇒ Object
readonly
Returns the value of attribute original_body.
-
#original_filename ⇒ Object
readonly
Returns the value of attribute original_filename.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #alternate_links ⇒ Object
- #contents ⇒ Object
- #date ⇒ Object
- #filename ⇒ Object
- #has_tag(tag) ⇒ Object
- #html_path ⇒ Object
- #id ⇒ Object
- #id_matches?(id) ⇒ Boolean
-
#initialize(filename, renderer) ⇒ Post
constructor
A new instance of Post.
- #is_blog_post? ⇒ Boolean
- #is_erb? ⇒ Boolean
- #is_html? ⇒ Boolean
- #layout ⇒ Object
- #matches_path(path) ⇒ Object
- #natural_date ⇒ Object
- #parse_body(body_text) ⇒ Object
- #parse_post ⇒ Object
- #post_id ⇒ Object
- #reading_time ⇒ Object
- #render(renderer = nil, app = nil) ⇒ Object
- #render_before_fold ⇒ Object
- #render_erb(content, app) ⇒ Object
- #short_date ⇒ Object
- #show_byline? ⇒ Boolean
- #tags ⇒ Object
- #title ⇒ Object
- #topic ⇒ Object
- #view ⇒ Object
Constructor Details
#initialize(filename, renderer) ⇒ Post
Returns a new instance of Post.
151 152 153 154 155 156 157 |
# File 'lib/simple_posts/posts.rb', line 151 def initialize(filename, renderer) @file = filename @original_filename = filename @name = self.class.normalize_name(filename) @renderer = renderer parse_post end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
149 150 151 |
# File 'lib/simple_posts/posts.rb', line 149 def body @body end |
#docid ⇒ Object
Returns the value of attribute docid.
148 149 150 |
# File 'lib/simple_posts/posts.rb', line 148 def docid @docid end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
149 150 151 |
# File 'lib/simple_posts/posts.rb', line 149 def headers @headers end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
149 150 151 |
# File 'lib/simple_posts/posts.rb', line 149 def name @name end |
#original_body ⇒ Object (readonly)
Returns the value of attribute original_body.
149 150 151 |
# File 'lib/simple_posts/posts.rb', line 149 def original_body @original_body end |
#original_filename ⇒ Object (readonly)
Returns the value of attribute original_filename.
149 150 151 |
# File 'lib/simple_posts/posts.rb', line 149 def original_filename @original_filename end |
Class Method Details
.normalize_name(post) ⇒ Object
172 173 174 |
# File 'lib/simple_posts/posts.rb', line 172 def self.normalize_name(post) return post.downcase.strip.sub(/\.(html|md|pdf)(\.erb)?$/,'').sub(/\d{4}-\d{2}-\d{2}-/, '') end |
Instance Method Details
#[](key) ⇒ Object
227 228 229 |
# File 'lib/simple_posts/posts.rb', line 227 def [](key) return @headers[key] end |
#alternate_links ⇒ Object
239 240 241 242 243 244 245 |
# File 'lib/simple_posts/posts.rb', line 239 def alternate_links if @headers.has_key?('alternate_links') return @headers['alternate_links'].split(/\s+/) else return [] end end |
#contents ⇒ Object
212 213 214 215 216 |
# File 'lib/simple_posts/posts.rb', line 212 def contents @contents ||= File.open(filename, 'r:utf-8') do |file| file.read end end |
#date ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/simple_posts/posts.rb', line 263 def date if is_blog_post? if @headers['date'] Time.strptime(@headers['date'], DATE_FORMAT) else Time.strptime(@file, SHORT_DATE_FORMAT) end elsif @headers['date'] Time.strptime(@headers['date'], SHORT_DATE_FORMAT) end end |
#filename ⇒ Object
218 219 220 |
# File 'lib/simple_posts/posts.rb', line 218 def filename Rails.root.join("app", "posts", @file).to_s end |
#has_tag(tag) ⇒ Object
247 248 249 |
# File 'lib/simple_posts/posts.rb', line 247 def has_tag(tag) .detect { |t| t == tag } end |
#html_path ⇒ Object
291 292 293 |
# File 'lib/simple_posts/posts.rb', line 291 def html_path "/blog/#{@name}" end |
#id ⇒ Object
259 260 261 |
# File 'lib/simple_posts/posts.rb', line 259 def id post_id end |
#id_matches?(id) ⇒ Boolean
203 204 205 206 |
# File 'lib/simple_posts/posts.rb', line 203 def id_matches?(id) links = alternate_links + [self.post_id] links.include? id end |
#is_blog_post? ⇒ Boolean
176 177 178 |
# File 'lib/simple_posts/posts.rb', line 176 def is_blog_post? return filename =~ DATE_REGEX end |
#is_erb? ⇒ Boolean
195 196 197 |
# File 'lib/simple_posts/posts.rb', line 195 def is_erb? original_filename.end_with?('.erb') end |
#is_html? ⇒ Boolean
199 200 201 |
# File 'lib/simple_posts/posts.rb', line 199 def is_html? original_filename =~ /\.html(\.erb)?$/ end |
#layout ⇒ Object
299 300 301 |
# File 'lib/simple_posts/posts.rb', line 299 def layout @headers.has_key?('layout') ? @headers['layout'].to_sym : nil end |
#matches_path(path) ⇒ Object
222 223 224 225 |
# File 'lib/simple_posts/posts.rb', line 222 def matches_path(path) normalized = self.class.normalize_name(path) return @name == normalized || @headers['id'] == normalized end |
#natural_date ⇒ Object
279 280 281 |
# File 'lib/simple_posts/posts.rb', line 279 def natural_date date ? date.strftime("%e %B %Y") : '' end |
#parse_body(body_text) ⇒ Object
166 167 168 169 170 |
# File 'lib/simple_posts/posts.rb', line 166 def parse_body(body_text) @original_body = body_text @before_fold, after_fold = body_text.split("--fold--") @body = body_text.sub("--fold--", '') end |
#parse_post ⇒ Object
159 160 161 162 163 164 |
# File 'lib/simple_posts/posts.rb', line 159 def parse_post if contents =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m @headers = YAML.load($1) parse_body($3) end end |
#post_id ⇒ Object
255 256 257 |
# File 'lib/simple_posts/posts.rb', line 255 def post_id @headers['id'] end |
#reading_time ⇒ Object
275 276 277 |
# File 'lib/simple_posts/posts.rb', line 275 def reading_time ([body.split(/\s+/).length / 180.0, 1].max).to_i end |
#render(renderer = nil, app = nil) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/simple_posts/posts.rb', line 180 def render(renderer=nil, app=nil) content = is_erb? ? render_erb(@body, app) : @body if is_html? content else (renderer || @renderer).render(content) end end |
#render_before_fold ⇒ Object
208 209 210 |
# File 'lib/simple_posts/posts.rb', line 208 def render_before_fold @renderer.render(@before_fold) end |
#render_erb(content, app) ⇒ Object
189 190 191 192 193 |
# File 'lib/simple_posts/posts.rb', line 189 def render_erb(content, app) template = ERB.new(content) @app = app template.result(binding) end |
#short_date ⇒ Object
283 284 285 |
# File 'lib/simple_posts/posts.rb', line 283 def short_date date ? date.strftime("%e %b %Y") : '' end |
#show_byline? ⇒ Boolean
303 304 305 |
# File 'lib/simple_posts/posts.rb', line 303 def is_blog_post? end |
#tags ⇒ Object
231 232 233 234 235 236 237 |
# File 'lib/simple_posts/posts.rb', line 231 def if @headers.has_key?('tags') return @headers['tags'].split(/,\s+/) else return [] end end |
#title ⇒ Object
251 252 253 |
# File 'lib/simple_posts/posts.rb', line 251 def title @headers['title'] end |
#topic ⇒ Object
287 288 289 |
# File 'lib/simple_posts/posts.rb', line 287 def topic headers['topic'] end |
#view ⇒ Object
295 296 297 |
# File 'lib/simple_posts/posts.rb', line 295 def view @headers.has_key?('view') ? @headers['view'].to_sym : nil end |