Class: Staticpress::Content::Base
- Inherits:
-
Object
- Object
- Staticpress::Content::Base
show all
- Extended by:
- Helpers
- Includes:
- Helpers
- Defined in:
- lib/staticpress/content/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helpers
config, extensionless_basename, extensionless_path, hash_from_array, hash_from_match_data, paginate, settings, spider_map, titleize
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/staticpress/content/base.rb', line 13
def initialize(params = {})
clean_params = params.select { |key, value| value }.map do |key, value|
cast_value = case Staticpress::Route::REGEX_STUBS[key].klass
when :integer then Integer value rescue value
when :symbol then value.to_sym
else value
end
[ key, cast_value ]
end
@params = optional_param_defaults.merge Hash[clean_params]
@template_types = []
end
|
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
11
12
13
|
# File 'lib/staticpress/content/base.rb', line 11
def params
@params
end
|
#template_types ⇒ Object
Returns the value of attribute template_types.
11
12
13
|
# File 'lib/staticpress/content/base.rb', line 11
def template_types
@template_types
end
|
Class Method Details
.find_by_url_path(url_path) ⇒ Object
202
203
204
205
|
# File 'lib/staticpress/content/base.rb', line 202
def self.find_by_url_path(url_path)
params = Staticpress::Route. config.routes[type], url_path
new params if params
end
|
.theme ⇒ Object
207
208
209
|
# File 'lib/staticpress/content/base.rb', line 207
def self.theme
Staticpress::Theme.theme
end
|
.type ⇒ Object
211
212
213
|
# File 'lib/staticpress/content/base.rb', line 211
def self.type
name.split('::').last.downcase
end
|
Instance Method Details
#==(other) ⇒ Object
28
29
30
31
|
# File 'lib/staticpress/content/base.rb', line 28
def ==(other)
other.respond_to?(:params) && (params == other.params) &&
other.respond_to?(:url_path) && (url_path == other.url_path)
end
|
#content ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/staticpress/content/base.rb', line 33
def content
return @content if @content
regex_frontmatter = /^-{3}${1}(?<frontmatter>.*)^-{3}${1}/m
regex_text = /(?<text>.*)/m
regex = /(#{regex_frontmatter})?#{regex_text}/
c = template_path_content
@content = if Tilt.mappings.include?(template_path.extname[1..-1])
c.match regex
else
{ :text => c }
end
end
|
#content_type ⇒ Object
49
50
51
|
# File 'lib/staticpress/content/base.rb', line 49
def content_type
Rack::Mime.mime_type output_path.extname
end
|
#exist? ⇒ Boolean
53
54
55
|
# File 'lib/staticpress/content/base.rb', line 53
def exist?
template_path.file?
end
|
#full_title ⇒ Object
57
58
59
60
61
62
|
# File 'lib/staticpress/content/base.rb', line 57
def full_title
[
title,
config.title
].join config.title_separators.site
end
|
#layout ⇒ Object
64
65
66
67
68
|
# File 'lib/staticpress/content/base.rb', line 64
def layout
if meta.layout || config.markup_templates.include?(template_path.extname[1..-1])
Tilt.new theme.layout_for(*preferred_layout_names).to_s
end
end
|
#markup_template? ⇒ Boolean
70
71
72
|
# File 'lib/staticpress/content/base.rb', line 70
def markup_template?
config.markup_templates.include?(template_path.extname[1..-1])
end
|
74
75
76
77
78
79
|
# File 'lib/staticpress/content/base.rb', line 74
def meta
Staticpress::Metadata.new(content[:frontmatter] ? YAML.load(content[:frontmatter]) : {})
rescue Psych::SyntaxError => e
warn "Could not parse frontmatter for #{template_path}", content[:frontmatter]
raise e
end
|
#optional_param_defaults ⇒ Object
81
82
83
|
# File 'lib/staticpress/content/base.rb', line 81
def optional_param_defaults
{}
end
|
#output_path ⇒ Object
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/staticpress/content/base.rb', line 85
def output_path
base = Staticpress.blog_path + config.destination_path + url_path.sub(/^\//, '')
if (markup_template? && config.index_file && (Pathname(config.index_file).extname != base.extname))
base + config.index_file
else
base
end
end
|
#published? ⇒ Boolean
96
97
98
99
100
101
102
103
|
# File 'lib/staticpress/content/base.rb', line 96
def published?
published = meta.published
if published.is_a? Time
published <= Time.utc
else
published.nil? ? true : published
end
end
|
#raw ⇒ Object
105
106
107
|
# File 'lib/staticpress/content/base.rb', line 105
def raw
content[:text].strip
end
|
#render(locals = {}) ⇒ Object
109
110
111
112
113
114
115
116
117
|
# File 'lib/staticpress/content/base.rb', line 109
def render(locals = {})
if l = layout
l.render template_context, locals do
render_partial locals
end
else
render_partial locals
end
end
|
#render_partial(locals = {}) ⇒ Object
119
120
121
122
123
124
|
# File 'lib/staticpress/content/base.rb', line 119
def render_partial(locals = {})
template_types.inject(raw) do |result, template_type|
template = Tilt.new(template_type.to_s, template_engine_options(template_type)) { result }
template.render template_context, locals
end
end
|
#save ⇒ Object
126
127
128
|
# File 'lib/staticpress/content/base.rb', line 126
def save
save! unless output_path.file?
end
|
#save! ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/staticpress/content/base.rb', line 130
def save!
if settings.verbose
width = config.routes.to_hash.keys.max_by { |sym| sym.to_s.length }.to_s.length
type = self.class.type.rjust width
path = output_path.to_s.sub Staticpress.blog_path.to_s + '/', ''
puts "#{type} #{path}"
end
FileUtils.mkdir_p output_path.dirname
output_path.open('w') { |f| f.write render }
end
|
#template_context ⇒ Object
#template_engine_options(template_type) ⇒ Object
146
147
148
|
# File 'lib/staticpress/content/base.rb', line 146
def template_engine_options(template_type)
(config.template_engine_options[template_type] || {}).to_hash
end
|
#template_extension ⇒ Object
150
151
152
|
# File 'lib/staticpress/content/base.rb', line 150
def template_extension
template_types.empty? ? '' : ".#{template_types.reverse.join('.')}"
end
|
#template_path_content ⇒ Object
154
155
156
|
# File 'lib/staticpress/content/base.rb', line 154
def template_path_content
exist? ? template_path.read : ''
end
|
#theme ⇒ Object
158
159
160
|
# File 'lib/staticpress/content/base.rb', line 158
def theme
self.class.theme
end
|
#title ⇒ Object
162
163
164
165
166
167
168
|
# File 'lib/staticpress/content/base.rb', line 162
def title
if meta.title
meta.title
else
titleize(url_path)
end
end
|
#to_s ⇒ Object
170
171
172
|
# File 'lib/staticpress/content/base.rb', line 170
def to_s
"#<#{self.class} url_path=#{url_path}, params=#{Hash[params.sort]}>"
end
|
#url_path ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/staticpress/content/base.rb', line 174
def url_path
pattern = config.routes[self.class.type].clone
optional_segment_finder = /\((?<optional_segment>.+)\)\?/
pattern.gsub! optional_segment_finder do |optional_segment|
optional_key = /:(?<optional_key>[0-9a-z_]+)/.match(optional_segment)[:optional_key].to_sym
optional_segment_finder.match(optional_segment)[:optional_segment] if params[optional_key] && (params[optional_key] != optional_param_defaults[optional_key])
end
Staticpress::Route::REGEX_STUBS.keys.inject(pattern) do |p, key|
p.gsub /:#{key}/, params[key].to_s
end
end
|