Class: Pipin::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/pipin.rb,
lib/pipin/templates/plugins/genarate_rss.rb

Constant Summary collapse

@@compilers =
[[nil, lambda {|post| post.body }]]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Post

Returns a new instance of Post.



116
117
118
119
120
121
# File 'lib/pipin.rb', line 116

def initialize(filename)
  @filename = filename
  @header, @body = Dir.chdir(config[:dir][:posts]) { File.read(@filename) }.split(/^__$/, 2)
  @header, @body = nil, @header unless @body
  @header and p(@header)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



115
116
117
# File 'lib/pipin.rb', line 115

def body
  @body
end

#filenameObject (readonly)

Returns the value of attribute filename.



115
116
117
# File 'lib/pipin.rb', line 115

def filename
  @filename
end

#headerObject (readonly)

Returns the value of attribute header.



115
116
117
# File 'lib/pipin.rb', line 115

def header
  @header
end

Class Method Details

.add_compiler(extname = nil, &block) ⇒ Object



111
112
113
# File 'lib/pipin.rb', line 111

def self.add_compiler(extname = nil, &block)
  @@compilers.unshift [extname, block]
end

.find(pattern, options = {}) ⇒ Object



90
91
92
# File 'lib/pipin.rb', line 90

def self.find(pattern, options = {})
  self.find_srcs(pattern, options).map {|e| Post.new(e) }
end

.find_srcs(pattern, options = {}) ⇒ Object



94
95
96
97
# File 'lib/pipin.rb', line 94

def self.find_srcs(pattern, options = {})
  files = Dir.chdir(config[:dir][:posts]) { Dir.glob(pattern + '.{txt,html}') }.sort.reverse
  options[:limit] ? files[0, options[:limit]] : files 
end

.year_monthsObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/pipin.rb', line 99

def self.year_months
  result = {}
  files = find_srcs(Diary_pattern)
  first_year = files.last[/^\d{4}/]
  last_year = files.first[/^\d{4}/]
  (first_year..last_year).to_a.reverse.map do |year|
    result[year] = ('01'..'12').select {|month| find_srcs("#{year}#{month}*")[0] }
  end
  result
end

Instance Method Details

#dateObject



127
128
129
130
# File 'lib/pipin.rb', line 127

def date
  m = label.match(/^(\d{4})(\d{2})(\d{2})/)
  m ? "#{m[1]}-#{m[2]}-#{m[3]}" : nil
end

#labelObject



123
124
125
# File 'lib/pipin.rb', line 123

def label
  File.basename(filename, '.*')
end

#rss_bodyObject



41
42
43
# File 'lib/pipin/templates/plugins/genarate_rss.rb', line 41

def rss_body
  to_html
end

#rss_description(n = 128) ⇒ Object



33
34
35
# File 'lib/pipin/templates/plugins/genarate_rss.rb', line 33

def rss_description(n = 128)
  /^.{0,#{n}}/m.match(to_html.gsub(/(.*<\/h3>)|(<[^>]*>)|(\s+)/mi, '')).to_s + '...'
end

#rss_tileObject



37
38
39
# File 'lib/pipin/templates/plugins/genarate_rss.rb', line 37

def rss_tile
   to_html[/<h2>.*<\/h2>/i].to_s.gsub(/<[^>]*>/, '')
end

#timeObject



45
46
47
# File 'lib/pipin/templates/plugins/genarate_rss.rb', line 45

def time
  Time.parse(date.to_s)
end

#to_htmlObject



132
133
134
135
136
# File 'lib/pipin.rb', line 132

def to_html
  (@@compilers.assoc(File.extname filename) || @@compilers.last)[1].call(self)
rescue
  Haml::Helpers::html_escape $!
end