-
-
Notifications
You must be signed in to change notification settings - Fork 499
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
Add ability to specify attributes for embedded engines #653
Add ability to specify attributes for embedded engines #653
Conversation
Hello, Eric! Some thoughts from a random the Internet guy:
By the way, thank you very much for your attention to the problem! |
70810d5
to
7c79d4f
Compare
553dd58
to
e941e38
Compare
07b6fa5
to
41e9dbf
Compare
72e0947
to
7358b52
Compare
Thanks a lot @ardecvz and @minad for the advice! I've updated my implementation:
I didn't attempt to implement this for the remaining embedded engines (markdown, creole, rdoc, etc.) since there doesn't seem to be a one-to-one mapping to a specific tag in those cases, so I wouldn't know where to put the attributes in the result. Please let me know if you have any more comments/questions! |
Hey, any feedback on this? @minad |
This looks good in general. However I would like to have a solution which also passes options to the underlying tilt engines. Any ideas about that? |
It seems the PR is stalled and I just wanted to remind you about it. @minad Personally, I've already found the proposed solution simple and elegant. I don't understand about what options you're talking, could you elaborate more, please? As far as I understand, there're are two separate entities - Tilt's engine options (such as There's no reason and it's plainly impossible to pass them to Tilt engines - we just pass templates and capture the output. As for the former, we already may pass options to tilt engines by something like this configuration string from README - And two additional points to the PR:
In my opinion, it's better to handle multiple embedded's attributes differently than tag's ones as it's completely legitimate to override them at least after |
lib/slim/parser.rb
Outdated
parse_attributes(attrs) | ||
@line = @orig_line | ||
attrs | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like how this parsing is done. I would prefer if the parse_attributes method is somehow generalized to work also for the embedded attributes.
3ec1b7d
to
0cb2362
Compare
name = name.to_sym | ||
raise(Temple::FilterError, "Embedded engine #{name} is disabled") unless enabled?(name) | ||
@engines[name] ||= self.class.create(name, options) | ||
@engines[name].on_slim_embedded(name, body) | ||
@engines[name].on_slim_embedded(name, body, attrs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed the nil
check here, but this requires that all the engines' on_slim_embedded
methods take an attrs
param.
One alternative is to do something like this:
if @engines[name].is_a?(TagEngine)
@engines[name].on_slim_embedded(name, body, attrs)
else
@engines[name].on_slim_embedded(name, body)
end
@minad Thanks for your comments! I've updated the implementation to remove the |
|
Is there any plan for merge this PR? |
I read the comment of @ardecvz (#653 (comment)).
In order for this PR to be merged, is it necessary to make the following modifications ? diff --git a/lib/slim/embedded.rb b/lib/slim/embedded.rb
index 80e5528..05b0c6b 100644
--- a/lib/slim/embedded.rb
+++ b/lib/slim/embedded.rb
@@ -210,17 +210,10 @@ module Slim
def on_slim_embedded(engine, body, attrs)
- unless options[:attributes].empty?
- options[:attributes].map do|k, v|
- attrs << [:html, :attr, k, [:static, v]]
- end
- end
-
if options[:engine]
opts = {}.update(options)
opts.delete(:engine)
opts.delete(:tag)
- opts.delete(:attributes)
@engine ||= options[:engine].new(opts)
body = @engine.on_slim_embedded(engine, body, attrs)
end
@@ -235,7 +228,7 @@ module Slim
class JavaScriptEngine < TagEngine
disable_option_validator!
- set_options tag: :script, attributes: {}
+ set_options tag: :script
def on_slim_embedded(engine, body, attrs)
super(engine, [:html, :js, body], attrs)
@@ -262,10 +255,10 @@ module Slim
# These engines are executed at compile time
register :coffee, JavaScriptEngine, engine: TiltEngine
register :opal, JavaScriptEngine, engine: TiltEngine
- register :less, TagEngine, tag: :style, attributes: { type: 'text/css' }, engine: TiltEngine
- register :styl, TagEngine, tag: :style, attributes: { type: 'text/css' }, engine: TiltEngine
- register :sass, TagEngine, :pretty, tag: :style, attributes: { type: 'text/css' }, engine: SassEngine
- register :scss, TagEngine, :pretty, tag: :style, attributes: { type: 'text/css' }, engine: SassEngine
+ register :less, TagEngine, tag: :style, engine: TiltEngine
+ register :styl, TagEngine, tag: :style, engine: TiltEngine
+ register :sass, TagEngine, :pretty, tag: :style, engine: SassEngine
+ register :scss, TagEngine, :pretty, tag: :style, engine: SassEngine
# These engines are precompiled, code is embedded
register :erb, ERBEngine
@@ -274,7 +267,7 @@ module Slim
# Embedded javascript/css
register :javascript, JavaScriptEngine
- register :css, TagEngine, tag: :style, attributes: { type: 'text/css' }
+ register :css, TagEngine, tag: :style
# Embedded ruby code
register :ruby, RubyEngine
diff --git a/test/core/test_embedded_engines.rb b/test/core/test_embedded_engines.rb
index 449d1b3..afca2d3 100644
--- a/test/core/test_embedded_engines.rb
+++ b/test/core/test_embedded_engines.rb
@@ -116,7 +116,7 @@ wiki:
css:
h1 { color: blue }
}
- assert_html "<style type=\"text/css\">h1 { color: blue }</style>", source
+ assert_html "<style>h1 { color: blue }</style>", source
end
def test_render_with_css_empty_attributes
@@ -124,7 +124,7 @@ css:
css []:
h1 { color: blue }
}
- assert_html "<style type=\"text/css\">h1 { color: blue }</style>", source
+ assert_html "<style>h1 { color: blue }</style>", source
end
def test_render_with_css_attribute
@@ -132,7 +132,7 @@ css []:
css scoped = "true":
h1 { color: blue }
}
- assert_html "<style scoped=\"true\" type=\"text/css\">h1 { color: blue }</style>", source
+ assert_html "<style scoped=\"true\">h1 { color: blue }</style>", source
end
def test_render_with_css_multiple_attributes
@@ -140,7 +140,7 @@ css scoped = "true":
css class="myClass" scoped = "true" :
h1 { color: blue }
}
- assert_html "<style class=\"myClass\" scoped=\"true\" type=\"text/css\">h1 { color: blue }</style>", source
+ assert_html "<style class=\"myClass\" scoped=\"true\">h1 { color: blue }</style>", source
end
def test_render_with_javascript
@@ -254,7 +254,7 @@ scss:
$color: #f00;
body { color: $color; }
}
- assert_html "<style type=\"text/css\">body{color:red}</style>", source
+ assert_html "<style>body{color:red}</style>", source
end
def test_render_with_scss_attribute
@@ -263,7 +263,7 @@ scss [class="myClass"]:
$color: #f00;
body { color: $color; }
}
- assert_html "<style class=\"myClass\" type=\"text/css\">body{color:red}</style>", source
+ assert_html "<style class=\"myClass\">body{color:red}</style>", source
end
def test_render_with_sass
@@ -273,7 +273,7 @@ sass:
body
color: $color
}
- assert_html "<style type=\"text/css\">body{color:red}</style>", source
+ assert_html "<style>body{color:red}</style>", source
end
def test_render_with_sass_attribute
@@ -283,7 +283,7 @@ sass [class="myClass"]:
body
color: $color
}
- assert_html "<style class=\"myClass\" type=\"text/css\">body{color:red}</style>", source
+ assert_html "<style class=\"myClass\">body{color:red}</style>", source
end
diff --git a/test/core/test_erb_converter.rb b/test/core/test_erb_converter.rb
index 4c9d93c..303f2ca 100644
--- a/test/core/test_erb_converter.rb
+++ b/test/core/test_erb_converter.rb
@@ -51,7 +51,7 @@ multiline comment-->
</script><!--[if lt IE 9]>
<script src="old-ie1.js">
</script><script src="old-ie2.js">
-</script><![endif]--><style type="text/css">body{background-color:red}
+</script><![endif]--><style>body{background-color:red}
</style>
</head><body>
diff --git a/test/core/test_pretty.rb b/test/core/test_pretty.rb
index c167d8e..92ee7f4 100644
--- a/test/core/test_pretty.rb
+++ b/test/core/test_pretty.rb
@@ -56,7 +56,7 @@ html
<script src="old-ie1.js"></script>
<script src="old-ie2.js"></script>
<![endif]-->
- <style type="text/css">
+ <style>
body {
background-color: red;
} |
0cb2362
to
bc42e4b
Compare
Is this PR still being considered? Would love to see it get merged if possible. |
Hey @minad, I'd love to see this PR merged. Anything we can do to help with? |
Hey, yes, this PR is still considered. Back then it didn't seem ready too me, but we can soon revisit it. @stonean What do you think? |
All tests past after resolving some simple merge conflicts. https://travis-ci.org/slim-template/slim/builds/400183616 Merging into master |
Merged into master. Thank you @etdev All tests passed: https://travis-ci.org/slim-template/slim/builds/400191013 I want to go through more pull requests and issues before releasing a new gem version, but it's available for testing. Thanks again. |
Adds support for the following syntax (as described in Issue #620 by @elstgav):
which generates:
and
which generates:
This is my first PR to this repo so I would highly appreciate any feedback!