markaby 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aed95364f1421acaab2191897e35fb1a278051cd
4
- data.tar.gz: 605b3d61e47bdc08256f2034fd5717ab080585f7
3
+ metadata.gz: 455850946266c24fdba0054e72e738909a92f031
4
+ data.tar.gz: fb9ea63ab1b9d64366d7af5deda7c4af1c09ec2a
5
5
  SHA512:
6
- metadata.gz: 93daf68ab25910cffadcd442f9dd0e05d9e2afb245f16f308776e27e0876d54a9068624d065cf52f03798925aafb4626c14f7ef51d7970d038f4c9a8a2c6e778
7
- data.tar.gz: 824f9a360031c25711dadad5bd32a0f84ec12533c2d88bcb57835a9c8ef32804a7a097a338987f48e2c2bd46e029c5830d5195d0fb754a87248cc0bcf55a5e50
6
+ metadata.gz: 89dd42bd927211894995fc1fd98633345d140d047e379fb647f8c84c88a8cda28fee5677c7fe0283691045659ed8f96a982987e512828f1d2f97fa7fa4f42616
7
+ data.tar.gz: 4dcbdfcaa7c11683e572595f24f2cd9267c09d1280a97f38151abaa1e2fdb6bdfc53ae77c04e9cd4a67e8692476a22af11436b9cdb7992136f5bf6d33db886c2
data/.gitignore CHANGED
@@ -18,4 +18,5 @@ rdoc
18
18
  spec/reports
19
19
  test/tmp
20
20
  test/version_tmp
21
- tmp
21
+ tmp
22
+ .byebug_history
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 1.8
5
+ - 1.9
6
+ - 2.0
7
+ - 2.1
8
+ - 2.2
9
+ - 2.3
10
+ - 2.4
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: 2.5
14
+ - rvm: jruby-9.1.15.0
15
+ - rvm: rbx-2
16
+ install: bundle install --without=development --jobs=3 --retry=3
data/Gemfile CHANGED
@@ -5,4 +5,8 @@ gem 'builder'
5
5
  group :test do
6
6
  gem 'rake'
7
7
  gem 'rspec'
8
- end
8
+ gem 'test-unit', :platform => [:mri_22, :mri_23, :mri_24, :mri_25]
9
+ gem 'byebug', :platform => [:mri_20, :mri_21, :mri_22, :mri_23, :mri_24, :mri_25]
10
+ gem 'ruby-debug', :platform => [:mri_18]
11
+ gem 'ruby-debug19', :platform => [:mri_19]
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2006-2018 markaby group (Scott Taylor (smtlaissezfaire), Jonathan Gillette (_why), and related maintainers)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'markaby/version'
4
+ require 'markaby'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "markaby"
@@ -5,35 +5,84 @@ It is an alternative to ERb which weaves the two languages together.
5
5
  Also a replacement for templating languages which use primitive languages
6
6
  that blend with HTML.
7
7
 
8
- === Install it as a gem
8
+ == Install it
9
9
 
10
- gem install markaby
10
+ Just do what everyone else does:
11
11
 
12
- == Using Markaby with Sinatra (1.0+)
12
+ # in Gemfile:
13
+ gem 'markaby'
13
14
 
14
- get '/foo' do
15
- mab :my_template # my_template.mab in the sinatra view path
16
- end
15
+ then bundle install:
17
16
 
18
- If you are looking for sinatra support pre 0.7, see http://github.com/sbfaulkner/sinatra-markaby
17
+ bundle install
18
+
19
+ or gem install if you are a weirdo not using bundler.
20
+
21
+ === Using Markaby with Rails 4/5+:
22
+
23
+ Install the gem (using bundler), then:
24
+
25
+ # in config/initializers/markaby.rb:
26
+
27
+ require 'markaby/rails'
19
28
 
20
- A note on Tilt - using markaby with html5 doesn't quite yet work properly. If you'd like to render a template with html 5, call this at the start of a .mab template:
29
+ Markaby::Rails::TemplateHandler.register!({
30
+ tagset: Markaby::HTML5,
31
+ indent: 2,
32
+ })
21
33
 
22
- enable_html5!
34
+ Note, Markaby hasn't been tested on anything below Rails 5. Please submit bugs
35
+ and/or patches if you'd like support for anything before rails 5.
23
36
 
24
- Or enable html 5 globally:
37
+ Name your templates with an html.mab extension. You'll also want to configure
38
+ your text editor to see .mab as ruby.
25
39
 
26
- Markaby::Builder.class_eval do
27
- @@options = Markaby::Builder::HTML5_OPTIONS
28
- end
40
+ Here's how you'd do that for Atom:
29
41
 
