faml 0.5.1 → 0.6.0

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: 467a84dc8e9b891942ce31fc121c273080fcc72a
4
- data.tar.gz: 9523d713adcd1dc6daf5dd384cf74fc26e5be883
3
+ metadata.gz: 3a3b36d8389b63138cf89889812b0fd7d4534090
4
+ data.tar.gz: fc0f8be17b23d50a7b921a7d8480a81d35d37bfd
5
5
  SHA512:
6
- metadata.gz: a2f64b49c6b59166fc97eab6078a4a0e529ed9c0b9fa45257891606c0c49582b100d08fd64e38e5ef0bf9c2aa689e79888db4381ddc5ce119268163f6c647c9f
7
- data.tar.gz: 9ddc8674331446ae2459eeeb0360f924d383f88b2cbdef065a47a765f420d3ae9e6ebee590ca241db3e3ae02a958903082818eb38700d7872ed5c792bd894d42
6
+ metadata.gz: 6894952372619bbb11ce192021df6b7640f0d509eee42f315087dc9009b6800f8f7437e519b1d11d3c93ee2d2bc8f86c8b08bd009ad493500400eab3f9ca7a91
7
+ data.tar.gz: 19d60336ee8bb7fb09837540de41db71bc3f368e693dcd9d39507aae9888aa49b7523215a2777c1177d178af80434f9448ad607e39280ccd24a992c8a876f38a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.6.0 (2015-11-21)
2
+ - Disable Faml::Helpers by default
3
+ - https://github.com/eagletmt/faml/pull/35
4
+ - Set `Faml::Engine.options[:extend_helpers] = true` to enable `preserve` helper method
5
+
1
6
  ## 0.5.1 (2015-11-18)
2
7
  - Fix id ordering of object reference
3
8
  - https://github.com/eagletmt/faml/issues/33
data/README.md CHANGED
@@ -80,9 +80,12 @@ is always rendered as
80
80
 
81
81
  It's equivalent to haml's "ugly" mode.
82
82
 
83
- ### No Haml::Helpers except for preserve
84
- I won't provide helper methods of Haml::Helpers except for `preserve` .
85
- If you really need other helper methods, please open an issue.
83
+ ### No Haml::Helpers by default
84
+ I won't provide helper methods of Haml::Helpers.
85
+ If you really need helper methods, please open an issue.
86
+
87
+ Only `preserve` helper method is supported by engine option.
88
+ You have to set `Faml::Engine.options[:extend_helpers] = true` to use `preserve` .
86
89
 
87
90
  ### Others
88
91
  If you find other incompatibility, please report it to me :-p.
data/faml.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.required_ruby_version = '>= 2.0.0'
22
22
 
23
23
  spec.add_dependency 'escape_utils'
24
- spec.add_dependency 'haml_parser', '>= 0.3.0'
24
+ spec.add_dependency 'haml_parser', '>= 0.4.0'
25
25
  spec.add_dependency 'parser'
26
26
  spec.add_dependency 'temple', '>= 0.7.0'
27
27
  spec.add_dependency 'tilt'
@@ -1,7 +1,7 @@
1
1
  # Incompatibilities
2
2
  ## Versions
3
3
  - Haml 4.0.7
4
- - Faml 0.5.1
4
+ - Faml 0.6.0
5
5
  - Hamlit 1.7.2
6
6
 
7
7
  ## Table of contents
@@ -1,5 +1,5 @@
1
1
  # [./spec/render/helpers_spec.rb:4](../../../spec/render/helpers_spec.rb#L4)
2
- ## Input
2
+ ## Input (with options={:extend_helpers=>true})
3
3
  ```haml
4
4
  %span!= preserve "hello\nworld !"
5
5
  ```
@@ -4,19 +4,25 @@ require_relative 'object_ref'
4
4
  module Faml
5
5
  class AttributeCompiler
6
6
  def compile(ast)
7
- if !ast.object_ref && ast.attributes.empty?
7
+ if !ast.object_ref && !ast.old_attributes && !ast.new_attributes
8
8
  return compile_static_id_and_class(ast.static_id, ast.static_class)
9
9
  end
10
10
 
11
11
  unless ast.object_ref
12
- attrs = try_optimize_attributes(ast.attributes, ast.static_id, ast.static_class)
12
+ attrs = try_optimize_attributes(ast.old_attributes, ast.new_attributes, ast.static_id, ast.static_class)
13
13
  if attrs
14
- line_count = ast.attributes.count("\n")
14
+ line_count = 0
15
+ if ast.old_attributes
16
+ line_count += ast.old_attributes.count("\n")
17
+ end
18
+ if ast.new_attributes
19
+ line_count += ast.new_attributes.count("\n")
20
+ end
15
21
  return [:multi, [:html, :attrs, *attrs]].concat([[:newline]] * line_count)
16
22
  end
17
23
  end
18
24
 
19
- compile_slow_attributes(ast.attributes, ast.static_id, ast.static_class, ast.object_ref)
25
+ compile_slow_attributes(ast.old_attributes, ast.new_attributes, ast.static_id, ast.static_class, ast.object_ref)
20
26
  end
