module Shell2html

Constants

COLORS

Public Instance Methods

css() click to toggle source
# File lib/shell2html.rb, line 97
def css
  back = []
  COLORS.each do |_, c|
    o = []
    css = c[:style].each do |k, v|
      o << "#{k}: #{v}"
    end
    back << ".#{c[:css]} { #{o.join(';')} }"
  end
  back.join("\n") + "\n"
end
sass() click to toggle source
# File lib/shell2html.rb, line 109
def sass
  back = []
  COLORS.each do |_, c|
    o = []
    back << ".#{c[:css]}"
    c[:style].each do |k, v|
      back << "  #{k}: #{v}"
    end
  end
  back.join("\n") + "\n"
end
to_html(text, inline = false) click to toggle source
# File lib/shell2html.rb, line 52
def to_html(text, inline = false)
  count = 0
  text = CGI.escapeHTML(text)
  text.gsub!(/(https?:\/\/[^'"\s\e\n<]*)/, "<a class=\"sh_a\" href=\"\\1\">\\1</a>")
  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
        count = 1
        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(';')
            "#{'</span>' * count}<span style=\"#{span_style}\">#{t}"
          else
            span_class = css.map { |c| c[:css] }.join(' ')
            "#{'</span>' * count}<span class=\"#{span_class}\">#{t}"
          end
        else
          "#{'</span>' * count}<span>#{t}"
        end
      end
    else
      e
    end
  end.join
end