commit 706ab4b241613c7311462a20acdd4093bebeae9e Author: Wlodek Bzyl Date: Mon Jun 1 19:53:37 2009 +0200 First commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3052a87 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +pkg/* +*~ +\#* +.\#* + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6dbf589 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +(The MIT License) + +Copyright (c) 2009 Wlodek Bzyl + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..8ddeef5 --- /dev/null +++ b/README.markdown @@ -0,0 +1,142 @@ +# Sinatra Extension: StaticAssets + +Gem *sinatra-static-assets* implements the following helpers methods: + +* `image_tag` +* `stylesheet_link_tag` +* `javascript_script_tag` +* `link_tag` + +To install it, run: + + sudo gem install wbzyl-sinatra-static-assets -s http://gems.github.com + +All these methods are simple wrappers around the `url_for` method +from the [sinatra-url-for](http://github.com/emk/sinatra-url-for/) gem. + +## When will you need it? + +Whenever you use the +[Passenger module for Apache2](http://www.modrails.com/documentation/Users%20guide%20Apache.html#deploying_rack_to_sub_uri) +or use `Rack::URLMap` to dispatch an application to +sub URI. + +Example: Suppose that we already have a virtual host `hitch.local` +and two Sinatra applications that live in +`/home/me/www/summer` and `/home/me/www/winter` +directories, respectively. +We want our Sinatra applications to be accessible from +the following sub URI: + + http://hitch.local/summer + +and + + http://hitch.local/winter + +To configure Apache2 and Passenger to serve our applications +we need to create a new configuration file with the following content: + + + ServerName hitch.local + DocumentRoot /srv/www/hitch.local + + RackBaseURI /summer + RackBaseURI /winter + + +and a link to the applications directories in `/srv/www/hitch.local`: + + ln -s /home/me/www/summer/public /srv/www/hitch.local/summer + ln -s /home/me/www/winter/public /srv/www/hitch.local/winter + +After restarting an Apache2 server and visiting, for example, the first +application at `http://hitch.local/summer` we see that links to +images, stylesheets and javascripts are broken. + +The hitch here is that in Sinatra applications we usually refer to +images/stylesheets/javascripts with absolute URI: + + /images/tatry1.jpg /stylesheets/app.css /javascripts/app.js + +That setup **works** whenever we are running applications locally. +The absolute URI above tells a browser to request images +(stylesheets and javascripts) from: + + http://localhost:4567/images/tatry1.jpg + +which in turn, tells a server to send a file: + + /home/me/www/summer/public/images/tatry1.jpg + +The `public` directory is the default directory where static files +should be served from. +So, the `/images/tatry1.jpg` picture will be there and will be served +unless we had changed that default directory. + +But these absolute URIs do not work when, for example, +the *summer* application is dispatched to `/summer` sub URI. +As a result the images are at: + + http://hitch.local/summer/images/tatry1.jpg + +but we request them from: + + http://hitch.local/images/tatry1.jpg + +And this **does not work** because there is no application +dispatched to *images* sub URI. + +The recommended way to deal with an absolute URI +is to use a helper method that automatically converts +`/images/tatry1.jpg` to `/summer/images/tatry1.jpg` +for application dispatched to `/summer` sub URI. + +In the above example you can simply remove the `` +HTML tag and replace it with a Ruby inline code like this: + + <%= image_tag("/images/tatry1.jpg", :alt => "Błyszcz, 2159 m") %> + +See also, [How to fix broken images/CSS/JavaScript URIs in sub-URI +deployments](http://www.modrails.com/documentation/Users%20guide%20Apache.html#sub_uri_deployment_uri_fix) + +## Usage examples + +In HTML `` and `` tags have no end tag. +In XHTML, on the contrary, these tags must be properly closed. + +We can choose the appropriate behaviour with *closed* option: + + image_tag "/images/tatry1.jpg", :alt => "Błyszcz, 2159 m", :closed => true + +The default value of *closed* option is `false`. + + stylesheet_link_tag "/stylesheets/screen.css", "/stylesheets/summer.css", :media => "projection" + javascript_script_tag "/javascripts/jquery.js", "/javascripts/summer.js", :charset => "iso-8859-2" + link_to "Tatry Mountains Rescue Team", "/topr" + +In order to use include the following in a Sinatra application: + + gem 'wbzyl-sinatra-static-assets' + require 'sinatra/static_assets' + +Or, if subclassing `Sinatra::Base`, include helpers manually: + + gem 'wbzyl-sinatra-static-assets' + require 'sinatra/static_assets' + + class Summer < Sinatra::Base + helpers Sinatra::StaticAssets + # ... + end + + +## Miscellaneous stuff + +1. The `examples` directory contains *summer* and *winter* applications. + +2. In order to create a virual host add the following to */etc/hosts/*: + + 127.0.0.1 localhost.localdomain localhost hitch.local + +3. TODO: write tests diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..80dd49a --- /dev/null +++ b/Rakefile @@ -0,0 +1,40 @@ +require 'rake' +require 'rake/testtask' + +begin + require 'jeweler' + Jeweler::Tasks.new do |gemspec| + gemspec.name = "sinatra-static-assets" + gemspec.summary = "Sinatra extension providing helper methods to output tags for static assetgemspec." + gemspec.email = "matwb@univ.gda.pl" + gemspec.homepage = "http://github.com/wbzyl/sinatra-static-assets" + gemspec.authors = ["Wlodek Bzyl"] + + gemspec.description = <<-EOF +This Sinatra extensions provides following helper methods: + - image_tag + - stylesheet_link_tag + - javascript_script_tag + EOF + + gemspec.files = %w{TODO VERSION.yml} + FileList['lib/**/*.rb', 'test/**/*.rb', 'examples/**/*'] + + gemspec.add_runtime_dependency 'rack', '>=1.0.0' + gemspec.add_runtime_dependency 'sinatra' + gemspec.add_runtime_dependency 'emk-sinatra-url-for', '>=0.2.1' + + gemspec.add_development_dependency 'rack-test', '>=0.3.0' + + gemspec.rubyforge_project = 'sinatra-static-assets' + end +rescue LoadError + puts "Jeweler not available." + puts "Install it with:" + puts " sudo gem install technicalpickles-jeweler -s http://gems.github.com" +end + +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = false +end diff --git a/TODO b/TODO new file mode 100644 index 0000000..e69de29 diff --git a/VERSION.yml b/VERSION.yml new file mode 100644 index 0000000..0066fa4 --- /dev/null +++ b/VERSION.yml @@ -0,0 +1,4 @@ +--- +:major: 0 +:minor: 1 +:patch: 0 diff --git a/examples/mapp1/config.ru b/examples/mapp1/config.ru new file mode 100644 index 0000000..a4f7af0 --- /dev/null +++ b/examples/mapp1/config.ru @@ -0,0 +1,14 @@ +require 'mapp' + +Mapp1 = Rack::Builder.new do + use Rack::ShowExceptions + use Rack::Lint + + use Rack::Static, :urls => ["/stylesheets", "/images"], :root => "public" + + map '/' do + run Sinatra::Mapp1.new + end +end + +run Mapp1 diff --git a/examples/mapp1/mapp.rb b/examples/mapp1/mapp.rb new file mode 100644 index 0000000..658bd71 --- /dev/null +++ b/examples/mapp1/mapp.rb @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +require 'sinatra/base' + +gem 'sinatra-static-assets' +require 'sinatra/static_assets' + +module Sinatra + class Mapp1 < Sinatra::Base + helpers Sinatra::UrlForHelper + helpers Sinatra::StaticAssets + + set :app_file, __FILE__ + set :static, true + + #set :root, File.dirname(__FILE__) + #set :views, Proc.new { File.join(root, "views") } + + get '/' do + @title = "Tatra Mountains, Błyszcz (2159 m)" + erb :app + end + end +end diff --git a/examples/mapp1/public/images/tatry1.jpg b/examples/mapp1/public/images/tatry1.jpg new file mode 100644 index 0000000..24e68b0 Binary files /dev/null and b/examples/mapp1/public/images/tatry1.jpg differ diff --git a/examples/mapp1/public/javascripts/mapp.js b/examples/mapp1/public/javascripts/mapp.js new file mode 100644 index 0000000..e6dc3eb --- /dev/null +++ b/examples/mapp1/public/javascripts/mapp.js @@ -0,0 +1 @@ +/* mapp1 */ diff --git a/examples/mapp1/public/stylesheets/mapp.css b/examples/mapp1/public/stylesheets/mapp.css new file mode 100644 index 0000000..5fbc656 --- /dev/null +++ b/examples/mapp1/public/stylesheets/mapp.css @@ -0,0 +1,14 @@ +html { + margin: 0; + padding: 0; + background: #ABA418 url(src/background.png); +} + +body { + width: 600px; + margin: 1em auto; + padding: 1em 2em; + border: black solid 1px; + background-color: #D1C704; + font: normal 14px/1.6 Arial, Helvetica, sans-serif; +} diff --git a/examples/mapp1/public/stylesheets/src/background.png b/examples/mapp1/public/stylesheets/src/background.png new file mode 100644 index 0000000..f14083e Binary files /dev/null and b/examples/mapp1/public/stylesheets/src/background.png differ diff --git a/examples/mapp1/tmp/always_restart.txt b/examples/mapp1/tmp/always_restart.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/mapp1/views/app.erb b/examples/mapp1/views/app.erb new file mode 100644 index 0000000..76a2a12 --- /dev/null +++ b/examples/mapp1/views/app.erb @@ -0,0 +1,3 @@ +

