Parent

Class Index [+]

Quicksearch

Postage::Post

Main class for handle text files. The Post class load file and extract all attributes from name. Examples:

If you want load a file, use:

  post = Postage::Post.load("posts/20090710-my_post_file.ruby.postage.mkd")
  # => post.title        : My post file
  # => post.publish_date : 2009-07-10
  # => post.tags         : ruby, postage
  # => post.filter       : markdown

Or, if you want initialize a new post, use:

  post = Postage::Post.new :title        => "Creating new `entry` from test unit",
                           :publish_date => Date.new(2009,7,10),
                           :tags         => %w(ruby postage),
                           :filter       => :markdown,
                           :content      => <<-end_content.gsub(/[ ]{2}/,'')
    Ok. This is a test for create new `entry` from test unit and this paragraph will be
    the post summary.

    In this file, I'll write any content ... only for test.
    Postage is a lightweight API for load posts from flat file that contains
    text filtered by [Markdown][] syntax.

    [markdown]: http://daringfireball.net/projects/markdown/
  end_content

Attributes

publish_date[R]

Post publish date, of course.

title[R]

The title accepts Markdown syntax.

tags[R]

Tags accepts only one word per tag.

summary[R]

Summary is a first paragraph of content.

content[R]

(Not documented)

filter[R]

Filter for render post file.

file[R]

(Not documented)

Public Class Methods

load(file_name) click to toggle source

Load all attributes from file name and read content.

    # File lib/postage/post.rb, line 51
51:   def self.load(file_name)
52:     new.extract_attributes(file_name)
53:   end
new(options = {}) click to toggle source

Initialize new post using options.

    # File lib/postage/post.rb, line 46
46:   def initialize(options = {})
47:     options.instance_variables_set_to(self)
48:   end

Public Instance Methods

<=>(post) click to toggle source

Sort posts by format.

    # File lib/postage/post.rb, line 94
94:   def <=>(post)
95:     to_s <=> post.to_s
96:   end
build_file() click to toggle source

Build post file name and return following format: yyyymmdd-post_name.tags.separated.by.points.filter

    # File lib/postage/post.rb, line 75
75:   def build_file
76:     @file = "#{build_publish_date}-#{build_file_name}.#{build_tags}.#{build_filter}"
77:   end
create_into(directory) click to toggle source

Get post file name and creates content and save into directory.

    # File lib/postage/post.rb, line 80
80:   def create_into(directory)
81:     build_file
82:     directory.join(@file).open 'w+' do |file|
83:       post = self
84:       file << ERB.new(load_template).result(binding)
85:     end
86:   end
extract_attributes(file_name) click to toggle source

Check and extract all attributes from file name.

    # File lib/postage/post.rb, line 56
56:   def extract_attributes(file_name)
57:     extract_publish_date(file_name)
58:     extract_tags(file_name)
59:     extract_filter(file_name)
60:     extract_title_and_content(file_name)
61:     @file    = Pathname.new(file_name).expand_path
62:     @title   = @file.gsub('_', ' ').capitalize if @title.to_s.empty?
63:     @summary = @content.match(%r{<p>.*</p>}).to_s
64:     self
65:   end
matched?(regexp) click to toggle source

Test pattern in title or filename.

    # File lib/postage/post.rb, line 89
89:   def matched?(regexp)
90:     @title.match(regexp) || @file.match(regexp)
91:   end
to_s() click to toggle source

Return post name formatted (“year/month/day/name“).

    # File lib/postage/post.rb, line 68
68:   def to_s
69:     @file.to_s.scan(%r{(\d{4})(\d{2})(\d{2})(.*?)-(.*?)\..*}) do |year,month,day,time,name|
70:       return "#{year}/#{month}/#{day}/#{name}"
71:     end
72:   end

Private Instance Methods

build_file_name() click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 150
150:   def build_file_name
151:     @title.downcase.gsub(/ /, '_').gsub(/[^a-z0-9_]/, '').squeeze('_')
152:   end
build_filter() click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 162
162:   def build_filter
163:     case @filter
164:       when :markdown
165:         "mkd"
166:       when :textile
167:         "txl"
168:       else :none
169:         ""
170:     end
171:   end
build_publish_date() click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 158
158:   def build_publish_date
159:     @publish_date.strftime('%Y%m%d')
160:   end
build_tags() click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 154
154:   def build_tags
155:     @tags.join('.')
156:   end
extract_filter(file_name) click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 120
120:   def extract_filter(file_name)
121:     file_name.scan(%r{.*\.(.*)}) do |filter|
122:       @filter = case filter.to_s
123:                 when /md|mkd|mark.*/
124:                   :markdown
125:                 when /tx|txl|text.*/
126:                   :textile
127:                 else
128:                   :text
129:                 end
130:     end
131:   end
extract_publish_date(file_name) click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 100
100:   def extract_publish_date(file_name)
101:     file_name.scan(%r{/(\d{4})(\d{2})(\d{2})(.*?)-.*}) do |year,month,day,time|
102:       return extract_publish_datetime(file_name) unless time.empty?
103:       @publish_date = Date.new(year.to_i, month.to_i, day.to_i)
104:     end
105:   end
extract_publish_datetime(file_name) click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 107
107:   def extract_publish_datetime(file_name)
108:     file_name.scan(%r{/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})-.*}) do |year,month,day,hour,min,sec|
109:       @publish_date = DateTime.new(year.to_i, month.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i)
110:     end
111:   end
extract_tags(file_name) click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 116
116:   def extract_tags(file_name)
117:     @tags = file_name.scan(%r{.*?-.*?\.(.*)\..*}).to_s.split('.')
118:   end
extract_title(file_name) click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 112
112:   def extract_title(file_name)
113:     Maruku.new(title).to_html.gsub(/<[hp]\d{0,1}.*?>(.*)<\/[hp]\d{0,1}>/){$1}
114:   end
extract_title_and_content(file_name) click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 142
142:   def extract_title_and_content(file_name)
143:     _content = File.readlines(file_name)
144:     _title   = _content.shift
145:     _title  += _content.shift if (_content.first =~ /==/)
146:     @title   = Maruku.new(_title).to_html.gsub(/<h1.*?>(.*)<\/h1>/){$1}
147:     @content = Maruku.new(_content.to_s).to_html
148:   end
find_file(year, month, day, name) click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 133
133:   def find_file(year, month, day, name)
134:     # TODO: check posts directory
135:     Dir["#{year}#{month}#{day}-#{name}**.*"].first
136:   end
load_file(file) click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 138
138:   def load_file(file)
139:     File.read(file)
140:   end
load_template() click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 173
173:   def load_template
174:     File.read(template)
175:   end
template() click to toggle source

(Not documented)

     # File lib/postage/post.rb, line 177
177:   def template
178:     ROOT.join('templates','post.erb')
179:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.