Permalink
Browse files

Mirrored ERB inline syntax to single line syntax in templates

  • Loading branch information...
1 parent 4f66c6d commit 43a647a1b78f3fd94fae2f4d06d4d744193bc0fd @disolovyov disolovyov committed Mar 16, 2011
@@ -6,9 +6,9 @@
<link rel="stylesheet" href="/bundle.css" media="screen" />
</head>
<body>
- <%! view %>
- <% ctx.javascripts.each_key do |js| %>
- <script><%= File.read js %></script>
- <% end %>
+ %! view
+ %~ ctx.javascripts.each_key do |js|
+ <script><%= File.read js %></script>
+ %~ end
</body>
</html>
@@ -1,3 +1,3 @@
-% if visual_child
-<%! visual_child.view -%>
-% end
+%~ if visual_child
+%! visual_child.view
+%~ end
@@ -39,14 +39,30 @@
<string>comment.block.erb</string>
</dict>
<dict>
+ <key>begin</key>
+ <string>^\s*%#</string>
+ <key>captures</key>
+ <dict>
+ <key>0</key>
+ <dict>
+ <key>name</key>
+ <string>punctuation.definition.comment.erb</string>
+ </dict>
+ </dict>
+ <key>end</key>
+ <string>$</string>
+ <key>name</key>
+ <string>comment.block.erb</string>
+ </dict>
+ <dict>
<key>match</key>
<string>^\s*%%|&lt;%%</string>
<key>name</key>
<string>text.html.basic</string>
</dict>
<dict>
<key>begin</key>
- <string>&lt;%[=!?_]?</string>
+ <string>&lt;%[~=!?_]?</string>
<key>captures</key>
<dict>
<key>0</key>
@@ -89,20 +105,14 @@
</dict>
<dict>
<key>begin</key>
- <string>^\s*%</string>
+ <string>^\s*%[~=!?_]?</string>
<key>end</key>
<string>$</string>
<key>name</key>
<string>source.ruby.tanuki.embedded.html</string>
<key>patterns</key>
<array>
<dict>
- <key>match</key>
- <string>%%&gt;</string>
- <key>name</key>
- <string>punctuation.section.embedded.ruby</string>
- </dict>
- <dict>
<key>captures</key>
<dict>
<key>1</key>
@@ -112,7 +122,7 @@
</dict>
</dict>
<key>match</key>
- <string>(#).*?(?=-?%&gt;)</string>
+ <string>(#).*?$</string>
<key>name</key>
<string>comment.line.number-sign.ruby</string>
</dict>
@@ -130,6 +140,6 @@
<key>scopeName</key>
<string>text.html.tanuki</string>
<key>uuid</key>
- <string>11082450-51D9-4728-9E38-88798954ED88</string>
+ <string>C9ED17EE-64B8-416C-B20F-2E44D71EDE32</string>
</dict>
</plist>
@@ -27,6 +27,22 @@
<string>comment.block.erb</string>
</dict>
<dict>
+ <key>begin</key>
+ <string>^\s*%#</string>
+ <key>captures</key>
+ <dict>
+ <key>0</key>
+ <dict>
+ <key>name</key>
+ <string>punctuation.definition.comment.erb</string>
+ </dict>
+ </dict>
+ <key>end</key>
+ <string>$</string>
+ <key>name</key>
+ <string>comment.block.erb</string>
+ </dict>
+ <dict>
<key>match</key>
<string>^\s*%%|&lt;%%</string>
<key>name</key>
@@ -77,20 +93,14 @@
</dict>
<dict>
<key>begin</key>
- <string>^\s*%</string>
+ <string>^\s*%[~=!?_]?</string>
<key>end</key>
<string>$</string>
<key>name</key>
<string>source.ruby.tanuki.embedded.text</string>
<key>patterns</key>
<array>
<dict>
- <key>match</key>
- <string>%%&gt;</string>
- <key>name</key>
- <string>punctuation.section.embedded.ruby</string>
- </dict>
- <dict>
<key>captures</key>
<dict>
<key>1</key>
@@ -100,7 +110,7 @@
</dict>
</dict>
<key>match</key>
- <string>(#).*?(?=-?%&gt;)</string>
+ <string>(#).*?$</string>
<key>name</key>
<string>comment.line.number-sign.ruby</string>
</dict>
@@ -114,6 +124,6 @@
<key>scopeName</key>
<string>text.tanuki</string>
<key>uuid</key>
- <string>2C70B745-C5B1-4EEB-BF91-2D62E1880C7D</string>
+ <string>0C83B921-7833-44CC-BB21-1C7D9004BE5D</string>
</dict>
</plist>
@@ -8,14 +8,23 @@ module Tanuki
# The following tags are recognized:
#
# <% Ruby code -- output to stdout %>
+ # <%~ Ruby code -- output to stdout %>
# <%= Ruby expression -- replace with result %>
# <%# comment -- ignored -- useful in testing %>
- # % a line of Ruby code -- treated as <% line %>
- # %% replaced with % if first thing on a line
# <%% or %%> -- replace with <% or %> respectively
# <%! Ruby expression -- must return a template %> -- renders a template
# <%_visitor Ruby code %> -- see Tanuki::Application::visitor for details
# <l10n><en>English text</en> ... -- other localizations </l10n>
+ #
+ # All of these tags, except +l10n+, have a single line syntax:
+ #
+ # % a line of Ruby code -- treated as <% line -%>
+ # %~ a line of Ruby code -- treated as <% line -%>
+ # %= Ruby expression -- treated as <%= line -%>
+ # %# comment -- ignored -- treated as <%# line -%>
+ # %% -- replace with % if first thing on a line
+ # %! Ruby expression that returns a template -- treated as <%! line -%>
+ # %_visitor Ruby code -- treated as <%_visitor line -%>
class TemplateCompiler
class << self
@@ -167,13 +176,13 @@ def process_code_state(ios, src, state)
src.strip!
src.gsub!(/^[ \t]+/, '')
case state
- when :code_line, :code_span then
+ when /code_(?:line_)?span/ then
ios << "\n#{src}"
- when :code_print then
+ when /code_(?:line_)?print/ then
ios << "\n_.((#{src}),ctx)"
- when :code_template then
+ when /code_(?:line_)?template/ then
ios << "\n(#{src}).(_,ctx)"
- when :code_visitor
+ when /code_(?:line_)?visitor/
m = src.match(/^([^ \(]+)?(\([^\)]*\))?\s*(.*)$/)
ios << "\n#{m[1]}_result=(#{m[3]}).(#{m[1]}_visitor#{m[2]},ctx)"
when :l10n then
@@ -184,9 +193,9 @@ def process_code_state(ios, src, state)
# Returns the next expected pattern for a given +state+.
def expect_pattern(state)
case state
- when :outer then %r{^\s*%%?|<%[=!_#%]?|<l10n>}
- when :code_line then %r{\n|\Z}
- when /code_(?:span|print|template|visitor|comment)/ then %r{[-%]?%>}
+ when :outer then %r{(?:^\s*|<)%[~=!_#%]?|<l10n>}
+ when /\Acode_line/ then %r{\n|\Z}
+ when /\Acode_(?:span|print|template|visitor|comment)/ then %r{[-%]?%>}
when :l10n then %r{<\/l10n>}
end
end
@@ -196,18 +205,22 @@ def next_state(state, match)
case state
when :outer then
case match
- when /\A\s*%\Z/ then :code_line
- when /\A\s*%%\Z/ then :code_skip
when '<%' then :code_span
when '<%=' then :code_print
when '<%!' then :code_template
when '<%_' then :code_visitor
when '<%#' then :code_comment
when '<%%' then :code_skip
when '<l10n>' then :l10n
+ when /\A\s*%~?\Z/ then :code_line_span
+ when /\A\s*%=\Z/ then :code_line_print
+ when /\A\s*%!\Z/ then :code_line_template
+ when /\A\s*%_\Z/ then :code_line_visitor
+ when /\A\s*%#\Z/ then :code_line_comment
+ when /\A\s*%%\Z/ then :code_skip
end
- when :code_line then :outer
- when /code_(?:span|print|template|visitor|comment)/ then
+ when /\Acode_line/ then :outer
+ when /\Acode_(?:span|print|template|visitor|comment)/ then
case match
when '%%>' then :code_skip
else :outer
@@ -17,36 +17,56 @@ module Tanuki
@ios.string.should == %Q{\n_.("hello",ctx)}
end
- it 'should treat % at the beginning of lines as Ruby code' do
- TemplateCompiler.compile(@ios, "% code\n %code")
- @ios.string.should == %Q{\ncode\ncode}
- end
-
it 'should treat <% %> and <% -%> as Ruby code' do
TemplateCompiler.compile(@ios, "<% code %>\n <% code -%>\ntext")
@ios.string.should == %Q{\ncode\n_.("\\n ",ctx)\ncode\n_.("text",ctx)}
end
+ it 'should treat % at the beginning of lines as Ruby code' do
+ TemplateCompiler.compile(@ios, "% code\n %code")
+ @ios.string.should == %Q{\ncode\ncode}
+ end
+
it 'should treat <%= %> and <%= -%> as printable Ruby code' do
TemplateCompiler.compile(@ios, 'text<%= code %>text')
@ios.string.should == %Q{\n_.("text",ctx)\n_.((code),ctx)\n_.("text",ctx)}
end
+ it 'should treat %= at the beginning of lines as printable Ruby code' do
+ TemplateCompiler.compile(@ios, "text\n%= code\ntext")
+ @ios.string.should == %Q{\n_.("text\\n",ctx)\n_.((code),ctx)\n_.("text",ctx)}
+ end
+
it 'should treat <%! %> and <%! -%> as Ruby code that returns a template' do
TemplateCompiler.compile(@ios, 'text<%! code %>text')
@ios.string.should == %Q{\n_.("text",ctx)\n(code).(_,ctx)\n_.("text",ctx)}
end
+ it 'should treat %! at the beginning of lines as Ruby code that returns a template' do
+ TemplateCompiler.compile(@ios, "text\n%! code\ntext")
+ @ios.string.should == %Q{\n_.("text\\n",ctx)\n(code).(_,ctx)\n_.("text",ctx)}
+ end
+
it 'should treat <%_ %> and <%_ -%> as Ruby code that calls a visitor' do
TemplateCompiler.compile(@ios, "<%_foo code %><%_foo(x, y) code %>")
@ios.string.should == %Q{\nfoo_result=(code).(foo_visitor,ctx)\nfoo_result=(code).(foo_visitor(x, y),ctx)}
end
+ it 'should treat %_ at the beginning of lines as Ruby code that calls a visitor' do
+ TemplateCompiler.compile(@ios, "%_foo code\n%_foo(x, y) code")
+ @ios.string.should == %Q{\nfoo_result=(code).(foo_visitor,ctx)\nfoo_result=(code).(foo_visitor(x, y),ctx)}
+ end
+
it 'should treat <%# %> and <%# -%> as comments' do
TemplateCompiler.compile(@ios, "<% code %><%# comment -%>\ntext")
@ios.string.should == %Q{\ncode\n_.("text",ctx)}
end
+ it 'should treat %# at the beginning of lines as a comment' do
+ TemplateCompiler.compile(@ios, "% code\n%# comment\ntext")
+ @ios.string.should == %Q{\ncode\n_.("text",ctx)}
+ end
+
it 'should treat <%% and %%> as escaped <% and %>' do
TemplateCompiler.compile(@ios, '<% code %>text<%% code %><% code %%><% code %>')
@ios.string.should == %Q{\ncode\n_.("text<% code %>",ctx)\ncode %><% code}

0 comments on commit 43a647a

Please sign in to comment.