class SimplePosts::Post
Constants
- DATE_FORMAT
- DATE_REGEX
- SHORT_DATE_FORMAT
Attributes
body[R]
docid[RW]
headers[R]
name[R]
original_body[R]
original_filename[R]
Public Class Methods
new(filename, renderer)
click to toggle source
# 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
normalize_name(post)
click to toggle source
# 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
Public Instance Methods
[](key)
click to toggle source
# File lib/simple_posts/posts.rb, line 227 def [](key) return @headers[key] end
alternate_links()
click to toggle source
# 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()
click to toggle source
# File lib/simple_posts/posts.rb, line 212 def contents @contents ||= File.open(filename, 'r:utf-8') do |file| file.read end end
date()
click to toggle source
# 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()
click to toggle source
# File lib/simple_posts/posts.rb, line 218 def filename Rails.root.join("app", "posts", @file).to_s end
has_tag(tag)
click to toggle source
# File lib/simple_posts/posts.rb, line 247 def has_tag(tag) tags.detect { |t| t == tag } end
html_path()
click to toggle source
# File lib/simple_posts/posts.rb, line 291 def html_path "/blog/#{@name}" end
id()
click to toggle source
# File lib/simple_posts/posts.rb, line 259 def id post_id end
id_matches?(id)
click to toggle source
# 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?()
click to toggle source
# File lib/simple_posts/posts.rb, line 176 def is_blog_post? return filename =~ DATE_REGEX end
is_erb?()
click to toggle source
# File lib/simple_posts/posts.rb, line 195 def is_erb? original_filename.end_with?('.erb') end
is_html?()
click to toggle source
# File lib/simple_posts/posts.rb, line 199 def is_html? original_filename =~ /\.html(\.erb)?$/ end
layout()
click to toggle source
# File lib/simple_posts/posts.rb, line 299 def layout @headers.has_key?('layout') ? @headers['layout'].to_sym : nil end
matches_path(path)
click to toggle source
# 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()
click to toggle source
# File lib/simple_posts/posts.rb, line 279 def natural_date date ? date.strftime("%e %B %Y") : '' end
parse_body(body_text)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/simple_posts/posts.rb, line 255 def post_id @headers['id'] end
reading_time()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/simple_posts/posts.rb, line 208 def render_before_fold @renderer.render(@before_fold) end
render_erb(content, app)
click to toggle source
# 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()
click to toggle source
# File lib/simple_posts/posts.rb, line 283 def short_date date ? date.strftime("%e %b %Y") : '' end
show_byline?()
click to toggle source
# File lib/simple_posts/posts.rb, line 303 def show_byline? is_blog_post? end
title()
click to toggle source
# File lib/simple_posts/posts.rb, line 251 def title @headers['title'] end
topic()
click to toggle source
# File lib/simple_posts/posts.rb, line 287 def topic headers['topic'] end
view()
click to toggle source
# File lib/simple_posts/posts.rb, line 295 def view @headers.has_key?('view') ? @headers['view'].to_sym : nil end