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.
- #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 |
# 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") 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
43 44 45 |
# File 'lib/plato/site.rb', line 43 def config @config ||= Config.read(ConfigDSL, File.read(config_path)) end |
#content ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/plato/site.rb', line 75 def content return @content if @content @content = config["content_categories"] categories = @content.values content_manifests = @content.keys.map do |c| Manifest.new(File.join(content_path, c), :hash) end content_manifests.each do |manifest| manifest.each do |path, content_data| if category = categories.find {|c| c.match path } data = category.match(path).merge(content_data) category.documents << Document.new(category, data) end end end @content end |
#detect_zip_path(path) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/plato/site.rb', line 31 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
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/plato/site.rb', line 19 def generate! RenderContext.load_view_helpers(File.join(template_path, 'view_helpers.rb')) [ resources.save_to(cache_path), template_resources.save_to(cache_path), rendered_templates.save_to(cache_path), rendered_content.save_to(cache_path) ].each do |ps| puts ps.map{|s| " ยป #{File.join(cache_path,s)}" } end end |
#render(template, format, document) ⇒ Object
115 116 117 |
# File 'lib/plato/site.rb', line 115 def render(template, format, document) RenderContext.new(self, document).render_with_layout(template, format) end |
#rendered_content ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/plato/site.rb', line 127 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
119 120 121 122 123 124 125 |
# File 'lib/plato/site.rb', line 119 def rendered_templates templates.map(:string) do |path, template| if path !~ /\A_/ { path => render(template, template.format, nil) } end end end |
#resources ⇒ Object
102 103 104 |
# File 'lib/plato/site.rb', line 102 def resources @resources ||= Manifest.new(resources_path, :refs) end |
#template_resources ⇒ Object
97 98 99 100 |
# File 'lib/plato/site.rb', line 97 def template_resources templates unless @templates @template_resources end |
#templates ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/plato/site.rb', line 47 def templates return @templates if @templates manifest = Manifest.new template_path, { :codec => :template, :filter => lambda {|p| p !~ /\A(config\.rb|view_helpers\.rb)/ } } path_parser = PathTemplate.new(":name*.:format.:engine") sass_parser = PathTemplate.new(":name*.sass") @template_resources = Manifest.new({}, :refs) @templates = manifest.map do |path, template| if template.is_a? String # could not find a template engine, assume we're a raw resource @template_resources[path] = template nil else if match = path_parser.parse(path) name, format = match.values_at("name", "format") { "#{name}.#{format}" => Template.new(template, format) } else name = sass_parser.parse(path).values_at("name") { "#{name}.css" => Template.new(template, 'css') } end end end end |