Class: Plato::Site
- Inherits:
-
Object
- Object
- Plato::Site
- Defined in:
- lib/plato/site.rb
Defined Under Namespace
Classes: Template
Constant Summary collapse
- DETECT_EXT =
/(?:(.*)\/)?([^\/]+)\.([^.]+)\Z/
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#cache_path ⇒ Object
readonly
Returns the value of attribute cache_path.
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#content_path ⇒ Object
readonly
Returns the value of attribute content_path.
-
#resources_path ⇒ Object
readonly
Returns the value of attribute resources_path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#template_path ⇒ Object
readonly
Returns the value of attribute template_path.
Instance Method Summary collapse
- #config ⇒ Object
- #content ⇒ Object
- #detect_zip_path(path) ⇒ Object
- #generate! ⇒ Object
-
#initialize(base_url, template = 'template', cache = 'cache', root = '.') ⇒ Site
constructor
A new instance of Site.
- #initialize_templates! ⇒ Object
- #render(template, format, document) ⇒ Object
- #rendered_content ⇒ Object
- #rendered_templates ⇒ Object
- #resources ⇒ Object
- #template_resources ⇒ Object
- #templates ⇒ Object
Constructor Details
#initialize(base_url, template = 'template', cache = 'cache', root = '.') ⇒ Site
Returns a new instance of Site.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/plato/site.rb', line 7 def initialize(base_url, template = 'template', cache = 'cache', root = '.') @base_url = base_url @root = File.(root) @cache_path = File.(cache, @root) @template_path = detect_zip_path File.(template, @root) @config_path = File.join(@template_path, 'config.rb') @content_path = File.join(@root) @resources_path = File.join(@root, "resources") RenderContext.load_view_helpers(File.join(@template_path, 'view_helpers.rb')) end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
3 4 5 |
# File 'lib/plato/site.rb', line 3 def base_url @base_url end |
#cache_path ⇒ Object (readonly)
Returns the value of attribute cache_path.
5 6 7 |
# File 'lib/plato/site.rb', line 5 def cache_path @cache_path end |
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
5 6 7 |
# File 'lib/plato/site.rb', line 5 def config_path @config_path end |
#content_path ⇒ Object (readonly)
Returns the value of attribute content_path.
5 6 7 |
# File 'lib/plato/site.rb', line 5 def content_path @content_path end |
#resources_path ⇒ Object (readonly)
Returns the value of attribute resources_path.
5 6 7 |
# File 'lib/plato/site.rb', line 5 def resources_path @resources_path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
4 5 6 |
# File 'lib/plato/site.rb', line 4 def root @root end |
#template_path ⇒ Object (readonly)
Returns the value of attribute template_path.
5 6 7 |
# File 'lib/plato/site.rb', line 5 def template_path @template_path end |
Instance Method Details
#config ⇒ Object
35 36 37 |
# File 'lib/plato/site.rb', line 35 def config @config ||= Config.read(ConfigDSL, File.read(config_path)) end |
#content ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/plato/site.rb', line 49 def content return @content if @content @content = config["content_categories"] categories = @content.values content_repos = @content.keys.map do |c| Repo.new(File.join(content_path, c)) end content_repos.each do |repo| repo.each do |path, file| if category = categories.find {|c| c.match path } data = category.match(path).merge( HeadersCodec.inflate(file.read) ) category.documents << Document.new(category, data) end end end @content end |
#detect_zip_path(path) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/plato/site.rb', line 78 def detect_zip_path(path) path = "#{path}.zip" if !File.exist? path and File.exist? "#{path}.zip" if File.exist? path and !File.directory? path and path.match(DETECT_EXT) dir, base, ext = path.match(DETECT_EXT).values_at(1,2,3) [dir, "#{base}.#{ext}!", base].compact.join("/") else path end end |
#generate! ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/plato/site.rb', line 21 def generate! [ ["resources", resources], ["template resources", template_resources], ["rendered templates", rendered_templates], ["rendered content", rendered_content] ].each do |(name, manifest)| puts "## #{name}:" manifest.save_to(cache_path) manifest.to_h.keys.map{|p| File.join cache_path, p }.each do |path| puts " #{path}" end end end |
#initialize_templates! ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/plato/site.rb', line 90 def initialize_templates! path_parser = PathTemplate.new(":name*.:format.:engine") sass_parser = PathTemplate.new(":name*.sass") @template_resources = Manifest.new @templates = {} Repo.new(template_path).each do |path, file| next if path =~ /\A(config\.rb|view_helpers\.rb)/ if tilt_class = Tilt[path] tilt_opts = config["options"][Tilt.mappings.index(tilt_class).to_sym] tilt = tilt_class.new(File.join(template_path, path), tilt_opts || {}) if match = path_parser.parse(path) name, format = match.values_at("name", "format") @templates["#{name}.#{format}"] = Template.new(tilt, format) else name = sass_parser.parse(path).values_at("name") @templates["#{name}.css"] = Template.new(tilt, 'css') end else # could not find a template engine, assume we're a raw resource @template_resources[path] = file nil end end end |
#render(template, format, document) ⇒ Object
127 128 129 |
# File 'lib/plato/site.rb', line 127 def render(template, format, document) RenderContext.new(self, document).render_with_layout(template, format) end |
#rendered_content ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/plato/site.rb', line 139 def rendered_content rendered = content.values.inject({}) do |hash, category| if template = templates["_#{category.name}.html"] category.documents.each do |document| hash[document.path] = render(template, "html", document) end end hash end Manifest.new(rendered) end |
#rendered_templates ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/plato/site.rb', line 131 def rendered_templates Manifest.new(templates).map do |path, template| if path !~ /\A_/ { path => render(template, template.format, nil) } end end end |
#resources ⇒ Object
73 74 75 |
# File 'lib/plato/site.rb', line 73 def resources @resources ||= Manifest.new(Repo.new(resources_path)) end |
#template_resources ⇒ Object
44 45 46 47 |
# File 'lib/plato/site.rb', line 44 def template_resources initialize_templates! unless @templates @template_resources end |
#templates ⇒ Object
39 40 41 42 |
# File 'lib/plato/site.rb', line 39 def templates initialize_templates! unless @templates @templates end |