Class: Plato::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/plato/site.rb

Defined Under Namespace

Classes: Template

Constant Summary collapse

DETECT_EXT =
/(?:(.*)\/)?([^\/]+)\.([^.]+)\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path(root)

  @cache_path     = File.expand_path(cache, @root)
  @template_path  = detect_zip_path File.expand_path(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_urlObject (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_pathObject (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_pathObject (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_pathObject (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_pathObject (readonly)

Returns the value of attribute resources_path.



5
6
7
# File 'lib/plato/site.rb', line 5

def resources_path
  @resources_path
end

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/plato/site.rb', line 4

def root
  @root
end

#template_pathObject (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

#configObject



43
44
45
# File 'lib/plato/site.rb', line 43

def config
  @config ||= Config.read(ConfigDSL, File.read(config_path))
end

#contentObject



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_contentObject



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_templatesObject



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

#resourcesObject



102
103
104
# File 'lib/plato/site.rb', line 102

def resources
  @resources ||= Manifest.new(resources_path, :refs)
end

#template_resourcesObject



97
98
99
100
# File 'lib/plato/site.rb', line 97

def template_resources
  templates unless @templates
  @template_resources
end

#templatesObject



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