lib/fast_haml/parser.rb in fast_haml-0.1.8 vs lib/fast_haml/parser.rb in fast_haml-0.1.9
- old
+ new
@@ -3,10 +3,11 @@
require 'fast_haml/filter_parser'
require 'fast_haml/indent_tracker'
require 'fast_haml/line_parser'
require 'fast_haml/parser_utils'
require 'fast_haml/ruby_multiline'
+require 'fast_haml/script_parser'
require 'fast_haml/syntax_error'
module FastHaml
class Parser
def initialize(options = {})
@@ -40,22 +41,18 @@
@ast
end
private
-
DOCTYPE_PREFIX = '!'
ELEMENT_PREFIX = '%'
- SCRIPT_PREFIX = '='
COMMENT_PREFIX = '/'
SILENT_SCRIPT_PREFIX = '-'
DIV_ID_PREFIX = '#'
DIV_CLASS_PREFIX = '.'
FILTER_PREFIX = ':'
ESCAPE_PREFIX = '\\'
- PRESERVE_PREFIX = '~'
- SANITIZE_PREFIX = '&'
def parse_line(line)
text, indent = @indent_tracker.process(line, @line_parser.lineno)
if text.empty?
@@ -72,58 +69,29 @@
when ESCAPE_PREFIX
parse_plain(text[1 .. -1])
when ELEMENT_PREFIX
parse_element(text)
when DOCTYPE_PREFIX
- case
- when text.start_with?('!!!')
+ if text.start_with?('!!!')
parse_doctype(text)
- when text.start_with?('!==')
- parse_plain(text[3 .. -1].lstrip, escape_html: false)
- when text[1] == SCRIPT_PREFIX
- parse_script(text)
- when text[1] == PRESERVE_PREFIX
- parse_script("!=#{text[2 .. -1].lstrip}", preserve: true)
- when text[1] == ' '
- parse_plain(text[1 .. -1].lstrip, escape_html: false)
else
- parse_plain(text)
+ parse_script(text)
end
when COMMENT_PREFIX
parse_comment(text)
- when SCRIPT_PREFIX
- if text[1] == SCRIPT_PREFIX
- parse_plain(text[2 .. -1].strip)
- else
- parse_script(text)
- end
when SILENT_SCRIPT_PREFIX
parse_silent_script(text)
- when PRESERVE_PREFIX
- # preserve has no meaning in non-html_escape mode
- parse_script(text)
when DIV_ID_PREFIX, DIV_CLASS_PREFIX
if text.start_with?('#{')
- parse_plain(text)
+ parse_script(text)
else
parse_line("#{indent}%div#{text}")
end
when FILTER_PREFIX
parse_filter(text)
- when SANITIZE_PREFIX
- case
- when text.start_with?('&==')
- parse_plain(text[3 .. -1].lstrip)
- when text[1] == SCRIPT_PREFIX
- parse_script(text)
- when text[1] == PRESERVE_PREFIX
- parse_script("&=#{text[2 .. -1].lstrip}", preserve: true)
- else
- parse_plain(text[1 .. -1].strip)
- end
else
- parse_plain(text)
+ parse_script(text)
end
end
def parse_doctype(text)
@ast << Ast::Doctype.new(text[3 .. -1].strip)
@@ -150,33 +118,19 @@
else
syntax_error!('Unmatched brackets in conditional comment')
end
end
- def parse_plain(text, escape_html: true)
- @ast << Ast::Text.new(text, escape_html)
+ def parse_plain(text)
+ @ast << Ast::Text.new(text)
end
def parse_element(text)
- @ast << ElementParser.new(text, @line_parser.lineno, @line_parser).parse
+ @ast << ElementParser.new(@line_parser).parse(text)
end
- def parse_script(text, preserve: false)
- m = text.match(/\A([!&~])?[=~] *(.*)\z/)
- script = m[2]
- if script.empty?
- syntax_error!("No Ruby code to evaluate")
- end
- script += RubyMultiline.read(@line_parser, script)
- node = Ast::Script.new([], script)
- case m[1]
- when '!'
- node.escape_html = false
- when '&'
- node.escape_html = true
- end
- node.preserve = preserve
- @ast << node
+ def parse_script(text)
+ @ast << ScriptParser.new(@line_parser).parse(text)
end
def parse_silent_script(text)
if text.start_with?('-#')
@ast << Ast::HamlComment.new