Class: Troy::Page
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/troy/page.rb
Instance Attribute Summary collapse
-
#meta ⇒ Object
readonly
Set the meta data for this particular page.
-
#path ⇒ Object
readonly
Set the page path, which must contain a valid meta section and page content.
-
#site ⇒ Object
readonly
Set the current site object, which contains reference to all existing pages.
Instance Method Summary
collapse
Constructor Details
#initialize(site, path) ⇒ Page
Initialize a new page, which can be simply rendered or persisted to the filesystem.
24
25
26
27
28
|
# File 'lib/troy/page.rb', line 24
def initialize(site, path)
@site = site
@path = path
@meta = Meta.new(path)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
32
33
34
35
|
# File 'lib/troy/page.rb', line 32
def method_missing(name, *args, &block)
return meta[name.to_s] if meta.key?(name.to_s)
super
end
|
Instance Attribute Details
Set the meta data for this particular page.
14
15
16
|
# File 'lib/troy/page.rb', line 14
def meta
@meta
end
|
#path ⇒ Object
Set the page path, which must contain a valid meta section and page content.
10
11
12
|
# File 'lib/troy/page.rb', line 10
def path
@path
end
|
#site ⇒ Object
Set the current site object, which contains reference to all existing pages.
19
20
21
|
# File 'lib/troy/page.rb', line 19
def site
@site
end
|
Instance Method Details
#compress(content) ⇒ Object
66
67
68
69
|
# File 'lib/troy/page.rb', line 66
def compress(content)
content = HtmlPress.press(content) if config.assets.compress_html
content
end
|
#filename ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/troy/page.rb', line 90
def filename
ExtensionMatcher.new(path)
.default { "#{permalink}.html" }
.on("builder") { "#{permalink}.xml" }
.on("xml") { "#{permalink}.xml" }
.on("txt") { "#{permalink}.txt" }
.match
end
|
#layout ⇒ Object
101
102
103
|
# File 'lib/troy/page.rb', line 101
def layout
site.root.join("layouts/#{meta.fetch("layout", "default")}.erb")
end
|
#output_file ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/troy/page.rb', line 130
def output_file
base = File.dirname(path)
.gsub(site.root.join("source").to_s, "")
.gsub(%r[^/], "")
site.root.join("public", base, filename)
end
|
#permalink ⇒ Object
84
85
86
|
# File 'lib/troy/page.rb', line 84
def permalink
meta.fetch("permalink", File.basename(path).gsub(/\..*?$/, ""))
end
|
#render ⇒ Object
73
74
75
76
77
78
79
80
|
# File 'lib/troy/page.rb', line 73
def render
ExtensionMatcher.new(path)
.default { content }
.on("html") { compress render_erb }
.on("md") { compress render_erb }
.on("erb") { compress render_erb }
.match
end
|
#render_erb ⇒ Object
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/troy/page.rb', line 107
def render_erb
if layout.exist?
EmbeddedRuby.new(
layout.read,
to_context.merge(:content => content)
).render
else
content
end
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
39
40
41
|
# File 'lib/troy/page.rb', line 39
def respond_to_missing?(name, include_private = false)
meta.key?(name.to_s)
end
|
#save ⇒ Object
140
141
142
143
|
# File 'lib/troy/page.rb', line 140
def save
FileUtils.mkdir_p(File.dirname(output_file))
save_to(output_file)
end
|
#save_to(path) ⇒ Object
Save current page to the specified path.
120
121
122
123
124
125
126
|
# File 'lib/troy/page.rb', line 120
def save_to(path)
File.open(path, "w") do |file|
I18n.with_locale(meta.locale) do
file << render
end
end
end
|
#to_context ⇒ Object
57
58
59
60
61
62
|
# File 'lib/troy/page.rb', line 57
def to_context
{
:page => self,
:site => site
}
end
|