sinatra-static-assets 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- # Sinatra Extension: StaticAssets
1
+ \# Sinatra Extension: StaticAssets
2
2
 
3
3
  Gem *sinatra-static-assets* implements the following helpers methods:
4
4
 
@@ -9,7 +9,7 @@ Gem *sinatra-static-assets* implements the following helpers methods:
9
9
 
10
10
  To install it, run:
11
11
 
12
- sudo gem install sinatra-static-assets -s http://gemcutter.com
12
+ sudo gem install sinatra-static-assets -s http://gemcutter.org
13
13
 
14
14
  All these methods are simple wrappers around the `url_for` method
15
15
  from the [sinatra-url-for](http://github.com/emk/sinatra-url-for/) gem.
@@ -200,3 +200,6 @@ respectively.
200
200
  2\. In order to create a virual host add the following to */etc/hosts/*:
201
201
 
202
202
  127.0.0.1 localhost.localdomain localhost hitch.local
203
+
204
+ or, read [editing /etc/hosts is waste of my
205
+ time](http://www.taylorluk.com/articles/2009/08/12/hey-pac-man-sup-subdomains).
@@ -1,4 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 4
3
+ :minor: 5
4
4
  :patch: 0
5
+ :build:
@@ -3,67 +3,81 @@ require 'sinatra/url_for'
3
3
 
4
4
  module Sinatra
5
5
  module StaticAssets
6
- # In HTML <link> and <img> tags have no end tag.
7
- # In XHTML, on the contrary, these tags must be properly closed.
8
- #
9
- # We can choose the appropriate behaviour with +closed+ option:
10
- #
11
- # image_tag "/images/foo.png", :alt => "Foo itself", :closed => true
12
- #
13
- # The default value of +closed+ option is +false+.
14
- #
15
- def image_tag(source, options = {})
16
- closed = options.delete(:closed)
17
- options[:src] = url_for(source)
18
- tag("img", options, closed)
19
- end
6
+ module Helpers
7
+ # In HTML <link> and <img> tags have no end tag.
8
+ # In XHTML, on the contrary, these tags must be properly closed.
9
+ #
10
+ # We can choose the appropriate behaviour with +closed+ option:
11
+ #
12
+ # image_tag "/images/foo.png", :alt => "Foo itself", :closed => true
13
+ #
14
+ # The default value of +closed+ option is +false+.
15
+ #
16
+ def image_tag(source, options = {})
17
+ options[:src] = url_for(source)
18
+ tag("img", options)
19
+ end
20
20
 
21
- def stylesheet_link_tag(*sources)
22
- list, options = extract_options(sources)
23
- closed = options.delete(:closed)
24
- list.collect { |source| stylesheet_tag(source, options, closed) }.join("\n")
25
- end
21
+ def stylesheet_link_tag(*sources)
22
+ list, options = extract_options(sources)
23
+ list.collect { |source| stylesheet_tag(source, options) }.join("\n")
24
+ end
26
25
 
27
- def javascript_script_tag(*sources)
28
- list, options = extract_options(sources)
29
- list.collect { |source| javascript_tag(source, options) }.join("\n")
30
- end
31
-
32
- def link_to(desc, url)
33
- "<a href='#{url_for(url)}'>#{desc}</a>"
34
- end
35
-
36
- private
37
-
38
- def tag(name, options = {}, closed = false)
39
- "<#{name}#{tag_options(options) if options}#{closed ? " />" : ">"}"
40
- end
41
-
42
- def tag_options(options)
43
- unless options.empty?
44
- attrs = []
45
- attrs = options.map { |key, value| %(#{key}="#{value}") }
46
- " #{attrs.sort * ' '}" unless attrs.empty?
26
+ def javascript_script_tag(*sources)
27
+ list, options = extract_options(sources)
28
+ list.collect { |source| javascript_tag(source, options) }.join("\n")
47
29
  end
48
- end
49
-
50
- def stylesheet_tag(source, options, closed = false)
51
- tag("link", { :type => "text/css",
30
+
31
+ def link_to(desc, url, options = {})
32
+ tag("a", options.merge(:href => url_for(url))) do
33
+ desc
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def tag(name, local_options = {})
40
+ start_tag = "<#{name}#{tag_options(local_options) if local_options}"
41
+ if block_given?
42
+ content = yield
43
+ "#{start_tag}>#{content}</#{name}>"
44
+ else
45
+ "#{start_tag}#{"/" if options.xhtml}>"
46
+ end
47
+ end
48
+
49
+ def tag_options(options)
50
+ unless options.empty?
51
+ attrs = []
52
+ attrs = options.map { |key, value| %(#{key}="#{Rack::Utils.escape_html(value)}") }
53
+ " #{attrs.sort * ' '}" unless attrs.empty?
54
+ end
55
+ end
56
+
57
+ def stylesheet_tag(source, options = {})
58
+ tag("link", { :type => "text/css",
52
59
  :charset => "utf-8", :media => "screen", :rel => "stylesheet",
53
- :href => url_for(source) }.merge(options), closed)
54
- end
55
-
56
- def javascript_tag(source, options)
57
- tag("script", { :type => "text/javascript", :charset => "utf-8",
58
- :src => url_for(source) }.merge(options), false) + "</script>"
59
- end
60
-
61
- def extract_options(a)
62
- opts = a.last.is_a?(::Hash) ? a.pop : {}
63
- [a, opts]
60
+ :href => url_for(source) }.merge(options))
61
+ end
62
+
63
+ def javascript_tag(source, options = {})
64
+ tag("script", { :type => "text/javascript", :charset => "utf-8",
65
+ :src => url_for(source) }.merge(options)) do
66
+ end
67
+ end
68
+
69
+ def extract_options(a)
70
+ opts = a.last.is_a?(::Hash) ? a.pop : {}
71
+ [a, opts]
72
+ end
73
+
64
74
  end
65
75
 
76
+ def self.registered(app)
77
+ app.helpers StaticAssets::Helpers
78
+ app.disable :xhtml
79
+ end
66
80
  end
67
81
 
68
- helpers StaticAssets
82
+ register StaticAssets
69
83
  end
@@ -0,0 +1,26 @@
1
+ path = File.expand_path("../lib" + File.dirname(__FILE__))
2
+ $:.unshift(path) unless $:.include?(path)
3
+
4
+ require 'rubygems'
5
+ require 'sinatra/base'
6
+
7
+ require 'sinatra/static_assets'
8
+
9
+ module Sinatra
10
+ class XHTML < Sinatra::Base
11
+ helpers Sinatra::UrlForHelper
12
+ register Sinatra::StaticAssets
13
+
14
+ enable :xhtml
15
+
16
+ get '/image_tag' do
17
+ content_type "text/plain"
18
+ "#{image_tag("/images/foo.jpg")}"
19
+ end
20
+
21
+ get '/link_tag' do
22
+ content_type "text/plain"
23
+ "#{stylesheet_link_tag("/stylesheets/winter.css")}"
24
+ end
25
+ end
26
+ end
@@ -50,7 +50,7 @@ EOD
50
50
  get '/link_to_tag', {}, 'SCRIPT_NAME' => '/bar'
51
51
  assert last_response.ok?
52
52
  assert_equal last_response.body, <<EOD
53
- <a href='/bar/topr'>Tatry Mountains Rescue Team</a>
53
+ <a href="/bar/topr">Tatry Mountains Rescue Team</a>
54
54
  EOD
55
55
  end
56
56
 
@@ -0,0 +1,25 @@
1
+ require 'sinatra_baseapp'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+
5
+ class SintraStaticAssetsXHTMLTest < Test::Unit::TestCase
6
+ include Rack::Test::Methods
7
+
8
+ def app
9
+ Sinatra::XHTML.new
10
+ end
11
+
12
+ def test_image_tag_closes
13
+ get '/image_tag'
14
+ assert last_response.ok?
15
+ assert_equal last_response.body, "<img src=\"/images/foo.jpg\"/>"
16
+ end
17
+
18
+ def test_link_tag_closes
19
+ get '/link_tag'
20
+ assert last_response.ok?
21
+ assert_equal last_response.body,
22
+ "<link charset=\"utf-8\" href=\"/stylesheets/winter.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\"/>"
23
+ end
24
+
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-static-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wlodek Bzyl
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-19 00:00:00 +02:00
12
+ date: 2009-11-23 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -108,7 +108,9 @@ files:
108
108
  - examples/winter/winter.rb
109
109
  - lib/sinatra/static_assets.rb
110
110
  - test/sinatra_app.rb
111
+ - test/sinatra_baseapp.rb
111
112
  - test/sinatra_static_assets_test.rb
113
+ - test/sinatra_static_assets_xhtml_test.rb
112
114
  - LICENSE
113
115
  - README.markdown
114
116
  has_rdoc: true
@@ -140,9 +142,11 @@ signing_key:
140
142
  specification_version: 3
141
143
  summary: Sinatra extension providing helper methods to output tags for static assetgemspec.
142
144
  test_files:
143
- - test/sinatra_static_assets_test.rb
145
+ - test/sinatra_static_assets_xhtml_test.rb
144
146
  - test/sinatra_app.rb
147
+ - test/sinatra_static_assets_test.rb
148
+ - test/sinatra_baseapp.rb
145
149
  - examples/summer/summer.rb
146
- - examples/rsummer/summer.rb
147
150
  - examples/rwinter/winter.rb
148
151
  - examples/winter/winter.rb
152
+ - examples/rsummer/summer.rb