Class: Broadway::Post
- Inherits:
-
Object
- Object
- Broadway::Post
- Includes:
- Convertible, Resource, Comparable
- Defined in:
- lib/broadway/post.rb
Constant Summary collapse
- SRC_MATCHER =
/^(.+\/)*(?:(\d+-\d+-\d+)-)?(.*)(\.[^.]+)$/
- URL_MATCHER =
/^(.+\/)*(.*)$/
Class Method Summary collapse
- .to_xml(site) ⇒ Object
-
.valid?(name, site) ⇒ Boolean
Post name validator.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Spaceship is based on Post#date, slug.
-
#initialize(options = {}) ⇒ Post
constructor
Initialize this Post instance.
- #inspect ⇒ Object
-
#render(layouts, site_payload) ⇒ Object
Add any necessary layouts to this post
layouts
is a Hash of => “layout”site_payload
is the site payload hash. - #template ⇒ Object
-
#to_liquid ⇒ Object
Convert this post into a Hash for use in Liquid templates.
-
#url ⇒ Object
The generated relative url of this post e.g.
Methods included from Resource
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( = {}) self.site = [:site] self.path = [:path] if .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 = .has_key?(:dir) ? [:dir] : File.dirname(path.gsub(/#{site.config[:source]}\/?/, "")) base = self.dir == "." ? "" : self.dir self.categories = base.split('/').reject { |x| x.empty? } process() 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. post..join(",") } end } } xml.to_xml 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 |
#inspect ⇒ Object
107 108 109 |
# File 'lib/broadway/post.rb', line 107 def inspect "#<Broadway:Post @url=#{self.url.inspect} @categories=#{self.categories.inspect} @tags=#{self..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 |
#template ⇒ Object
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_liquid ⇒ Object
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., "content" => self.content }.deep_merge(self.data) end |
#url ⇒ Object
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 |