module Verse::Sanitizer

Constants

ANSI_MATCHER
LINE_BREAK

Public Class Methods

ansi?(string) click to toggle source

Check if string is an ANSI code

@param [String] string

the string to check

@return [Boolean]

@api public

# File lib/verse/sanitizer.rb, line 29
def ansi?(string)
  !!(string =~ /^(\[)?\033(\[)?[;?\d]*[\dA-Za-z]([\];])?$/)
end
replace(text, separator = LINE_BREAK) click to toggle source

Replace separator with whitespace

@example

replace(" \n ") # => "  "
replace("\n")   # => " "

@param [String] text

@param [String] separator

@return [String]

@api public

# File lib/verse/sanitizer.rb, line 47
def replace(text, separator = LINE_BREAK)
  text.gsub(/([ ]+)#{separator}/, "\\1")
      .gsub(/#{separator}(?<space>[ ]+)/, "\\k<space>")
      .gsub(/#{separator}/, ' ')
end
sanitize(text) click to toggle source

Strip ANSI characters from the text

@param [String] text

@return [String]

@api public

# File lib/verse/sanitizer.rb, line 16
def sanitize(text)
  text.gsub(ANSI_MATCHER, '')
end

Private Instance Methods

ansi?(string) click to toggle source

Check if string is an ANSI code

@param [String] string

the string to check

@return [Boolean]

@api public

# File lib/verse/sanitizer.rb, line 29
def ansi?(string)
  !!(string =~ /^(\[)?\033(\[)?[;?\d]*[\dA-Za-z]([\];])?$/)
end
replace(text, separator = LINE_BREAK) click to toggle source

Replace separator with whitespace

@example

replace(" \n ") # => "  "
replace("\n")   # => " "

@param [String] text

@param [String] separator

@return [String]

@api public

# File lib/verse/sanitizer.rb, line 47
def replace(text, separator = LINE_BREAK)
  text.gsub(/([ ]+)#{separator}/, "\\1")
      .gsub(/#{separator}(?<space>[ ]+)/, "\\k<space>")
      .gsub(/#{separator}/, ' ')
end
sanitize(text) click to toggle source

Strip ANSI characters from the text

@param [String] text

@return [String]

@api public

# File lib/verse/sanitizer.rb, line 16
def sanitize(text)
  text.gsub(ANSI_MATCHER, '')
end