Class: Broadway::Page

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Convertible

#content_type, #do_layout, #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>



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

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.name = 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

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



7
8
9
# File 'lib/broadway/page.rb', line 7

def basename
  @basename
end

#categoriesObject

Returns the value of attribute categories.



8
9
10
# File 'lib/broadway/page.rb', line 8

def categories
  @categories
end

#childrenObject

Returns the value of attribute children.



8
9
10
# File 'lib/broadway/page.rb', line 8

def children
  @children
end

#contentObject

Returns the value of attribute content.



8
9
10
# File 'lib/broadway/page.rb', line 8

def content
  @content
end

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/broadway/page.rb', line 8

def data
  @data
end

#dirObject

Returns the value of attribute dir.



7
8
9
# File 'lib/broadway/page.rb', line 7

def dir
  @dir
end

#extObject

Returns the value of attribute ext.



7
8
9
# File 'lib/broadway/page.rb', line 7

def ext
  @ext
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/broadway/page.rb', line 7

def name
  @name
end

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/broadway/page.rb', line 8

def output
  @output
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/broadway/page.rb', line 7

def path
  @path
end

#siteObject

Returns the value of attribute site.



6
7
8
# File 'lib/broadway/page.rb', line 6

def site
  @site
end

#slugObject

Returns the value of attribute slug.



7
8
9
# File 'lib/broadway/page.rb', line 7

def slug
  @slug
end

#tagsObject

Returns the value of attribute tags.



8
9
10
# File 'lib/broadway/page.rb', line 8

def tags
  @tags
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

#idObject

The UID for this post (useful in feeds) e.g. /2008/11/05/my-awesome-post

Returns <String>



36
37
38
# File 'lib/broadway/page.rb', line 36

def id
  File.join(self.dir, self.name)
end

#inspectObject



128
129
130
# File 'lib/broadway/page.rb', line 128

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


40
41
42
# File 'lib/broadway/page.rb', line 40

def menu_title
  self.data && self.data['menu_title']
end

#parentObject



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

def parent
  url ? site.find_page_by_url(url[1..-1].split("/").first) : nil
end


48
49
50
# File 'lib/broadway/page.rb', line 48

def permalink
  self.data && self.data['permalink']
end

#process(options = {}) ⇒ Object

Extract information from the page filename

+name+ is the String filename of the page file

Returns nothing



90
91
92
93
94
# File 'lib/broadway/page.rb', line 90

def process(options = {})
  self.read_yaml(path)
  
  self.tags = self.data.pluralized_array("tag", "tags")
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



101
102
103
104
# File 'lib/broadway/page.rb', line 101

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

#show_children?Boolean

Returns:

  • (Boolean)


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

def show_children?
  self.data && (!self.data.has_key?("show_children") || self.data["show_children"] == true)
end

#templateObject



78
79
80
81
82
83
84
# File 'lib/broadway/page.rb', line 78

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

#titleObject



56
57
58
# File 'lib/broadway/page.rb', line 56

def title
  self.data && (self.data['title'] || self.name)
end

#tooltipObject



44
45
46
# File 'lib/broadway/page.rb', line 44

def tooltip
  self.data && self.data["tooltip"]
end

#urlObject

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

Returns <String>



68
69
70
71
72
73
74
75
76
# File 'lib/broadway/page.rb', line 68

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

#write(dest_prefix, dest_suffix = nil) ⇒ Object

Write the generated page file to the destination directory.

+dest_prefix+ is the String path to the destination dir
+dest_suffix+ is a suffix path to the destination dir

Returns nothing



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/broadway/page.rb', line 111

def write(dest_prefix, dest_suffix = nil)
  dest = File.join(dest_prefix, @dir)
  dest = File.join(dest, dest_suffix) if dest_suffix
  FileUtils.mkdir_p(dest)

  # The url needs to be unescaped in order to preserve the correct filename
  path = File.join(dest, CGI.unescape(self.url))
  if self.ext == '.html' && self.url[/\.html$/].nil?
    FileUtils.mkdir_p(path)
    path = File.join(path, "index.html")
  end

  File.open(path, 'w') do |f|
    f.write(self.output)
  end
end