Class: Bonsai::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/bonsai/page.rb

Defined Under Namespace

Classes: NotFound

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Page

Returns a new instance of Page.



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

def initialize(path)
  @disk_path = path
end

Class Attribute Details

.pagesObject

Returns the value of attribute pages.



22
23
24
# File 'lib/bonsai/page.rb', line 22

def pages
  @pages
end

.pathObject

Returns the value of attribute path.



22
23
24
# File 'lib/bonsai/page.rb', line 22

def path
  @path
end

Instance Attribute Details

#disk_pathObject (readonly)

Returns the value of attribute disk_path.



46
47
48
# File 'lib/bonsai/page.rb', line 46

def disk_path
  @disk_path
end

Class Method Details

.all(dir_path = path, pattern = "*/**") ⇒ Object



26
27
28
# File 'lib/bonsai/page.rb', line 26

def all(dir_path = path, pattern = "*/**")
  Dir["#{dir_path}/#{pattern}/*.yml"].map {|p| Page.new p }
end

.find(permalink) ⇒ Object



30
31
32
# File 'lib/bonsai/page.rb', line 30

def find(permalink)
  pages[permalink] ||= find!(permalink)
end

Instance Method Details

#==(other) ⇒ Object



122
123
124
# File 'lib/bonsai/page.rb', line 122

def ==(other)
  self.permalink == other.permalink
end

#ancestorsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bonsai/page.rb', line 108

def ancestors
  ancestors = []
  # Remove leading slash
  page_ref = permalink.gsub(/^\//, '')
  
  # Find pages up the permalink tree if possible
  while(page_ref) do
    page_ref = page_ref[/(.+\/)[^\/]*\/$/, 1]
    ancestors << self.class.find(page_ref) rescue NotFound
  end
  
  ancestors.compact.reverse
end

#assetsObject

This method is used for the exporter to copy assets



76
77
78
79
80
# File 'lib/bonsai/page.rb', line 76

def assets
  Dir["#{directory}/**/*"].sort.select{|path| !File.directory?(path) && !File.basename(path).include?("yml") }.map do |file|
    file_to_hash(file)
  end
end

#childrenObject



104
105
106
# File 'lib/bonsai/page.rb', line 104

def children
  self.class.all(File.dirname(disk_path), "*").delete_if{|p| p.floating? }.sort_by{|p| p.disk_path }
end

#contentObject



130
131
132
133
134
# File 'lib/bonsai/page.rb', line 130

def content
  YAML::load(File.read(disk_path)) || {}
rescue ArgumentError
  raise "Page '#{permalink}' has badly formatted content"
end

#ctimeObject



64
# File 'lib/bonsai/page.rb', line 64

def ctime; File.ctime(disk_path); end

#floating?Boolean

“Floating pages” are pages that are not prefixed with a numeric eg: 1.about-us These pages are not present in the ‘children` or other meta-content arrays

Returns:

  • (Boolean)


84
85
86
# File 'lib/bonsai/page.rb', line 84

def floating?
  !!(File.dirname(disk_path) =~ /\/[a-zA-z][\w-]+$/)
end

#mtimeObject



65
# File 'lib/bonsai/page.rb', line 65

def mtime; File.mtime(disk_path); end

#nameObject



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

def name
  slug.gsub(/\W/, " ").gsub(/\d\./, '').gsub(/^\w/){$&.upcase}
end

#parentObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bonsai/page.rb', line 88

def parent
  id = permalink[/\/(.+\/)[^\/]*\/$/, 1]
  return nil if id.nil?
  
  parent = Page.find(id)
  return nil if parent == self
  
  parent
rescue NotFound
  nil
end


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

def permalink
  web_path(directory) + '/'
end

#renderObject



126
127
128
# File 'lib/bonsai/page.rb', line 126

def render
  Tilt.new(template.path, :path => template.class.path).render(self, to_hash)
end

#siblingsObject



100
101
102
# File 'lib/bonsai/page.rb', line 100

def siblings
  self.class.all(File.dirname(disk_path[/(.+)\/[^\/]*$/, 1]), "*").delete_if{|p| p == self}
end

#slugObject



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

def slug
  permalink.gsub(/\/$/, '').split('/').pop
end

#templateObject



71
72
73
# File 'lib/bonsai/page.rb', line 71

def template
  Template.find(template_name)
end

#to_hashObject Also known as: to_liquid

This hash is available to all templates, it will map common properties, content file results, as well as any “magic” hashes for file system contents



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/bonsai/page.rb', line 139

def to_hash
  hash = {
    :slug         => slug, 
    :permalink    => permalink, 
    :name         => name, 
    :children     => children,
    :siblings     => siblings,
    :parent       => parent, 
    :ancestors    => ancestors,
    :navigation   => Bonsai::Navigation.tree,
    :updated_at   => mtime,
    :created_at   => ctime
  }.merge(formatted_content).merge(disk_assets).merge(Bonsai.site)
  
  hash.stringify_keys
end

#write_pathObject



67
68
69
# File 'lib/bonsai/page.rb', line 67

def write_path
  "#{permalink}index.html"
end