Class: Troy::Page

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/troy/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, path, meta = nil) ⇒ Page

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



26
27
28
29
30
# File 'lib/troy/page.rb', line 26

def initialize(site, path, meta = nil)
  @site = site
  @path = path
  @meta = 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
36
# 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

#metaObject (readonly)

Set the meta data for this particular page.



16
17
18
# File 'lib/troy/page.rb', line 16

def meta
  @meta
end

#pathObject (readonly)

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



12
13
14
# File 'lib/troy/page.rb', line 12

def path
  @path
end

#siteObject (readonly)

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



21
22
23
# File 'lib/troy/page.rb', line 21

def site
  @site
end

Instance Method Details

#compress(content) ⇒ Object



61
62
63
64
# File 'lib/troy/page.rb', line 61

def compress(content)
  content = HtmlPress.press(content) if config.assets.compress_html
  content
end

#configObject



128
129
130
# File 'lib/troy/page.rb', line 128

def config
  Troy.configuration
end

#contentObject



42
43
44
45
46
47
48
49
50
51
52
# 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("md.erb") { Markdown.new(EmbeddedRuby.new(meta.content, to_context).render).to_html } # rubocop:disable Layout/LineLength
    .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

#filenameObject



81
82
83
84
85
86
87
88
# File 'lib/troy/page.rb', line 81

def filename
  ExtensionMatcher.new(path)
                  .default { "#{permalink}.html" }
                  .on("builder") { "#{permalink}.xml" }
                  .on("xml") { "#{permalink}.xml" }
                  .on("txt") { "#{permalink}.txt" }
                  .match
end

#layoutObject



90
91
92
# File 'lib/troy/page.rb', line 90

def layout
  site.root.join("layouts/#{meta.fetch('layout', 'default')}.erb")
end

#output_fileObject



115
116
117
118
119
120
121
# File 'lib/troy/page.rb', line 115

def output_file
  base = File.dirname(path)
             .gsub(site.root.join("source").to_s, "")
             .gsub(%r{^/}, "")

  site.root.join("public", base, filename)
end


77
78
79
# File 'lib/troy/page.rb', line 77

def permalink
  meta.fetch("permalink", File.basename(path).gsub(/\..*?$/, ""))
end

#renderObject

Render the current page.



68
69
70
71
72
73
74
75
# File 'lib/troy/page.rb', line 68

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_erbObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/troy/page.rb', line 94

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

Returns:

  • (Boolean)


38
39
40
# File 'lib/troy/page.rb', line 38

def respond_to_missing?(name, _include_private = false)
  meta.key?(name.to_s)
end

#saveObject



123
124
125
126
# File 'lib/troy/page.rb', line 123

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.



107
108
109
110
111
112
113
# File 'lib/troy/page.rb', line 107

def save_to(path)
  File.open(path, "w") do |file|
    I18n.with_locale(meta.locale) do
      file << render
    end
  end
end

#to_contextObject



54
55
56
57
58
59
# File 'lib/troy/page.rb', line 54

def to_context
  {
    page: self,
    site: site
  }
end