Skip to content

Commit

Permalink
Somewhat saner handling of doctypes.
Browse files Browse the repository at this point in the history
git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@97 7063305b-7217-0410-af8c-cdc13e5119b9
  • Loading branch information
nex3 committed Oct 22, 2006
1 parent ce13c44 commit 0105ca0
Showing 1 changed file with 48 additions and 39 deletions.
87 changes: 48 additions & 39 deletions lib/haml/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Engine
# Designates an XHTML/XML comment.
COMMENT = '/'[0]

# Designates an XHTML doctype.
DOCTYPE = '!'[0]

# Designates script, the result of which is output.
SCRIPT = '='[0]

Expand All @@ -40,13 +43,17 @@ class Engine
# When following SILENT_SCRIPT, designates a comment that is not output.
SILENT_COMMENT = '#'[0]

# Designates a non-parsed line. Not actually a character.
PLAIN_TEXT = -1

# Keeps track of the ASCII values of the characters that begin a
# specially-interpreted line.
SPECIAL_CHARACTERS = [
ELEMENT,
DIV_CLASS,
DIV_ID,
COMMENT,
DOCTYPE,
SCRIPT,
FLAT_SCRIPT,
SILENT_SCRIPT
Expand Down Expand Up @@ -142,53 +149,55 @@ def do_precompile
# This method doesn't return anything; it simply processes the line and
# adds the appropriate code to <tt>@precompiled</tt>.
def process_line(count, line, index)
if line.lstrip[0, 3] == '!!!'
push_text '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
if count > @to_close_stack.size

# Indentation has been increased without a new tag
if @latest_command == SILENT_SCRIPT

else
if count > @to_close_stack.size
# The indentation was increased after silent script,
# it must be a block
@to_close_stack.push '_haml_end_block'
end

# Indentation has been increased without a new tag
if @latest_command == SILENT_SCRIPT
elsif count <= @to_close_stack.size && @to_close_stack.size > 0 &&
(line.length == 0 || line[0] != SILENT_SCRIPT || !MID_BLOCK_KEYWORDS.include?(line[1..-1].split[0]))

# The indentation was increased after silent script,
# it must be a block
@to_close_stack.push '_haml_end_block'
# The tabulation has gone down, and it's not because of one of
# Ruby's mid-block keywords
(@to_close_stack.size - count).times { close }
end

if line.length > 0
@latest_command = line[0]
case @latest_command
when DIV_CLASS, DIV_ID
render_div(line, index)
when ELEMENT
render_tag(line, index)
when COMMENT
render_comment(line)
when SCRIPT
push_script(line[1..-1], false, index)
when FLAT_SCRIPT
push_script(line[1..-1], true, index)
when SILENT_SCRIPT
sub_line = line[1..-1]
unless sub_line[0] == SILENT_COMMENT
push_silent(sub_line, index)
else
@latest_command = SILENT_COMMENT
end

elsif count <= @to_close_stack.size && @to_close_stack.size > 0 &&
(line.length == 0 || line[0] != SILENT_SCRIPT || !MID_BLOCK_KEYWORDS.include?(line[1..-1].split[0]))

# The tabulation has gone down, and it's not because of one of
# Ruby's mid-block keywords
(@to_close_stack.size - count).times { close }
end

if line.length > 0
@latest_command = line[0]
case @latest_command
when DIV_CLASS, DIV_ID
render_div(line, index)
when ELEMENT
render_tag(line, index)
when COMMENT
render_comment(line)
when SCRIPT
push_script(line[1..-1], false, index)
when FLAT_SCRIPT
push_script(line[1..-1], true, index)
when SILENT_SCRIPT
sub_line = line[1..-1]
unless sub_line[0] == SILENT_COMMENT
push_silent(sub_line, index)
else
@latest_command = SILENT_COMMENT
end
when DOCTYPE
if line[0...3] == '!!!'
push_text '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
else
@latest_command = PLAIN_TEXT
push_text line.strip
end
else
@latest_command = PLAIN_TEXT
push_text line.strip
end

end
end

Expand Down

0 comments on commit 0105ca0

Please sign in to comment.