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
18
19
# 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")

  RenderContext.load_view_helpers(File.join(@template_path, 'view_helpers.rb'))
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



35
36
37
# File 'lib/plato/site.rb', line 35

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

#contentObject



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_contentObject



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_templatesObject



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

#resourcesObject



73
74
75
# File 'lib/plato/site.rb', line 73

def resources
  @resources ||= Manifest.new(Repo.new(resources_path))
end

#template_resourcesObject



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

def template_resources
  initialize_templates! unless @templates
  @template_resources
end

#templatesObject



39
40
41
42
# File 'lib/plato/site.rb', line 39

def templates
  initialize_templates! unless @templates
  @templates
end