Module: TermColorLight

Defined in:
lib/termcolorlight/html.rb,
lib/termcolorlight.rb

Overview

TermColorLight.to_html 実装用エクステンション

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

VERSION =
"1.1.1"
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;)]
@@colors =

色定義 フォーマット: “name” => [“暗い色”, “明るい色”]

{
  "black" => %w(black #888), "white" => %w(#bbb white),
  "red" => %w(indianred red), "green" => %w(green lime),
  "yellow" => %w(goldenrod yellow), "blue" => %w(#33c #33f),
  "magenta" => %w(darkmagenta magenta), "cyan" => %w(darkcyan cyan),
  "fg_default" => %w(white white),
  "bg_default" => %w(#333 #333)
}
@@decoration_styles =
{
  "underline" => "text-decoration:underline",
  "underscore" => "text-decoration:underline",
  "bold" => "font-weight:bold",
  "blink" => "text-decoration:blink",
  "concealed" => "visibility:hidden",
}

Class Method Summary collapse

Class Method Details

.build_color(fgcolor, bgcolor, intensity, reverse) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/termcolorlight/html.rb', line 125

def build_color(fgcolor, bgcolor, intensity, reverse)
  style = []
  if reverse
    fg_intensity = 0
    bg_intensity = intensity ? 1 : 0
    tmp = fgcolor
    fgcolor = bgcolor
    bgcolor = tmp
  else
    fg_intensity = intensity ? 1 : 0
    bg_intensity = 0
  end
  if fgcolor != "fg_default"
    color = @@colors[fgcolor][fg_intensity]
    if @@before_fg_color != color
      style << "color:#{color}"
      @@before_fg_color = color
    end
  end
  if bgcolor != "bg_default"
    color = @@colors[bgcolor][bg_intensity]
    if @@before_bg_color != color
      style << "background:#{color}"
      @@before_bg_color = color
    end
  end
  style
end

.color_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/termcolorlight/html.rb', line 117

def color_tag?(tag)
  @@colors.include?(tag)
end

.create_escape_sequence(indexes) ⇒ Object

:nodoc:



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

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

.default_background_color=(color) ⇒ Object



44
45
46
# File 'lib/termcolorlight/html.rb', line 44

def default_background_color=(color)
  @@colors["bg_default"] = [color, color]
end

.default_foreground_color=(color) ⇒ Object



40
41
42
# File 'lib/termcolorlight/html.rb', line 40

def default_foreground_color=(color)
  @@colors["fg_default"] = [color, color]
end

.escape(str) ⇒ Object



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

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

.influence_of_color?(tag) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/termcolorlight/html.rb', line 121

def influence_of_color?(tag)
  TAGS_INFLUENCE_OF_COLOR.include?(tag)
end

.output_test_sheetsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/termcolorlight/html.rb', line 154

def output_test_sheets
  puts <<-HTML
<html><head>
<style>body{color:#{@@colors["fg_default"][0]};background:#{@@colors["bg_default"][0]}}</style>
</head><body><table>
  HTML
  @@colors.each_key do |color|
    next if color =~ /default/
    puts "<tr><td>"
    puts to_html("<#{color}>#{color}</#{color}>")
    puts "</td><td>"
    puts to_html("<bold><#{color}>#{color}</#{color}></bold>")
    puts "</td><td>"
    puts to_html("<on_#{color}>on_#{color}</on_#{color}>")
    puts "</td><td>"
    puts to_html("<reverse><bold><#{color}>#{color}</#{color}></bold></reverse>")
    puts "</td></tr>"
  end
  puts "</table></body></html>"
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
# File 'lib/termcolorlight.rb', line 31

def parse(str)
  stack = []
  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

.parse_for_html(ss, fgcolor = "fg_default", bgcolor = "bg_default", intensity = false, reverse = false, parent = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/termcolorlight/html.rb', line 54

def parse_for_html(ss, fgcolor = "fg_default", bgcolor = "bg_default",
                   intensity = false, reverse = false, parent = nil)
  buffer = ""
  until ss.eos?
    case
    when ss.scan(/[^<]+/)
      buffer.concat(ss.matched)
    when ss.scan(/<([a-z_]+?)>/i)
      tag = ss[1].downcase
      unless valid_tag?(tag)
        raise ParseError, "Unknown tag name (got '#{tag}')"
      end
      tmp_fg = fgcolor
      tmp_bg = bgcolor
      tmp_intensity = intensity
      tmp_reverse = reverse
      influence_of_color = false
      styles = []
      case
      when color_tag?(tag)
        tmp_fg = tag
        influence_of_color = true
      when tag =~ /^on_(.+)$/
        tmp_bg = $1
        unless valid_tag?(tmp_bg)
          raise ParseError, "Unknown tag name (got '#{tag}')"
        end
        influence_of_color = true
      when tag == "bold"
        tmp_intensity = true
        influence_of_color = true
      when tag == "reverse"
        tmp_reverse = true
        influence_of_color = true
      end
      if influence_of_color
        styles += build_color(tmp_fg, tmp_bg, tmp_intensity, tmp_reverse)
      end
      decoration = @@decoration_styles[tag]
      styles << decoration if decoration
      buffer.concat("<span style=\"#{styles.join(";")}\">")
      buffer.concat(parse_for_html(ss, tmp_fg, tmp_bg, tmp_intensity, tmp_reverse, tag))
      unless parent
        @@before_fg_color = @@before_bg_color = nil
      end
    when ss.scan(/<\/([a-z_]+?)>/i)
      tag = ss[1].downcase
      if tag != parent
        raise ParseError, "Missing end tag for '#{parent}' (got '#{tag}')"
      end
      return buffer.concat("</span>")
    end
  end
  if parent
    raise ParseError, "Missing end tag for '#{parent}'"
  end
  buffer
end

.set_colors(hash) ⇒ Object



32
33
34
# File 'lib/termcolorlight/html.rb', line 32

def set_colors(hash)
  @@colors.merge!(hash)
end

.set_styles(hash) ⇒ Object



36
37
38
# File 'lib/termcolorlight/html.rb', line 36

def set_styles(hash)
  @@decoration_styles.merge!(hash)
end

.strip_tag(str, do_unescape = true) ⇒ Object



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

def strip_tag(str, do_unescape = true)
  result = str.gsub(/<.+?>/, "")
  result = TermColorLight.unescape(result) if do_unescape
  result
end

.tags_select_by_index(index) ⇒ Object

:nodoc:



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

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

.to_html(str) ⇒ Object



48
49
50
51
52
# File 'lib/termcolorlight/html.rb', line 48

def to_html(str)
  @@before_fg_color = @@before_bg_color = nil
  ss = StringScanner.new(str.to_s)
  result = parse_for_html(ss)
end

.unescape(str) ⇒ Object



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

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

.valid_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/termcolorlight/html.rb', line 113

def valid_tag?(tag)
  TAGS.include?(tag)
end