faml 0.2.12 → 0.2.13

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7a063fee1f2806cfe4b482c790ec377b5870653
4
- data.tar.gz: 60fe5ca8e1aa9a9c3b5dc2d6fd5e52583f49ab11
3
+ metadata.gz: 13af244db12ca1e99e25c6f757a58390e966b37e
4
+ data.tar.gz: 9528efe51e42a8d73fa0eee94e831509b4f46912
5
5
  SHA512:
6
- metadata.gz: 1eb70dba3088438ee8139cdfdcda59b572c4bc2fa6ea9f858d4b621bebfe6e909e0ac24bd2623d1932a5d70a95699cd073f9e7eedf1fc663dcf5d232f4f7736d
7
- data.tar.gz: dc3cffec0d339450dc104958de8e3aed251660f1d1b0e75cb76af6214325a4e70ed334783c9606f563ee1e0f2a50cef05e7ab80c984cc30b31bf2e7f7370a00b
6
+ metadata.gz: da4116520b13a085b7cebeb5ecc3a6a36787461db58fe5eb37cec1a3554de3a171494719e494eb64b9267e24fa765fa62311bbeceebb721d0ea745700d9e037f
7
+ data.tar.gz: bf0d92f3dde0f11aba126fa2f592429e149de0445f82a662589df132b462bea2b75adb959143fb82759c5258165e8f8f13009efc02e2a15774ac6c5bd12cb9d1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.13 (2015-04-14)
2
+ - Fix attribute rendering in non-html format when the value is true
3
+ - Fix Faml::Engine constructor to allow user to set options via `Faml::Engine.options`
4
+
1
5
  ## 0.2.12 (2015-04-12)
2
6
  - Remove duplicated class values
3
7
  - For compatibility
data/README.md CHANGED
@@ -23,6 +23,16 @@ Or install it yourself as:
23
23
  $ gem install faml
24
24
 
25
25
  ## Usage
26
+ You can set several options via `Faml::Engine.options` .
27
+
28
+ ```ruby
29
+ # Render in XHTML format
30
+ Faml::Engine.options[:format] = :xhtml
31
+ # Disable autoclose
32
+ Faml::Engine.options[:autoclose] = []
33
+ # Disable auto preserve
34
+ Faml::Engine.options[:preserve] = []
35
+ ```
26
36
 
27
37
  ### Rails, sinatra
28
38
  Just replace your `gem 'haml'` with `gem 'faml'` .
@@ -17,7 +17,7 @@ slim_code = ARGV[1] ? File.read(ARGV[1]) : nil
17
17
  Benchmark.ips do |x|
18
18
  x.report('Haml') { Haml::Engine.new(haml_code, ugly: true, escape_html: true) }
19
19
  x.report('Faml') { Faml::Engine.new.call(haml_code) }
20
- x.report('Hamlit') { Hamlit::Engine.new(escape_html: true).call(haml_code) }
20
+ x.report('Hamlit') { Hamlit::Engine.new.call(haml_code) }
21
21
  if slim_code
22
22
  x.report('Slim') { Slim::Engine.new.call(slim_code) }
23
23
  end
@@ -21,8 +21,8 @@ Benchmark.ips do |x|
21
21
  obj.instance_eval %{
22
22
  def faml_array; #{Faml::Engine.new.call(haml_code)}; end
23
23
  def faml_string; #{Faml::Engine.new(generator: Temple::Generators::RailsOutputBuffer).call(haml_code)}; end
24
- def hamlit_array; #{Hamlit::Engine.new(escape_html: true).call(haml_code)}; end
25
- def hamlit_string; #{Hamlit::Engine.new(escape_html: true, generator: Temple::Generators::RailsOutputBuffer).call(haml_code)}; end
24
+ def hamlit_array; #{Hamlit::Engine.new.call(haml_code)}; end
25
+ def hamlit_string; #{Hamlit::Engine.new(generator: Temple::Generators::RailsOutputBuffer).call(haml_code)}; end
26
26
  }
27
27
  if slim_code
