Method: Shell2html.to_html

Defined in:
lib/shell2html.rb

.to_html(text, inline = false) ⇒ Object



48
49
50
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
88
89
90
91
92
# File 'lib/shell2html.rb', line 48

def to_html(text, inline = false)
  count = 0
  text = CGI.escapeHTML(text)
  text.gsub!(/\n/, '<br>')
  text.gsub!(/  /, ' &nbsp;')
  text.split(27.chr).map do |e|
    if /^\[([0-9;]+)m(.*)$/.match e
      case $1
      when '0'
        span = '</span>' * count
        count = 0
        "#{span}#{$2}"
      else
        key = $1
        t = $2
        css = []
        key.split(';').each do |i|
          css << COLORS["#{i.to_i}"] if COLORS["#{i.to_i}"]
        end
        if css.count > 0
          if inline
            span_style = css.map do |c|
              o = []
              c[:style].each do |k,v|
                o << "#{k}:#{v}"
              end
              o
            end.flatten.join(';')
            count = 1
            "#{'</span>' * count}<span style=\"#{span_style}\">#{t}"
          else
            count = 1
            span_class = css.map { |c| c[:css] }.join(' ')
            "#{'</span>' * count}<span class=\"#{span_class}\">#{t}"
          end
        else
          count = 1
          "#{'</span>' * count}<span>#{t}"
        end
      end
    else
      e
    end
  end.join
end