toolmantim-fancyviews 1.2
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.
- data/Rakefile +14 -0
- data/fancyviews.gemspec +19 -0
- data/lib/sinatra/fancyviews.rb +115 -0
- data/test/test.rb +48 -0
- metadata +76 -0
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
task :default => :test
|
2
|
+
|
3
|
+
desc "Run example test app"
|
4
|
+
task :test do
|
5
|
+
dir = File.dirname(__FILE__)
|
6
|
+
system "cd #{dir} && ruby test/test.rb -p 2222"
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
gem "sr-mg", "0.0.2"
|
11
|
+
require "mg"
|
12
|
+
MG.new("fancyviews.gemspec")
|
13
|
+
rescue LoadError
|
14
|
+
end
|
data/fancyviews.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "fancyviews"
|
3
|
+
s.rubyforge_project = 'fancyviews'
|
4
|
+
s.version = "1.2"
|
5
|
+
s.summary = "Fancy Views"
|
6
|
+
s.description = "Fancy Views"
|
7
|
+
s.email = "myles@myles.id.au"
|
8
|
+
s.authors = ["Myles Byrne"]
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.require_paths = ["lib"]
|
11
|
+
s.files = %w[
|
12
|
+
Rakefile
|
13
|
+
fancyviews.gemspec
|
14
|
+
lib/sinatra/fancyviews.rb
|
15
|
+
test/test.rb
|
16
|
+
]
|
17
|
+
s.add_dependency("sinatra", [">= 0.9.1.1"])
|
18
|
+
s.add_dependency("haml", [">= 2.2"])
|
19
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'haml'
|
3
|
+
require 'sass'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
|
7
|
+
module StyleFilter
|
8
|
+
include Haml::Filters::Base
|
9
|
+
|
10
|
+
def compile(precompiler, text)
|
11
|
+
precompiler.send :push_silent, %(fancyviews.include_style(fancyviews.view_name, #{text.inspect}))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
Haml::Filters.defined['style'] = StyleFilter
|
15
|
+
|
16
|
+
module ScriptFilter
|
17
|
+
include Haml::Filters::Base
|
18
|
+
|
19
|
+
def compile(precompiler, text)
|
20
|
+
precompiler.send :push_silent, %(fancyviews.include_script(fancyviews.view_name, #{text.inspect}))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
Haml::Filters.defined['script'] = ScriptFilter
|
24
|
+
|
25
|
+
class FancyViews
|
26
|
+
|
27
|
+
# current view
|
28
|
+
attr_accessor :view_name
|
29
|
+
|
30
|
+
def initialize(app)
|
31
|
+
@app = app
|
32
|
+
end
|
33
|
+
|
34
|
+
def include_style(name, sass)
|
35
|
+
included_styles << [name, sass] unless included_styles.detect { |s| s == [name, sass] }
|
36
|
+
end
|
37
|
+
|
38
|
+
def included_styles
|
39
|
+
@included_styles ||= []
|
40
|
+
end
|
41
|
+
|
42
|
+
def include_script(name, js)
|
43
|
+
included_scripts << [name, js] unless included_scripts.detect { |s| s == [name, js] }
|
44
|
+
end
|
45
|
+
|
46
|
+
def included_scripts
|
47
|
+
@included_scripts ||= []
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
module FancyHelpers
|
53
|
+
|
54
|
+
# use this instead of `haml` when rendering a template, unlike the haml
|
55
|
+
# method, layout is false by default
|
56
|
+
def view(name, options = {})
|
57
|
+
parent_view = fancyviews.view_name.to_s
|
58
|
+
fancyviews.view_name = name.to_s
|
59
|
+
options[:layout] = false unless options.has_key?(:layout)
|
60
|
+
rendered = haml(name, options)
|
61
|
+
fancyviews.view_name = parent_view
|
62
|
+
rendered
|
63
|
+
end
|
64
|
+
|
65
|
+
# same as `view` but has layout by default
|
66
|
+
def page(name, options = {})
|
67
|
+
options[:layout] = true unless options.has_key?(:layout)
|
68
|
+
view(name, options)
|
69
|
+
end
|
70
|
+
|
71
|
+
# renders all the styles captured by the :style filter
|
72
|
+
def styles(options = {})
|
73
|
+
imported = options.has_key?(:import) ?
|
74
|
+
File.read("#{self.options.views}/#{options[:import]}.sass") : ''
|
75
|
+
|
76
|
+
rendered_styles = fancyviews.included_styles.map do |name, sass|
|
77
|
+
# would be nice if construction took an :offest to go along with the :filename
|
78
|
+
eng = Sass::Engine.new(imported + "\n" + sass, :attribute_syntax => :normal)
|
79
|
+
"\n/* -- #{name} -- */\n" + eng.render
|
80
|
+
end.join
|
81
|
+
|
82
|
+
style_tag(rendered_styles, options[:media])
|
83
|
+
end
|
84
|
+
|
85
|
+
def style_tag(styles, media=nil)
|
86
|
+
capture_haml do
|
87
|
+
haml_tag(:style, styles, :type => ("text/css" if haml_format != :html5), :media => media)
|
88
|
+
end.strip
|
89
|
+
end
|
90
|
+
|
91
|
+
# renders all the scripts captured by the :script filter
|
92
|
+
def scripts
|
93
|
+
script_tag(fancyviews.included_scripts.map do |name, js|
|
94
|
+
"\n/* -- #{name} -- */\n" + js
|
95
|
+
end.join)
|
96
|
+
end
|
97
|
+
|
98
|
+
def script_tag(scripts)
|
99
|
+
capture_haml do
|
100
|
+
haml_tag(:script, scripts, :type => ("text/javascript" if haml_format != :html5))
|
101
|
+
end.strip
|
102
|
+
end
|
103
|
+
|
104
|
+
def haml_format
|
105
|
+
(options.haml && options.haml[:format]) || :xhtml
|
106
|
+
end
|
107
|
+
|
108
|
+
def fancyviews
|
109
|
+
@fancyviews ||= FancyViews.new(self)
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
helpers FancyHelpers
|
115
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sinatra'
|
3
|
+
require File.dirname(__FILE__) + '/../lib/sinatra/fancyviews'
|
4
|
+
|
5
|
+
set :haml, :format => :html5
|
6
|
+
|
7
|
+
get('/') { page :home }
|
8
|
+
|
9
|
+
__END__
|
10
|
+
|
11
|
+
@@layout
|
12
|
+
!!!
|
13
|
+
%head
|
14
|
+
%title FancyTest
|
15
|
+
= styles
|
16
|
+
%body
|
17
|
+
= yield
|
18
|
+
= scripts
|
19
|
+
|
20
|
+
@@home
|
21
|
+
:style
|
22
|
+
#fancydiv
|
23
|
+
:border 10px solid goldenrod
|
24
|
+
:position fixed
|
25
|
+
:top 20%
|
26
|
+
:left 50%
|
27
|
+
:width 600px
|
28
|
+
:margin-left -300px
|
29
|
+
:cursor pointer
|
30
|
+
|
31
|
+
#fancydiv= view :some_partial
|
32
|
+
|
33
|
+
|
34
|
+
@@some_partial
|
35
|
+
:style
|
36
|
+
#fancy
|
37
|
+
:font-family Helvetica
|
38
|
+
:font-size 200px
|
39
|
+
:font-weight bold
|
40
|
+
:color black
|
41
|
+
:margin 0
|
42
|
+
:padding 10px
|
43
|
+
:letter-spacing -4px
|
44
|
+
|
45
|
+
:script
|
46
|
+
document.getElementById('fancy').onclick = function() { alert('Fancy click') }
|
47
|
+
|
48
|
+
%h1#fancy Oooh! Fancy
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toolmantim-fancyviews
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Myles Byrne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sinatra
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.1.1
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: haml
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "2.2"
|
34
|
+
version:
|
35
|
+
description: Fancy Views
|
36
|
+
email: myles@myles.id.au
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- Rakefile
|
45
|
+
- fancyviews.gemspec
|
46
|
+
- lib/sinatra/fancyviews.rb
|
47
|
+
- test/test.rb
|
48
|
+
has_rdoc: false
|
49
|
+
homepage:
|
50
|
+
licenses:
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: fancyviews
|
71
|
+
rubygems_version: 1.3.5
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: Fancy Views
|
75
|
+
test_files: []
|
76
|
+
|