Class: Lazibi::Filter::Beautify

Inherits:
FilterBase show all
Includes:
Helper::BeautifyFilterHelper
Defined in:
lib/filter/beautify_filter.rb

Instance Method Summary collapse

Methods included from Helper::BeautifyFilterHelper

#indent_exp, #outdent_exp, #tab_size, #tab_str

Methods inherited from FilterBase

#down

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

#add_line(line, tab) ⇒ Object



20
21
22
23
24
# File 'lib/filter/beautify_filter.rb', line 20

def add_line(line,tab)
  line.strip!
  line = make_tab(tab)+line if line.length > 0
  return line + "\n"
end

#join_sep_line(source) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/filter/beautify_filter.rb', line 26

def join_sep_line( source )
  lines = source.split("\n")
  continue = false
  buffer = ''
  new_lines = []
  for line in lines
    if clean_line(line, true).strip =~ /#/
      if continue
        new_lines << buffer
        new_lines << line
        buffer = ''
      else
        new_lines << line
      end
      next
    end

    if continue
      if line.strip == ''
        continue = false
        new_lines << buffer
        new_lines << line
        buffer = ''
      elsif line.strip[-1..-1] == "\\"
        continue = true
        buffer += ' ' + line.strip[0...-1].strip
      else
        continue = false
        buffer += ' ' + line.lstrip
        new_lines << buffer
        buffer = ''
      end
    else
      if line.strip == ''
        new_lines << line
      elsif line.strip[-1..-1] == "\\"
        continue = true
        buffer += line.rstrip[0...-1].strip
      else
        new_lines << line
      end
    end
  end

  new_lines.join("\n")
end

#make_tab(tab) ⇒ Object



16
17
18
# File 'lib/filter/beautify_filter.rb', line 16

def make_tab(tab)
  return (tab < 0) ? "": tab_str * tab_size * tab
end

#up(source) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
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
111
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
# File 'lib/filter/beautify_filter.rb', line 73

def up( source )
  source = split_end(source)
  commentBlock = false
  multiLineArray = Array.new
  multiLineStr = ""
  tab = 0
  dest = ""
  source.split("\n").each do |line|
    # combine continuing lines
    if(!(clean_line(line, true) =~ /\s*#/) && line =~ /[^\\]\\\s*$/)
      multiLineArray.push line
      multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
      next
    end

    # add final line
    if(multiLineStr.length > 0)
      multiLineArray.push line
      multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
    end

    tline = ((multiLineStr.length > 0)?multiLineStr:line).strip
    if(tline =~ /^=begin/)
      commentBlock = true
    end
    if(commentBlock)
      # add the line unchanged
      dest += line + "\n"
    else
      commentLine = (tline =~ /^#/)
      if(!commentLine)
        # throw out sequences that will
        # only sow confusion
        tline = clean_line(tline)
        outdent_exp.each do |re|
          if(tline =~ re)
            tab -= 1
            break
          end
        end
      end
      if (multiLineArray.length > 0)
        first_line = true
        multiLineArray.each do |ml|
          if first_line
            dest += add_line(ml,tab)
            first_line = false
          elsif ml.strip =~ /^unless/ || ml.strip =~ /^if/
            dest = dest.chop.rstrip.chop.rstrip + ' ' + ml.strip + "\n"
          else
            dest += add_line(ml,tab)
          end
        end
        multiLineArray.clear
        multiLineStr = ""
      else
        dest += add_line(line,tab)
      end
      if(!commentLine)
        indent_exp.each do |re|
          if(tline =~ re) && !(tline =~ /\s+end\s*$/)
            tab += 1
            break
          end
        end
      end
    end
    if(tline =~ /^=end/)
      commentBlock = false
    end
  end
  dest
end