Skip to content

Instantly share code, notes, and snippets.

@wrs
Created July 5, 2010 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrs/464709 to your computer and use it in GitHub Desktop.
Save wrs/464709 to your computer and use it in GitHub Desktop.
Haml+CDATA filter for Haml
# Generates HAML surrounded by CDATA tags.
# Useful for JavaScript templates (e.g., jquery.mustache or jQote).
#
# Example:
# %script#foo_tmpl(type="text/x-mustache")
# :hamlcdata
# %p This is HAML enclosed in CDATA.
# %p You can use it for templating {{stuff}}.
# =>
# <script id='foo_tmpl' type='text/x-mustache'>
# <![CDATA[
# <p>This is HAML enclosed in CDATA.</p>
# <p>You can use it for templating {{stuff}}.</p>
# ]]>
# </script>
module Haml
module Filters
module Hamlcdata
include Base
# Don't let interpolation happen in this context -- wait for the inner Haml render.
# According to the Haml source this could break in new versions. If so, copy the revised
# code from lib/haml/filters.rb and remove the interpolation step.
#
def compile(precompiler, text)
resolve_lazy_requires
filter = self
precompiler.instance_eval do
# REMOVED from Base.compile
# if contains_interpolation?(text)
# return if options[:suppress_eval]
#
# text = unescape_interpolation(text).gsub(/(\\+)n/) do |s|
# escapes = $1.size
# next s if escapes % 2 == 0
# ("\\" * (escapes - 1)) + "\n"
# end
# newline if text.gsub!(/\n"\Z/, "\\n\"")
# push_script <<RUBY.strip, :escape_html => false
# find_and_preserve(#{filter.inspect}.render_with_options(#{text}, _hamlout.options))
# RUBY
# return
# end
rendered = Haml::Helpers::find_and_preserve(filter.render_with_options(text, precompiler.options), precompiler.options[:preserve])
if !options[:ugly]
push_text(rendered.rstrip.gsub("\n", "\n#{' ' * @output_tabs}"))
else
push_text(rendered.rstrip)
end
(text.count("\n") - 1).times {newline}
resolve_newlines
newline
end
end
def render(text)
rendered = ::Haml::Engine.new(text).render
<<-HTML
<![CDATA[
#{rendered}
]]>
HTML
end
end
end
end
# Author: Walter Smith http://waltersmith.us/
# Copyright 2010 Informed Biometry Corporation
# Copyright 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment