Class: Pipin::Post
- Inherits:
-
Object
- Object
- Pipin::Post
- 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Class Method Summary collapse
- .add_compiler(extname = nil, &block) ⇒ Object
- .find(pattern, options = {}) ⇒ Object
- .find_srcs(pattern, options = {}) ⇒ Object
- .year_months ⇒ Object
Instance Method Summary collapse
- #date ⇒ Object
-
#initialize(filename) ⇒ Post
constructor
A new instance of Post.
- #label ⇒ Object
- #rss_body ⇒ Object
- #rss_description(n = 128) ⇒ Object
- #rss_tile ⇒ Object
- #time ⇒ Object
- #to_html ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
115 116 117 |
# File 'lib/pipin.rb', line 115 def body @body end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
115 116 117 |
# File 'lib/pipin.rb', line 115 def filename @filename end |
#header ⇒ Object (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, = {}) self.find_srcs(pattern, ).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, = {}) files = Dir.chdir(config[:dir][:posts]) { Dir.glob(pattern + '.{txt,html}') }.sort.reverse [:limit] ? files[0, [:limit]] : files end |
.year_months ⇒ Object
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
#date ⇒ Object
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 |
#label ⇒ Object
123 124 125 |
# File 'lib/pipin.rb', line 123 def label File.basename(filename, '.*') end |
#rss_body ⇒ Object
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_tile ⇒ Object
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 |
#time ⇒ Object
45 46 47 |
# File 'lib/pipin/templates/plugins/genarate_rss.rb', line 45 def time Time.parse(date.to_s) end |
#to_html ⇒ Object
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 |