Class: Plate::Page

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/plate/page.rb

Direct Known Subclasses

Asset, DynamicPage, Partial, Post, StaticPage

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

included

Constructor Details

#initialize(site, file = nil, load_on_initialize = true) ⇒ Page

Returns a new instance of Page.



18
19
20
21
22
23
24
25
26
# File 'lib/plate/page.rb', line 18

def initialize(site, file = nil, load_on_initialize = true)
  self.site = site
  self.file = file
  self.meta = {}
  self.content = ""
  self.partials = []

  load! if load_on_initialize and file?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#layoutObject

The layout to use when rendering this page. Returns nil if no default layout is available, or the layout has specifically been turned off within the config.



149
150
151
# File 'lib/plate/page.rb', line 149

def layout
  @layout
end

#metaObject

Returns the value of attribute meta.



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

def meta
  @meta
end

#partialsObject

If set, will return an array of partial names used within this page.

In watch mode, this allows the current page to be reloaded when any of its partials are reloaded.



16
17
18
# File 'lib/plate/page.rb', line 16

def partials
  @partials
end

#rawObject (readonly)

The raw source of this page, before any manipulation.



10
11
12
# File 'lib/plate/page.rb', line 10

def raw
  @raw
end

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

Instance Method Details

#<=>(other) ⇒ Object

Compare two posts, by date.



288
289
290
# File 'lib/plate/page.rb', line 288

def <=>(other)
  self.path <=> other.path
end

#==(other) ⇒ Object

Is this page equal to another page being sent?



282
283
284
285
# File 'lib/plate/page.rb', line 282

def ==(other)
  other = other.relative_file if other.respond_to?(:relative_file)
  self.id == other or self.relative_file == other
end

#base_pathObject



47
48
49
# File 'lib/plate/page.rb', line 47

def base_path
  Pathname.new(File.join(self.site.source, 'content'))
end

#basenameObject Also known as: name

The name of the file, without any path data



42
43
44
# File 'lib/plate/page.rb', line 42

def basename
  File.basename(self.file)
end

#directoryObject

The directory this page is located in, relative to the site root.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/plate/page.rb', line 52

def directory
  return @directory if @directory

  current = Pathname.new(self.file)

  dirs = current.relative_path_from(base_path).to_s.split('/')

  if dirs.size > 1
    dirs.pop
    @directory = "/#{dirs.join('/')}"
  else
    @directory = "/"
  end
end

#downgrade?Boolean

Check a static page to see if it should be converted into a Page

Returns:

  • (Boolean)


68
69
70
# File 'lib/plate/page.rb', line 68

def downgrade?
  !raw.start_with?('---')
end

#enginesObject



72
73
74
# File 'lib/plate/page.rb', line 72

def engines
  @engines ||= self.extensions.reverse.collect { |e| self.site.registered_page_engines[e.gsub(/\./, '').to_sym] }.reject { |e| !e }
end

#extensionObject

the last file extension for this page



77
78
79
# File 'lib/plate/page.rb', line 77

def extension
  extensions.last.gsub(/^\./, '')
end

#extensionsObject

List of all extensions for this page



82
83
84
# File 'lib/plate/page.rb', line 82

def extensions
  @extensions ||= self.basename.scan(/\.[^.]+/)
end

#file?Boolean

Does the file exist or not.

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/plate/page.rb', line 87

def file?
  return false if self.file.nil?
  File.exists?(self.file)
end

#file_nameObject

Returns just the file name, no extension.



93
94
95
# File 'lib/plate/page.rb', line 93

def file_name
  File.basename(self.file, '.*')
end

#file_pathObject

The full file path of where this file will be written to. (Relative to site root)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/plate/page.rb', line 98

