mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Began implementing filters using Tilt, WIP
Note that this is a work in progress on issue #469 and will be rebased before it's merged into master.
This commit is contained in:
parent
54d4789915
commit
e1201cc6b1
8 changed files with 60 additions and 233 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -10,4 +10,5 @@
|
|||
Gemfile.lock
|
||||
.rvmrc
|
||||
.rbx
|
||||
tmp
|
||||
.sass-cache
|
||||
|
|
5
Gemfile
5
Gemfile
|
@ -4,3 +4,8 @@ gemspec
|
|||
platform :mri do
|
||||
gem "ruby-prof"
|
||||
end
|
||||
|
||||
gem "therubyracer"
|
||||
gem "less"
|
||||
gem "coffee-script"
|
||||
gem "RedCloth"
|
|
@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
|
|||
spec.has_rdoc = false
|
||||
spec.test_files = Dir["test/**/*_test.rb"]
|
||||
|
||||
spec.add_dependency "tilt"
|
||||
|
||||
spec.add_development_dependency 'yard', '>= 0.5.3'
|
||||
spec.add_development_dependency 'maruku', '>= 0.5.9'
|
||||
spec.add_development_dependency 'hpricot'
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
require "tilt"
|
||||
|
||||
module Haml
|
||||
# The module containing the default Haml filters,
|
||||
|
@ -185,6 +186,19 @@ rescue LoadError; end
|
|||
|
||||
module Haml
|
||||
module Filters
|
||||
|
||||
# Filters for other template engines are provided by Tilt.
|
||||
["Sass", "Scss", "Less", "CoffeeScript", "Maruku"].each do |name|
|
||||
module_eval(<<-END)
|
||||
module #{name}
|
||||
include Base
|
||||
def render(text)
|
||||
Tilt::#{name}Template.new {text}.render
|
||||
end
|
||||
end
|
||||
END
|
||||
end
|
||||
|
||||
# Does not parse the filtered text.
|
||||
# This is useful for large blocks of text without HTML tags,
|
||||
# when you don't want lines starting with `.` or `-`
|
||||
|
@ -302,28 +316,6 @@ END
|
|||
end
|
||||
end
|
||||
|
||||
# Parses the filtered text with {Sass} to produce CSS output.
|
||||
module Sass
|
||||
include Base
|
||||
lazy_require 'sass/plugin'
|
||||
|
||||
# @see Base#render
|
||||
def render(text)
|
||||
::Sass::Engine.new(text, ::Sass::Plugin.engine_options).render
|
||||
end
|
||||
end
|
||||
|
||||
# Parses the filtered text with {Sass} to produce CSS output using SCSS syntax.
|
||||
module Scss
|
||||
include Base
|
||||
lazy_require 'sass/plugin'
|
||||
|
||||
# @see Base#render
|
||||
def render(text)
|
||||
::Sass::Engine.new(text, ::Sass::Plugin.engine_options.merge(:syntax => :scss)).render
|
||||
end
|
||||
end
|
||||
|
||||
# Parses the filtered text with ERB.
|
||||
# Not available if the {file:REFERENCE.md#suppress_eval-option `:suppress_eval`} option is set to true.
|
||||
# Embedded Ruby code is evaluated in the same context as the Haml template.
|
||||
|
@ -340,65 +332,6 @@ END
|
|||
end
|
||||
end
|
||||
|
||||
# Parses the filtered text with [Textile](http://www.textism.com/tools/textile).
|
||||
# Only works if [RedCloth](http://redcloth.org) is installed.
|
||||
module Textile
|
||||
include Base
|
||||
lazy_require 'redcloth'
|
||||
|
||||
# @see Base#render
|
||||
def render(text)
|
||||
::RedCloth.new(text).to_html(:textile)
|
||||
end
|
||||
end
|
||||
# An alias for the Textile filter,
|
||||
# since the only available Textile parser is RedCloth.
|
||||
# @api public
|
||||
RedCloth = Textile
|
||||
Filters.defined['redcloth'] = RedCloth
|
||||
|
||||
# Parses the filtered text with [Markdown](http://daringfireball.net/projects/markdown).
|
||||
# Only works if [RDiscount](https://github.com/rtomayko/rdiscount),
|
||||
# [RPeg-Markdown](https://github.com/rtomayko/rpeg-markdown),
|
||||
# [Maruku](http://maruku.rubyforge.org),
|
||||
# [Redcarpet](https://github.com/tanoku/redcarpet),
|
||||
# or [Kramdown](https://github.com/gettalong/kramdown) are installed.
|
||||
module Markdown
|
||||
include Base
|
||||
lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth', 'redcarpet', 'kramdown'
|
||||
|
||||
# @see Base#render
|
||||
def render(text)
|
||||
engine = case @required
|
||||
when 'rdiscount'
|
||||
::RDiscount
|
||||
when 'peg_markdown'
|
||||
::PEGMarkdown
|
||||
when 'maruku'
|
||||
::Maruku
|
||||
when 'bluecloth'
|
||||
::BlueCloth
|
||||
when 'redcarpet'
|
||||
::Redcarpet
|
||||
when 'kramdown'
|
||||
::Kramdown::Document
|
||||
end
|
||||
engine.new(text).to_html
|
||||
end
|
||||
end
|
||||
|
||||
# Parses the filtered text with [Maruku](http://maruku.rubyforge.org),
|
||||
# which has some non-standard extensions to Markdown.
|
||||
module Maruku
|
||||
include Base
|
||||
lazy_require 'maruku'
|
||||
|
||||
# @see Base#render
|
||||
def render(text)
|
||||
::Maruku.new(text).to_html
|
||||
end
|
||||
end
|
||||
|
||||
# Parses the filtered text with [Redcarpet](https://github.com/tanoku/redcarpet)
|
||||
module Redcarpet
|
||||
include Base
|
||||
|
|
34
test/filters_test.rb
Normal file
34
test/filters_test.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
require 'test_helper'
|
||||
|
||||
class FiltersTest < MiniTest::Unit::TestCase
|
||||
|
||||
def render(text, options = {}, &block)
|
||||
scope = options.delete(:scope) || Object.new
|
||||
locals = options.delete(:locals) || {}
|
||||
Haml::Engine.new(text, options).to_html(scope, locals, &block)
|
||||
end
|
||||
|
||||
TESTS = {
|
||||
:sass => ["sass", /width: 100;/, ":sass\n p\n width: 100"],
|
||||
:scss => ["sass", /width: 100;/, ":scss\n $width: 100;\n p {\n width: $width;\n }"],
|
||||
:less => ["less", /width: 100;/, ":less\n @width: 100;\n p {\n width: @width;\n }"],
|
||||
:coffeescript => ["coffee_script", /var foo;/, ":coffeescript\n foo = 'bar'"],
|
||||
:maruku => ["maruku", /h1/, ":maruku\n # foo"],
|
||||
}
|
||||
|
||||
TESTS.each do |key, value|
|
||||
library, pattern, haml = value
|
||||
|
||||
define_method("test_#{key}_filter") do
|
||||
begin
|
||||
Haml::Util.silence_warnings do
|
||||
require library
|
||||
assert_match(pattern, render(haml))
|
||||
end
|
||||
rescue LoadError
|
||||
warn "Could not load #{key} filter's dependencies"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,69 +0,0 @@
|
|||
<style>
|
||||
/* line 1 */
|
||||
p { border-style: dotted; border-width: 22px; border-color: fuchsia; }
|
||||
|
||||
/* line 6 */
|
||||
h1 { font-weight: normal; }
|
||||
</style>
|
||||
<style>
|
||||
/* line 1 */
|
||||
p { border-style: dotted; border-width: 22px; }
|
||||
|
||||
/* line 7 */
|
||||
h1 { font-weight: normal; }
|
||||
</style>
|
||||
TESTING HAHAHAHA!
|
||||
<p>
|
||||
<script type='text/javascript'>
|
||||
//<![CDATA[
|
||||
function newline(str) {
|
||||
return "\n" + str;
|
||||
}
|
||||
//]]>
|
||||
</script>
|
||||
</p>
|
||||
This
|
||||
Is
|
||||
Plain
|
||||
Text
|
||||
%strong right?
|
||||
#{not interpolated}
|
||||
\3
|
||||
\#{also not}
|
||||
\\
|
||||
<p>
|
||||
<pre>This pre is pretty deeply
 nested.
 Does interpolation work?
|
||||
This one is, too.
Nested, that is.
</pre>
|
||||
</p>
|
||||
<ul>
|
||||
|
||||
<li>a</li>
|
||||
|
||||
<li>b</li>
|
||||
|
||||
<li>c</li>
|
||||
|
||||
<li>d</li>
|
||||
|
||||
<li>e</li>
|
||||
|
||||
<li>f</li>
|
||||
|
||||
<li>g</li>
|
||||
|
||||
<li>h</li>
|
||||
|
||||
<li>i</li>
|
||||
|
||||
<li>j</li>
|
||||
|
||||
|
||||
|
||||
</ul>
|
||||
<div class='res'>178</div>
|
||||
Text!
|
||||
Hello, World!
|
||||
How are you doing today?
|
||||
<div class="foo">
|
||||
<p>I think &mdash; or do I?</p>
|
||||
</div>
|
|
@ -41,10 +41,10 @@ end
|
|||
|
||||
class TemplateTest < MiniTest::Unit::TestCase
|
||||
TEMPLATE_PATH = File.join(File.dirname(__FILE__), "templates")
|
||||
TEMPLATES = %w{ very_basic standard helpers
|
||||
whitespace_handling original_engine list helpful
|
||||
silent_script tag_parsing just_stuff partials
|
||||
filters nuke_outer_whitespace nuke_inner_whitespace
|
||||
TEMPLATES = %w{ very_basic standard helpers
|
||||
whitespace_handling original_engine list helpful
|
||||
silent_script tag_parsing just_stuff partials
|
||||
nuke_outer_whitespace nuke_inner_whitespace
|
||||
render_layout }
|
||||
# partial layouts were introduced in 2.0.0
|
||||
TEMPLATES << 'partial_layout' unless ActionPack::VERSION::MAJOR < 2
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
%style
|
||||
- width = 5 + 17
|
||||
:sass
|
||||
p
|
||||
:border
|
||||
:style dotted
|
||||
:width #{width}px
|
||||
:color #ff00ff
|
||||
h1
|
||||
:font-weight normal
|
||||
|
||||
%style
|
||||
- width = 5 + 17
|
||||
:scss
|
||||
p {
|
||||
border: {
|
||||
style: dotted;
|
||||
width: #{width}px;
|
||||
}
|
||||
}
|
||||
h1 {
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
:test
|
||||
This
|
||||
Should
|
||||
Not
|
||||
Print
|
||||
|
||||
%p
|
||||
:javascript
|
||||
function newline(str) {
|
||||
return "\n" + str;
|
||||
}
|
||||
|
||||
:plain
|
||||
This
|
||||
Is
|
||||
Plain
|
||||
Text
|
||||
%strong right?
|
||||
\#{not interpolated}
|
||||
\\#{1 + 2}
|
||||
\\\#{also not}
|
||||
\\
|
||||
|
||||
- last = "noitalo"
|
||||
%p
|
||||
%pre
|
||||
:preserve
|
||||
This pre is pretty deeply
|
||||
nested.
|
||||
Does #{"interp" + last.reverse} work?
|
||||
:preserve
|
||||
This one is, too.
|
||||
Nested, that is.
|
||||
|
||||
- num = 10
|
||||
%ul
|
||||
:erb
|
||||
<% num.times do |c| %>
|
||||
<li><%= (c+97).chr %></li>
|
||||
<% end %>
|
||||
<% res = 178 %>
|
||||
|
||||
.res= res
|
||||
|
||||
= "Text!"
|
||||
|
||||
- var = "Hello"
|
||||
:ruby
|
||||
printf "%s, World!\n", var
|
||||
print "How are you doing today?\n"
|
||||
|
||||
:escaped
|
||||
<div class="foo">
|
||||
<p>I think — or do I?</p>
|
||||
</div>
|
Loading…
Reference in a new issue