Class: Broadway::Page

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

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 = {}) ⇒ Page

Initialize a new Page.

+site+ is the Site
+base+ is the String path to the <source>
+dir+ is the String path between <source> and the file
+name+ is the String filename of the file

Returns <Page>



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/broadway/page.rb', line 15

def initialize(options = {})
  self.site = options[:site]
  self.path = options[:path] if options.has_key?(:path)
  self.data = {}
  self.dir = path =~ /\// ? File.dirname(path.gsub(/#{site.config[:source]}\/?/, "")).gsub(/^\//, "") : path
  self.slug = self.dir == "." ? "" : File.basename(self.dir)
  self.ext = File.extname(path)
  self.basename = File.basename(path).split('.')[0..-2].first

  self.categories ||= []
  self.categories.concat self.dir.split('/').reject { |x| x.empty? }
  self.children ||= []
  process(options) unless options.has_key?(:process) and options[:process] == false
end

Class Method Details

.to_xml(site) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/broadway/api.rb', line 27

def Page.to_xml(site)
  xml = Nokogiri::XML::Builder.new { |xml| 
    xml.send("pages", :type => "array") {
      site.pages.each do |post|
        xml.page {
          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

Instance Method Details

#<=>(other) ⇒ Object



52
53
54
# File 'lib/broadway/page.rb', line 52

def <=>(other)
  return self.position <=> other.position
end

#inspectObject



66
67
68
# File 'lib/broadway/page.rb', line 66

def inspect
  "#<Broadway:Page @url=#{self.url.inspect} @slug=#{self.slug.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 {"slug" => "layout"}
+site_payload+ is the site payload hash

Returns nothing



61
62
63
64
# File 'lib/broadway/page.rb', line 61

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

#templateObject



44
45
46
47
48
49
50
# File 'lib/broadway/page.rb', line 44

def template
  if self.site.permalink_style == :pretty
    "/:categories/:slug"
  else
    "/:categories/:slug.html"
  end
end

#urlObject

The generated relative url of this page e.g. /about

Returns <String>



34
35
36
37
38
39
40
41
42
# File 'lib/broadway/page.rb', line 34

def url
  return permalink if permalink
  @url ||= {
    "slug"      => CGI.escape(slug),
    "categories" => categories[0..-2].join('/')
  }.inject(template) { |result, token|
    result.gsub(/:#{token.first}/, token.last)
  }.gsub(/#{site.config[:posts]}/, "").squeeze("/")
end