Skip to content

Commit

Permalink
Disable Faml::Helpers module by default
Browse files Browse the repository at this point in the history
`Object#extend` could do harm to performance because of MRI's method
cache invalidation. If you need `preserve` method, you have to set
`Faml::Engine.options[:extend_helpers] = true` .
  • Loading branch information
eagletmt committed Nov 18, 2015
1 parent b90a1d9 commit 12539b4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ is always rendered as

It's equivalent to haml's "ugly" mode.

### No Haml::Helpers except for preserve
I won't provide helper methods of Haml::Helpers except for `preserve` .
If you really need other helper methods, please open an issue.
### No Haml::Helpers by default
I won't provide helper methods of Haml::Helpers.
If you really need helper methods, please open an issue.

Only `preserve` helper method is supported by engine option.
You have to set `Faml::Engine.options[:extend_helpers] = true` to use `preserve` .

### Others
If you find other incompatibility, please report it to me :-p.
Expand Down
2 changes: 1 addition & 1 deletion incompatibilities/spec/render/helpers_spec.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# [./spec/render/helpers_spec.rb:4](../../../spec/render/helpers_spec.rb#L4)
## Input
## Input (with options={:extend_helpers=>true})
```haml
%span!= preserve "hello\nworld !"
```
Expand Down
9 changes: 6 additions & 3 deletions lib/faml/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ class CLI < Thor

desc 'render FILE', 'Render haml template'
option :format, type: :string, default: :html, desc: 'HTML format'
option :extend_helpers, type: :boolean, default: false, desc: 'Extend Faml::Helpers or not'
def render(file)
code = compile_file(file, format: options[:format].to_sym)
code = compile_file(file, format: options[:format].to_sym, extend_helpers: options[:extend_helpers])
puts instance_eval(code, file)
end

desc 'compile FILE', 'Compile haml template'
option :format, type: :string, default: :html, desc: 'HTML format'
option :extend_helpers, type: :boolean, default: false, desc: 'Extend Faml::Helpers or not'
def compile(file)
puts compile_file(file, format: options[:format].to_sym)
puts compile_file(file, format: options[:format].to_sym, extend_helpers: options[:extend_helpers])
end

desc 'temple FILE', 'Render temple AST'
option :format, type: :string, default: :html, desc: 'HTML format'
option :extend_helpers, type: :boolean, default: false, desc: 'Extend Faml::Helpers or not'
def temple(file)
require 'pp'
pp Faml::Compiler.new(filename: file, format: options[:format].to_sym).call(parse_file(file))
pp Faml::Compiler.new(filename: file, format: options[:format].to_sym, extend_helpers: options[:extend_helpers]).call(parse_file(file))
end

desc 'stats FILE/DIR ...', 'Show statistics'
Expand Down
8 changes: 6 additions & 2 deletions lib/faml/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Compiler < Temple::Parser
preserve: DEFAULT_PRESERVE_TAGS,
use_html_safe: false,
filename: nil,
extend_helpers: false,
)

def initialize(*)
Expand Down Expand Up @@ -78,9 +79,12 @@ def compile(ast)
end

def compile_root(ast)
[:multi, [:code, "extend ::#{helper_module.name}"]].tap do |temple|
compile_children(ast, temple)
temple = [:multi]
if options[:extend_helpers]
temple << [:code, "extend ::#{helper_module.name}"]
end
compile_children(ast, temple)
temple
end

def helper_module
Expand Down
1 change: 1 addition & 0 deletions lib/faml/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Engine < Temple::Engine
define_options(
generator: Temple::Generators::ArrayBuffer,
filename: nil,
extend_helpers: false,
)

use HamlParser::Parser
Expand Down
7 changes: 7 additions & 0 deletions spec/rails/spec/requests/faml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
end

describe 'preserve helper' do
around do |example|
extend_helpers = Faml::Engine.options[:extend_helpers]
Faml::Engine.options[:extend_helpers] = true
example.run
Faml::Engine.options[:extend_helpers] = extend_helpers
end

it 'returns html_safe string' do
get '/books/preserve'
expect(response).to be_ok
Expand Down
2 changes: 1 addition & 1 deletion spec/render/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

RSpec.describe Faml::Helpers, type: :render do
it 'has preserve method' do
expect(render_string('%span!= preserve "hello\nworld !"')).to eq("<span>hello&#x000A;world !</span>\n")
expect(render_string('%span!= preserve "hello\nworld !"', extend_helpers: true)).to eq("<span>hello&#x000A;world !</span>\n")
end
end

0 comments on commit 12539b4

Please sign in to comment.