1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Add a Javascript filter.

Based lightly on Les Hill's patch at http://pastie.org/156177.
This commit is contained in:
Nathan Weizenbaum 2008-02-23 01:56:16 -08:00
parent a3388f9873
commit d827f57815
5 changed files with 59 additions and 26 deletions

View file

@ -546,39 +546,42 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
#
# Haml has the following filters defined:
#
# [plain] 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 <tt>.</tt> or <tt>-</tt>
# to be parsed.
# [plain] 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 <tt>.</tt> or <tt>-</tt>
# to be parsed.
#
# [ruby] Parses the filtered text with the normal Ruby interpreter.
# All output sent to <tt>$stdout</tt>, like with +puts+,
# is output into the Haml document.
# Not available if the <tt>suppress_eval</tt> option is set to true.
# The Ruby code is evaluated in the same context as the Haml template.
# [javascript] Surrounds the filtered text with <script> and CDATA tags.
# Useful for including inline Javascript.
#
# [preserve] Inserts the filtered text into the template with whitespace preserved.
# <tt>preserve</tt>d blocks of text aren't indented,
# and newlines are replaced with the HTML escape code for newlines,
# to preserve nice-looking output.
# [ruby] Parses the filtered text with the normal Ruby interpreter.
# All output sent to <tt>$stdout</tt>, like with +puts+,
# is output into the Haml document.
# Not available if the <tt>suppress_eval</tt> option is set to true.
# The Ruby code is evaluated in the same context as the Haml template.
#
# [erb] Parses the filtered text with ERB, like an RHTML template.
# Not available if the <tt>suppress_eval</tt> option is set to true.
# Embedded Ruby code is evaluated in the same context as the Haml template.
# [preserve] Inserts the filtered text into the template with whitespace preserved.
# <tt>preserve</tt>d blocks of text aren't indented,
# and newlines are replaced with the HTML escape code for newlines,
# to preserve nice-looking output.
#
# [sass] Parses the filtered text with Sass to produce CSS output.
# [erb] Parses the filtered text with ERB, like an RHTML template.
# Not available if the <tt>suppress_eval</tt> option is set to true.
# Embedded Ruby code is evaluated in the same context as the Haml template.
#
# [redcloth] Parses the filtered text with RedCloth (http://whytheluckystiff.net/ruby/redcloth),
# which uses both Textile and Markdown syntax.
# Only works if RedCloth is installed.
# [sass] Parses the filtered text with Sass to produce CSS output.
#
# [textile] Parses the filtered text with Textile (http://www.textism.com/tools/textile).
# Only works if RedCloth is installed.
# [redcloth] Parses the filtered text with RedCloth (http://whytheluckystiff.net/ruby/redcloth),
# which uses both Textile and Markdown syntax.
# Only works if RedCloth is installed.
#
# [markdown] Parses the filtered text with Markdown (http://daringfireball.net/projects/markdown).
# Only works if RedCloth or BlueCloth (http://www.deveiate.org/projects/BlueCloth)
# is installed
# (BlueCloth takes precedence if both are installed).
# [textile] Parses the filtered text with Textile (http://www.textism.com/tools/textile).
# Only works if RedCloth is installed.
#
# [markdown] Parses the filtered text with Markdown (http://daringfireball.net/projects/markdown).
# Only works if RedCloth or BlueCloth (http://www.deveiate.org/projects/BlueCloth)
# is installed
# (BlueCloth takes precedence if both are installed).
#
# You can also define your own filters (see Setting Options, below).
#

View file

@ -41,6 +41,7 @@ module Haml
:filters => {
'sass' => Haml::Filters::Sass,
'plain' => Haml::Filters::Plain,
'javascript' => Haml::Filters::Javascript,
'preserve' => Haml::Filters::Preserve,
'redcloth' => Haml::Filters::RedCloth,
'textile' => Haml::Filters::Textile,

View file

@ -142,6 +142,20 @@ module Haml
def render(text); text; end
end
module Javascript
include Base
def render(text)
<<END
<script type='text/javascript'>
//<![CDATA[
#{text.rstrip.gsub("\n", "\n ")}
//]]>
</script>
END
end
end
module Ruby
include Base
lazy_require 'stringio'

View file

@ -21,6 +21,15 @@ Wowie-zowie!</code></pre>
<p><em>pretty much the same as above</em></p>
<p>
<script type='text/javascript'>
//<![CDATA[
function newline(str) {
return "\n" + str;
}
//]]>
</script>
</p>
This
Is
Plain

View file

@ -30,6 +30,12 @@
_pretty much the same as above_
%p
:javascript
function newline(str) {
return "\n" + str;
}
:plain
This
Is