Class: Lazibi::Filter::OptionalEnd

Inherits:
FilterBase show all
Defined in:
lib/filter/optional_end_filter.rb

Instance Method Summary collapse

Methods included from Helper::FilterHelper

#begin_keys, #clean_block, #clean_line, #comment_at_end, #end_anchor?, #end_keys, #get_indent, #get_rest, #group_match, #middle_anchor?, #middle_keys, #nasty_line?, #split_end, #start_anchor?

Instance Method Details

#down(source) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/filter/optional_end_filter.rb', line 42

def down( source )
  content = source
  return '' if content.strip == ''
  lines = content.split("\n")
  first_line = lines.first.strip
  return lines[1..-1].join("\n") if first_line == '#skip_parse'
  insert_end content
end

#find_end(lines, indent) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/filter/optional_end_filter.rb', line 89

def find_end( lines, indent )
  return 0 if lines.size == 1

  anchor = 0

  lines = lines[1..-1]
  lines.each_index do |i|
    l = lines[i]
    next if l.strip == ''
    if l.strip =~ /^#/
      if get_indent(l) > indent
        anchor = i + 1
      end
      next
    end
    return anchor if get_indent(l) < indent
    if get_indent(l) == indent
      rest = get_rest l
      if start_anchor? rest
        return anchor
      elsif end_anchor? rest
        return false
      elsif middle_anchor? rest
        anchor = i + 1
        next
      else
        return anchor
      end
    end
    anchor =  i + 1
  end
  return anchor
end

#insert_end(content) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/filter/optional_end_filter.rb', line 51

def insert_end( content )
  @lines = content.split("\n")
  progress = 0
  while progress < @lines.size
    lines = @lines[progress..-1]
    lines.each_index do |index|
      l = lines[index]
      safe_l = clean_block(clean_line(get_rest(l)))
      if start_anchor? safe_l
        relative_index_for_end = find_end( lines[index..-1], get_indent(l))
        unless relative_index_for_end
          progress += 1
          break
        end
        index_for_end = relative_index_for_end + index

        if relative_index_for_end == 0 && !comment_at_end(l)
          #l = lines[index_for_end]
          lines[index_for_end] = lines[index_for_end].rstrip + '; end'
        else
          lines[index_for_end] = lines[index_for_end] + "\n" +  ' ' * get_indent(l) + "end"
        end
        head = @lines[0...progress]
        tail = lines[index..-1].join("\n").split("\n")

        @lines = head + tail


        progress += 1
        break
      end
      progress += 1
    end
  end

  result = @lines.join("\n")
end

#remove_colon_at_end(l) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/filter/optional_end_filter.rb', line 34

def remove_colon_at_end(l)
  if comment_at_end(l)
    l.sub /(\s*;\s*)(#.*)/, ' \2'
  else
    l.sub /;*$/, ''
  end
end

#up(source) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/filter/optional_end_filter.rb', line 7

def up( source )
  rb = source
  py = []
  lines = rb.split("\n")
  lines.each_index do |index|
    l = lines[index]
    if l.strip =~ /^end$/
      next
    end
    l = remove_colon_at_end(l)

    s = l
    if comment_at_end(l)
      s = s.sub(/(\s*;\s*end*\s*)(#.*)/, ' \2')
      s = s.sub(/(\s+end*\s*)(#.*)/, ' \2')
    else
      s = s.sub(/\s+end\s*$/, '')
      s = remove_colon_at_end(s)
    end

    py << s
  end

  py.join("\n")
end