Class: Broadway::Page
- Inherits:
-
Object
- Object
- Broadway::Page
- Includes:
- Convertible
- Defined in:
- lib/broadway/page.rb
Instance Attribute Summary collapse
-
#basename ⇒ Object
Returns the value of attribute basename.
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#children ⇒ Object
Returns the value of attribute children.
-
#content ⇒ Object
Returns the value of attribute content.
-
#data ⇒ Object
Returns the value of attribute data.
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#ext ⇒ Object
Returns the value of attribute ext.
-
#name ⇒ Object
Returns the value of attribute name.
-
#output ⇒ Object
Returns the value of attribute output.
-
#path ⇒ Object
Returns the value of attribute path.
-
#site ⇒ Object
Returns the value of attribute site.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#tags ⇒ Object
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
-
#id ⇒ Object
The UID for this post (useful in feeds) e.g.
-
#initialize(options = {}) ⇒ Page
constructor
Initialize a new Page.
- #inspect ⇒ Object
- #menu_title ⇒ Object
- #parent ⇒ Object
- #permalink ⇒ Object
-
#process(options = {}) ⇒ Object
Extract information from the page filename
name
is the String filename of the page file. -
#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. - #show_children? ⇒ Boolean
- #template ⇒ Object
- #title ⇒ Object
- #tooltip ⇒ Object
-
#url ⇒ Object
The generated relative url of this page e.g.
-
#write(dest_prefix, dest_suffix = nil) ⇒ Object
Write the generated page file to the destination directory.
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( = {}) self.site = [:site] self.path = [:path] if .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() unless .has_key?(:process) and [:process] == false end |
Instance Attribute Details
#basename ⇒ Object
Returns the value of attribute basename.
7 8 9 |
# File 'lib/broadway/page.rb', line 7 def basename @basename end |
#categories ⇒ Object
Returns the value of attribute categories.
8 9 10 |
# File 'lib/broadway/page.rb', line 8 def categories @categories end |
#children ⇒ Object
Returns the value of attribute children.
8 9 10 |
# File 'lib/broadway/page.rb', line 8 def children @children end |
#content ⇒ Object
Returns the value of attribute content.
8 9 10 |
# File 'lib/broadway/page.rb', line 8 def content @content end |
#data ⇒ Object
Returns the value of attribute data.
8 9 10 |
# File 'lib/broadway/page.rb', line 8 def data @data end |
#dir ⇒ Object
Returns the value of attribute dir.
7 8 9 |
# File 'lib/broadway/page.rb', line 7 def dir @dir end |
#ext ⇒ Object
Returns the value of attribute ext.
7 8 9 |
# File 'lib/broadway/page.rb', line 7 def ext @ext end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/broadway/page.rb', line 7 def name @name end |
#output ⇒ Object
Returns the value of attribute output.
8 9 10 |
# File 'lib/broadway/page.rb', line 8 def output @output end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/broadway/page.rb', line 7 def path @path end |
#site ⇒ Object
Returns the value of attribute site.
6 7 8 |
# File 'lib/broadway/page.rb', line 6 def site @site end |
#slug ⇒ Object
Returns the value of attribute slug.
7 8 9 |
# File 'lib/broadway/page.rb', line 7 def slug @slug end |
#tags ⇒ Object
Returns the value of attribute tags.
8 9 10 |
# File 'lib/broadway/page.rb', line 8 def @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. post..join(",") } end } } xml.to_xml end |
Instance Method Details
#id ⇒ Object
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 |
#inspect ⇒ Object
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..inspect} @data=#{self.data.inspect}>" end |
#menu_title ⇒ Object
40 41 42 |
# File 'lib/broadway/page.rb', line 40 def self.data && self.data['menu_title'] end |
#parent ⇒ Object
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 |
#permalink ⇒ Object
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( = {}) self.read_yaml(path) self. = 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
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 |
#template ⇒ Object
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 |
#title ⇒ Object
56 57 58 |
# File 'lib/broadway/page.rb', line 56 def title self.data && (self.data['title'] || self.name) end |
#tooltip ⇒ Object
44 45 46 |
# File 'lib/broadway/page.rb', line 44 def tooltip self.data && self.data["tooltip"] end |
#url ⇒ Object
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 |