glog 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/glog/server.rb +0 -4
  2. data/lib/glog/template.rb +56 -0
  3. metadata +3 -2
data/lib/glog/server.rb CHANGED
@@ -12,13 +12,9 @@ module Glog
12
12
 
13
13
  def send_path(path)
14
14
  path = path[1..-1] # /my/page => my/page
15
- # First trying to load page
16
15
  if page = Page.get(path)
17
- puts page
18
16
  send_page(page)
19
- # Then trying to load directory index if any
20
17
  elsif page = try_dir_index(path)
21
- puts page
22
18
  send_page(page)
23
19
  else
24
20
  send_not_found
@@ -0,0 +1,56 @@
1
+ require 'haml'
2
+
3
+ module Glog
4
+ class Template
5
+
6
+ include Haml
7
+
8
+ attr_accessor :page, :locals, :template
9
+
10
+ def self.wrap(page, locals = {})
11
+ new(page, locals)
12
+ end
13
+
14
+ def initialize(page, locals = nil)
15
+ @page = page
16
+ @locals = locals
17
+ if page.template
18
+ @template = File.read(build_template_path(page.template))
19
+ else
20
+ @template = find_for(page.path_with_parent)
21
+ end
22
+ end
23
+
24
+ def render(path = nil, locals = {})
25
+ template = path ? File.read("templates" + File::SEPARATOR + path + '.haml') : @template
26
+ render_string(template, locals)
27
+ end
28
+
29
+ private
30
+
31
+ def render_string(string, locals = {})
32
+ Engine.new(string).render(self, @locals.merge(locals))
33
+ end
34
+
35
+ def find_for(page_path)
36
+ template_path = possible_paths_for(page_path).detect { |path| File.exist?(path) }
37
+ File.read template_path || 'templates/default.haml'
38
+ end
39
+
40
+ def possible_paths_for(page_path)
41
+ return [] if page_path.nil?
42
+ segments = page_path.split(File::SEPARATOR)
43
+ paths = [] << build_template_path(segments) # twin template
44
+ segments.size.downto(1) do |ix|
45
+ segments.pop
46
+ paths.push build_template_path(segments[0..ix] + ['default'])
47
+ end
48
+ paths
49
+ end
50
+
51
+ def build_template_path(path)
52
+ ["templates", path].flatten.join(File::SEPARATOR) + '.haml'
53
+ end
54
+
55
+ end
56
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Antono Vasiljev
@@ -113,6 +113,7 @@ files:
113
113
  - lib/glog.rb
114
114
  - lib/glog/page.rb
115
115
  - lib/glog/server.rb
116
+ - lib/glog/template.rb
116
117
  has_rdoc: true
117
118
  homepage: http://github.com/antono/glog
118
119
  licenses: []