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

Deprecating ~.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@353 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-02-06 08:08:32 +00:00
parent cae1979804
commit b200bb4920
7 changed files with 175 additions and 70 deletions

View file

@ -195,31 +195,27 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
# <div class='description'>What a cool item!</div>
# </div>
#
# ==== = and ~
# ==== =
#
# <tt>=</tt> and <tt>~</tt> are placed at the end of a tag definition,
# <tt>=</tt> is placed at the end of a tag definition,
# after class, id, and attribute declarations.
# They're just shortcuts for inserting Ruby code into an element.
# They work the same as <tt>=</tt> and <tt>~</tt> without a tag;
# see below for documentation of those.
# It's just a shortcut for inserting Ruby code into an element.
# It works the same as <tt>=</tt> without a tag:
# it inserts the result of the Ruby code into the template.
# However, if the result is short enough,
# it is displayed entirely on one line.
# For example:
#
# %p= "hello"
# %h1~ 1 + 2
#
# is not quite the same as:
#
# %p
# = "hello"
# %h1
# ~ 1 + 2
#
# It's compiled to:
#
# <p>hello</p>
# <h1>3</h1>
#
# === XHTML Helpers
#
@ -418,12 +414,20 @@ $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.
#
# [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.
#
# [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.
#
# [erb] Parses the filtered text with ERB, like an RHTML template.
# Not available if the <tt>suppress_eval</tt> option is set to true.
# At the moment, this doesn't support access to variables
@ -464,52 +468,6 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
# yo
# </p>
#
# ==== ~
#
# The tilde character works the same as the equals character,
# but the output is modified in such a way
# that newlines in whitespace-sensitive elements work properly.
# For example:
#
# %foo
# = "Woah <pre> this is \n</pre> crazy"
# %foo2
# ~ "Woah <pre> this is \n</pre> crazy"
#
# is compiled to:
#
# <foo>
# Woah <pre> this is
# </pre> crazy
# </foo>
# <foo2>
# Woah <pre> this is &#x000A;</pre> crazy
# </foo2>
#
# If the ~ character isn't followed by text,
# it doesn't evaluate Ruby at all.
# Instead, an indented section following it will be rendered
# in a whitespace-sensitive manner,
# using HTML encodings for newlines.
# For example:
#
# For example:
#
# .house
# %pre
# ~
# /^^^\
# |[] []|
# |_____|
#
# is compiled to:
#
# <div class="house">
# <pre>
# &#x000A; /^^^\&#x000A;|[] []|&#x000A;|_____|&#x000A;
# </pre>
# </div>
#
# ==== -
#
# The hyphen character makes the text following it into "silent script":

View file

@ -52,7 +52,7 @@ module Haml
# instance_eval.
def push_script(result, tabulation, flattened)
if flattened
result = find_and_flatten(result)
result = Haml::Helpers.find_and_preserve(result)
end
unless result.nil?
result = result.to_s
@ -200,16 +200,6 @@ module Haml
def one_liner?(value)
value.length <= ONE_LINER_LENGTH && value.scan(/\n/).empty?
end
# Isolates the whitespace-sensitive tags in the string and uses Haml::Helpers#flatten
# to convert any endlines inside them into html entities.
def find_and_flatten(input)
input = input.to_s
input.scan(/<(textarea|code|pre)[^>]*>(.*?)<\/\1>/im) do |tag, contents|
input = input.gsub(contents, Haml::Helpers.flatten(contents))
end
input
end
end
end

View file

