Class: Shinmun::Post
- Inherits:
-
Object
- Object
- Shinmun::Post
- Defined in:
- lib/shinmun/post.rb
Overview
This class represents a post or page. Each post has a header, encoded as YAML and a body.
Example:
---
category: Ruby
date: 2008-09-05
title: BlueCloth, a Markdown library
---
This is the summary, which is by definition the first paragraph of the
article. The summary shows up in list views and rss feeds.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#file ⇒ Object
Returns the value of attribute file.
-
#head ⇒ Object
Returns the value of attribute head.
-
#mtime ⇒ Object
Returns the value of attribute mtime.
- #name ⇒ Object
-
#src ⇒ Object
Returns the value of attribute src.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #body_html ⇒ Object
- #changed? ⇒ Boolean
-
#dump ⇒ Object
Convert to string representation.
- #eql?(obj) ⇒ Boolean
-
#initialize(attributes = {}) ⇒ Post
constructor
Initialize empty post and set specified attributes.
- #load ⇒ Object
- #method_missing(id, *args) ⇒ Object
-
#month ⇒ Object
Shortcut for month of date.
-
#parse(src) ⇒ Object
Split up the source into header and body.
- #path ⇒ Object
- #save ⇒ Object
- #summary ⇒ Object
- #tag_list ⇒ Object
-
#transform(src, options = {}) ⇒ Object
Transform the body of this post.
-
#year ⇒ Object
Shortcut for year of date.
Constructor Details
#initialize(attributes = {}) ⇒ Post
Initialize empty post and set specified attributes.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/shinmun/post.rb', line 26 def initialize(attributes={}) @head = {} @body = '' @type = 'md' attributes.each do |k, v| send "#{k}=", v end load if file end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/shinmun/post.rb', line 42 def method_missing(id, *args) key = id.to_s if @head.has_key?(key) @head[key] else raise NoMethodError, "undefined method `#{id}' for #{self}", caller(1) end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
23 24 25 |
# File 'lib/shinmun/post.rb', line 23 def body @body end |
#file ⇒ Object
Returns the value of attribute file.
23 24 25 |
# File 'lib/shinmun/post.rb', line 23 def file @file end |
#head ⇒ Object
Returns the value of attribute head.
23 24 25 |
# File 'lib/shinmun/post.rb', line 23 def head @head end |
#mtime ⇒ Object
Returns the value of attribute mtime.
23 24 25 |
# File 'lib/shinmun/post.rb', line 23 def mtime @mtime end |
#name ⇒ Object
38 39 40 |
# File 'lib/shinmun/post.rb', line 38 def name @name ||= title.to_s.downcase.gsub(/[ -]+/, '-').gsub(/[^-a-z0-9_]+/, '') end |
#src ⇒ Object
Returns the value of attribute src.
23 24 25 |
# File 'lib/shinmun/post.rb', line 23 def src @src end |
#type ⇒ Object
Returns the value of attribute type.
23 24 25 |
# File 'lib/shinmun/post.rb', line 23 def type @type end |
Instance Method Details
#==(obj) ⇒ Object
135 136 137 |
# File 'lib/shinmun/post.rb', line 135 def ==(obj) Post === obj and file == obj.file end |
#body_html ⇒ Object
65 66 67 |
# File 'lib/shinmun/post.rb', line 65 def body_html @body_html ||= transform(@body) end |
#changed? ⇒ Boolean
86 87 88 |
# File 'lib/shinmun/post.rb', line 86 def changed? File.mtime(file) != mtime end |
#dump ⇒ Object
Convert to string representation
113 114 115 |
# File 'lib/shinmun/post.rb', line 113 def dump head.to_yaml + "---" + body end |
#eql?(obj) ⇒ Boolean
131 132 133 |
# File 'lib/shinmun/post.rb', line 131 def eql?(obj) self == obj end |
#load ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/shinmun/post.rb', line 78 def load self.type = File.extname(file)[1..-1] self.name = File.basename(file).chomp(".#{type}") self.mtime = File.mtime(file) parse(File.read(file)) end |
#month ⇒ Object
Shortcut for month of date
57 58 59 |
# File 'lib/shinmun/post.rb', line 57 def month date.month end |
#parse(src) ⇒ Object
Split up the source into header and body. Load the header as yaml document.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/shinmun/post.rb', line 99 def parse(src) if src =~ /\A---(.*?)---(.*)/m @head = YAML.load($1) @body = $2 else @body = src end @body_html = nil @tag_list = nil @summary = nil end |
#path ⇒ Object
73 74 75 76 |
# File 'lib/shinmun/post.rb', line 73 def path folder = date ? "posts/#{year}/#{month}" : 'pages' "#{folder}/#{name}.#{type}" end |
#save ⇒ Object
90 91 92 93 94 95 |
# File 'lib/shinmun/post.rb', line 90 def save FileUtils.mkpath(File.dirname(path)) File.open(path, 'w') do |io| io << dump end end |
#summary ⇒ Object
69 70 71 |
# File 'lib/shinmun/post.rb', line 69 def summary @summary ||= body_html.split("\n\n")[0] end |
#tag_list ⇒ Object
61 62 63 |
# File 'lib/shinmun/post.rb', line 61 def tag_list @tag_list ||= .to_s.split(",").map { |s| s.strip } end |
#transform(src, options = {}) ⇒ Object
Transform the body of this post. Defaults to Markdown.
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/shinmun/post.rb', line 118 def transform(src, ={}) case type when 'html' RubyPants.new(src).to_html when 'tt' RubyPants.new(RedCloth.new(src).to_html).to_html else bluecloth = BlueCloth.new(src) bluecloth.code_css = [:code_css] RubyPants.new(bluecloth.to_html).to_html end end |
#year ⇒ Object
Shortcut for year of date
52 53 54 |
# File 'lib/shinmun/post.rb', line 52 def year date.year end |