28
28
  obj.instance_eval %{
data/benchmark/slim.rb CHANGED
@@ -15,7 +15,7 @@ context = Context.new
15
15
  Haml::Engine.new(haml_code, ugly: true, escape_html: true).def_method(context, :haml)
16
16
  context.instance_eval %{
17
17
  def faml; #{Faml::Engine.new(generator: Temple::Generators::RailsOutputBuffer).call(haml_code)}; end
18
- def hamlit; #{Hamlit::Engine.new(escape_html: true, generator: Temple::Generators::RailsOutputBuffer).call(haml_code)}; end
18
+ def hamlit; #{Hamlit::Engine.new(generator: Temple::Generators::RailsOutputBuffer).call(haml_code)}; end
19
19
  def slim; #{Slim::Engine.new(generator: Temple::Generators::RailsOutputBuffer).call(slim_code)}; end
20
20
  }
21
21
 
@@ -161,7 +161,7 @@ put_attribute(VALUE attr_quote, VALUE key, VALUE value)
161
161
  }
162
162
 
163
163
  static VALUE
164
- build_attribute(VALUE attr_quote, VALUE key, VALUE value)
164
+ build_attribute(VALUE attr_quote, int is_html, VALUE key, VALUE value)
165
165
  {
166
166
  const char *key_cstr = StringValueCStr(key);
167
167
  if (strcmp(key_cstr, "class") == 0) {
@@ -199,10 +199,14 @@ build_attribute(VALUE attr_quote, VALUE key, VALUE value)
199
199
  return put_attribute(attr_quote, key, rb_ary_join(ary, rb_str_new_cstr("_")));
200
200
  }
201
201
  } else if (RB_TYPE_P(value, T_TRUE)) {
202
- VALUE attr = rb_str_buf_new(1 + RSTRING_LEN(key));
203
- rb_str_buf_cat(attr, " ", 1);
204
- rb_str_buf_append(attr, key);
205
- return attr;
202
+ if (is_html) {
203
+ VALUE attr = rb_str_buf_new(1 + RSTRING_LEN(key));
204
+ rb_str_buf_cat(attr, " ", 1);
205
+ rb_str_buf_append(attr, key);
206
+ return attr;
207
+ } else {
208
+ return put_attribute(attr_quote, key, key);
209
+ }
206
210
  } else if (RB_TYPE_P(value, T_FALSE) || NIL_P(value)) {
207
211
  return Qnil;
208
212
  } else {
@@ -212,17 +216,19 @@ build_attribute(VALUE attr_quote, VALUE key, VALUE value)
212
216
  }
213
217
 
214
218
  static VALUE
215
- m_build(int argc, VALUE *argv, VALUE self)
219
+ m_build(int argc, VALUE *argv, RB_UNUSED_VAR(VALUE self))
216
220
  {
217
221
  VALUE attr_quote, attributes, keys, buf;
222
+ int is_html;
218
223
  long len, i;
219
224
 
220
- rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
225
+ rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS);
221
226
  attr_quote = argv[0];
227
+ is_html = RTEST(argv[1]);
222
228
  attributes = rb_hash_new();
223
229
  rb_hash_aset(attributes, rb_str_new_cstr("id"), rb_ary_new());
224
230
  rb_hash_aset(attributes, rb_str_new_cstr("class"), rb_ary_new());
225
- merge(attributes, argc-1, argv+1);
231
+ merge(attributes, argc-2, argv+2);
226
232
 
227
233
  keys = rb_funcall(attributes, id_keys, 0);
228
234
  rb_funcall(keys, id_sort_bang, 0);
@@ -230,14 +236,14 @@ m_build(int argc, VALUE *argv, VALUE self)
230
236
  buf = rb_ary_new_capa(len);
231
237
  for (i = 0; i < len; i++) {
232
238
  VALUE k = RARRAY_AREF(keys, i);
233
- rb_ary_push(buf, build_attribute(attr_quote, k, rb_hash_lookup(attributes, k)));
239
+ rb_ary_push(buf, build_attribute(attr_quote, is_html, k, rb_hash_lookup(attributes, k)));
234
240
  }
235
241
 
236
242
  return rb_ary_join(buf, Qnil);
237
243
  }
238
244
 
239
245
  static VALUE
240
- m_normalize_data(VALUE self, VALUE data)
246
+ m_normalize_data(RB_UNUSED_VAR(VALUE self), VALUE data)
241
247
  {
242
248
  return normalize_data(data);
243
249
  }
@@ -1,3 +1,4 @@
1
1
  require 'mkmf'
2
2
 
3
+ $CFLAGS << ' -Wall -W'
3
4
  create_makefile('faml/attribute_builder')
data/faml.gemspec CHANGED
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "coffee-script"
30
30
  spec.add_development_dependency "coveralls"
31
31
  spec.add_development_dependency "haml" # for benchmark
