class Ansible::Output::Ansi2Html

Converter for strings containing with ANSI escape sequences

Constants

CLOSE_SPAN_TAG
COLOR

Hash of colors to convert shell colours to CSS

END_ESCAPE_SEQUENCE_PATTERN
IGNORED_OUTPUT
OPEN_SPAN_TAG
SUPPORTED_STYLE_PATTERN
UNSUPPORTED_STYLE_PATTERN

Public Class Methods

new(line) click to toggle source

Create StringScanner for string @param line [String] a stream or string (that supports +<<+) to which generated HTML will be appended

# File lib/ansible/output.rb, line 43
def initialize(line)
  # ensure any HTML tag characters are escaped
  @strscan = StringScanner.new(ERB::Util.h line)
end

Public Instance Methods

to_html(stream) click to toggle source

Generate HTML from string formatted with ANSI escape sequences @return [String, IO] the HTML

# File lib/ansible/output.rb, line 50
def to_html(stream)
  until @strscan.eos?
    stream << generate_html
  end

  stream
end

Private Instance Methods

generate_html() click to toggle source

Scan string and generate HTML

# File lib/ansible/output.rb, line 62
def generate_html
  if @strscan.scan SUPPORTED_STYLE_PATTERN
    open_tag
  elsif @strscan.scan END_ESCAPE_SEQUENCE_PATTERN
    CLOSE_SPAN_TAG
  elsif @strscan.scan UNSUPPORTED_STYLE_PATTERN
    OPEN_SPAN_TAG
  else
    @strscan.scan IGNORED_OUTPUT
  end
end
open_tag() click to toggle source

Generate opening HTML tag, which may contain a style attribute @return [String] opening tag

# File lib/ansible/output.rb, line 76
def open_tag
  bold, colour = @strscan[1], @strscan[2]
  styles = []

  styles << COLOR[bold] if bold.to_i == 1
  styles << COLOR[colour]

  # in case of invalid colours, although this may be impossible
  if styles.compact.empty?
    OPEN_SPAN_TAG
  else
    %{<span style="#{styles*'; '};">}
  end
end