Class: Nesta::Page

Inherits:
FileModel show all
Defined in:
lib/nesta/models.rb

Constant Summary

Constants inherited from FileModel

FileModel::FORMATS

Instance Attribute Summary

Attributes inherited from FileModel

#filename, #mtime

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileModel

#abspath, #coderay, #description, find_all, #index_page?, #initialize, #keywords, #last_modified, #layout, load, menu_items, #metadata, needs_loading?, #path, #permalink, purge_cache, #template, #to_html

Constructor Details

This class inherits a constructor from Nesta::FileModel

Class Method Details

.find_articlesObject



162
163
164
165
166
# File 'lib/nesta/models.rb', line 162

def self.find_articles
  find_all.select do |page|
    page.date && page.date < DateTime.now
  end.sort { |x, y| y.date <=> x.date }
end

.find_by_path(path) ⇒ Object



158
159
160
# File 'lib/nesta/models.rb', line 158

def self.find_by_path(path)
  load(path)
end

.model_path(basename = nil) ⇒ Object



154
155
156
# File 'lib/nesta/models.rb', line 154

def self.model_path(basename = nil)
  Nesta::Config.page_path(basename)
end

Instance Method Details

#==(other) ⇒ Object



168
169
170
# File 'lib/nesta/models.rb', line 168

def ==(other)
  other.respond_to?(:path) && (self.path == other.path)
end

#articlesObject



283
284
285
# File 'lib/nesta/models.rb', line 283

def articles
  Page.find_articles.select { |article| article.categories.include?(self) }
end

#atom_idObject



207
208
209
# File 'lib/nesta/models.rb', line 207

def atom_id
  ("atom id")
end

#bodyObject



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/nesta/models.rb', line 227

def body
  case @format
  when :mdown
    body_text = markup.sub(/^#[^#].*$\r?\n(\r?\n)?/, "")
    Maruku.new(body_text).to_html
  when :haml
    body_text = markup.sub(/^\s*%h1\s+.*$\r?\n(\r?\n)?/, "")
    Haml::Engine.new(body_text).render
  when :textile
    body_text = markup.sub(/^\s*h1\.\s+.*$\r?\n(\r?\n)?/, "")
    RedCloth.new(body_text).to_html
  end
end

#categoriesObject



241
242
243
244
245
246
247
# File 'lib/nesta/models.rb', line 241

def categories
  paths = category_strings.map { |specifier| specifier.sub(/:-?\d+$/, '') }
  pages = valid_paths(paths).map { |p| Page.find_by_path(p) }
  pages.sort do |x, y|
    x.heading.downcase <=> y.heading.downcase
  end
end

#date(format = nil) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/nesta/models.rb', line 197

def date(format = nil)
  @date ||= if ("date")
              if format == :xmlschema
                Time.parse(("date")).xmlschema
              else
                DateTime.parse(("date"))
              end
            end
end

#headingObject



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/nesta/models.rb', line 172

def heading
  regex = case @format
    when :mdown
      /^#\s*(.*)/
    when :haml
      /^\s*%h1\s+(.*)/
    when :textile
      /^\s*h1\.\s+(.*)/
    end
  markup =~ regex
  Regexp.last_match(1)
end

#pagesObject



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/nesta/models.rb', line 270

def pages
  Page.find_all.select do |page|
    page.date.nil? && page.categories.include?(self)
  end.sort do |x, y|
    by_priority = y.priority(path) <=> x.priority(path)
    if by_priority == 0
      x.heading.downcase <=> y.heading.downcase
    else
      by_priority
    end
  end
end

#parentObject



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/nesta/models.rb', line 256

def parent
  if abspath == '/'
    nil
  else
    parent_path = File.dirname(path)
    while parent_path != '.' do
      parent = Page.load(parent_path)
      return parent unless parent.nil?
      parent_path = File.dirname(parent_path)
    end
    Page.load('index')
  end
end

#priority(category) ⇒ Object



249
250
251
252
253
254
# File 'lib/nesta/models.rb', line 249

def priority(category)
  category_string = category_strings.detect do |string|
    string =~ /^#{category}([,:\s]|$)/
  end
  category_string && category_string.split(':', 2)[-1].to_i 
end

#read_moreObject



211
212
213
# File 'lib/nesta/models.rb', line 211

def read_more
  ("read more") || "Continue reading"
end

#summaryObject



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/nesta/models.rb', line 215

def summary
  if summary_text = ("summary")
    summary_text.gsub!('\n', "\n")
    case @format
    when :textile
      RedCloth.new(summary_text).to_html
    else
      Maruku.new(summary_text).to_html
    end
  end
end

#titleObject



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/nesta/models.rb', line 185

def title
  if ('title')
    ('title')
  elsif parent && (! parent.heading.nil?)
    "#{heading} - #{parent.heading}"
  elsif heading
    "#{heading} - #{Nesta::Config.title}"
  elsif abspath == '/'
    Nesta::Config.title
  end
end