Class: Nesta::Page
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, #description, find_file_for_path, #flagged_as?, #index_page?, #initialize, #keywords, #last_modified, #layout, load, #metadata, needs_loading?, #parse_metadata, #path, #permalink, purge_cache, #template, #to_html
Class Method Details
.find_all ⇒ Object
15
16
17
|
# File 'lib/nesta/models/page.rb', line 15
def self.find_all
super.select { |p| ! p.hidden? }
end
|
.find_articles ⇒ Object
19
20
21
22
23
|
# File 'lib/nesta/models/page.rb', line 19
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
10
11
12
13
|
# File 'lib/nesta/models/page.rb', line 10
def self.find_by_path(path)
page = load(path)
page && page.hidden? ? nil : page
end
|
.model_path(basename = nil) ⇒ Object
6
7
8
|
# File 'lib/nesta/models/page.rb', line 6
def self.model_path(basename = nil)
Nesta::Config.page_path(basename)
end
|
Instance Method Details
#articles ⇒ Object
140
141
142
|
# File 'lib/nesta/models/page.rb', line 140
def articles
Page.find_articles.select { |article| article.categories.include?(self) }
end
|
#atom_id ⇒ Object
69
70
71
|
# File 'lib/nesta/models/page.rb', line 69
def atom_id
metadata('atom id')
end
|
#body(scope = Object.new) ⇒ Object
95
96
97
|
# File 'lib/nesta/models/page.rb', line 95
def body(scope = Object.new)
convert_to_html(@format, scope, body_markup)
end
|
#body_markup ⇒ Object
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/nesta/models/page.rb', line 84
def body_markup
case @format
when :mdown, :md
markup.sub(/^#[^#].*$\r?\n(\r?\n)?/, '')
when :haml
markup.sub(/^\s*%h1\s+.*$\r?\n(\r?\n)?/, '')
when :textile
markup.sub(/^\s*h1\.\s+.*$\r?\n(\r?\n)?/, '')
end
end
|
#categories ⇒ Object
99
100
101
102
|
# File 'lib/nesta/models/page.rb', line 99
def categories
paths = category_strings.map { |specifier| specifier.sub(/:-?\d+$/, '') }
valid_paths(paths).map { |p| Page.find_by_path(p) }
end
|
#date(format = nil) ⇒ Object
59
60
61
62
63
64
65
66
67
|
# File 'lib/nesta/models/page.rb', line 59
def date(format = nil)
@date ||= if metadata("date")
if format == :xmlschema
Time.parse(metadata("date")).xmlschema
else
DateTime.parse(metadata("date"))
end
end
end
|
#draft? ⇒ Boolean
25
26
27
|
# File 'lib/nesta/models/page.rb', line 25
def draft?
flagged_as?('draft')
end
|
#heading ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/nesta/models/page.rb', line 33
def heading
regex = case @format
when :mdown, :md
/^#\s*(.*?)(\s*#+|$)/
when :haml
/^\s*%h1\s+(.*)/
when :textile
/^\s*h1\.\s+(.*)/
end
markup =~ regex
Regexp.last_match(1) or raise HeadingNotSet, "#{abspath} needs a heading"
end
|
#hidden? ⇒ Boolean
29
30
31
|
# File 'lib/nesta/models/page.rb', line 29
def hidden?
draft? && Nesta::App.production?
end
|
#link_text ⇒ Object
46
47
48
49
50
|
# File 'lib/nesta/models/page.rb', line 46
def link_text
metadata('link text') || heading
rescue HeadingNotSet
raise LinkTextNotSet, "Need to link to '#{abspath}' but can't get link text"
end
|
#pages ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/nesta/models/page.rb', line 126
def pages
in_category = Page.find_all.select do |page|
page.date.nil? && page.categories.include?(self)
end
in_category.sort do |x, y|
by_priority = y.priority(path) <=> x.priority(path)
if by_priority == 0
x.link_text.downcase <=> y.link_text.downcase
else
by_priority
end
end
end
|
#parent ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/nesta/models/page.rb', line 111
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
return categories.first unless categories.empty?
Page.load('index')
end
end
|
#priority(category) ⇒ Object
104
105
106
107
108
109
|
# File 'lib/nesta/models/page.rb', line 104
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_more ⇒ Object
73
74
75
|
# File 'lib/nesta/models/page.rb', line 73
def read_more
metadata('read more') || Nesta::Config.read_more
end
|
144
145
146
|
# File 'lib/nesta/models/page.rb', line 144
def
! date.nil?
end
|
#summary ⇒ Object
77
78
79
80
81
82
|
# File 'lib/nesta/models/page.rb', line 77
def summary
if summary_text = metadata("summary")
summary_text.gsub!('\n', "\n")
convert_to_html(@format, Object.new, summary_text)
end
end
|
#title ⇒ Object
52
53
54
55
56
57
|
# File 'lib/nesta/models/page.rb', line 52
def title
metadata('title') || link_text
rescue LinkTextNotSet
return Nesta::Config.title if abspath == '/'
raise
end
|