class Troy::Page

Attributes

meta[R]

Set the meta data for this particular page.

path[R]

Set the page path, which must contain a valid meta section and page content.

site[R]

Set the current site object, which contains reference to all existing pages.

Public Class Methods

new(site, path, meta = nil) click to toggle source

Initialize a new page, which can be simply rendered or persisted to the filesystem.

# File lib/troy/page.rb, line 26
def initialize(site, path, meta = nil)
  @site = site
  @path = path
  @meta = meta || Meta.new(path)
end

Public Instance Methods

compress(content) click to toggle source
# File lib/troy/page.rb, line 59
def compress(content)
  content = HtmlPress.press(content) if config.assets.compress_html
  content
end
config() click to toggle source
# File lib/troy/page.rb, line 126
def config
  Troy.configuration
end
content() click to toggle source
# File lib/troy/page.rb, line 42
def content
  ExtensionMatcher.new(path)
                  .default { meta.content }
                  .on("builder") { XML.new(meta.content, to_context).to_xml }
                  .on("erb") { EmbeddedRuby.new(meta.content, to_context).render }
                  .on("md") { Markdown.new(meta.content).to_html }
                  .on("txt") { EmbeddedRuby.new(meta.content, to_context).render }
                  .match
end
filename() click to toggle source
# File lib/troy/page.rb, line 79
def filename
  ExtensionMatcher.new(path)
                  .default { "#{permalink}.html" }
                  .on("builder") { "#{permalink}.xml" }
                  .on("xml") { "#{permalink}.xml" }
                  .on("txt") { "#{permalink}.txt" }
                  .match
end
layout() click to toggle source
# File lib/troy/page.rb, line 88
def layout
  site.root.join("layouts/#{meta.fetch('layout', 'default')}.erb")
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# 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
output_file() click to toggle source
# File lib/troy/page.rb, line 113
def output_file
  base = File.dirname(path)
             .gsub(site.root.join("source").to_s, "")
             .gsub(%r{^/}, "")

  site.root.join("public", base, filename)
end
render() click to toggle source

Render the current page.

# File lib/troy/page.rb, line 66
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() click to toggle source
# File lib/troy/page.rb, line 92
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) click to toggle source
# File lib/troy/page.rb, line 38
def respond_to_missing?(name, _include_private = false)
  meta.key?(name.to_s)
end
save() click to toggle source
# File lib/troy/page.rb, line 121
def save
  FileUtils.mkdir_p(File.dirname(output_file))
  save_to(output_file)
end
save_to(path) click to toggle source

Save current page to the specified path.

# File lib/troy/page.rb, line 105
def save_to(path)
  File.open(path, "w") do |file|
    I18n.with_locale(meta.locale) do
      file << render
    end
  end
end
to_context() click to toggle source
# File lib/troy/page.rb, line 52
def to_context
  {
    page: self,
    site: site
  }
end