30
- == Using Markaby with other frameworks
42
+ 1. Install the file-types module:
43
+
44
+ apm install file-types
45
+
46
+ 1. in your config: Atom -> Config:
47
+
48
+ "*":
49
+ "file-types":
50
+ "\\.mab$": "source.ruby"
51
+
52
+ Now that's some chunky bacon!
53
+
54
+ === Using Markaby in helpers:
55
+
56
+ Just call Markaby::Builder with a block as below.
57
+
58
+ You can also require 'markaby/kernel_method' to make it even easier:
59
+
60
+ # my_helper.rb:
61
+ require 'markaby/kernel_method' # or put this in an initializer
62
+
63
+ module MyHelper
64
+ # note - you can also use Markaby::Builder.new { }.to_s
65
+ def chunky_bacon
66
+ mab do
67
+ p "Chunky Bacon!"
68
+ end
69
+ end
70
+ end
71
+
72
+ === Using Markaby with Sinatra (1.0+)
73
+
74
+ get '/foo' do
75
+ mab :my_template # my_template.mab in the sinatra view path
76
+ end
77
+
78
+ If you are looking for sinatra support pre 0.7, see http://github.com/sbfaulkner/sinatra-markaby
79
+
80
+ === Using Markaby with other frameworks
31
81
 
32
82
  Tilt has a Markaby module, so in principle, any web framework that supports
33
83
  Tilt will also support Markaby. See the appropriate tilt documentation:
34
84
 
35
- http://github.com/rtomayko/tilt
36
-
85
+ http://github.com/rtomayko/tilt
37
86
 
38
87
  == Using Markaby as a Ruby class
39
88
 
@@ -220,11 +269,26 @@ won't work with this technique.
220
269
  end
221
270
  end
222
271
 
272
+ == Some other notes, so you aren't confused:
273
+
274
+ === On using different tagsets:
275
+
276
+ Because of the ways various frameworks sub-render templates, to use a different
277
+ tagset in a rendered sub template, you may need to set it at the top of your
278
+ sub-template:
279
+
280
+ self.tagset = Markaby::HTML5
281
+ # or Markaby::Transitional, Markaby::XHTMLStrict, Markaby::XHTMLFrameset
282
+
283
+ Note, this is only necessary if you were rendering, say, a one off page as
284
+ html transitional but had the default engine as html5.
285
+
286
+
223
287
  = Credits
224
288
 
225
289
  Markaby is a work of immense hope by Tim Fletcher and why the lucky stiff.
226
290
  It is maintained by joho, spox, and smtlaissezfaire.
227
- Thankyou for giving it a whirl.
291
+ Thank you for giving it a whirl.
228
292
 
229
293
  Markaby is inspired by the HTML library within cgi.rb. Hopefully it will
230
294
  turn around and take some cues.
@@ -17,10 +17,15 @@ $:.unshift File.expand_path(File.dirname(__FILE__))
17
17
  # * Markaby::Tags: lists the roles of various XHTML tags to help Builder
18
18
  # use these tags as they are intended.
19
19
  module Markaby
20
+ MAJOR = 0
21
+ MINOR = 9
22
+ TINY = 0
23
+
24
+ VERSION = "#{MAJOR}.#{MINOR}.#{TINY}"
25
+
20
26
  class InvalidXhtmlError < StandardError; end
21
27
  end
22
28
 
23
- require "markaby/version"
24
29
  require 'builder' unless defined?(Builder)
25
30
  require 'markaby/builder'
26
31
  require 'markaby/cssproxy'
@@ -24,29 +24,13 @@ module Markaby
24
24
  class Builder
25
25
  include Markaby::BuilderTags
26
26
 
