Class: Haml::Parser

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/haml/parser.rb

Defined Under Namespace

Classes: AttributeMerger=Object.new, DynamicAttributes

Constant Summary collapse

ELEMENT =

Designates an XHTML/XML element.

?%
DIV_CLASS =

Designates a <div> element with the given class.

?.
DIV_ID =

Designates a <div> element with the given id.

?#
COMMENT =

Designates an XHTML/XML comment.

?/
DOCTYPE =

Designates an XHTML doctype or script that is never HTML-escaped.

?!
SCRIPT =

Designates script, the result of which is output.

?=
SANITIZE =

Designates script that is always HTML-escaped.

?&
FLAT_SCRIPT =

Designates script, the result of which is flattened and output.

?~
SILENT_SCRIPT =

Designates script which is run but not output.

?-
SILENT_COMMENT =

When following SILENT_SCRIPT, designates a comment that is not output.

?#
ESCAPE =

Designates a non-parsed line.

?\\
FILTER =

Designates a block of filtered text.

?:
PLAIN_TEXT =

Designates a non-parsed line. Not actually a character.

-1
SPECIAL_CHARACTERS =

Keeps track of the ASCII values of the characters that begin a specially-interpreted line.

[
  ELEMENT,
  DIV_CLASS,
  DIV_ID,
  COMMENT,
  DOCTYPE,
  SCRIPT,
  SANITIZE,
  FLAT_SCRIPT,
  SILENT_SCRIPT,
  ESCAPE,
  FILTER
].freeze
MULTILINE_CHAR_VALUE =

The value of the character that designates that a line is part of a multiline string.

?|
BLOCK_WITH_SPACES =

Regex to check for blocks with spaces around arguments. Not to be confused with multiline script. For example: foo.each do | bar | = bar

/do\s*\|\s*[^\|]*\s+\|\z/
MID_BLOCK_KEYWORDS =
%w[else elsif rescue ensure end when].freeze
START_BLOCK_KEYWORDS =
%w[if begin case unless].freeze
START_BLOCK_KEYWORD_REGEX =

Try to parse assignments to block starters as best as possible

/(?:\w+(?:,\s*\w+)*\s*=\s*)?(#{START_BLOCK_KEYWORDS.join('|')})/
BLOCK_KEYWORD_REGEX =
/^-?\s*(?:(#{MID_BLOCK_KEYWORDS.join('|')})|#{START_BLOCK_KEYWORD_REGEX.source})\b/
DOCTYPE_REGEX =

The Regex that matches a Doctype command.

/(\d(?:\.\d)?)?\s*([a-z]*)\s*([^ ]+)?/i
LITERAL_VALUE_REGEX =

The Regex that matches a literal string or symbol value

/:(\w*)|(["'])((?!\\|\#\{|\#@|\#\$|\2).|\\.)*\2/
ID_KEY =
'id'.freeze
CLASS_KEY =
'class'.freeze
METHOD_CALL_PREFIX =

Used for scanning old attributes, substituting the first '{'

'a('

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#check_encoding, #check_haml_encoding, #contains_interpolation?, escape_html_safe, #handle_interpolation, #human_indentation, #inspect_obj, #rails_xss_safe?, #silence_warnings, #unescape_interpolation

Constructor Details

#initialize(options) ⇒ Parser

Returns a new instance of Parser.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/haml/parser.rb', line 99

def initialize(options)
  @options = ParserOptions.new(options)
  # Record the indent levels of "if" statements to validate the subsequent
  # elsif and else statements are indented at the appropriate level.
  @script_level_stack = []
  @template_index     = 0
  @template_tabs      = 0
  # When used in Haml::Engine, which gives options[:generator] to every filter
  # in the engine, including Haml::Parser, we don't want to throw exceptions.
  # However, when Haml::Parser is used as a library, we want to throw exceptions.
  @raise_error = !options.key?(:generator)
end

Instance Attribute Details

#root (readonly)

Returns the value of attribute root.



12
13
14
# File 'lib/haml/parser.rb', line 12

def root
  @root
end

Instance Method Details

#call(template)



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/haml/parser.rb', line 112

def call(template)
  template = Haml::Util.check_haml_encoding(template) do |msg, line|
    raise Haml::Error.new(msg, line)
  end

  match = template.rstrip.scan(/(([ \t]+)?(.*?))(?:\Z|\r\n|\r|\n)/m)
  # discard the last match which is always blank
  match.pop
  @template = match.each_with_index.map do |(full, whitespace, text), index|
    Line.new(whitespace, text.rstrip, full, index, self, false)
  end
  # Append special end-of-document marker
  @template << Line.new(nil, '-#', '-#', @template.size, self, true)

  @root = @parent = ParseNode.new(:root)
  @flat = false
  @filter_buffer = nil
  @indentation = nil
  @line = next_line

  raise SyntaxError.new(Error.message(:indenting_at_start), @line.index) if @line.tabs != 0

  loop do
    next_line

    process_indent(@line) unless @line.text.empty?

    if flat?
      text = @line.full.dup
      text = "" unless text.gsub!(/^#{@flat_spaces}/, '')
      @filter_buffer << "#{text}\n"
      @line = @next_line
      next
    end

    @tab_up = nil
    process_line(@line) unless @line.text.empty?
    if block_opened? || @tab_up
      @template_tabs += 1
      @parent = @parent.children.last
    end

    if !flat? && @next_line.tabs - @line.tabs > 1
      raise SyntaxError.new(Error.message(:deeper_indenting, @next_line.tabs - @line.tabs), @next_line.index)
    end

    @line = @next_line
  end
  # Close all the open tags
  close until @parent.type == :root
  @root
rescue Haml::Error => e
  e.backtrace.unshift "#{@options.filename}:#{(e.line ? e.line + 1 : @line.index + 1) + @options.line - 1}"
  raise if @raise_error
  error_with_lineno(e)
end

#compute_tabs(line)

Raises:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/haml/parser.rb', line 169

def compute_tabs(line)
  return 0 if line.text.empty? || !line.whitespace

  if @indentation.nil?
    @indentation = line.whitespace

    if @indentation.include?(?\s) && @indentation.include?(?\t)
      raise SyntaxError.new(Error.message(:cant_use_tabs_and_spaces), line.index)
    end

    @flat_spaces = @indentation * (@template_tabs+1) if flat?
    return 1
  end

  tabs = line.whitespace.length / @indentation.length
  return tabs if line.whitespace == @indentation * tabs
  return @template_tabs + 1 if flat? && line.whitespace =~ /^#{@flat_spaces}/

  message = Error.message(:inconsistent_indentation,
    human_indentation(line.whitespace),
    human_indentation(@indentation)
  )
  raise SyntaxError.new(message, line.index)
end