module HtmlToAnsi::Html

Constants

ENTITIES

Public Class Methods

decode(text) click to toggle source

Decode a String's HTML entities

@param text [String] @return [String]

# File lib/html_to_ansi/html.rb, line 38
def self.decode(text)
  ENTITIES.each { |k, v|
    text = text.gsub(k, v)
  }
  text
end
encode(text) click to toggle source

Encode a String with HTML entities

@param text [String] @return [String]

# File lib/html_to_ansi/html.rb, line 26
def self.encode(text)
  encoded = text
  ENTITIES.each { |k, v|
    encoded = encoded.gsub(v, k)
  }
  encoded
end
fix_ampersands(text) click to toggle source

Convert ampersands to &

@param text [String] @return [String]

# File lib/html_to_ansi/html.rb, line 12
def self.fix_ampersands(text)
  codes = []
  ENTITIES.keys.each { |e|
    codes.push e[1..-1]
  }
  piped = codes.join('|')
  re = Regexp.new("&(?!(#{piped}))")
  text.gsub(re, '&\1')
end
parse(code) click to toggle source

Parse a String into an XML document

@param code [String] @return [REXML::Document]

# File lib/html_to_ansi/html.rb, line 49
def self.parse(code)
  code = fix_ampersands(code).strip
  last = nil
  begin
    doc = REXML::Document.new code
  rescue REXML::ParseException => e
    # Convert invalid < characters to &lt;
    if e.source.buffer != last and e.source.buffer[0,1] == '<'
      code = code[0,(code.length - e.source.buffer.length)] + '&lt;' + e.source.buffer[1..-1]
      last = e.source.buffer
      retry
    end
    raise e
  end
end