27
- DEFAULT_OPTIONS = {
27
+ GENERIC_OPTIONS = {
28
28
  :indent => 0,
29
- :output_helpers => true,
30
- :output_xml_instruction => true,
31
- :output_meta_tag => 'xhtml',
32
29
  :auto_validation => true,
33
- :tagset => Markaby::XHTMLTransitional,
34
- :root_attributes => {
35
- :xmlns => 'http://www.w3.org/1999/xhtml',
36
- :'xml:lang' => 'en',
37
- :lang => 'en'
38
- }
39
30
  }
40
31
 
41
- HTML5_OPTIONS = {
42
- :indent => 0,
43
- :output_helpers => true,
44
- :output_xml_instruction => false,
45
- :output_meta_tag => 'html5',
46
- :auto_validation => true,
47
- :tagset => Markaby::HTML5,
48
- :root_attributes => {}
49
- }
32
+ HTML5_OPTIONS = HTML5.default_options.dup
33
+ DEFAULT_OPTIONS = GENERIC_OPTIONS.merge(HTML5_OPTIONS)
50
34
 
51
35
  @@options = DEFAULT_OPTIONS.dup
52
36
 
@@ -54,10 +38,6 @@ module Markaby
54
38
  @@options = DEFAULT_OPTIONS.dup
55
39
  end
56
40
 
57
- def self.set_html5_options!
58
- @@options = HTML5_OPTIONS.dup
59
- end
60
-
61
41
  def self.set(option, value)
62
42
  @@options[option] = value
63
43
  end
@@ -66,15 +46,15 @@ module Markaby
66
46
  @@options[option]
67
47
  end
68
48
 
69
- def self.ignored_helpers
70
- @@ignored_helpers ||= []
71
- end
49
+ attr_reader :tagset
72
50
 
73
- def self.ignore_helpers(*helpers)
74
- ignored_helpers.concat helpers
75
- end
51
+ def tagset=(tagset)
52
+ @tagset = tagset
76
53
 
77
- attr_accessor :output_helpers, :tagset
54
+ tagset.default_options.each do |k, v|
55
+ self.instance_variable_set("@#{k}".to_sym, v)
56
+ end
57
+ end
78
58
 
79
59
  # Create a Markaby builder object. Pass in a hash of variable assignments to
80
60
  # +assigns+ which will be available as instance variables inside tag construction
@@ -171,6 +151,8 @@ module Markaby
171
151
  # the arguments are the same as the tags implemented via method_missing.
172
152
  def tag!(tag, *args, &block)
173
153
  ele_id = nil
154
+
155
+ # TODO: Move this logic to the tagset so that the tagset itself can validate + raise when invalid
174
156
  if @auto_validation && @tagset
175
157
  if !@tagset.tagset.has_key?(tag)
176
158
  raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
@@ -183,15 +165,19 @@ module Markaby
183
165
 
184
166
  attrs.each do |k, v|
185
167
  atname = k.to_s.downcase.intern
186
- unless k =~ /:/ or @tagset.tagset[tag].include? atname
168
+
169
+ unless k =~ /:/ or @tagset.tagset[tag].include?(atname) or (@tagset == Markaby::HTML5 && atname.to_s =~ /^data-/)
187
170
  raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
188
171
  end
172
+
189
173
  if atname == :id
190
174
  ele_id = v.to_s
175
+
191
176
  if @used_ids.has_key? ele_id
192
177
  raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
193
178
  end
194
179
  end
180
+
195
181
  if AttrsBoolean.include? atname
196
182
  if v
197
183
  attrs[k] = atname.to_s
@@ -229,13 +215,8 @@ module Markaby
229
215
  # method_missing used to be the lynchpin in Markaby, but it's no longer used to handle
230
216
  # HTML tags. See html_tag for that.
231
217
  def method_missing(sym, *args, &block)
232
- if @_helper.respond_to?(sym, true) && !self.class.ignored_helpers.include?(sym)
233
- r = @_helper.send(sym, *args, &block)
234
- if @output_helpers && r.respond_to?(:to_str)
235
- fragment { @builder << r }
236
- else
237
- r
238
- end
218
+ if @_helper.respond_to?(sym, true)
219
+ @_helper.send(sym, *args, &block)
239
220
  elsif @assigns.has_key?(sym)
240
221
  @assigns[sym]
241
222
  elsif @assigns.has_key?(stringy_key = sym.to_s)
@@ -56,16 +56,12 @@ module Markaby
56
56
 
57
57
  # Builds an html tag with HTML5 doctype instead.
58
58
  def html5(attrs = {}, &block)
59
- enable_html5!
59
+ self.tagset = Markaby::HTML5
60
60
  html5_html(attrs, &block)
61
61
  end
62
62
 
63
63
  def enable_html5!
64
- self.tagset = Markaby::HTML5
65
-
66
- Markaby::Builder::HTML5_OPTIONS.each do |k, v|
67
- self.instance_variable_set("@#{k}".to_sym, v)
68
- end
64
+ raise NotImplementedError, "Deprecated! Call self.tagset = Markaby::HTML5"
69
65
  end
70
66
 
71
67
  private
@@ -1,8 +1,9 @@
1
1
  # You'll need to <tt>require 'markaby/kernel_method'</tt> for this.
2
+ require 'markaby'
3
+
2
4
  module Kernel
3
5
  # Shortcut for creating a quick block of Markaby.
4
6
  def mab(*args, &block)
5
7
  Markaby::Builder.new(*args, &block).to_s
6
8
  end
7
9
  end
8
-
@@ -0,0 +1,36 @@
1
+ require 'markaby'
2
+
3
+ module Markaby
4
+ module Rails
5
+ class TemplateHandler
6
+ class << self
7
+ def register!(options={})
8
+ self.options = options
9
+ ActionView::Template.register_template_handler(:mab, new)
10
+ end
11
+
12
+ # TODO: Do we need this?
13
+ # Default format used by Markaby
14
+ # class_attribute :default_format
15
+ # self.default_format = :html
16
+
17
+ def options
18
+ @options ||= {}
19
+ end
20
+
21
+ def options=(val)
22
+ self.options.merge!(val)
23
+ self.options
24
+ end
25
+ end
26
+
27
+ def call(template)
28
+ <<-CODE
29
+ Markaby::Builder.new(Markaby::Rails::TemplateHandler.options, self) do
30
+ #{template.source}
31
+ end.to_s
32
+ CODE
33
+ end
34
+ end
35
+ end
36
+ end
@@ -29,12 +29,36 @@ module Markaby
29
29
  :compact, :declare, :noresize, :noshade, :nowrap # deprecated or unused
30
30
  ]
