Class: XRT::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/xrt/syntax.rb

Instance Method Summary collapse

Instance Method Details

#beginning_block?(statement) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/xrt/syntax.rb', line 19

def beginning_block? statement
  without_comment = remove_comment statement
  without_comment =~ beginning_block_regexp
end

#beginning_block_regexpObject



30
31
32
33
# File 'lib/xrt/syntax.rb', line 30

def beginning_block_regexp
  keywords = %w(IF UNLESS FOR FOREACH WHILE SWITCH MACRO BLOCK WRAPPER FILTER)
  /\[%.?\s*\b(#{keywords.join('|')})\b/i
end

#block?(statement) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/xrt/syntax.rb', line 15

def block? statement
  statement =~ /\A\[%.+%\]\Z/m
end

#block_level(statement) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/xrt/syntax.rb', line 43

def block_level(statement)
  stripped = statement.strip
  return 0 unless block? stripped
  level = 0
  level += 1 if beginning_block? stripped
  level -= 1 if end_block? stripped
  level
end

#end_block?(statement) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/xrt/syntax.rb', line 24

def end_block? statement
  return unless block? statement
  without_comment = remove_comment statement
  without_comment =~ end_block_regexp
end

#end_block_regexpObject



35
36
37
# File 'lib/xrt/syntax.rb', line 35

def end_block_regexp
  /\bEND\b/i
end

#remove_comment(statement) ⇒ Object



39
40
41
# File 'lib/xrt/syntax.rb', line 39

def remove_comment(statement)
  statement.gsub(/\s*#.*$/, '')
end

#tag_end?(statement) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/xrt/syntax.rb', line 11

def tag_end? statement
  statement == '>'
end

#tag_start?(statement) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/xrt/syntax.rb', line 7

def tag_start? statement
  statement == '<'
end

#whitespace?(statement) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/xrt/syntax.rb', line 3

def whitespace? statement
  statement.match(/\A\s*\Z/)
end