hyper 0.3.0beta1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Hyper
2
+
3
+ Hyper is a dead simple, zero-config framework for static HTML websites. Use it to create small HTML sites with static content; host your site anywhere that supports Rack.
4
+
5
+ ## Quick Start
6
+
7
+ Five steps from install to a functioning website:
8
+
9
+ $ gem install hyper
10
+ $ hyper new my_website
11
+ $ cd my_website
12
+ $ bundle install
13
+ $ bundle exec rackup
14
+
15
+ Point your browser at <a href="http://0.0.0.0:9292/">0.0.0.0:9292</a> and you'll see your website's home page.
16
+
17
+ ## Adding Content
18
+
19
+ To add content to your site, you just need to know two things:
20
+
21
+ 1. Shared content (stuff that goes on every page of your website) goes in <code>templates/layout.html.erb</code>
22
+ 2. Page-specific content goes in <code>templates/views/{page}.html.erb</code>
23
+
24
+ Put your navigation and header in <code>layout.html.erb</code>. Put the content for your about page in <code>views/about.html.erb</code> (link to the page using <code>/about</code>). It's that simple.
25
+
26
+ Also note: your home page content goes in <code>views/index.html.erb</code>; Hyper knows to map the root URL to that file.
27
+
28
+ ## Stylesheets, Images, Javascripts, Etc
29
+
30
+ Put your stylesheets, images, javascripts, etc in <code>public</code>: Hyper will handle the rest. When referencing these files in your HTML, drop the "public" from the path &mdash; <code>public/images/logo.png</code> should become <code>&lt;img src="/images/logo.png" /&gt;</code>.
31
+
32
+ ## Deployment
33
+
34
+ If you love Heroku, deployment is easy:
35
+
36
+ $ cd my_website
37
+ $ heroku create
38
+ $ git push heroku master
39
+
40
+ Any other rack-compatible host should work too.
41
+
42
+ ## In-Depth
43
+
44
+ There's not a whole lot of depth to go into: hyper is intentionally simple. Once you've generated a new site, you'll get the following files and folders:
45
+
46
+ <table>
47
+ <tr>
48
+ <th>File/Folder</th>
49
+ <th>Info</th>
50
+ </tr>
51
+ <tr>
52
+ <td><code>templates/layout.html.erb</code></td>
53
+ <td>The layout for your website. Stuff that's the same on every page (header, navigation, etc) should go here</td>
54
+ </tr>
55
+ <tr>
56
+ <td><code>templates/views</code></td>
57
+ <td>This is where your page-specific content lives. See "Adding Content for more details"</td>
58
+ </tr>
59
+ <tr>
60
+ <td><code>public</code></td>
61
+ <td>Put your static assets here</td>
62
+ </tr>
63
+ <tr>
64
+ <td><code>config.ru</code></td>
65
+ <td>Your site's rack config file. Customize your rack app here (if you don't know rack, just ignore this file)</td>
66
+ </tr>
67
+ <tr>
68
+ <td><code>Gemfile</code></td>
69
+ <td>For use with Bundler</td>
70
+ </tr>
71
+ </table>
72
+
73
+ Defaults are set so you don't have to worry about anything outside <code>templates</code> if you don't want to. If you understand rack and want to add middleware, do that in <code>config.ru</code> (but note that Hyper already knows to serve static content from <code>public</code>, so you don't need to use <code>Rack::Static</code>).
74
+
75
+ ## Author
76
+
77
+ I'm <a href="http://www.jameswilding.net">James Wilding</a> and I make Ruby and Rails software.
78
+
79
+ ## License
80
+
81
+ Hyper is released under <a href="http://www.opensource.org/licenses/mit-license.php">the MIT license</a>.
data/bin/hyper ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ require 'hyper'
3
+ require 'optparse'
4
+
5
+ options = {}
6
+ parser = OptionParser.new do |opts|
7
+ opts.banner = "Usage: hyper new NAME [options]"
8
+
9
+ opts.on('-f', '--force', 'Overwrite existing files') do
10
+ options[:force] = true
11
+ end
12
+ end
13
+
14
+ case ARGV.first
15
+ when 'new'
16
+ if name = ARGV[1]
17
+ parser.parse!
18
+ Hyper::Generator.new(name, options).generate
19
+ else
20
+ puts "Please provide a name"
21
+ end
22
+ else
23
+ puts parser.help
24
+ end
data/lib/hyper.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'rack/builder'
2
+
3
+ module Hyper
4
+ autoload :Generator, 'hyper/generator'
5
+ autoload :Response, 'hyper/response'
6
+ autoload :Static, 'hyper/static'
7
+ autoload :Utils, 'hyper/utils'
8
+ autoload :Version, 'hyper/version'
9
+
10
+ extend self
11
+
12
+ # @api public
13
+ def call(env)
14
+ builder = Rack::Builder.new do
15
+ use Hyper::Static
16
+ run Hyper::Response.new
17
+ end
18
+
19
+ builder.to_app.call(env)
20
+ end
21
+
22
+ # @api public
23
+ def root
24
+ Pathname.new(Dir.pwd)
25
+ end
26
+
27
+ # @api public
28
+ def template_root=(path)
29
+ @template_root = Pathname.new(path)
30
+ end
31
+
32
+ def view_root
33
+ @view_root ||= template_root.join('views')
34
+ end
35
+
36
+ # @api private
37
+ def template_root
38
+ @template_root ||= root.join('templates')
39
+ end
40
+ end
@@ -0,0 +1,109 @@
1
+ require 'term/ansicolor'
2
+
3
+ module Hyper
4
+ # @api private
5
+ class Generator
6
+ include Term::ANSIColor
7
+
8
+ def initialize(name, options = {})
9
+ @name = name
10
+ @options = options
11
+ end
12
+
13
+ def generate
14
+ if cannot_generate?
15
+ error "#{@name} exists (use --force to overwrite)"
16
+ else
17
+ create_destination_folder
18
+
19
+ template_contents.each do |path|
20
+ generate_component(path)
21
+ end
22
+ end
23
+ end
24
+
25
+ def cannot_generate?
26
+ File.directory?(destination_folder) && !@options[:force]
27
+ end
28
+
29
+ private
30
+ # Paths
31
+
32
+ # Returns a glob of paths for files and folders in Hyper's template directory.
33
+ def template_contents
34
+ Dir[File.join(template_path, '**/*')]
35
+ end
36
+
37
+ # Returns the path to Hyper's template directory.
38
+ def template_path
39
+ File.expand_path(File.dirname(__FILE__) + '/template')
40
+ end
41
+
42
+ # Returns a path to use for creating a new file inside the destination directory.
43
+ def destination_path(path)
44
+ File.join(destination_folder, common_component_path(path))
45
+ end
46
+
47
+ # Returns the path for the new site directory the user wants to create.
48
+ def destination_folder
49
+ File.join(Dir.pwd, @name)
50
+ end
51
+
52
+ # Returns the path component which is shared between template and destination.
53
+ #
54
+ # Example:
55
+ #
56
+ # path = /users/bob/code/hyper/lib/hyper/template/config.ru
57
+ # common_component_path(path) # => '/config.ru'
58
+ #
59
+ # Used to build destination paths.
60
+ def common_component_path(path)
61
+ path.sub(template_path, '')
62
+ end
63
+
64
+ # Returns an easy to understand string representing and installation path.
65
+ #
66
+ # Example:
67
+ #
68
+ # generator = Hyper::Generator.new('my_site')
69
+ # path = /users/bob/code/hyper/lib/hyper/template/config.ru
70
+ #
71
+ # human_component_path(path) # => 'my_site/config.ru'
72
+ #
73
+ # Used in CLI output.
74
+ def human_component_path(path)
75
+ File.join(@name, common_component_path(path))
76
+ end
77
+
78
+ # Actions
79
+ def create_destination_folder
80
+ unless File.directory?(destination_folder)
81
+ run "mkdir #{destination_folder}"
82
+ created @name
83
+ end
84
+ end
85
+
86
+ # Copies a file or folder from Hyper's template to a new site folder.
87
+ def generate_component(path)
88
+ run "cp -r #{path} #{destination_path(path)}"
89
+ created human_component_path(path)
90
+ end
91
+
92
+ # Helpers
93
+ def created(path)
94
+ say :created, path, :green
95
+ end
96
+
97
+ def error(message)
98
+ say :error, message, :red
99
+ end
100
+
101
+ def say(status, message, color)
102
+ puts "#{send color}#{status} #{bold}#{message}#{reset}"
103
+ end
104
+
105
+ def run(command)
106
+ system command
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,34 @@
1
+ require 'rack/request'
2
+ require 'tilt'
3
+
4
+
5
+ module Hyper
6
+ # @api private
7
+ class Response
8
+ def call(env)
9
+ request = Rack::Request.new(env)
10
+ headers = {'Content-Type' => 'text/html'}
11
+ layout = Utils.layout_path
12
+ view = Utils.view_path(request.path)
13
+
14
+ if File.file?(view)
15
+ status = 200
16
+ body = render_templates(layout, view)
17
+ else
18
+ status = 404
19
+ body = "404 Not Found: #{request.path}"
20
+ end
21
+
22
+ [status, headers, StringIO.new(body)]
23
+ end
24
+
25
+ private
26
+ def render_templates(layout, view)
27
+ render_erb(layout) { render_erb(view) }
28
+ end
29
+
30
+ def render_erb(template, &block)
31
+ Tilt.new(template, :trim => '-').render(&block)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ require 'rack/builder'
2
+
3
+ module Hyper
4
+ # @api private
5
+ class Static
6
+ class << self
7
+ def urls
8
+ Dir[Hyper.root.join('public/**/*')].map do |file|
9
+ file.sub(Hyper.root.join('public'), '')
10
+ end
11
+ end
12
+ end
13
+
14
+ def initialize(app)
15
+ @app = Rack::Builder.new do
16
+ use Rack::Static, :urls => Hyper::Static.urls, :root => 'public'
17
+ run app
18
+ end
19
+ end
20
+
21
+ def call(env)
22
+ @app.call(env)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1 @@
1
+ gem 'hyper'
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rack'
4
+ require 'hyper'
5
+
6
+ builder = Rack::Builder.new do
7
+ # Add middleware here
8
+ run Hyper
9
+ end
10
+
11
+ run builder.to_app
@@ -0,0 +1,9 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <title>Hyper Site</title>
5
+ </head>
6
+ <body>
7
+ <%= yield %>
8
+ </body>
9
+ </html>
@@ -0,0 +1 @@
1
+ <p>Find me in <%= __FILE__ -%></p>
@@ -0,0 +1,34 @@
1
+ module Hyper
2
+ # @api private
3
+ module Utils
4
+ extend self
5
+
6
+ def layout_path
7
+ File.join(Hyper.template_root, 'layout.html.erb')
8
+ end
9
+
10
+ def view_path(url)
11
+ File.join(Hyper.template_root, 'views', normalize_url(url) + '.html.erb')
12
+ end
13
+
14
+ private
15
+ # Attempts to standardize URLs by adding 'index' to URLs
16
+ # which end in "/". Also replaces an empty URL with 'index'.
17
+ #
18
+ # normalize_url('') # => '/index'
19
+ # normalize_url('/') # => '/index'
20
+ # normalize_url('/folder/') # => '/folder/index'
21
+ # normalize_url('/page') # => '/page'
22
+ #
23
+ # @return [String]
24
+ def normalize_url(url)
25
+ if ['', '/'].include?(url) # Root URL
26
+ '/index'
27
+ elsif url =~ /.+\/$/ # Folder indexes
28
+ File.join(url, 'index')
29
+ else # Everything else
30
+ url
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ module Hyper
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 3
5
+ TINY = '0beta1'
6
+
7
+ def self.to_s
8
+ [MAJOR, MINOR, TINY].join('.')
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hyper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 62196369
5
+ prerelease: 5
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ - beta
11
+ - 1
12
+ version: 0.3.0beta1
13
+ platform: ruby
14
+ authors:
15
+ - James Wilding
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-05-18 00:00:00 +01:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: rack
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 27
32
+ segments:
33
+ - 1
34
+ - 2
35
+ - 2
36
+ version: 1.2.2
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: tilt
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 21
48
+ segments:
49
+ - 1
50
+ - 0
51
+ - 1
52
+ version: 1.0.1
53
+ type: :runtime
54
+ version_requirements: *id002
55
+ - !ruby/object:Gem::Dependency
56
+ name: term-ansicolor
57
+ prerelease: false
58
+ requirement: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ description: Hyper is a dead simple, zero-config, drop-in framework for static websites on ruby + rack
84
+ email: office@jameswilding.net
85
+ executables:
86
+ - hyper
87
+ extensions: []
88
+
89
+ extra_rdoc_files: []
90
+
91
+ files:
92
+ - lib/hyper/generator.rb
93
+ - lib/hyper/response.rb
94
+ - lib/hyper/static.rb
95
+ - lib/hyper/template/config.ru
96
+ - lib/hyper/template/Gemfile
97
+ - lib/hyper/template/templates/layout.html.erb
98
+ - lib/hyper/template/templates/views/index.html.erb
99
+ - lib/hyper/utils.rb
100
+ - lib/hyper/version.rb
101
+ - lib/hyper.rb
102
+ - README.md
103
+ - bin/hyper
104
+ has_rdoc: true
105
+ homepage: http://github.com/jameswilding/hyper
106
+ licenses: []
107
+
108
+ post_install_message:
109
+ rdoc_options: []
110
+
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">"
126
+ - !ruby/object:Gem::Version
127
+ hash: 25
128
+ segments:
129
+ - 1
130
+ - 3
131
+ - 1
132
+ version: 1.3.1
133
+ requirements: []
134
+
135
+ rubyforge_project:
136
+ rubygems_version: 1.6.2
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: Zero-config framework for static websites
140
+ test_files: []
141
+