31
31
 
32
- # All the tags and attributes from XHTML 1.0 Strict
33
- class XHTMLStrict
32
+ class Tagset
34
33
  class << self
35
34
  attr_accessor :tags, :tagset, :forms, :self_closing, :doctype
35
+
36
+ def default_options
37
+ {
38
+ :tagset => self
39
+ }
40
+ end
36
41
  end
42
+ end
37
43
 
44
+ class XmlTagset < Tagset
45
+ class << self
46
+ def default_options
47
+ super.merge({
48
+ :output_xml_instruction => true,
49
+ :output_meta_tag => 'xhtml',
50
+ :root_attributes => {
51
+ :xmlns => 'http://www.w3.org/1999/xhtml',
52
+ :'xml:lang' => 'en',
53
+ :lang => 'en'
54
+ }
55
+ })
56
+ end
57
+ end
58
+ end
59
+
60
+ # All the tags and attributes from XHTML 1.0 Strict
61
+ class XHTMLStrict < XmlTagset
38
62
  @doctype = ['-//W3C//DTD XHTML 1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd']
39
63
  @tagset = {
40
64
  :html => AttrI18n + [:id, :xmlns],
@@ -122,11 +146,7 @@ module Markaby
122
146
  end
123
147
 
124
148
  # Additional tags found in XHTML 1.0 Transitional
125
- class XHTMLTransitional
126
- class << self
127
- attr_accessor :tags, :tagset, :forms, :self_closing, :doctype
128
- end
129
-
149
+ class XHTMLTransitional < XmlTagset
130
150
  @doctype = ['-//W3C//DTD XHTML 1.0 Transitional//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd']
131
151
  @tagset = XHTMLStrict.tagset.merge({
132
152
  :strike => Attrs,
@@ -184,11 +204,7 @@ module Markaby
184
204
  end
185
205
 
186
206
  # Additional tags found in XHTML 1.0 Frameset
187
- class XHTMLFrameset
188
- class << self
189
- attr_accessor :tags, :tagset, :forms, :self_closing, :doctype
190
- end
191
-
207
+ class XHTMLFrameset < XmlTagset
192
208
  @doctype = ['-//W3C//DTD XHTML 1.0 Frameset//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd']
193
209
  @tagset = XHTMLTransitional.tagset.merge({
194
210
  :frameset => AttrCore + [:rows, :cols, :onload, :onunload],
@@ -200,9 +216,15 @@ module Markaby
200
216
  @self_closing = @tags & SELF_CLOSING_TAGS
201
217
  end
202
218
 
203
- class HTML5
219
+ class HTML5 < Tagset
204
220
  class << self
205
- attr_accessor :tags, :tagset, :forms, :self_closing, :doctype
221
+ def default_options
222
+ super.merge({
223
+ :output_xml_instruction => false,
224
+ :output_meta_tag => 'html5',
225
+ :root_attributes => {}
226
+ })
227
+ end
206
228
  end
207
229
 
208
230
  @doctype = ['html']
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
2
 
3
- class FragmentTest < Test::Unit::TestCase
3
+ describe "Fragments" do
4
4
  it "should have method_missing as a private instance method" do
5
5
  private_methods = Markaby::Fragment.private_instance_methods.dup
6
6
  private_methods.map! { |m| m.to_sym }
@@ -28,7 +28,9 @@ describe Markaby do
28
28
 
29
29
  it "should put correct xhtml charset meta" do
30
30
  document = mab { xhtml_strict { head { title 'OKay' } } }
31
- document.should include('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>')
31
+ document.should include('<meta')
32
+ document.should include('http-equiv="Content-Type"')
33
+ document.should include('content="text/html; charset=utf-8"')
32
34
  end
33
35
 
34
36
  it "should put correct html5 charset meta" do
@@ -95,6 +97,19 @@ describe Markaby do
95
97
  end
96
98
  end
97
99
 
98
- doc.should == "<!DOCTYPE html><html><input type=\"text\" name=\"foo\" value=\"bar\" placeholder=\"something\"/></html>"
100
+ doc.should include('<input type="text" ')
101
+ doc.should include('name="foo"')
102
+ doc.should include('value="bar"')
103
+ doc.should include('placeholder="something"')
104
+ end
105
+
106
+ it "should allow data attributes anywhere" do
107
+ doc = mab do
108
+ html5 do
109
+ div('data-foo' => 'bar')
110
+ end
111
+ end
112
+
113
+ doc.should include('<div data-foo="bar"/>')
99
114
  end
100
115
  end
@@ -1,6 +1,17 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
2
 
3
3
  describe Markaby do
4
+ it "should work with classes and ids" do
5
+ mab { div.one '' }.should == %{<div class="one"></div>}
6
+ mab { div.one.two '' }.should == %{<div class="one two"></div>}
7
+ mab { div.three! '' }.should == %{<div id="three"></div>}
8
+ mab { hr.hidden }.should == %{<hr class="hidden"/>}
9
+
10
+ out = mab { input.foo :id => 'bar' }
11
+ out.should match("<input.*class=\"foo\".*/>")
12
+ out.should match("<input.*name=\"bar\".*/>")
13
+ end
14
+
4
15
  it "can assign helpers after instantiation" do
5
16
  helper = double 'helper', :foo => :bar
6
17
 
@@ -36,10 +47,6 @@ describe Markaby do
36
47
  builder.three.should == "four"
37
48
  end
38
49
 
39
- def test_builder_bang_methods
40
- assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", mab { instruct! }
41
- end
42
-
43
50
  it "should be able to produce the correct html from a fragment" do
44
51
  str = ""
45
52
  str += "<div>"
@@ -63,105 +70,79 @@ describe Markaby do
63
70
  generated.should == str
64
71
  end
65
72
 
66
- def test_fragments
67
- assert_equal %{<div><h1>Monkeys</h1><h2>Giraffes <strong>Miniature</strong></h2><h3>Donkeys</h3></div>},
68
- mab { div { h1 "Monkeys"; h2 { "Giraffes #{strong 'Miniature' }" }; h3 "Donkeys" } }
69
-
70
- assert_equal %{<div><h1>Monkeys</h1><h2>Giraffes <small>Miniature</small> and <strong>Large</strong></h2><h3>Donkeys</h3><h4>Parakeet <strong>Large</strong> as well...</h4></div>},
71
- mab { div { @a = small 'Miniature'; @b = strong 'Large'; h1 "Monkeys"; h2 { "Giraffes #{@a} and #{@b}" }; h3 "Donkeys"; h4 { "Parakeet #{@b} as well..." } } }
72
- end
73
-
74
- def test_invalid_xhtml
75
- assert_exception(NoMethodError, "undefined method `dav'") { dav {} }
76
- assert_exception(Markaby::InvalidXhtmlError, "no attribute `styl' on div elements") { div(:styl => 'ok') {} }
77
- assert_exception(Markaby::InvalidXhtmlError, "no attribute `class' on tbody elements") { tbody.okay {} }
78
- end
79
-
80
- def test_full_doc_transitional
81
- doc = mab { xhtml_transitional { head { title 'OKay' } } }
82
- assert doc =~ /^<\?xml version="1.0" encoding="UTF-8"\?>/
83
- assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">})
84
- assert doc.include?(%{<title>OKay</title>})
85
- end
86
-
87
- def test_full_doc_strict
88
- doc = mab { xhtml_strict { head { title 'OKay' } } }
89
- assert doc =~ /^<\?xml version="1.0" encoding="UTF-8"\?>/
90
- assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">})
91
- assert doc.include?(%{<title>OKay</title>})
92
- end
73
+ it "should copy instance variables from a helper object" do
74
+ klass = Class.new do
75
+ def initialize
76
+ @hello = "hello there"
77
+ end
78
+ end
93
79
 
94
- def test_full_doc_frameset
95
- doc = mab { xhtml_frameset { head { title 'OKay' } } }
96
- assert doc =~ /^<\?xml version="1.0" encoding="UTF-8"\?>/
97
- assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">})
98
- assert doc.include?(%{<title>OKay</title>})
80
+ builder = Markaby::Builder.new({}, klass.new)
81
+ builder.capture { @hello }.should == "hello there"
99
82
  end
100
83
 
101
- def test_root_attributes_can_be_changed
102
- doc = mab { xhtml_strict(:lang => 'fr') { head { title { 'Salut!' } } } }
103
- assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">})
104
- assert doc.include?(%{<title>Salut!</title>})
105
- assert doc.include?(%{ lang="fr"})
84
+ describe Markaby::InvalidXhtmlError do
85
+ it "should inherit from StandardError" do
86
+ Markaby::InvalidXhtmlError.superclass.should == StandardError
87
+ end
106
88
  end
107
89
 
108
- def version_file
109
- File.expand_path(File.dirname(__FILE__) + "/../../VERSION")
110
- end
90
+ it "can assign helpers after instantiation" do
91
+ helper = double 'helper', :foo => :bar
111
92
 
112
- def test_markaby_should_have_correct_version
113
- assert_equal Markaby::VERSION, File.read(version_file).strip
93
+ builder = Markaby::Builder.new
94
+ builder.helper = helper
95
+ builder.foo.should == :bar
114
96
  end
115
97
 
116
- def test_duplicate_usage_of_same_id
117
- assert_raises Markaby::InvalidXhtmlError do
118
- mab do
119
- p.one!
120
- p.one!
121
- end
122
- end
98
+ it "should be able to set a local" do
99
+ builder = Markaby::Builder.new
100
+ builder.locals = { :foo => "bar" }
101
+ builder.foo.should == "bar"
123
102
  end
124
103
 
125
- # auto validation
126
-
127
- def test_tagging_with_invalid_tag_should_raise_error
128
- assert_raises Markaby::InvalidXhtmlError do
129
- mab do
130
- tag! :an_invalid_tag
131
- end
132
- end
104
+ it "should be able to set a different local value" do
105
+ builder = Markaby::Builder.new
106
+ builder.locals = { :foo => "baz" }
107
+ builder.foo.should == "baz"
133
108
  end
134
109
 
135
- def test_self_closing_html_tag_with_block_throws_errors
136
- assert_raises Markaby::InvalidXhtmlError do
137
- mab do
138
- html_tag :img do
139
- end
140
- end
141
- end
110
+ it "should assign the correct key" do
111
+ builder = Markaby::Builder.new
112
+ builder.locals = { :key => :value }
113
+ builder.key.should == :value
142
114
  end
143
115
 
144
- def test_local_assigning
145
- builder = Markaby::Builder.new(:variable => :a_value)
116
+ it "should be able to assign multiple locals" do
117
+ builder = Markaby::Builder.new
146
118
 
147
- assert_equal :a_value, builder.variable
148
- end
119
+ builder.locals = { :one => "two", :three => "four" }
149
120
 
150
- def test_local_assignment_with_strings
151
- builder = Markaby::Builder.new("variable" => :a_value)
152
- assert_equal :a_value, builder.variable
121
+ builder.one.should == "two"
122
+ builder.three.should == "four"
153
123
  end
154
124
 
155
- def test_local_assignment_prefers_symbols_to_strings
156
- builder = Markaby::Builder.new("variable" => "string_value", :variable => :symbol_value)
157
- assert_equal :symbol_value, builder.variable
158
- end
125
+ it "should be able to produce the correct html from a fragment" do
126
+ str = ""
127
+ str += "<div>"
128
+ str += "<h1>Monkeys</h1>"
129
+ str += "<h2>Giraffes <small>Miniature</small> and <strong>Large</strong></h2>"
130
+ str += "<h3>Donkeys</h3>"
131
+ str += "<h4>Parakeet <b><i>Innocent IV</i></b> in Classic Chartreuse</h4>"
132
+ str += "</div>"
159
133
 
160
- def test_method_missing_should_call_tag_if_no_tagset_present
161
- Markaby::Builder.set(:tagset, nil)
134
+ generated = mab {
135
+ div {
136
+ h1 "Monkeys"
137
+ h2 {
138
+ "Giraffes #{small('Miniature')} and #{strong 'Large'}"
139
+ }
140
+ h3 "Donkeys"
141
+ h4 { "Parakeet #{b { i 'Innocent IV' }} in Classic Chartreuse" }
142
+ }
143
+ }
162
144
 
163
- builder = Markaby::Builder.new
164
- builder.something.should == "<something/>"
145
+ generated.should == str
165
146
  end
166
147
 
167
148
  it "should copy instance variables from a helper object" do
@@ -181,4 +162,3 @@ describe Markaby do
181
162
  end
182
163
  end
183
164
  end
184
-
@@ -1,6 +1,9 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+ require 'test/unit'
2
3
 
3
4
  class MarkabyTest < Test::Unit::TestCase
5
+ include TestHelpers
6
+
4
7
  def teardown
5
8
  Markaby::Builder.restore_defaults!
6
9
  end
@@ -12,17 +15,6 @@ class MarkabyTest < Test::Unit::TestCase
12
15
  assert_equal "<p>foo</p>", mab { p { 'foo' } }
13
16
  end
14
17
 
15
- def test_classes_and_ids
16
- assert_equal %{<div class="one"></div>}, mab { div.one '' }
17
- assert_equal %{<div class="one two"></div>}, mab { div.one.two '' }
18
- assert_equal %{<div id="three"></div>}, mab { div.three! '' }
19
- assert_equal %{<hr class="hidden"/>}, mab { hr.hidden }
20
-
21
- out = mab { input.foo :id => 'bar' }
22
- out.should match("<input.*class=\"foo\".*/>")
23
- out.should match("<input.*name=\"bar\".*/>")
24
- end
25
-
26
18
  def test_escaping
27
19
  assert_equal "<h1>Apples &amp; Oranges</h1>", mab { h1 'Apples & Oranges' }
28
20
  assert_equal "<h1>Apples & Oranges</h1>", mab { h1 { 'Apples & Oranges' } }
@@ -49,13 +41,10 @@ class MarkabyTest < Test::Unit::TestCase
49
41
  assert_equal "<h1>Hello World</h1>", mab { @message = 'Hello World'; h1 message }
50
42
  end
51
43
 
52
- def spec_helpers
53
- Markaby::Builder.ignored_helpers.clear
44
+ def test_helpers
54
45
  assert_equal %{squirrels}, mab({}, MarkabyTestHelpers) { pluralize('squirrel') }
55
46
  assert_equal %{<a href="">edit</a>}, mab({}, MarkabyTestHelpers) { link_to('edit') }
56
- assert mab({}, MarkabyTestHelpers) { @output_helpers = false; link_to('edit'); nil }.empty?
57
- Markaby::Builder.ignore_helpers :pluralize
58
- assert_exception(NoMethodError, "undefined method `pluralize'", {}, MarkabyTestHelpers) { pluralize('squirrel') }
47
+ assert mab({}, MarkabyTestHelpers) { link_to('edit'); nil }.empty?
59
48
  end
60
49
 
61
50
  def test_uses_helper_instance_variable
@@ -66,71 +55,11 @@ class MarkabyTest < Test::Unit::TestCase
66
55
  builder = Markaby::Builder.new({}, helper)
67
56
  assert_equal :ivar_value, builder.some_ivar
68
57
  end
69
- end
70
-
71
- describe Markaby do
72
- it "can assign helpers after instantiation" do
73
- helper = double 'helper', :foo => :bar
74
-
75
- builder = Markaby::Builder.new
76
- builder.helper = helper
77
- builder.foo.should == :bar
78
- end
79
-
80
- it "should be able to set a local" do
81
- builder = Markaby::Builder.new
82
- builder.locals = { :foo => "bar" }
83
- builder.foo.should == "bar"
84
- end
85
-
86
- it "should be able to set a different local value" do
87
- builder = Markaby::Builder.new
88
- builder.locals = { :foo => "baz" }
89
- builder.foo.should == "baz"
90
- end
91
-
92
- it "should assign the correct key" do
93
- builder = Markaby::Builder.new
94
- builder.locals = { :key => :value }
95
- builder.key.should == :value
96
- end
97
-
98
- it "should be able to assign multiple locals" do
99
- builder = Markaby::Builder.new
100
-
101
- builder.locals = { :one => "two", :three => "four" }
102
-
103
- builder.one.should == "two"
104
- builder.three.should == "four"
105
- end
106
58
 
107
59
  def test_builder_bang_methods
108
60
  assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", mab { instruct! }
109
61
  end
110
62
 
111
- it "should be able to produce the correct html from a fragment" do
112
- str = ""
113
- str += "<div>"
114
- str += "<h1>Monkeys</h1>"
115
- str += "<h2>Giraffes <small>Miniature</small> and <strong>Large</strong></h2>"
116
- str += "<h3>Donkeys</h3>"
117
- str += "<h4>Parakeet <b><i>Innocent IV</i></b> in Classic Chartreuse</h4>"
118
- str += "</div>"
119
-
120
- generated = mab {
121
- div {
122
- h1 "Monkeys"
123
- h2 {
124
- "Giraffes #{small('Miniature')} and #{strong 'Large'}"
125
- }
126
- h3 "Donkeys"
127
- h4 { "Parakeet #{b { i 'Innocent IV' }} in Classic Chartreuse" }
128
- }
129
- }
130
-
131
- generated.should == str
132
- end
133
-
134
63
  def test_fragments
135
64
  assert_equal %{<div><h1>Monkeys</h1><h2>Giraffes <strong>Miniature</strong></h2><h3>Donkeys</h3></div>},
136
65
  mab { div { h1 "Monkeys"; h2 { "Giraffes #{strong 'Miniature' }" }; h3 "Donkeys" } }
@@ -178,7 +107,7 @@ describe Markaby do
178
107
  end
179
108
 
180
109
  def test_markaby_should_have_correct_version
181
- assert_equal Markaby::VERSION, File.read(version_file).strip
110
+ assert_equal Markaby::VERSION, '0.9.0'
182
111
  end
183
112
 
184
113
  def test_duplicate_usage_of_same_id
@@ -231,21 +160,4 @@ describe Markaby do
231
160
  builder = Markaby::Builder.new
232
161
  builder.something.should == "<something/>"
233
162
  end
234
-
235
- it "should copy instance variables from a helper object" do
236
- klass = Class.new do
237
- def initialize
238
- @hello = "hello there"
239
- end
240
- end
241
-
242
- builder = Markaby::Builder.new({}, klass.new)
243
- builder.capture { @hello }.should == "hello there"
244
- end
245
-
246
- describe Markaby::InvalidXhtmlError do
247
- it "should inherit from StandardError" do
248
- Markaby::InvalidXhtmlError.superclass.should == StandardError
249
- end
250
- end
251
163
  end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ # stub
4
+ module ActionView
5
+ module Template
6
+ class << self
7
+ def register_template_handler(sym, handler)
8
+ @template_handlers ||= {}
9
+ @template_handlers[sym] = handler
10
+ end
11
+
12
+ def template_handlers
13
+ @template_handlers ||= {}
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ describe Markaby::Rails do
20
+ it "should register the template handler" do
21
+ Markaby::Rails::TemplateHandler.register!
22
+ ActionView::Template.template_handlers[:mab].should be_a_kind_of(Markaby::Rails::TemplateHandler)
23
+ end
24
+
25
+ it "should be able to pass options" do
26
+ Markaby::Rails::TemplateHandler.register!(:indent => 2)
27
+ Markaby::Rails::TemplateHandler.options[:indent].should == 2
28
+ end
29
+ end
@@ -4,14 +4,17 @@ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
5
  require 'markaby'
6
6
  require 'markaby/kernel_method'
7
+ require "markaby/rails"
7
8
 
8
9
  module MarkabyTestHelpers
9
10
  def link_to(obj)
10
11
  %{<a href="">#{obj}</a>}
11
12
  end
13
+
12
14
  def pluralize(string)
13
15
  string + "s"
14
16
  end
17
+
15
18
  module_function :link_to, :pluralize
16
19
  end
17
20
 
@@ -25,14 +28,3 @@ module TestHelpers
25
28
  end
26
29
  end
27
30
  end
28
-
29
- module Test
30
- module Unit
31
- class TestCase
32
- include TestHelpers
33
- def self.it(name, &blk)
34
- define_method("test_#{name}", &blk)
35
- end
36
- end
37
- end
38
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markaby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Taylor
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-09-21 00:00:00.000000000 Z
13
+ date: 2018-01-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: builder
@@ -64,9 +64,11 @@ files:
64
64
  - ".gitignore"
65
65
  - ".rspec"
66
66
  - ".ruby-version"
67
+ - ".travis.yml"
67
68
  - CHANGELOG.rdoc
68
69
  - Gemfile
69
70
  - Gemfile.lock
71
+ - LICENSE
70
72
  - Markaby.gemspec
71
73
  - README.rdoc
72
74
  - Rakefile
@@ -75,14 +77,15 @@ files:
75
77
  - lib/markaby/builder_tags.rb
76
78
  - lib/markaby/cssproxy.rb
77
79
  - lib/markaby/kernel_method.rb
80
+ - lib/markaby/rails.rb
78
81
  - lib/markaby/tags.rb
79
- - lib/markaby/version.rb
80
82
  - spec/markaby/builder_spec.rb
81
83
  - spec/markaby/css_proxy_spec.rb
82
84
  - spec/markaby/fragment_spec.rb
83
85
  - spec/markaby/html5_spec.rb
84
86
  - spec/markaby/markaby_spec.rb
85
87
  - spec/markaby/markaby_test_unit_spec.rb
88
+ - spec/markaby/rails_spec.rb
86
89
  - spec/spec_helper.rb
87
90
  homepage: ''
88
91
  licenses:
@@ -104,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
107
  version: '0'
105
108
  requirements: []
106
109
  rubyforge_project:
107
- rubygems_version: 2.6.12
110
+ rubygems_version: 2.6.14
108
111
  signing_key:
109
112
  specification_version: 4
110
113
  summary: A pure ruby based, html markup language
@@ -115,4 +118,5 @@ test_files:
115
118
  - spec/markaby/html5_spec.rb
116
119
  - spec/markaby/markaby_spec.rb
117
120
  - spec/markaby/markaby_test_unit_spec.rb
121
+ - spec/markaby/rails_spec.rb
118
122
  - spec/spec_helper.rb
@@ -1,3 +0,0 @@
1
- module Markaby
2
- VERSION = '0.8.1'
3
- end