def file_path
  return @file_path if @file_path

  result = nil

  if self.meta.has_key?(:path)
    result = self.meta[:path]
    result = "/#{result}" unless result.start_with?('/')
  else
    result = directory
    result << '/' unless result =~ /\/$/
    result << slug unless slug == 'index'

    # Remove any double slashes
    result.gsub!(/\/\//, '/')

    # Remove file extensions, and cleanup URL
    result = result.split('/').reject{ |segment| segment =~ /^\.+$/ }.join('/')

    # Add a trailing slash
    result << '/' unless result =~ /\/$/

    # Tack on index.html for the folder
    result << 'index.html'
  end

  @file_path = result
end

#format_extensionObject



127
128
129
130
131
# File 'lib/plate/page.rb', line 127

def format_extension
  format = self.extensions.reverse.detect() { |e| !self.site.page_engine_extensions.include?(e) }
  format = ".html" if format.nil?
  format
end

#idObject

A unique ID for this page.



134
135
136
# File 'lib/plate/page.rb', line 134

def id
  @id ||= Digest::MD5.hexdigest(relative_file)
end

#inspectObject



138
139
140
# File 'lib/plate/page.rb', line 138

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} name=#{name.to_s.inspect}>"
end

#keywordsObject

Utility method to sanitize keywords output. Keywords are returned as an array.



143
144
145
# File 'lib/plate/page.rb', line 143

def keywords
  @keywords ||= (Array === self.meta[:keywords] ? self.meta[:keywords] : self.meta[:keywords].to_s.strip.split(',').collect(&:strip))
end

#load!Object

Read the file data for this page

Raises:



172
173
174
175
176
177
178
179
180
# File 'lib/plate/page.rb', line 172

def load!
  return if @loaded
  raise FileNotFound unless file?

  read_file!
  read_metadata!

  @loaded = true
end

#loaded?Boolean

Has this page been loaded from file?

Returns:

  • (Boolean)


167
168
169
# File 'lib/plate/page.rb', line 167

def loaded?
  !!@loaded
end

#pathObject



182
183
184
185
# File 'lib/plate/page.rb', line 182

def path
  return '/' if self.file_path == '/index.html'
  @path ||= self.file_path.sub(/(.*?)\/index\.html$/i, '\1')
end

#relative_fileObject

The file’s source path, relative to site root.



188
189
190
# File 'lib/plate/page.rb', line 188

def relative_file
  @relative_file ||= self.site.relative_path(self.file)
end

#reload!Object



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/plate/page.rb', line 192

def reload!
  self.meta = {}
  # self.partials = []
  self.content = nil

  @loaded = false
  @rendered_content = nil
  @body = nil
  @keywords = nil
  @layout = nil

  load!
end

#rendered_bodyObject

Returns the rendered body of this page, without the layout.

The callbacks ‘before_render` and `after_render` are called here. To perform custom actions before or after a page file is written to disk, use these callback methods.

See Callbacks for more information on setting up callbacks.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/plate/page.rb', line 213

def rendered_body
  return @body if @body

  result = ""

  around_callback :render do
    result = self.content

    view = View.new(self.site, self)

    self.engines.each do |engine|
      template = engine.new(self.file) { result }
      result = template.render(view, {})
    end

    view = nil

    @body = result
  end

  @body
end

#rendered_contentObject



236
237
238
# File 'lib/plate/page.rb', line 236

def rendered_content
  @rendered_content ||= self.apply_layout_to(rendered_body)
end

#slugObject

Name of the file to be saved. Just takes the current file name and removes any extensions.



241
242
243
# File 'lib/plate/page.rb', line 241

def slug
  self.basename.to_s.downcase.split('.')[0].dasherize.parameterize
end

#title_for_urlObject

The title from this page’s meta data, turned into a parameter for use in a url.



246
247
248
# File 'lib/plate/page.rb', line 246

def title_for_url
  self.title.to_s.dasherize.parameterize
end

#to_sObject

Returns this page’s content



251
252
253
# File 'lib/plate/page.rb', line 251

def to_s
  self.inspect
end

#urlObject

The full URL of this page. Depends on the site’s URL attribute and a config option of ‘:base_url`



256
257
258
# File 'lib/plate/page.rb', line 256

def url
  @url ||= "#{site.url}#{path}"
end

#write!Object

Write the compiled page file to the destination. The content is written to disk using the path designated in ‘file_path` and the content from `rendered_content`.

The callbacks ‘before_write` and `after_write` are included here. To perform custom actions before or after a page file is written to disk, use these callback methods.

See Callbacks for more information on setting up callbacks.



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/plate/page.rb', line 268

def write!
  path = File.join(site.build_destination, file_path)
  FileUtils.mkdir_p(File.dirname(path))

  around_callback :write do
    File.open(path, 'w') do |f|
      f.write(self.rendered_content)
    end
  end

  path
end