Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Faml::Helpers module by default #35

Merged
merged 1 commit into from
Nov 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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