Module: Hamlit::Parsers::Script

Extended by:
Concerns::Included
Includes:
Concerns::Error, Concerns::Indentable
Included in:
Hamlit::Parser
Defined in:
lib/hamlit/parsers/script.rb

Constant Summary collapse

INTERNAL_STATEMENTS =
%w[else elsif rescue ensure when].freeze
DEFAULT_SCRIPT_OPTIONS =
{ force_escape: false, disable_escape: false }.freeze
PREFIX_BY_STATEMENT =
{
  'when'   => 'case',
  'rescue' => 'begin',
}.freeze

Instance Method Summary collapse

Methods included from Concerns::Included

extended

Methods included from Concerns::Indentable

#count_indent, #has_block?, #next_indent, #reset_indent, #same_indent?, #validate_indentation!, #with_indented

Methods included from Concerns::Error

#assert_scan!, #copmile_error!, #syntax_error, #syntax_error!

Instance Method Details

#parse_script(scanner, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hamlit/parsers/script.rb', line 24

def parse_script(scanner, options = {})
  assert_scan!(scanner, /=|&=|!=|~/)
  options = DEFAULT_SCRIPT_OPTIONS.merge(options)

  code, with_comment = scan_code(scanner, comment_check: true)
  return syntax_error("There's no Ruby code for = to evaluate.") if code.empty? && !with_comment
  unless has_block?
    return [:dynamic, code] if options[:disable_escape]
    return escape_html([:dynamic, code], options[:force_escape])
  end

  ast = [:haml, :script, code, options]
  ast += with_indented { parse_lines }
  ast << [:code, 'end']
  ast
end

#parse_silent_script(scanner) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hamlit/parsers/script.rb', line 41

def parse_silent_script(scanner)
  assert_scan!(scanner, /-/)
  if scanner.scan(/#/)
    with_indented { skip_lines }
    return [:multi]
  end

  code = scan_code(scanner)
  ast = [:multi, [:code, code]]
  unless has_block?
    if internal_statement?(code) && !statement_continuing?
      ast << [:code, 'end']
    end
    return ast
  end

  ast += with_indented { parse_lines }
  unless statement_continuing?
    ast << [:code, 'end']
  end
  ast
end