tiny_site 0.1.7 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/tiny_site/version.rb +1 -1
  2. data/lib/tiny_site.rb +58 -32
  3. metadata +2 -2
@@ -1,3 +1,3 @@
1
1
  class TinySite
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/tiny_site.rb CHANGED
@@ -1,22 +1,28 @@
1
1
  require 'open-uri'
2
+ require 'forwardable'
2
3
  require 'RedCloth'
3
4
  require 'yaml'
4
5
  require 'haml'
5
6
 
6
7
  class TextileParts
7
- def self.parse(tp, image_path='', file_path_postfix='')
8
+ def self.parse(tp, app)
9
+ @app = app
10
+
8
11
  return {:title => '404 not found'} unless tp
9
12
 
10
13
  vars, *parts = tp.split(/^\+{4}([\w\d\-_]+)\+{4}$/)
11
14
  vars = YAML.load(vars) || {}
12
15
 
13
16
  parts = Hash[*parts]
14
- parts.each{|k,v| parts[k] = textilize(v,image_path,file_path_postfix)}
17
+ parts.each{|k,v| parts[k] = textilize(v)}
15
18
 
16
19
  vars.update(parts)
17
20
  end
18
- def self.textilize(string, image_path, file_path_postfix='')
19
- string.gsub!(%r{!([\w\d\-\._]+)!}){ |a| "!#{image_path}/#{$1}#{file_path_postfix}!" }
21
+ def self.image_url_for(img_name)
22
+ @app.image_url_for img_name
23
+ end
24
+ def self.textilize(string)
25
+ string.gsub!(%r{!([\w\d\-\._]+)!}){ |a| "!#{image_url_for $1}!" }
20
26
 
21
27
  RedCloth.new(string).to_html
22
28
  end
@@ -68,37 +74,56 @@ class CachedHttpFile
68
74
  end
69
75
 
70
76
  class TinySite
77
+ class View
78
+ extend Forwardable
79
+ def_delegators :@app, :request_path, :query_string, :global, :page, :file_url_for, :image_url_for
80
+
81
+ def initialize(app) ; @app = app ; end
82
+ end
83
+ end
84
+
85
+ class TinySite
86
+ attr_reader :file_path, :file_path_postfix, :file_extension, :image_path, :request_path, :query_string,
87
+ :request_path, :query_string
88
+
71
89
  def initialize(opts)
72
90
  @file_path = opts[:file_path]
73
91
  @file_path_postfix = opts[:file_path_postfix] || ''
74
- @file_extension = opts[:file_extension] || 'textile'
75
- @image_path = opts[:image_path] || File.join(@file_path, 'images')
76
- @cache_buster = opts[:cache_buster] || 'bust'
92
+ @file_extension = opts[:file_extension] || 'textile'
93
+ @image_path = opts[:image_path] || File.join(@file_path, 'images')
94
+ @cache_buster = opts[:cache_buster] || 'bust'
77
95
  end
78
96
 
79
- def remote_file_url_for(filename)
80
- File.join @file_path, "#{filename}.#{@file_extension}#{@file_path_postfix}"
97
+ def global
98
+ _, global_file = CachedHttpFile.get page_content_url_for('__global')
99
+ TextileParts.parse global_file, self
81
100
  end
82
101
 
83
- def render
84
- global_file_fetch_tread = Thread.new{
85
- _, @global_file = CachedHttpFile.get remote_file_url_for('__global') } # get __global in background
86
- @status, page_file = CachedHttpFile.get remote_file_url_for(@path)
87
- _, page_file = CachedHttpFile.get remote_file_url_for(@status) if @status != 200
88
- global_file_fetch_tread.join
89
-
90
- global = TextileParts.parse @global_file, @image_path, @file_path_postfix
91
- page = TextileParts.parse page_file, @image_path, @file_path_postfix
92
-
93
- render_layout :global => global, :page => page, :env => {:path => @path, :query_string => @query_string}
102
+ def page
103
+ @status, page_file = CachedHttpFile.get page_content_url_for(@request_path)
104
+ _, page_file = CachedHttpFile.get page_content_url_for(@status.to_s) if @status != 200
105
+ TextileParts.parse page_file, self
94
106
  end
95
107
 
96
- def render_layout(params)
97
- layout = params[:page][:layout] || params[:global][:layout] || 'layout'
98
-
99
- puts "rendering layout '#{layout}'"
100
- haml = Haml::Engine.new File.open("#{layout}.haml").read, :format => :html5
101
- haml.render Object.new, params
108
+ def status
109
+ @status or page && @status
110
+ end
111
+
112
+ def image_url_for(img_name)
113
+ file_url_for img_name, @image_path
114
+ end
115
+ def file_url_for(filename, path=file_path)
116
+ File.join path, "#{filename}#{file_path_postfix}"
117
+ end
118
+ def page_content_url_for(filename)
119
+ file_url_for "#{filename.gsub(%r{^/$},'/index')}.#{file_extension}"
120
+ end
121
+
122
+ def layout
123
+ page[:layout] || global[:layout] || 'layout'
124
+ end
125
+ def body
126
+ Haml::Engine.new(File.open("#{layout}.haml").read, :format => :html5).render view
102
127
  end
103
128
 
104
129
  def caching_header
@@ -107,14 +132,15 @@ class TinySite
107
132
  CachedHttpFile.bust and return { 'Cache-Control' => 'no-cache' }
108
133
  end
109
134
 
135
+ def view
136
+ @view ||= View.new self
137
+ end
138
+
110
139
  def call(env)
111
- @path, @query_string = env['PATH_INFO'], env['QUERY_STRING']
112
- @path = '/index' if @path == '/'
113
-
114
- return [301, {'Location' => @path}, ''] if @path.gsub!(/\/$/,'')
140
+ @request_path, @query_string = env['PATH_INFO'], env['QUERY_STRING']
115
141
 
116
- body = render # render the body first to set @status
117
- [@status, caching_header, body]
142
+ return [301, {'Location' => @request_path}, ''] if @request_path.gsub!(/(.)\/$/,'\\1')
143
+ [status, caching_header, body]
118
144
  rescue => e
119
145
  puts "#{e.class}: #{e.message} #{e.backtrace}"
120
146
  [500, {}, 'Sorry, but something went wrong']
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: tiny_site
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.7
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Niko Dittmann
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-03 00:00:00 +01:00
13
+ date: 2011-03-08 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency