Module: Lazibi::Helper::FilterHelper

Included in:
Filter::FilterBase
Defined in:
lib/helper/filter_helper.rb

Instance Method Summary collapse

Instance Method Details

#begin_keysObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/helper/filter_helper.rb', line 14

def begin_keys
  [
    /^module\b/,
    /(=\s*|^|<<\s*)if\b/,
    /(=\s*|^|<<\s*)until\b/,
    /(=\s*|^|<<\s*)for\b/,
    /(=\s*|^|<<\s*)unless\b/,
    /(=\s*|^|<<\s*)while\b/,
    /(=\s*|^|<<\s*)begin\b/,
    /(=\s*|^|<<\s*)loop\b\s*do/,
    /\bcase\b/,
    /^class\b/,
    /^def\b/,
    /\sdo\b/,
    /^do\b/
  ]
end

#clean_block(line) ⇒ Object



113
114
115
# File 'lib/helper/filter_helper.rb', line 113

def clean_block(line)
  line.gsub /do\b.*?;\s*end/, ''
end

#clean_line(line, leave_comment = false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/helper/filter_helper.rb', line 85

def clean_line( line, leave_comment = false )
  tline = line.dup.sub /[;]*$/, ''

  tline.gsub!(/#\{.*?\}/,"")
  tline.gsub!(/\\\"/,"'")

  # deal with nested syntax

  tline.gsub!(/"\/*?"/,"\"\"")
  tline.gsub!(/\/.*?[^\\]\//,"//")

  # again
  tline.gsub!(/".*?"/,"\"\"")
  tline.gsub!(/'.*?'/,"''")
  tline.gsub!(/`.*?`/,"''")

  # more ..
  tline.gsub!(/%[qwsrx]\{.*?\}/, "''")
  tline.gsub!(/%[qwsrx]\(.*?\)/, "''")
  tline.gsub!(/%[qwsrx]\[.*?\]/, "''")
  tline.gsub!(/%[qwsrx](.).*?\1/, "''")

  tline.gsub!(/#.*$/, '') unless leave_comment

  tline
end

#comment_at_end(line) ⇒ Object



80
81
82
83
# File 'lib/helper/filter_helper.rb', line 80

def comment_at_end(line)
  return false if line.strip ==''
  clean_line(line, true) =~ /#/
end

#end_anchor?(str) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/helper/filter_helper.rb', line 67

def end_anchor?(str)
  str =~ end_keys ? true : false
end

#end_keysObject



44
45
46
# File 'lib/helper/filter_helper.rb', line 44

def end_keys
  /^end\b/
end

#get_indent(line) ⇒ Object



4
5
6
7
# File 'lib/helper/filter_helper.rb', line 4

def get_indent( line )
  line =~ /( *)/
  $1.size
end

#get_rest(line) ⇒ Object



9
10
11
12
# File 'lib/helper/filter_helper.rb', line 9

def get_rest( line )
  line =~/( *)/
  $'
end

#group_match(str, patterns) ⇒ Object



60
61
62
63
64
65
# File 'lib/helper/filter_helper.rb', line 60

def group_match(str, patterns)
  for pattern in patterns
    return true if str =~ pattern
  end
  false
end

#middle_anchor?(str) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/helper/filter_helper.rb', line 54

def middle_anchor?( str )
  str = clean_line str
  group_match str, middle_keys
end

#middle_keysObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/helper/filter_helper.rb', line 32

def middle_keys
  [
    /^then\b/,
    /^else\b/,
    /^elsif\b/,
    /^ensure\b/,
    /^rescue\b/,
    /^when\b/
  ]
end

#nasty_line?(line) ⇒ Boolean

why?

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/helper/filter_helper.rb', line 73

def nasty_line?(line)
  return false
  s = %w! \/ ' " \( \{ \[ !
  p = Regexp.new(s.join('|'))
  return line =~ p
end

#split_end(source) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/helper/filter_helper.rb', line 117

def split_end( source )
  lines = source.split("\n")
  new_lines = []
  for line in lines
    if line =~ /^\s*#/
      new_lines << line
    elsif line =~ /^\s*end\./
      new_lines << line
    else
      new_lines << line.gsub( /\send\./, "\nend." )
    end
  end

  new_lines.join("\n")
end

#start_anchor?(str) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/helper/filter_helper.rb', line 48

def start_anchor?( str )
  return false if str =~ /^#/
  str = clean_line str
  group_match str, begin_keys
end