Class: Shinmun::Post

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#bodyObject

Returns the value of attribute body.



23
24
25
# File 'lib/shinmun/post.rb', line 23

def body
  @body
end

#fileObject

Returns the value of attribute file.



23
24
25
# File 'lib/shinmun/post.rb', line 23

def file
  @file
end

#headObject

Returns the value of attribute head.



23
24
25
# File 'lib/shinmun/post.rb', line 23

def head
  @head
end

#mtimeObject

Returns the value of attribute mtime.



23
24
25
# File 'lib/shinmun/post.rb', line 23

def mtime
  @mtime
end

#nameObject



38
39
40
# File 'lib/shinmun/post.rb', line 38

def name
  @name ||= title.to_s.downcase.gsub(/[ -]+/, '-').gsub(/[^-a-z0-9_]+/, '')
end

#srcObject

Returns the value of attribute src.



23
24
25
# File 'lib/shinmun/post.rb', line 23

def src
  @src
end

#typeObject

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_htmlObject



65
66
67
# File 'lib/shinmun/post.rb', line 65

def body_html
  @body_html ||= transform(@body)
end

#changed?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/shinmun/post.rb', line 86

def changed?
  File.mtime(file) != mtime
end

#dumpObject

Convert to string representation



113
114
115
# File 'lib/shinmun/post.rb', line 113

def dump
  head.to_yaml + "---" + body
end

#eql?(obj) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/shinmun/post.rb', line 131

def eql?(obj)
  self == obj
end

#loadObject



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

#monthObject

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

#pathObject



73
74
75
76
# File 'lib/shinmun/post.rb', line 73

def path
  folder = date ? "posts/#{year}/#{month}" : 'pages'
  "#{folder}/#{name}.#{type}"
end

#saveObject



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

#summaryObject



69
70
71
# File 'lib/shinmun/post.rb', line 69

def summary
  @summary ||= body_html.split("\n\n")[0]
end

#tag_listObject



61
62
63
# File 'lib/shinmun/post.rb', line 61

def tag_list
  @tag_list ||= tags.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, options={})
  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 = options[:code_css]
    RubyPants.new(bluecloth.to_html).to_html
  end
end

#yearObject

Shortcut for year of date



52
53
54
# File 'lib/shinmun/post.rb', line 52

def year
  date.year
end