markup_helpers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/markup_helpers.rb +42 -0
- data/lib/markup_helpers/haml.rb +3 -0
- data/lib/markup_helpers/tilt.rb +40 -0
- data/lib/markup_helpers/version.rb +3 -0
- data/markup_helpers.gemspec +28 -0
- data/spec/markup_helpers_spec.rb +140 -0
- data/spec/spec_helper.rb +9 -0
- metadata +136 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "markup_helpers/version"
|
2
|
+
require "markup_helpers/tilt" if Object.const_defined? :Tilt
|
3
|
+
|
4
|
+
module MarkupHelpers
|
5
|
+
include Haml::Helpers if const_defined? :Haml
|
6
|
+
|
7
|
+
def tag tag_name, content_or_attrs = {}, attrs = nil, &block
|
8
|
+
if attrs.nil? && Hash === content_or_attrs
|
9
|
+
attrs = content_or_attrs
|
10
|
+
content = nil
|
11
|
+
else
|
12
|
+
attrs ||= {}
|
13
|
+
content = content_or_attrs
|
14
|
+
end
|
15
|
+
|
16
|
+
attrs = attrs.map do |name, val|
|
17
|
+
next if val.nil? || val == []
|
18
|
+
val == true ? name : %{#{name}="#{[*val].join(' ')}"}
|
19
|
+
end.compact.join(' ')
|
20
|
+
|
21
|
+
attrs = " #{attrs}" unless attrs.empty?
|
22
|
+
if block_given?
|
23
|
+
content = respond_to?(:capture) ? capture(&block) : yield
|
24
|
+
end
|
25
|
+
output = %{<#{tag_name}#{attrs}>#{content}</#{tag_name}>}
|
26
|
+
block_given? && respond_to?(:concat) ? concat(output) : output
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def erb_capture
|
31
|
+
old_buffer = erb_buffer.dup
|
32
|
+
erb_buffer.clear and yield
|
33
|
+
content = erb_buffer.dup
|
34
|
+
erb_buffer.replace(old_buffer) and content
|
35
|
+
end
|
36
|
+
|
37
|
+
def erb_buffer
|
38
|
+
outvar = @__tilt_context.instance_variable_get(:@outvar)
|
39
|
+
outvar or raise('Please pass the outvar option when instantiating the erb template')
|
40
|
+
instance_variable_get outvar
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Tilt
|
2
|
+
class Template
|
3
|
+
def render scope = Object.new, locals = {}, &block
|
4
|
+
scope.instance_variable_set :@__tilt_context, self
|
5
|
+
output = evaluate scope, locals || {}, &block
|
6
|
+
if scope.instance_variable_get :@__tilt_context
|
7
|
+
scope.send :remove_instance_variable, :@__tilt_context
|
8
|
+
end
|
9
|
+
output
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module MarkupHelpers
|
15
|
+
module TiltHelpers
|
16
|
+
def capture &block
|
17
|
+
case @__tilt_context
|
18
|
+
when Tilt::ErubisTemplate, Tilt::ERBTemplate
|
19
|
+
erb_capture &block
|
20
|
+
when Tilt::HamlTemplate
|
21
|
+
capture_haml &block
|
22
|
+
else
|
23
|
+
yield
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def concat content
|
28
|
+
case @__tilt_context
|
29
|
+
when Tilt::ErubisTemplate, Tilt::ERBTemplate
|
30
|
+
erb_buffer << content
|
31
|
+
when Tilt::HamlTemplate
|
32
|
+
haml_concat content
|
33
|
+
else
|
34
|
+
content
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
include TiltHelpers
|
40
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "markup_helpers/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "markup_helpers"
|
7
|
+
s.version = MarkupHelpers::VERSION
|
8
|
+
s.authors = ["Macario"]
|
9
|
+
s.email = ["macarui@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Framework agnostic markup helpers}
|
12
|
+
s.description = %q{Framework agnostic markup helpers}
|
13
|
+
|
14
|
+
s.rubyforge_project = "markup_helpers"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency 'rake'
|
22
|
+
s.add_development_dependency 'rspec'
|
23
|
+
s.add_development_dependency 'capybara'
|
24
|
+
s.add_development_dependency 'erubis'
|
25
|
+
s.add_development_dependency 'haml'
|
26
|
+
s.add_development_dependency 'tilt'
|
27
|
+
s.add_development_dependency 'sinatra'
|
28
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'markup helpers' do
|
5
|
+
include MarkupHelpers
|
6
|
+
|
7
|
+
let(:output) do
|
8
|
+
Capybara::Node::Simple.new(@output)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'tag' do
|
12
|
+
describe 'basic' do
|
13
|
+
describe 'attributes and content' do
|
14
|
+
before do
|
15
|
+
@output = tag(:li, "Hello", :class => 'item', :id => 'hello')
|
16
|
+
end
|
17
|
+
it { output.should have_css 'li' }
|
18
|
+
it { output.should have_css 'li', :text => "Hello" }
|
19
|
+
it { output.should have_css 'li.item' }
|
20
|
+
it { output.should have_css 'li#hello' }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'blank attribute values' do
|
24
|
+
before do
|
25
|
+
@output = tag(:li, "Hello", :class => [], :id => nil)
|
26
|
+
end
|
27
|
+
it { @output.should == '<li>Hello</li>'}
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'true attribute' do
|
31
|
+
before do
|
32
|
+
@output = tag(:li, "Hello", 'data-something' => true)
|
33
|
+
end
|
34
|
+
it { @output.should == '<li data-something>Hello</li>'}
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'multiple classes' do
|
38
|
+
before do
|
39
|
+
@output = tag(:li, "Hello", :class => %w(item in-stock))
|
40
|
+
end
|
41
|
+
it { output.should have_css 'li.item' }
|
42
|
+
it { output.should have_css 'li.in-stock' }
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'in erb' do
|
46
|
+
before do
|
47
|
+
@output = Tilt['erb'].new { '<%= tag(:li, "Hello") %>' }.render(self)
|
48
|
+
end
|
49
|
+
it { output.should have_css 'li', :text => "Hello" }
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'in haml' do
|
53
|
+
before do
|
54
|
+
@output = Tilt['haml'].new { '= tag(:li, "Hello")' }.render(self)
|
55
|
+
end
|
56
|
+
it { output.should have_css 'li', :text => "Hello" }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'shallow blocks' do
|
61
|
+
describe 'in ruby' do
|
62
|
+
before do
|
63
|
+
@output = tag(:li) { tag(:a, 'Hello') }
|
64
|
+
end
|
65
|
+
it { output.should have_css 'li' }
|
66
|
+
it { output.should have_css 'li > a', :text => 'Hello' }
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'in erb' do
|
70
|
+
before do
|
71
|
+
@output = Tilt['erb'].new(:outvar => '@_out_buf') do
|
72
|
+
<<-ERB
|
73
|
+
<% tag(:li) do %>
|
74
|
+
<%= tag(:a, 'Hello') %>
|
75
|
+
<% end %>
|
76
|
+
ERB
|
77
|
+
end.render(self)
|
78
|
+
end
|
79
|
+
it { output.should have_css 'li' }
|
80
|
+
it { output.should have_css 'li > a', :text => 'Hello' }
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'in haml' do
|
84
|
+
before do
|
85
|
+
@output = Tilt['haml'].new(:outvar => '@_out_buf') do
|
86
|
+
<<-ERB
|
87
|
+
- tag(:li) do
|
88
|
+
= tag(:a, 'Hello')
|
89
|
+
ERB
|
90
|
+
end.render(self)
|
91
|
+
end
|
92
|
+
it { output.should have_css 'li' }
|
93
|
+
it { output.should have_css 'li > a', :text => 'Hello' }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe 'deeper blocks' do
|
98
|
+
describe 'in ruby' do
|
99
|
+
before do
|
100
|
+
@output = tag(:li) { tag(:a, 'Hello') { tag(:img) } }
|
101
|
+
end
|
102
|
+
it { output.should have_css 'li' }
|
103
|
+
it { output.should have_css 'li > a' }
|
104
|
+
it { output.should have_css 'li > a > img' }
|
105
|
+
end
|
106
|
+
|
107
|
+
describe 'in erb' do
|
108
|
+
before do
|
109
|
+
@output = Tilt['erb'].new(:outvar => '@_out_buf') do
|
110
|
+
<<-ERB
|
111
|
+
<% tag(:li) do %>
|
112
|
+
<% tag(:a, 'Hello') do %>
|
113
|
+
<%= tag(:img) %>
|
114
|
+
<% end %>
|
115
|
+
<% end %>
|
116
|
+
ERB
|
117
|
+
end.render(self)
|
118
|
+
end
|
119
|
+
it { output.should have_css 'li' }
|
120
|
+
it { output.should have_css 'li > a' }
|
121
|
+
it { output.should have_css 'li > a > img' }
|
122
|
+
end
|
123
|
+
|
124
|
+
describe 'in haml' do
|
125
|
+
before do
|
126
|
+
@output = Tilt['haml'].new(:outvar => '@_out_buf') do
|
127
|
+
<<-ERB
|
128
|
+
- tag(:li) do
|
129
|
+
- tag(:a, 'Hello') do
|
130
|
+
= tag(:img)
|
131
|
+
ERB
|
132
|
+
end.render(self)
|
133
|
+
end
|
134
|
+
it { output.should have_css 'li' }
|
135
|
+
it { output.should have_css 'li > a' }
|
136
|
+
it { output.should have_css 'li > a > img' }
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markup_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Macario
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-28 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70284969005480 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70284969005480
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70284969004580 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70284969004580
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: capybara
|
38
|
+
requirement: &70284968994120 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70284968994120
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: erubis
|
49
|
+
requirement: &70284968993540 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70284968993540
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: haml
|
60
|
+
requirement: &70284968993080 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70284968993080
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: tilt
|
71
|
+
requirement: &70284968992420 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70284968992420
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: sinatra
|
82
|
+
requirement: &70284968991900 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70284968991900
|
91
|
+
description: Framework agnostic markup helpers
|
92
|
+
email:
|
93
|
+
- macarui@gmail.com
|
94
|
+
executables: []
|
95
|
+
extensions: []
|
96
|
+
extra_rdoc_files: []
|
97
|
+
files:
|
98
|
+
- .gitignore
|
99
|
+
- .rspec
|
100
|
+
- Gemfile
|
101
|
+
- Rakefile
|
102
|
+
- lib/markup_helpers.rb
|
103
|
+
- lib/markup_helpers/haml.rb
|
104
|
+
- lib/markup_helpers/tilt.rb
|
105
|
+
- lib/markup_helpers/version.rb
|
106
|
+
- markup_helpers.gemspec
|
107
|
+
- spec/markup_helpers_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
homepage: ''
|
110
|
+
licenses: []
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project: markup_helpers
|
129
|
+
rubygems_version: 1.8.12
|
130
|
+
signing_key:
|
131
|
+
specification_version: 3
|
132
|
+
summary: Framework agnostic markup helpers
|
133
|
+
test_files:
|
134
|
+
- spec/markup_helpers_spec.rb
|
135
|
+
- spec/spec_helper.rb
|
136
|
+
has_rdoc:
|