21
27
 
22
28
  private
@@ -32,8 +38,8 @@ module Faml
32
38
  end
33
39
  end
34
40
 
35
- def try_optimize_attributes(text, static_id, static_class)
36
- static_attributes, dynamic_attributes = AttributeOptimizer.new.try_optimize(text, static_id, static_class)
41
+ def try_optimize_attributes(old_attributes, new_attributes, static_id, static_class)
42
+ static_attributes, dynamic_attributes = AttributeOptimizer.new.try_optimize(old_attributes, new_attributes, static_id, static_class)
37
43
  if static_attributes
38
44
  (static_attributes.keys + dynamic_attributes.keys).sort.flat_map do |k|
39
45
  if static_attributes.key?(k)
@@ -71,7 +77,7 @@ module Faml
71
77
  [[:haml, :attr, key, [:dvalue, value]]]
72
78
  end
73
79
 
74
- def compile_slow_attributes(text, static_id, static_class, object_ref)
80
+ def compile_slow_attributes(old_attributes, new_attributes, static_id, static_class, object_ref)
75
81
  h = {}
76
82
  unless static_class.empty?
77
83
  h[:class] = static_class.split(/ +/)
@@ -89,8 +95,11 @@ module Faml
89
95
  unless h.empty?
90
96
  codes << h.inspect
91
97
  end
92
- unless text.empty?
93
- codes << text
98
+ if new_attributes
99
+ codes << "{#{new_attributes}}"
100
+ end
101
+ if old_attributes
102
+ codes << old_attributes
94
103
  end
95
104
  [:haml, :attrs, codes.join(', ')]
96
105
  end
@@ -4,15 +4,15 @@ require_relative 'static_hash_parser'
4
4
 
5
5
  module Faml
6
6
  class AttributeOptimizer
7
- def try_optimize(text, static_id, static_class)
7
+ def try_optimize(old_attributes, new_attributes, static_id, static_class)
8
8
  parser = StaticHashParser.new
9
- unless parser.parse("{#{text}}")
10
- assert_valid_ruby_code!(text)
9
+ unless parser.parse("{#{new_attributes}#{old_attributes}}")
10
+ assert_valid_ruby_code!(old_attributes)
11
11
  return [nil, nil]
12
12
  end
13
13
 
14
14
  static_attributes, dynamic_attributes = build_optimized_attributes(parser, static_id, static_class)
15
- if optimizable?(text, static_attributes, dynamic_attributes)
15
+ if optimizable?(old_attributes, new_attributes, static_attributes, dynamic_attributes)
16
16
  [static_attributes, dynamic_attributes]
17
17
  else
18
18
  [nil, nil]
@@ -78,7 +78,7 @@ module Faml
78
78
  dynamic_attributes
79
79
  end
80
80
 
81
- def optimizable?(text, static_attributes, dynamic_attributes)
81
+ def optimizable?(old_attributes, new_attributes, static_attributes, dynamic_attributes)
82
82
  if static_attributes.nil?
83
83
  return false
84
84
  end
@@ -88,7 +88,9 @@ module Faml
88
88
  return false
89
89
  end
90
90
 
91
- if text.include?("\n") && !dynamic_attributes.empty?
91
+ old_newline = old_attributes && old_attributes.include?("\n")
92
+ new_newline = new_attributes && new_attributes.include?("\n")
93
+ if (old_newline || new_newline) && !dynamic_attributes.empty?
92
94
  # XXX: Quit optimization to keep newlines
93
95
  # https://github.com/eagletmt/faml/issues/18
94
96
  return false
data/lib/faml/cli.rb CHANGED
@@ -7,22 +7,25 @@ module Faml
7
7
 
8
8
  desc 'render FILE', 'Render haml template'
9
9
  option :format, type: :string, default: :html, desc: 'HTML format'
10
+ option :extend_helpers, type: :boolean, default: false, desc: 'Extend Faml::Helpers or not'
10
11
  def render(file)
11
- code = compile_file(file, format: options[:format].to_sym)
12
+ code = compile_file(file, format: options[:format].to_sym, extend_helpers: options[:extend_helpers])
12
13
  puts instance_eval(code, file)
13
14
  end
14
15
 
15
16
  desc 'compile FILE', 'Compile haml template'
16
17
  option :format, type: :string, default: :html, desc: 'HTML format'
18
+ option :extend_helpers, type: :boolean, default: false, desc: 'Extend Faml::Helpers or not'
17
19
  def compile(file)
18
- puts compile_file(file, format: options[:format].to_sym)
20
+ puts compile_file(file, format: options[:format].to_sym, extend_helpers: options[:extend_helpers])
19
21
  end
20
22
 
21
23
  desc 'temple FILE', 'Render temple AST'
22
24
  option :format, type: :string, default: :html, desc: 'HTML format'
25
+ option :extend_helpers, type: :boolean, default: false, desc: 'Extend Faml::Helpers or not'
23
26
  def temple(file)
24
27
  require 'pp'
