Class: Broadway::Post

Inherits:
Object
  • Object
show all
Includes:
Convertible, Resource, Comparable
Defined in:
lib/broadway/post.rb

Constant Summary collapse

SRC_MATCHER =
/^(.+\/)*(?:(\d+-\d+-\d+)-)?(.*)(\.[^.]+)$/
URL_MATCHER =
/^(.+\/)*(.*)$/

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

included

Methods included from Convertible

#content_type, #do_layout, #read, #read_yaml, #to_s, #transform

Constructor Details

#initialize(options = {}) ⇒ Post

Initialize this Post instance.

+site+ is the Site
+base+ is the String path to the dir containing the post file
+name+ is the String filename of the post file
+categories+ is an Array of Strings for the categories for this post

Returns <Post>



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/broadway/post.rb', line 26

def initialize(options = {})
  self.site = options[:site]
  self.path = options[:path] if options.has_key?(:path)
  self.data = {}
  
  n, cats, date, slug, ext = *path.match(SRC_MATCHER)
  n, cats, slug = *path.match(URL_MATCHER) unless slug
  self.date = Time.parse(date) if date
  self.slug = slug
  self.ext = ext
  self.dir = options.has_key?(:dir) ? options[:dir] : File.dirname(path.gsub(/#{site.config[:source]}\/?/, ""))
  base = self.dir == "." ? "" : self.dir
  self.categories = base.split('/').reject { |x| x.empty? }
  
  process(options)
end

Class Method Details

.to_xml(site) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/broadway/api.rb', line 4

def Post.to_xml(site)
  xml = Nokogiri::XML::Builder.new { |xml| 
    xml.send("posts", :type => "array") {
      site.posts.each do |post|
        xml.post {
          xml.title post.data["title"]
          xml.url post.url
          xml.categories post.categories.join(",")
          xml.date(:type => "date") {
            xml.text post.date.to_s
          }
          xml.slug post.slug
          xml.published(:type => "boolean") {
            xml.text post.published.to_s
          }
          xml.tags post.tags.join(",")
        }
      end
    }
  }
  xml.to_xml
end

.valid?(name, site) ⇒ Boolean

Post name validator. Post filenames must be like:

2008-11-05-my-awesome-post.textile

Returns <Bool>

Returns:

  • (Boolean)


15
16
17
# File 'lib/broadway/post.rb', line 15

def self.valid?(name, site)
  site.config[:posts_include].include?(File.extname(name))
end

Instance Method Details

#<=>(other) ⇒ Object

Spaceship is based on Post#date, slug

Returns -1, 0, 1



46
47
48
49
50
51
52
# File 'lib/broadway/post.rb', line 46

def <=>(other)
  if self.date and other.date
    self.date <=> other.date
  else
    self.position <=> other.position
  end
end

#inspectObject



107
108
109
# File 'lib/broadway/post.rb', line 107

def inspect
  "#<Broadway:Post @url=#{self.url.inspect} @categories=#{self.categories.inspect} @tags=#{self.tags.inspect} @data=#{self.data.inspect}>"
end

#render(layouts, site_payload) ⇒ Object

Add any necessary layouts to this post

+layouts+ is a Hash of {"name" => "layout"}
+site_payload+ is the site payload hash

Returns nothing



89
90
91
92
# File 'lib/broadway/post.rb', line 89

def render(layouts, site_payload)
  payload = {"site" => {}, "page" => self.to_liquid}.deep_merge(site_payload)
  do_layout(payload, layouts)
end

#templateObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/broadway/post.rb', line 54

def template
  case self.site.permalink_style
  when :pretty
    "/:categories/:year/:month/:day/:title"
  when :none
    "/:categories/:title.html"
  when :date
    "/:categories/:year/:month/:day/:title.html"
  else
    self.site.permalink_style.to_s
  end
end

#to_liquidObject

Convert this post into a Hash for use in Liquid templates.

Returns <Hash>



97
98
99
100
101
102
103
104
105
# File 'lib/broadway/post.rb', line 97

def to_liquid
  { "title"      => self.title || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '),
    "url"        => self.url,
    "date"       => self.date,
    "id"         => self.id,
    "categories" => self.categories,
    "tags"       => self.tags,
    "content"    => self.content }.deep_merge(self.data)
end

#urlObject

The generated relative url of this post e.g. /2008/11/05/my-awesome-post.html

Returns <String>



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/broadway/post.rb', line 71

def url
  return permalink if permalink
  @url ||= {
    "year"       => date ? date.strftime("%Y") : "",
    "month"      => date ? date.strftime("%m") : "",
    "day"        => date ? date.strftime("%d") : "",
    "title"      => CGI.escape(slug),
    "categories" => categories.join('/')
  }.inject(template) { |result, token|
    result.gsub(/:#{token.first}/, token.last)
  }.gsub(/#{site.config[:posts]}/, "").squeeze("/")
end