Module: Util::ANSIColor2CSS

Extended by:
ANSIColor2CSS
Included in:
ANSIColor2CSS
Defined in:
lib/nuggets/util/ansicolor2css.rb

Constant Summary collapse

COLOUR_RE =
%r{\e\[((?:[0-9]|[34][0-7])(?:;(?:[0-9]|[34][0-7]))*)m}
OPEN =
'<span style="'.freeze
OPENC =
'">'.freeze
CLOSE =
'</span>'.freeze
ATTRIBUTES =
{
   '0' => CLOSE,                            # clear
   '1' => 'font-weight: bold',              # bold
   '2' => '',                               # dark
   '3' => 'font-style: italic',             # italic -- not widely implemented
   '4' => 'text-decoration: underline',     # underline
   '5' => 'text-decoration: blink',         # blink
   '6' => 'text-decoration: blink',         # rapid blink -- not widely implemented
   '7' => '',                               # negative
   '8' => '',                               # concealed
   '9' => 'text-decoration: line-through',  # strikethrough -- not widely implemented
  '30' => 'color: black',                   # black
  '31' => 'color: red',                     # red
  '32' => 'color: green',                   # green
  '33' => 'color: yellow',                  # yellow
  '34' => 'color: blue',                    # blue
  '35' => 'color: magenta',                 # magenta
  '36' => 'color: cyan',                    # cyan
  '37' => 'color: white',                   # white
  '40' => 'background-color: black',        # on black
  '41' => 'background-color: red',          # on red
  '42' => 'background-color: green',        # on green
  '43' => 'background-color: yellow',       # on yellow
  '44' => 'background-color: blue',         # on blue
  '45' => 'background-color: magenta',      # on magenta
  '46' => 'background-color: cyan',         # on cyan
  '47' => 'background-color: white'         # on white
}

Instance Method Summary collapse

Instance Method Details

#convert(string) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/nuggets/util/ansicolor2css.rb', line 69

def convert(string)
  string.gsub(COLOUR_RE) {
    subst, attrs = '', $1.split(';')

    subst << CLOSE if attrs.delete('0')
    subst << OPEN << attrs.map { |c| ATTRIBUTES[c] }.join('; ') << OPENC unless attrs.empty?

    subst
  }
end