32
- spec.add_development_dependency "hamlit" # for benchmark
32
+ spec.add_development_dependency "hamlit", ">= 0.6.0" # for benchmark
33
33
  spec.add_development_dependency "rake"
34
34
  spec.add_development_dependency "rake-compiler"
35
35
  spec.add_development_dependency "redcarpet"
data/lib/faml/engine.rb CHANGED
@@ -11,15 +11,6 @@ module Faml
11
11
  filename: nil,
12
12
  )
13
13
 
14
- DEFAULT_OPTIONS = {
15
- format: :html,
16
- attr_quote: "'",
17
- }.freeze
18
-
19
- def initialize(opts = {})
20
- super(DEFAULT_OPTIONS.merge(opts))
21
- end
22
-
23
14
  use Parser
24
15
  use Compiler
25
16
  use Html
data/lib/faml/html.rb CHANGED
@@ -2,6 +2,10 @@ require 'faml/attribute_builder'
2
2
 
3
3
  module Faml
4
4
  class Html < Temple::HTML::Fast
5
+ # Override temple's default
6
+ self.options[:format] = :html
7
+ self.options[:attr_quote] = "'"
8
+
5
9
  def on_haml_tag(name, self_closing, attrs, content = nil)
6
10
  name = name.to_s
7
11
  closed = self_closing && (!content || empty_exp?(content))
@@ -13,22 +17,18 @@ module Faml
13
17
  end
14
18
 
15
19
  def on_haml_attrs(code)
16
- [:dynamic, "::Faml::AttributeBuilder.build(#{options[:attr_quote].inspect}, #{code})"]
20
+ [:dynamic, "::Faml::AttributeBuilder.build(#{options[:attr_quote].inspect}, #{options[:format] == :html}, #{code})"]
17
21
  end
18
22
 
19
23
  def on_haml_attr(name, value)
20
24
  if empty_exp?(value)
21
- if @format == :html
22
- [:static, " #{name}"]
23
- else
24
- [:static, " #{name}=#{options[:attr_quote]}#{name}#{options[:attr_quote]}"]
25
- end
25
+ true_attribute(name)
26
26
  elsif value[0] == :dvalue
27
27
  sym = unique_name
28
28
  [:multi,
29
29
  [:code, "#{sym} = (#{value[1]})"],
30
30
  [:case, sym,
31
- ['true', [:static, " #{name}"]],
31
+ ['true', true_attribute(name)],
32
32
  ['false, nil', [:multi]],
33
33
  [:else, [:multi,
34
34
  [:static, " #{name}=#{options[:attr_quote]}"],
@@ -54,5 +54,15 @@ module Faml
54
54
  def on_haml_preserve(sym)
55
55
  [:dynamic, "::Faml::Compiler.find_and_preserve(#{sym}.to_s)"]
56
56
  end
57
+
58
+ private
59
+
60
+ def true_attribute(name)
61
+ if @format == :html
62
+ [:static, " #{name}"]
63
+ else
64
+ [:static, " #{name}=#{options[:attr_quote]}#{name}#{options[:attr_quote]}"]
65
+ end
66
+ end
57
67
  end
58
68
  end
data/lib/faml/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Faml
2
- VERSION = "0.2.12"
2
+ VERSION = "0.2.13"
3
3
  end
@@ -91,6 +91,8 @@ HAML
91
91
  context 'with xhtml format' do
92
92
  it 'renders name="name" if value is true' do
93
93
  expect(render_string(%q|%span{foo: true, bar: 1} hello|, format: :xhtml)).to eq(%Q{<span bar='1' foo='foo'>hello</span>\n})
94
+ expect(render_string(%Q|- foo = true\n%span{foo: foo, bar: 1} hello|, format: :xhtml)).to eq(%Q{<span bar='1' foo='foo'>hello</span>\n})
95
+ expect(render_string(%Q|- h = {foo: true, bar: 1}\n%span{h} hello|, format: :xhtml)).to eq(%Q{<span bar='1' foo='foo'>hello</span>\n})
94
96
  end
95
97
  end
96
98
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.12
4
+ version: 0.2.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kohei Suzuki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-12 00:00:00.000000000 Z
11
+ date: 2015-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -156,14 +156,14 @@ dependencies:
156
156
  requirements:
157
157
  - - ">="
158
158
  - !ruby/object:Gem::Version
159
- version: '0'
159
+ version: 0.6.0
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
- version: '0'
166
+ version: 0.6.0
167
167
  - !ruby/object:Gem::Dependency
168
168
  name: rake
169
169
  requirement: !ruby/object:Gem::Requirement