@ -92,6 +92,13 @@ module Haml
# is a member of this array.
MID_BLOCK_KEYWORDS = ['else', 'elsif', 'rescue', 'ensure', 'when']
FLAT_WARNING = <<END
Haml deprecation warning:
The ~ command is deprecated and will be removed in future Haml versions.
Use the :preserve filter, the preserve helper, or the find_and_preserve
helper instead.
END
# Creates a new instace of Haml::Engine that will compile the given
# template string when <tt>to_html</tt> is called.
# See README for available options.
@ -108,7 +115,8 @@ module Haml
:locals => {},
:filters => {
'sass' => Sass::Engine,
'plain' => Haml::Filters::Plain
'plain' => Haml::Filters::Plain,
'preserve' => Haml::Filters::Preserve
}
}
@ -278,6 +286,7 @@ module Haml
when SCRIPT
push_script(line[1..-1], false)
when FLAT_SCRIPT
warn(FLAT_WARNING) unless defined?(Test::Unit)
push_flat_script(line[1..-1])
when SILENT_SCRIPT
sub_line = line[1..-1]
@ -527,7 +536,13 @@ module Haml
raise HamlError.new("Filter \"#{filter}\" is not defined!")
end
else
push_text(filter.new(@filter_buffer).render.rstrip.gsub("\n", "\n#{' ' * @output_tabs}"))
filtered = filter.new(@filter_buffer).render
unless filter == Haml::Filters::Preserve
push_text(filtered.rstrip.gsub("\n", "\n#{' ' * @output_tabs}"))
else
push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n\"\n")
end
end
@filter_buffer = nil
@ -552,6 +567,11 @@ module Haml
end
flattened = (action == '~')
if flattened && !defined?(Test::Unit)
warn(FLAT_WARNING)
end
value_exists = !value.empty?
attributes_hash = "nil" unless attributes_hash
object_ref = "nil" unless object_ref

View file

@ -50,6 +50,16 @@ module Haml
end
end
class Preserve
def initialize(text)
@text = text
end
def render
Haml::Helpers.preserve(@text)
end
end
unless NOT_LOADED.include? 'bluecloth'
Markdown = BlueCloth unless defined?(Markdown)
end

View file

@ -16,13 +16,25 @@ module Haml
@@action_view
end
# Isolates the whitespace-sensitive tags in the string and uses preserve
# to convert any endlines inside them into HTML entities for endlines.
def find_and_preserve(input)
input = input.to_s
input.scan(/<(textarea|code|pre)[^>]*>(.*?)<\/\1>/im) do |tag, contents|
input = input.gsub(contents, preserve(contents))
end
input
end
# Takes any string, finds all the endlines and converts them to
# HTML entities for endlines so they'll render correctly in
# whitespace-sensitive tags without screwing up the indentation.
def flatten(input)
def preserve(input)
input.gsub(/\n/, '&#x000A;').gsub(/\r/, '')
end
alias_method :flatten, :preserve
# Takes an Enumerable object and a block
# and iterates over the object,
# yielding each element to a Haml block

View file

@ -48,4 +48,54 @@
foo&#x000A;
bar
</pre>
<div id='whitespace_test'>
<div class='text_area_test_area'>
<textarea>Oneline</textarea>
</div>
<textarea>BLAH
</textarea>
<div class='text_area_test_area'>
<textarea>Two&#x000A;lines</textarea>
</div>
<textarea>BLAH
</textarea>
<div class='text_area_test_area'>
<textarea>Oneline</textarea>
</div>
<textarea>BLAH&#x000A;</textarea>
<div class='text_area_test_area'>
<textarea>Two&#x000A;lines</textarea>
</div>
<textarea>BLAH&#x000A;</textarea>
<div id='flattened'>
<div class='text_area_test_area'>
<textarea>Two&#x000A;lines</textarea>
</div>
<textarea>BLAH&#x000A;</textarea>
</div>
</div>
<div class='hithere'>
Foo bar
<pre>foo bar</pre>
<pre>foo&#x000A;bar</pre>
<p><pre>foo&#x000A;bar</pre></p>
<p>
foo
bar
</p>
<pre>
___&#x000A; ,o88888&#x000A; ,o8888888'&#x000A; ,:o:o:oooo. ,8O88Pd8888"&#x000A; ,.::.::o:ooooOoOoO. ,oO8O8Pd888'"&#x000A; ,.:.::o:ooOoOoOO8O8OOo.8OOPd8O8O"&#x000A; , ..:.::o:ooOoOOOO8OOOOo.FdO8O8"&#x000A; , ..:.::o:ooOoOO8O888O8O,COCOO"&#x000A; , . ..:.::o:ooOoOOOO8OOOOCOCO"&#x000A; . ..:.::o:ooOoOoOO8O8OCCCC"o&#x000A; . ..:.::o:ooooOoCoCCC"o:o&#x000A; . ..:.::o:o:,cooooCo"oo:o:&#x000A; ` . . ..:.:cocoooo"'o:o:::'&#x000A; .` . ..::ccccoc"'o:o:o:::'&#x000A; :.:. ,c:cccc"':.:.:.:.:.'&#x000A; ..:.:"'`::::c:"'..:.:.:.:.:.' http://www.chris.com/ASCII/&#x000A; ...:.'.:.::::"' . . . . .'&#x000A; .. . ....:."' ` . . . ''&#x000A; . . . ...."'&#x000A; .. . ."' -hrr-&#x000A; .&#x000A;&#x000A;&#x000A; It's a planet!&#x000A;%strong This shouldn't be bold!&#x000A;
</pre>
<strong>This should!</strong>
<textarea>
___ ___ ___ ___&#x000A; /\__\ /\ \ /\__\ /\__\&#x000A; /:/ / /::\ \ /::| | /:/ /&#x000A; /:/__/ /:/\:\ \ /:|:| | /:/ /&#x000A; /::\ \ ___ /::\~\:\ \ /:/|:|__|__ /:/ /&#x000A; /:/\:\ /\__\ /:/\:\ \:\__\ /:/ |::::\__\ /:/__/&#x000A; \/__\:\/:/ / \/__\:\/:/ / \/__/~~/:/ / \:\ \&#x000A; \::/ / \::/ / /:/ / \:\ \&#x000A; /:/ / /:/ / /:/ / \:\ \&#x000A; /:/ / /:/ / /:/ / \:\__\&#x000A; \/__/ \/__/ \/__/ \/__/&#x000A;&#x000A; Many&#x000A; thanks&#x000A; to&#x000A; http://www.network-science.de/ascii/&#x000A;
<strong>indeed!</strong>
</textarea>
</div>
<div class='foo'>
13
</div>
<pre>
foo&#x000A;
bar
</pre>

