Class: Text::HTMLHighlighter

Inherits:
Highlighter show all
Defined in:
lib/wad/vendor/highlight.rb

Overview

Highlights using HTML. Fonts are highlighted using <span> tags, not <font>. Also note that reverse is translated to white on black. According to www.w3.org/TR/REC-CSS2/syndata.html#value-def-color, valid color keywords are: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow. Thus, no magenta or cyan.

Constant Summary

Constants inherited from Highlighter

Text::Highlighter::ANSI, Text::Highlighter::ATTRIBUTES, Text::Highlighter::HTML, Text::Highlighter::NONE, Text::Highlighter::VERSION

Instance Method Summary collapse

Methods inherited from Highlighter

#background, #code, #color, #foreground

Constructor Details

#initializeHTMLHighlighter

Returns a new instance of HTMLHighlighter.



114
115
116
117
# File 'lib/wad/vendor/highlight.rb', line 114

def initialize
  # we need to know what we're resetting from (bold, font, underlined ...)
  @stack = []
end

Instance Method Details

#color_value(cname) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/wad/vendor/highlight.rb', line 138

def color_value(cname)
  case cname
  when "cyan"
    "#00FFFF"
  when "magenta"
    "#FF00FF"
  else
    cname
  end
end

#end_styleObject

Returns the end tag (“</span>”).



134
135
136
# File 'lib/wad/vendor/highlight.rb', line 134

def end_style
  "</span>"
end

#name_to_code(name) ⇒ Object

Returns the code for the given name.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/wad/vendor/highlight.rb', line 151

def name_to_code(name)
  @stack << name

  case name
  when "none", "reset"
    @stack.pop
    str = ""
    if @stack.length > 0
      begin
        prev = @stack.pop
        case prev
        when "bold"
          str << "</b>"
        when "underscore", "underline"
          str << "</u>"
        when "blink"
          str << "</blink>"
        when "concealed"
          str << " -->"
        else
          str << end_style
        end
      end while @stack.length > 0
    end
    str
  when "bold"
    "<b>"
  when "underscore", "underline"
    "<u>"
  when "blink"
    "<blink>"
  when "concealed"
    "<!-- "
  else
    start_style(name)
  end
end

#start_style(name) ⇒ Object

Returns the start tag for the given name.



121
122
123
124
125
126
127
128
129
130
# File 'lib/wad/vendor/highlight.rb', line 121

def start_style(name)
  case name
  when "reverse"
    "<span style=\"color: white; background-color: black\">"
  when /on_(\w+)/
    "<span style=\"background-color: #{$1}\">"
  else
    "<span style=\"color: #{name}\">"
  end
end