25
- pp Faml::Compiler.new(filename: file, format: options[:format].to_sym).call(parse_file(file))
28
+ pp Faml::Compiler.new(filename: file, format: options[:format].to_sym, extend_helpers: options[:extend_helpers]).call(parse_file(file))
26
29
  end
27
30
 
28
31
  desc 'stats FILE/DIR ...', 'Show statistics'
data/lib/faml/compiler.rb CHANGED
@@ -22,6 +22,7 @@ module Faml
22
22
  preserve: DEFAULT_PRESERVE_TAGS,
23
23
  use_html_safe: false,
24
24
  filename: nil,
25
+ extend_helpers: false,
25
26
  )
26
27
 
27
28
  def initialize(*)
@@ -78,9 +79,12 @@ module Faml
78
79
  end
79
80
 
80
81
  def compile_root(ast)
81
- [:multi, [:code, "extend ::#{helper_module.name}"]].tap do |temple|
82
- compile_children(ast, temple)
82
+ temple = [:multi]
83
+ if options[:extend_helpers]
84
+ temple << [:code, "extend ::#{helper_module.name}"]
83
85
  end
86
+ compile_children(ast, temple)
87
+ temple
84
88
  end
85
89
 
86
90
  def helper_module
data/lib/faml/engine.rb CHANGED
@@ -10,6 +10,7 @@ module Faml
10
10
  define_options(
11
11
  generator: Temple::Generators::ArrayBuffer,
12
12
  filename: nil,
13
+ extend_helpers: false,
13
14
  )
14
15
 
15
16
  use HamlParser::Parser
data/lib/faml/stats.rb CHANGED
@@ -105,7 +105,7 @@ module Faml
105
105
  return
106
106
  end
107
107
 
108
- if ast.attributes.empty?
108
+ if !ast.old_attributes && !ast.new_attributes
109
109
  if ast.static_class.empty? && ast.static_id.empty?
110
110
  info.empty_attribute_count += 1
111
111
  else
@@ -113,20 +113,22 @@ module Faml
113
113
  end
114
114
  else
115
115
  static_hash_parser = StaticHashParser.new
116
- if static_hash_parser.parse("{#{ast.attributes}}")
116
+ if static_hash_parser.parse("{#{ast.new_attributes}#{ast.old_attributes}}")
117
117
  if static_hash_parser.dynamic_attributes.empty?
118
118
  info.static_attribute_count += 1
119
119
  else
120
120
  if static_hash_parser.dynamic_attributes.key?('data') || static_hash_parser.dynamic_attributes.key?(:data)
121
121
  info.dynamic_attribute_with_data_count += 1
122
- elsif ast.attributes.include?("\n")
122
+ elsif ast.old_attributes && ast.old_attributes.include?("\n")
123
+ info.dynamic_attribute_with_newline_count += 1
124
+ elsif ast.new_attributes && ast.new_attributes.include?("\n")
123
125
  info.dynamic_attribute_with_newline_count += 1
124
126
  else
125
127
  info.dynamic_attribute_count += 1
126
128
  end
127
129
  end
128
130
  else
129
- call_ast = Parser::CurrentRuby.parse("call(#{ast.attributes})")
131
+ call_ast = Parser::CurrentRuby.parse("call(#{ast.new_attributes}#{ast.old_attributes})")
130
132
  if call_ast.type == :send && call_ast.children[0].nil? && call_ast.children[1] == :call && !call_ast.children[3].nil?
131
133
  info.multi_attribute_count += 1
132
134
  else
data/lib/faml/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Faml
2
- VERSION = '0.5.1'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -60,6 +60,13 @@ RSpec.describe 'Faml with Rails', type: :request do
60
60
  end
61
61
 
62
62
  describe 'preserve helper' do
63
+ around do |example|
64
+ extend_helpers = Faml::Engine.options[:extend_helpers]
65
+ Faml::Engine.options[:extend_helpers] = true
66
+ example.run
67
+ Faml::Engine.options[:extend_helpers] = extend_helpers
68
+ end
69
+
63
70
  it 'returns html_safe string' do
64
71
  get '/books/preserve'
65
72
  expect(response).to be_ok
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Faml::Helpers, type: :render do
4
4
  it 'has preserve method' do
5
- expect(render_string('%span!= preserve "hello\nworld !"')).to eq("<span>hello&#x000A;world !</span>\n")
5
+ expect(render_string('%span!= preserve "hello\nworld !"', extend_helpers: true)).to eq("<span>hello&#x000A;world !</span>\n")
6
6
  end
7
7
  end
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.5.1
4
+ version: 0.6.0
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-11-18 00:00:00.000000000 Z
11
+ date: 2015-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.3.0
33
+ version: 0.4.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.3.0
40
+ version: 0.4.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: parser
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -516,7 +516,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
516
516
  version: '0'
517
517
  requirements: []
518
518
  rubyforge_project:
519
- rubygems_version: 2.4.5.1
519
+ rubygems_version: 2.5.0
520
520
  signing_key:
521
521
  specification_version: 4
522
522
  summary: Faster implementation of Haml template language.