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.



146
147
148
# File 'lib/plate/page.rb', line 146

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.



285
286
287
# File 'lib/plate/page.rb', line 285

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

#==(other) ⇒ Object

Is this page equal to another page being sent?



279
280
281
282
# File 'lib/plate/page.rb', line 279

def ==(other)
  other = other.relative_file if other.respond_to?(:relative_file)
  self.id == other or self.relative_file == other
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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/plate/page.rb', line 48

def directory
  return @directory if @directory

  base = Pathname.new(File.join(self.site.source, 'content'))
  current = Pathname.new(self.file)

  dirs = current.relative_path_from(base).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)


65
66
67
# File 'lib/plate/page.rb', line 65

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

#enginesObject



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

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



74
75
76
# File 'lib/plate/page.rb', line 74

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

#extensionsObject

List of all extensions for this page



79
80
81
# File 'lib/plate/page.rb', line 79

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

#file?Boolean

Does the file exist or not.

Returns:

  • (Boolean)


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

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

#file_nameObject

Returns just the file name, no extension.



90
91
92
# File 'lib/plate/page.rb', line 90

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)



95
96
97
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
# File 'lib/plate/page.rb', line 95

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



124
125
126
127
128
# File 'lib/plate/page.rb', line 124

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.



131
132
133
# File 'lib/plate/page.rb', line 131

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

#inspectObject



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

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.



140
141
142
# File 'lib/plate/page.rb', line 140

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:



169
170
171
172
173
174
175
176
177
# File 'lib/plate/page.rb', line 169

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)


164
165
166
# File 'lib/plate/page.rb', line 164

def loaded?
  !!@loaded
end

#pathObject



179
180
181
182
# File 'lib/plate/page.rb', line 179

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.



185
186
187
# File 'lib/plate/page.rb', line 185

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

#reload!Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/plate/page.rb', line 189

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.



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

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



233
234
235
# File 'lib/plate/page.rb', line 233

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.



238
239
240
# File 'lib/plate/page.rb', line 238

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.



243
244
245
# File 'lib/plate/page.rb', line 243

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

#to_sObject

Returns this page’s content



248
249
250
# File 'lib/plate/page.rb', line 248

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`



253
254
255
# File 'lib/plate/page.rb', line 253

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.



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/plate/page.rb', line 265

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