Module: TermColorLight

Extended by:
TermColorLight
Included in:
TermColorLight
Defined in:
lib/termcolorlight.rb

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

VERSION =
"1.0.0"
TAGS =
{
  # foreground colors
  "black" => "30", "red" => "31", "green" => "32", "yellow" => "33",
  "blue" => "34", "magenta" => "35", "cyan" => "36", "white" => "37",
  "gray" => "37",
  # background colors
  "on_black" => "40", "on_red" => "41", "on_green" => "42", "on_yellow" => "43",
  "on_blue" => "44", "on_magenta" => "45", "on_cyan" => "46", "on_white" => "47",
  "on_gray" => "47",
  # decorations
  "bold" => "1", "dark" => "2", "underline" => "4", "underscore" => "4", "blink" => "5",
  "reverse" => "7", "concealed" => "8",
}
ENTITIES =
Hash[*%w(& &amp; < &lt; > &gt; " &quot; ' &apos;)]

Instance Method Summary collapse

Instance Method Details

#create_escape_sequence(indexes) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
92
93
94
# File 'lib/termcolorlight.rb', line 85

def create_escape_sequence(indexes)   # :nodoc:
  unless indexes.kind_of?(Array)
    return "\e[#{indexes}m"
  end
  result = ""
  indexes.each do |index|
    result.concat("\e[#{index}m")
  end
  result
end

#escape(str) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/termcolorlight.rb', line 65

def escape(str)
  buf = str.to_s.dup
  ENTITIES.each do |entity|
    buf.gsub!(*entity)
  end
  buf
end

#parse(str) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/termcolorlight.rb', line 31

def parse(str)
  stack = []
  current_tag = ""
  ss = StringScanner.new(str.to_s)
  buffer = ""
  until ss.eos?
    case
    when ss.scan(/[^<]+/)
      buffer.concat(ss.matched)
    when ss.scan(/<([a-z_]+?)>/i)
      tag_name = ss[1].downcase
      tag_index = TAGS[tag_name]
      unless tag_index
        raise ParseError, "Unknown tag name (got '#{tag_name}')"
      end
      stack.push(tag_index)
      buffer.concat(create_escape_sequence(tag_index))
    when ss.scan(/<\/([a-z_]+?)>/i)
      tag_name = ss[1].downcase
      expect_tag_index = stack.pop
      if TAGS[tag_name] != expect_tag_index
        expect_tag_name = tags_select_by_index(expect_tag_index)
        raise ParseError, "Missing end tag for '#{expect_tag_name}' (got '#{tag_name}')"
      end
      buffer.concat("\e[0m#{create_escape_sequence(stack)}")
    end
  end
  unless stack.empty?
    for_tag_name = tags_select_by_index(stack.pop)
    raise ParseError, "Missing end tag for '#{for_tag_name}'"
  end
  TermColorLight.unescape(buffer)
end

#strip_tag(str) ⇒ Object



81
82
83
# File 'lib/termcolorlight.rb', line 81

def strip_tag(str)
  TermColorLight.unescape(str.gsub(/<.+?>/, ""))
end

#tags_select_by_index(index) ⇒ Object

:nodoc:



96
97
98
99
100
# File 'lib/termcolorlight.rb', line 96

def tags_select_by_index(index)   # :nodoc:
  TAGS.select { |_, idx|
    idx == index
  }.keys[0]
end

#unescape(str) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/termcolorlight.rb', line 73

def unescape(str)
  buf = str.to_s.dup
  ENTITIES.invert.each do |entity|
    buf.gsub!(*entity)
  end
  buf
end