brochure 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +52 -0
  3. data/lib/brochure.rb +115 -0
  4. metadata +97 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Sam Stephenson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ Brochure
2
+ ========
3
+
4
+ A Rack application for serving static sites with ERB templates.
5
+
6
+ Sample application structure:
7
+
8
+ app/
9
+ helpers/
10
+ analytics_helper.rb
11
+ formatting_helper.rb
12
+ templates/
13
+ help/
14
+ index.html.erb
15
+ index.html.erb
16
+ shared/
17
+ _header.html.erb
18
+ _footer.html.erb
19
+ signup.html.erb
20
+ config.ru
21
+ public/
22
+ ...
23
+
24
+ Sample `config.ru`:
25
+
26
+ require "brochure"
27
+ run Brochure::Application.new(File.dirname(__FILE__))
28
+
29
+ URLs are automatically mapped to template names. So `/` will render
30
+ `app/templates/index.html.erb`, `/signup` will render
31
+ `app/templates/signup.html.erb`, `/help/` will render
32
+ `app/templates/help/index.html.erb`, and so on.
33
+
34
+ Helpers should define a module that maps to their filename. So
35
+ `analytics_helper.rb` defines `AnalyticsHelper`,
36
+ `html/forms_helper.rb` defines `Html::FormsHelper`, and so on.
37
+
38
+ Templates can render partials. Partials are denoted by a leading
39
+ underscore in their filename. So `<%= render "shared/header" %>` will
40
+ render `app/templates/shared/_header.html.erb` inline.
41
+
42
+ # Installation
43
+
44
+ $ gem install brochure
45
+
46
+ Requires [Tilt](http://github.com/rtomayko/tilt).
47
+
48
+ # License
49
+
50
+ Copyright (c) 2010 Sam Stephenson.
51
+
52
+ Released under the MIT license. See `LICENSE` for details.
@@ -0,0 +1,115 @@
1
+ require "tilt"
2
+
3
+ module Brochure
4
+ class Application
5
+ def initialize(root)
6
+ @app_root = File.expand_path(root)
7
+ @helper_root = File.join(@app_root, "app", "helpers")
8
+ @template_root = File.join(@app_root, "app", "templates")
9
+ @context_class = Context.for(helpers)
10
+ @templates = {}
11
+ end
12
+
13
+ def helpers
14
+ @helpers ||= Dir[File.join(@helper_root, "**", "*.rb")].map do |helper_path|
15
+ base_name = helper_path[(@helper_root.length + 1)..-1][/(.*?)\.rb$/, 1]
16
+ module_names = base_name.split("/").map { |n| Brochure.camelize(n) }
17
+ load helper_path
18
+ module_names.inject(Kernel) { |mod, name| mod.const_get(name) }
19
+ end
20
+ end
21
+
22
+ def call(env)
23
+ logical_path = env["PATH_INFO"][/[^.]+/]
24
+ success render(logical_path)
25
+ rescue TemplateNotFound => e
26
+ not_found
27
+ rescue StandardError => e
28
+ error e
29
+ end
30
+
31
+ def find_template_path(logical_path, options = {})
32
+ if options[:partial]
33
+ path_parts = logical_path.split("/")
34
+ logical_path = (path_parts[0..-2] + ["_" + path_parts[-1]]).join("/")
35
+ else
36
+ return false if File.basename(logical_path)[/^_/]
37
+ end
38
+
39
+ template_path = if File.directory?(File.join(@template_root, logical_path))
40
+ File.join(@template_root, logical_path, "index.html.erb")
41
+ else
42
+ File.join(@template_root, logical_path + ".html.erb")
43
+ end
44
+
45
+ File.exists?(template_path) && template_path
46
+ end
47
+
48
+ def render(logical_path, options = {})
49
+ if template_path = find_template_path(logical_path, options)
50
+ context = @context_class.new(self)
51
+ locals = options[:locals] || {}
52
+ template_for(template_path).render(context, locals)
53
+ else
54
+ raise TemplateNotFound, "no such template '#{logical_path}'"
55
+ end
56
+ end
57
+
58
+ def template_for(template_path)
59
+ @templates[template_path] ||= Tilt.new(template_path)
60
+ end
61
+
62
+ def respond_with(status, body, content_type = "text/html, charset=utf-8")
63
+ headers = {
64
+ "Content-Type" => content_type,
65
+ "Content-Length" => body.length.to_s
66
+ }
67
+ [status, headers, body]
68
+ end
69
+
70
+ def success(body)
71
+ respond_with 200, body
72
+ end
73
+
74
+ def not_found
75
+ respond_with 404, <<-HTML
76
+ <!DOCTYPE html>
77
+ <html><head><title>Not Found</title></head>
78
+ <body><h1>404 Not Found</h1></body></html>
79
+ HTML
80
+ end
81
+
82
+ def error(exception)
83
+ warn ["#{exception.class.name}: #{exception}", *exception.backtrace].join("\n ")
84
+ respond_with 500, <<-HTML
85
+ <!DOCTYPE html>
86
+ <html><head><title>Internal Server Error</title></head>
87
+ <body><h1>500 Internal Server Error</h1></body></html>
88
+ HTML
89
+ end
90
+ end
91
+
92
+ class Context
93
+ include Tilt::CompileSite
94
+
95
+ def self.for(helpers)
96
+ context = Class.new(self)
97
+ context.send(:include, *helpers) if helpers.any?
98
+ context
99
+ end
100
+
101
+ def initialize(application)
102
+ @application = application
103
+ end
104
+
105
+ def render(logical_path, locals = {})
106
+ @application.render(logical_path, :partial => true, :locals => locals)
107
+ end
108
+ end
109
+
110
+ class TemplateNotFound < StandardError; end
111
+
112
+ def self.camelize(string)
113
+ string.gsub(/(^|_)(\w)/) { $2.upcase }
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brochure
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Sam Stephenson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-12 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: tilt
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rack-test
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: A Rack application for serving static sites with ERB templates.
50
+ email:
51
+ - sstephenson@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - lib/brochure.rb
60
+ - README.md
61
+ - LICENSE
62
+ has_rdoc: true
63
+ homepage: http://github.com/sstephenson/brochure
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Rack + ERB static sites
96
+ test_files: []
97
+