Class: XmlConv::View::Preformatted

Inherits:
HtmlGrid::Value
  • Object
show all
Defined in:
lib/xmlconv/view/preformatted.rb

Instance Method Summary collapse

Instance Method Details

#initObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/xmlconv/view/preformatted.rb', line 10

def init
  super
  pretty = ''
  if(@value)
    raw = @value.gsub(/>\s+</, "><").gsub(/\r\n?/, "\n")
    begin
      pretty = CGI.pretty(raw)
    rescue
      pretty = raw
    end
    wrap = ''
    pretty.each_line { |line|
      if(line.length < BREAK_WIDTH)
        wrap << line
      else
        indent = line[/^ +/].to_s
        indent = indent[0,indent.length % (BREAK_WIDTH / 3)]
        tmpparts = line.split(/(?<=") +(?=")/)
        parts = []
        tmpparts.each { |part|
          if(part.length > BREAK_WIDTH)
            parts.concat(part.split(/ /))
          else
            parts.push(part)
          end
        }
        wrapline = parts.shift
        while(part = parts.shift)
          if((wrapline.length + part.length) >= BREAK_WIDTH)
            wrap << wrapline
            wrap << "\n"
            wrapline = indent.dup << (' ' * 5) << part
          else
            wrapline << ' ' << part
          end
        end
        wrap << wrapline
      end
    }
    @value = CGI.escapeHTML(wrap)
  end
end