View file

@ -64,3 +64,68 @@
~
foo
bar
#whitespace_test
= render :file => "_text_area.haml", :locals => { :value => "Oneline" }
= render :file => "_text_area.haml", :locals => { :value => "Two\nlines" }
= find_and_preserve render(:file => "_text_area.haml", :locals => { :value => "Oneline" })
= find_and_preserve render(:file => "_text_area.haml", :locals => { :value => "Two\nlines" })
#flattened= find_and_preserve render(:file => "_text_area.haml", :locals => { :value => "Two\nlines" })
.hithere
= find_and_preserve("Foo bar")
= find_and_preserve("<pre>foo bar</pre>")
= find_and_preserve("<pre>foo\nbar</pre>")
%p= find_and_preserve("<pre>foo\nbar</pre>")
%p= find_and_preserve("foo\nbar")
%pre
:preserve
___
,o88888
,o8888888'
,:o:o:oooo. ,8O88Pd8888"
,.::.::o:ooooOoOoO. ,oO8O8Pd888'"
,.:.::o:ooOoOoOO8O8OOo.8OOPd8O8O"
, ..:.::o:ooOoOOOO8OOOOo.FdO8O8"
, ..:.::o:ooOoOO8O888O8O,COCOO"
, . ..:.::o:ooOoOOOO8OOOOCOCO"
. ..:.::o:ooOoOoOO8O8OCCCC"o
. ..:.::o:ooooOoCoCCC"o:o
. ..:.::o:o:,cooooCo"oo:o:
` . . ..:.:cocoooo"'o:o:::'
.` . ..::ccccoc"'o:o:o:::'
:.:. ,c:cccc"':.:.:.:.:.'
..:.:"'`::::c:"'..:.:.:.:.:.' http://www.chris.com/ASCII/
...:.'.:.::::"' . . . . .'
.. . ....:."' ` . . . ''
. . . ...."'
.. . ."' -hrr-
.
It's a planet!
%strong This shouldn't be bold!
%strong This should!
%textarea
:preserve
___ ___ ___ ___
/\__\ /\ \ /\__\ /\__\
/:/ / /::\ \ /::| | /:/ /
/:/__/ /:/\:\ \ /:|:| | /:/ /
/::\ \ ___ /::\~\:\ \ /:/|:|__|__ /:/ /
/:/\:\ /\__\ /:/\:\ \:\__\ /:/ |::::\__\ /:/__/
\/__\:\/:/ / \/__\:\/:/ / \/__/~~/:/ / \:\ \
\::/ / \::/ / /:/ / \:\ \
/:/ / /:/ / /:/ / \:\ \
/:/ / /:/ / /:/ / \:\__\
\/__/ \/__/ \/__/ \/__/
Many
thanks
to
http://www.network-science.de/ascii/
%strong indeed!
.foo
= find_and_preserve(13)
%pre
:preserve
foo
bar