<%= @title %>

+ +

<%= image_tag "/images/tatry1.jpg", :closed => true, :alt => @title, :title => @title %>

diff --git a/examples/mapp1/views/layout.erb b/examples/mapp1/views/layout.erb new file mode 100644 index 0000000..eec574f --- /dev/null +++ b/examples/mapp1/views/layout.erb @@ -0,0 +1,23 @@ + + + + + + + + <%= stylesheet_link_tag "/stylesheets/mapp.css" %> + <%= javascript_script_tag "/javascripts/mapp.js" %> + + <%= @title %> + + + + + <%= yield %> + + + diff --git a/examples/mapp2/config.ru b/examples/mapp2/config.ru new file mode 100644 index 0000000..fdfd354 --- /dev/null +++ b/examples/mapp2/config.ru @@ -0,0 +1,14 @@ +require 'mapp' + +Mapp2 = Rack::Builder.new do + use Rack::ShowExceptions + use Rack::Lint + + use Rack::Static, :urls => ["/stylesheets", "/images"], :root => "public" + + map '/' do + run Sinatra::Mapp2.new + end +end + +run Mapp2 diff --git a/examples/mapp2/mapp.rb b/examples/mapp2/mapp.rb new file mode 100644 index 0000000..6ca68de --- /dev/null +++ b/examples/mapp2/mapp.rb @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +require 'sinatra/base' + +gem 'sinatra-static-assets' +require 'sinatra/static_assets' + +module Sinatra + class Mapp2 < Sinatra::Base + helpers Sinatra::UrlForHelper + helpers Sinatra::StaticAssets + + set :app_file, __FILE__ + set :static, true + + get '/' do + @title = "Tatra Mountains, Dolina Gąsienicowa (1500 m)" + erb :mapp + end + end +end diff --git a/examples/mapp2/public/images/tatry2.jpg b/examples/mapp2/public/images/tatry2.jpg new file mode 100644 index 0000000..6c0a66c Binary files /dev/null and b/examples/mapp2/public/images/tatry2.jpg differ diff --git a/examples/mapp2/public/javascripts/mapp.js b/examples/mapp2/public/javascripts/mapp.js new file mode 100644 index 0000000..a09db85 --- /dev/null +++ b/examples/mapp2/public/javascripts/mapp.js @@ -0,0 +1 @@ +/* mapp.js */ diff --git a/examples/mapp2/public/stylesheets/mapp.css b/examples/mapp2/public/stylesheets/mapp.css new file mode 100644 index 0000000..659e0a5 --- /dev/null +++ b/examples/mapp2/public/stylesheets/mapp.css @@ -0,0 +1,14 @@ +html { + margin: 0; + padding: 0; + background: #ABA418 url(src/background.png); +} + +body { + width: 600px; + margin: 1em auto; + padding: 1em 2em; + border: black solid 1px; + background-color: #D8E8FF; + font: normal 14px/1.6 Arial, Helvetica, sans-serif; +} diff --git a/examples/mapp2/public/stylesheets/src/background.png b/examples/mapp2/public/stylesheets/src/background.png new file mode 100644 index 0000000..e0e604f Binary files /dev/null and b/examples/mapp2/public/stylesheets/src/background.png differ diff --git a/examples/mapp2/tmp/always_restart.txt b/examples/mapp2/tmp/always_restart.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/mapp2/views/layout.erb b/examples/mapp2/views/layout.erb new file mode 100644 index 0000000..18c6454 --- /dev/null +++ b/examples/mapp2/views/layout.erb @@ -0,0 +1,16 @@ + + + + + <%= stylesheet_link_tag "/stylesheets/mapp.css" %> + <%= javascript_script_tag "/javascripts/mapp.js" %> + + <%= @title %> + + + + + <%= yield %> + + + diff --git a/examples/mapp2/views/mapp.erb b/examples/mapp2/views/mapp.erb new file mode 100644 index 0000000..8b6d6e7 --- /dev/null +++ b/examples/mapp2/views/mapp.erb @@ -0,0 +1,3 @@ +

<%= @title %>

+ +

<%= image_tag "/images/tatry2.jpg", :closed => true, :alt => @title, :title => @title %>

diff --git a/examples/mconfig.ru b/examples/mconfig.ru new file mode 100644 index 0000000..3010d85 --- /dev/null +++ b/examples/mconfig.ru @@ -0,0 +1,19 @@ +require 'mapp1/mapp' +require 'mapp2/mapp' + +Mapp = Rack::Builder.new do + use Rack::ShowExceptions + use Rack::Lint + + #use Rack::Static, :urls => ["/stylesheets", "/images"], :root => "public" + + map '/mapp1' do + run Sinatra::Mapp1.new + end + + map '/mapp2' do + run Sinatra::Mapp2.new + end +end + +run Mapp diff --git a/examples/summer/config.ru b/examples/summer/config.ru new file mode 100644 index 0000000..bc48c61 --- /dev/null +++ b/examples/summer/config.ru @@ -0,0 +1,4 @@ +require 'summer' + +use Rack::ShowExceptions +run Sinatra::Application diff --git a/examples/summer/public/images/tatry1.jpg b/examples/summer/public/images/tatry1.jpg new file mode 100644 index 0000000..24e68b0 Binary files /dev/null and b/examples/summer/public/images/tatry1.jpg differ diff --git a/examples/summer/public/javascripts/app.js b/examples/summer/public/javascripts/app.js new file mode 100644 index 0000000..78b074f --- /dev/null +++ b/examples/summer/public/javascripts/app.js @@ -0,0 +1 @@ +/* summer: app.js */ diff --git a/examples/summer/public/stylesheets/app.css b/examples/summer/public/stylesheets/app.css new file mode 100644 index 0000000..be974d9 --- /dev/null +++ b/examples/summer/public/stylesheets/app.css @@ -0,0 +1,14 @@ +html { + margin: 0; + padding: 0; + background: #ABA418 url(src/bronzed_olive.png); +} + +body { + width: 600px; + margin: 1em auto; + padding: 1em 2em; + border: black solid 1px; + background-color: #D1C704; + font: normal 14px/1.6 Arial, Helvetica, sans-serif; +} diff --git a/examples/summer/public/stylesheets/src/bronzed_olive.png b/examples/summer/public/stylesheets/src/bronzed_olive.png new file mode 100644 index 0000000..f14083e Binary files /dev/null and b/examples/summer/public/stylesheets/src/bronzed_olive.png differ diff --git a/examples/summer/summer.rb b/examples/summer/summer.rb new file mode 100644 index 0000000..4c956da --- /dev/null +++ b/examples/summer/summer.rb @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- + +require 'sinatra' + +gem 'wbzyl-sinatra-static-assets' +require 'sinatra/static_assets' + +get "/?" do + @title = "Tatra Mountains, Błyszcz (2159 m)" + erb :index +end + +get "/:page" do + @title = "#{params[:page]}" + erb :"#{params[:page]}" +end diff --git a/examples/summer/tmp/always_restart.txt b/examples/summer/tmp/always_restart.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/summer/views/index.erb b/examples/summer/views/index.erb new file mode 100644 index 0000000..e7852e3 --- /dev/null +++ b/examples/summer/views/index.erb @@ -0,0 +1,6 @@ +

<%= @title %>

+ +

<%= image_tag "/images/tatry1.jpg", :closed => true, :alt => @title, :title => @title %>

+ +

Tatra Mountain in Winter

+

<%= link_to "Tatry Mountains Rescue Team", "/topr" %>

diff --git a/examples/summer/views/layout.erb b/examples/summer/views/layout.erb new file mode 100644 index 0000000..c6d1512 --- /dev/null +++ b/examples/summer/views/layout.erb @@ -0,0 +1,16 @@ + + + + + <%= stylesheet_link_tag "/stylesheets/app.css" %> + <%= javascript_script_tag "/javascripts/app.js" %> + + <%= @title %> + + + + + <%= yield %> + + + diff --git a/examples/summer/views/topr.erb b/examples/summer/views/topr.erb new file mode 100644 index 0000000..2ea8fc7 --- /dev/null +++ b/examples/summer/views/topr.erb @@ -0,0 +1,5 @@ +

Tatry Mountains Rescue Team

+ +

The home page: TOPR + +

The emergency telephone: +48 601 100 300

diff --git a/examples/winter/app2.rb b/examples/winter/app2.rb new file mode 100644 index 0000000..3d95cca --- /dev/null +++ b/examples/winter/app2.rb @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- + +require 'rubygems' +require 'sinatra' + +gem 'sinatra-static-assets' +require 'sinatra/static_assets' + +get "/?" do + @title = "Tatra Mountains, Dolina Gąsienicowa (1500 m)" + erb :index +end diff --git a/examples/winter/config.ru b/examples/winter/config.ru new file mode 100644 index 0000000..a476a09 --- /dev/null +++ b/examples/winter/config.ru @@ -0,0 +1,7 @@ +# run with: thin --rackup config.ru -p 4567 start + +require 'app2' + +use Rack::ShowExceptions +use Rack::Static, :urls => ["/stylesheets", "/javascripts", "/images"], :root => "public" +run Sinatra::Application diff --git a/examples/winter/public/images/tatry2.jpg b/examples/winter/public/images/tatry2.jpg new file mode 100644 index 0000000..6c0a66c Binary files /dev/null and b/examples/winter/public/images/tatry2.jpg differ diff --git a/examples/winter/public/javascripts/app2.js b/examples/winter/public/javascripts/app2.js new file mode 100644 index 0000000..c130577 --- /dev/null +++ b/examples/winter/public/javascripts/app2.js @@ -0,0 +1 @@ +/* app1.js */ diff --git a/examples/winter/public/stylesheets/app2.css b/examples/winter/public/stylesheets/app2.css new file mode 100644 index 0000000..ff6d8a5 --- /dev/null +++ b/examples/winter/public/stylesheets/app2.css @@ -0,0 +1,18 @@ +/* + Theme for app2: http://www.colourlovers.com/palette/81363/rainy +*/ + +html { + margin: 0; + padding: 0; + background: #659DF1 url(src/skating.png); +} + +body { + width: 600px; + margin: 1em auto; + padding: 1em 2em; + border: black solid 1px; + background-color: #D8E8FF; + font: normal 14px/1.6 Arial, Helvetica, sans-serif; +} diff --git a/examples/winter/public/stylesheets/src/skating.png b/examples/winter/public/stylesheets/src/skating.png new file mode 100644 index 0000000..e0e604f Binary files /dev/null and b/examples/winter/public/stylesheets/src/skating.png differ diff --git a/examples/winter/tmp/always_restart.txt b/examples/winter/tmp/always_restart.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/winter/views/index.erb b/examples/winter/views/index.erb new file mode 100644 index 0000000..3c95547 --- /dev/null +++ b/examples/winter/views/index.erb @@ -0,0 +1,3 @@ +

<%= @title %>

+ +

<%= image_tag "/images/tatry2.jpg", :alt => @title, :title => @title %>

diff --git a/examples/winter/views/layout.erb b/examples/winter/views/layout.erb new file mode 100644 index 0000000..8fc18e9 --- /dev/null +++ b/examples/winter/views/layout.erb @@ -0,0 +1,16 @@ + + + + + <%= stylesheet_link_tag "/stylesheets/app2.css" %> + <%= javascript_script_tag "/javascripts/app2.js" %> + + <%= @title %> + + + + + <%= yield %> + + + diff --git a/lib/sinatra/static_assets.rb b/lib/sinatra/static_assets.rb new file mode 100644 index 0000000..f886e94 --- /dev/null +++ b/lib/sinatra/static_assets.rb @@ -0,0 +1,72 @@ +gem 'sinatra', '~>0.9.2' +require 'sinatra/base' + +gem 'emk-sinatra-url-for', '>=0.2.1' +require 'sinatra/url_for' + +module Sinatra + module StaticAssets + # In HTML and tags have no end tag. + # In XHTML, on the contrary, these tags must be properly closed. + # + # We can choose the appropriate behaviour with +closed+ option: + # + # image_tag "/images/foo.png", :alt => "Foo itself", :closed => true + # + # The default value of +closed+ option is +false+. + # + def image_tag(source, options = {}) + closed = options.delete(:closed) + options[:src] = url_for(source) + tag("img", options, closed) + end + + def stylesheet_link_tag(*sources) + list, options = extract_options(sources) + closed = options.delete(:closed) + list.collect { |source| stylesheet_tag(source, options, closed) }.join("\n") + end + + def javascript_script_tag(*sources) + list, options = extract_options(sources) + list.collect { |source| javascript_tag(source, options) }.join("\n") + end + + def link_to(desc, url) + "#{desc}" + end + + private + + def tag(name, options = {}, closed = false) + "<#{name}#{tag_options(options) if options}#{closed ? " />" : ">"}" + end + + def tag_options(options) + unless options.empty? + attrs = [] + attrs = options.map { |key, value| %(#{key}="#{value}") } + " #{attrs.sort * ' '}" unless attrs.empty? + end + end + + def stylesheet_tag(source, options, closed = false) + tag("link", { :type => "text/css", + :charset => "utf-8", :media => "screen", :rel => "stylesheet", + :href => url_for(source) }.merge(options), closed) + end + + def javascript_tag(source, options) + tag("script", { :type => "text/javascript", :charset => "utf-8", + :src => url_for(source) }.merge(options), false) + "" + end + + def extract_options(a) + opts = a.last.is_a?(::Hash) ? a.pop : {} + [a, opts] + end + + end + + helpers StaticAssets +end diff --git a/sinatra-static-assets.gemspec b/sinatra-static-assets.gemspec new file mode 100644 index 0000000..d3ee640 --- /dev/null +++ b/sinatra-static-assets.gemspec @@ -0,0 +1,101 @@ +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{sinatra-static-assets} + s.version = "0.1.0" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Wlodek Bzyl"] + s.date = %q{2009-06-01} + s.description = %q{This Sinatra extensions provides following helper methods: + - image_tag + - stylesheet_link_tag + - javascript_script_tag +} + s.email = %q{matwb@univ.gda.pl} + s.extra_rdoc_files = [ + "LICENSE", + "README.markdown" + ] + s.files = [ + "TODO", + "VERSION.yml", + "examples/mapp1/config.ru", + "examples/mapp1/mapp.rb", + "examples/mapp1/public/images/tatry1.jpg", + "examples/mapp1/public/javascripts/mapp.js", + "examples/mapp1/public/stylesheets/mapp.css", + "examples/mapp1/public/stylesheets/src/background.png", + "examples/mapp1/tmp/always_restart.txt", + "examples/mapp1/views/app.erb", + "examples/mapp1/views/layout.erb", + "examples/mapp2/config.ru", + "examples/mapp2/mapp.rb", + "examples/mapp2/public/images/tatry2.jpg", + "examples/mapp2/public/javascripts/mapp.js", + "examples/mapp2/public/stylesheets/mapp.css", + "examples/mapp2/public/stylesheets/src/background.png", + "examples/mapp2/tmp/always_restart.txt", + "examples/mapp2/views/layout.erb", + "examples/mapp2/views/mapp.erb", + "examples/mconfig.ru", + "examples/summer/config.ru", + "examples/summer/public/images/tatry1.jpg", + "examples/summer/public/javascripts/app.js", + "examples/summer/public/stylesheets/app.css", + "examples/summer/public/stylesheets/src/bronzed_olive.png", + "examples/summer/summer.rb", + "examples/summer/tmp/always_restart.txt", + "examples/summer/views/index.erb", + "examples/summer/views/layout.erb", + "examples/summer/views/topr.erb", + "examples/winter/app2.rb", + "examples/winter/config.ru", + "examples/winter/public/images/tatry2.jpg", + "examples/winter/public/javascripts/app2.js", + "examples/winter/public/stylesheets/app2.css", + "examples/winter/public/stylesheets/src/skating.png", + "examples/winter/tmp/always_restart.txt", + "examples/winter/views/index.erb", + "examples/winter/views/layout.erb", + "lib/sinatra/static_assets.rb", + "test/sinatra_static_assets_test.rb", + "test/test_helper.rb" + ] + s.homepage = %q{http://github.com/wbzyl/sinatra-static-assets} + s.rdoc_options = ["--charset=UTF-8"] + s.require_paths = ["lib"] + s.rubyforge_project = %q{sinatra-static-assets} + s.rubygems_version = %q{1.3.4} + s.summary = %q{Sinatra extension providing helper methods to output tags for static assetgemspec.} + s.test_files = [ + "test/test_helper.rb", + "test/sinatra_static_assets_test.rb", + "examples/summer/summer.rb", + "examples/mapp2/mapp.rb", + "examples/mapp1/mapp.rb", + "examples/winter/app2.rb" + ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + s.add_runtime_dependency(%q, [">= 1.0.0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0.2.1"]) + s.add_development_dependency(%q, [">= 0.3.0"]) + else + s.add_dependency(%q, [">= 1.0.0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0.2.1"]) + s.add_dependency(%q, [">= 0.3.0"]) + end + else + s.add_dependency(%q, [">= 1.0.0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0.2.1"]) + s.add_dependency(%q, [">= 0.3.0"]) + end +end diff --git a/test/sinatra_static_assets_test.rb b/test/sinatra_static_assets_test.rb new file mode 100644 index 0000000..a9444ce --- /dev/null +++ b/test/sinatra_static_assets_test.rb @@ -0,0 +1,91 @@ +require File.dirname(__FILE__) + '/test_helper' + +class SinatraRDiscountTest < Test::Unit::TestCase + include Rack::Test::Methods + + def rdiscount_app(&block) + mock_app { + set :views, File.dirname(__FILE__) + '/views' + helpers Sinatra::RDiscount + set :show_exceptions, false + get '/', &block + } + get '/' + end + + def test_renders_inline_strings + rdiscount_app { rdiscount 'hello world' } + assert last_response.ok? + assert_equal "

hello world

\n", last_response.body + end + + def test_renders_inline_erb_string + rdiscount_app { rdiscount '{%= 1 + 1 %}' } + assert last_response.ok? + assert_equal "

2

\n", last_response.body + end + + def test_renders_files_in_views_path + rdiscount_app { rdiscount :hello } + assert last_response.ok? + assert_equal "

hello world

\n", last_response.body + end + + def test_takes_locals_option + rdiscount_app { + locals = {:foo => 'Bar'} + rdiscount "{%= foo %}", :locals => locals + } + assert last_response.ok? + assert_equal "

Bar

\n", last_response.body + end + + def test_renders_with_inline_layouts + rdiscount_app { + rdiscount 'Sparta', :layout => 'THIS. IS. <%= yield.upcase %>' + } + assert last_response.ok? + assert_equal "THIS. IS.

SPARTA

\n", last_response.body + end + + def test_renders_with_file_layouts + rdiscount_app { + rdiscount 'hello world', :layout => :layout2 + } + assert last_response.ok? + assert_equal "erb layout\n

hello world

\n\n", last_response.body + end + + def test_renders_erb_with_blocks + mock_app { + set :views, File.dirname(__FILE__) + '/views' + helpers Sinatra::RDiscount + + def container + yield + end + def is; + "THIS. IS. SPARTA!" + end + + get '/' do + rdiscount '{% container do %} {%= is %} {% end %}' + end + } + + get '/' + assert last_response.ok? + assert_equal "

THIS. IS. SPARTA!

\n", last_response.body + end + + def test_raises_error_if_template_not_found + mock_app { + set :views, File.dirname(__FILE__) + '/views' + helpers Sinatra::RDiscount + set :show_exceptions, false + + get('/') { rdiscount :no_such_template } + } + assert_raise(Errno::ENOENT) { get('/') } + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..87487c0 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,21 @@ +require 'rubygems' +require 'test/unit' +require 'rack/test' + +$LOAD_PATH.unshift(File.dirname(__FILE__)) +require 'sinatra/rdiscount' + +require 'rdiscount' + +class Test::Unit::TestCase + include Rack::Test::Methods + + attr_reader :app + + # Sets up a Sinatra::Base subclass defined with the block + # given. Used in setup or individual spec methods to establish + # the application. + def mock_app(base=Sinatra::Base, &block) + @app = Sinatra.new(base, &block) + end +end