text-highlight 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +28 -0
- data/install.rb +16 -0
- data/t/runtests.rb +80 -0
- data/t/test_ansihighlight.rb +367 -0
- data/t/test_htmlhighlight.rb +367 -0
- data/t/test_nonhighlight.rb +367 -0
- data/t/testcase.rb +67 -0
- data/text-highlight-1.0.2.gem +0 -0
- data/text-highlight.gemspec +21 -0
- data/text/highlight.rb +296 -0
- metadata +52 -0
data/t/testcase.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
$:.unshift(`pwd`.chomp + "/..")
|
5
|
+
$:.unshift(`pwd`.chomp + "/.")
|
6
|
+
|
7
|
+
require 'rubyunit'
|
8
|
+
require 'log'
|
9
|
+
|
10
|
+
STDOUT.sync = STDERR.sync = true
|
11
|
+
|
12
|
+
AppLog.level = Log::DEBUG
|
13
|
+
AppLog.set_widths 25, 5, -40
|
14
|
+
AppLog.debug ""
|
15
|
+
|
16
|
+
|
17
|
+
module My
|
18
|
+
|
19
|
+
class TestCase < RUNIT::TestCase
|
20
|
+
include Loggable
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# This is in 1.8, I think.
|
28
|
+
|
29
|
+
class IOString < IO
|
30
|
+
|
31
|
+
def initialize(str = "")
|
32
|
+
@str = str.dup
|
33
|
+
end
|
34
|
+
|
35
|
+
def puts(*args)
|
36
|
+
args.each do |a|
|
37
|
+
@str << a
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def printf(*args)
|
42
|
+
@str << sprintf(args)
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
@str
|
47
|
+
end
|
48
|
+
|
49
|
+
def ==(other)
|
50
|
+
@str == other
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
class String
|
57
|
+
|
58
|
+
alias oldeq ==
|
59
|
+
def ==(other)
|
60
|
+
if other.kind_of?(IOString)
|
61
|
+
other == self
|
62
|
+
else
|
63
|
+
oldeq(other)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
File without changes
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'date'
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = %q{text-highlight}
|
4
|
+
s.version = "1.0.2"
|
5
|
+
s.date = Date.today.to_s
|
6
|
+
s.summary = %q{A Ruby module for highlighting text, using ANSI escape sequences or HTML.}
|
7
|
+
s.description =<<DESCRIPTION
|
8
|
+
A Ruby module for highlighting text, using ANSI escape sequences or HTML.
|
9
|
+
DESCRIPTION
|
10
|
+
s.author = %q{Jeff Pace}
|
11
|
+
s.email = %q{jpace@incava.org}
|
12
|
+
s.homepage = %q{http://text-highlight.rubyforge.org/}
|
13
|
+
s.files = Dir.glob('**/*')
|
14
|
+
s.require_paths = %w{. text}
|
15
|
+
s.autorequire = %q{highlight}
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.rdoc_options = ["--main", "README"]
|
18
|
+
s.extra_rdoc_files = ["README"]
|
19
|
+
s.test_files = %w{t/runtests.rb}
|
20
|
+
s.rubyforge_project = %q{text-highlight}
|
21
|
+
end
|
data/text/highlight.rb
ADDED
@@ -0,0 +1,296 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
|
3
|
+
|
4
|
+
module Text
|
5
|
+
|
6
|
+
# Highlights text using either ANSI terminal codes, or HTML.
|
7
|
+
|
8
|
+
class Highlighter
|
9
|
+
|
10
|
+
VERSION = "1.0.2"
|
11
|
+
|
12
|
+
ATTRIBUTES = %w{
|
13
|
+
none
|
14
|
+
reset
|
15
|
+
bold
|
16
|
+
underscore
|
17
|
+
underline
|
18
|
+
blink
|
19
|
+
reverse
|
20
|
+
concealed
|
21
|
+
black
|
22
|
+
red
|
23
|
+
green
|
24
|
+
yellow
|
25
|
+
blue
|
26
|
+
magenta
|
27
|
+
cyan
|
28
|
+
white
|
29
|
+
on_black
|
30
|
+
on_red
|
31
|
+
on_green
|
32
|
+
on_yellow
|
33
|
+
on_blue
|
34
|
+
on_magenta
|
35
|
+
on_cyan
|
36
|
+
on_white
|
37
|
+
}
|
38
|
+
|
39
|
+
NONE = Object.new
|
40
|
+
HTML = Object.new
|
41
|
+
ANSI = Object.new
|
42
|
+
|
43
|
+
# Colorizes the given object. If a block is passed, its return value is used
|
44
|
+
# and the stream is reset. If a String is provided as the object, it is
|
45
|
+
# colorized and the stream is reset. Otherwise, only the code for the given
|
46
|
+
# color name is returned.
|
47
|
+
|
48
|
+
def color(colorname, obj = self, &blk)
|
49
|
+
# ^^^^ this is the Module self
|
50
|
+
result = name_to_code(colorname)
|
51
|
+
if blk
|
52
|
+
result << blk.call
|
53
|
+
result << name_to_code("reset")
|
54
|
+
elsif obj.kind_of?(String)
|
55
|
+
result << obj
|
56
|
+
result << name_to_code("reset")
|
57
|
+
end
|
58
|
+
result
|
59
|
+
end
|
60
|
+
|
61
|
+
ATTRIBUTES.each do |attr|
|
62
|
+
code = <<-EODEF
|
63
|
+
def #{attr}(&blk)
|
64
|
+
color("#{attr}", &blk)
|
65
|
+
end
|
66
|
+
|
67
|
+
EODEF
|
68
|
+
|
69
|
+
eval code
|
70
|
+
end
|
71
|
+
|
72
|
+
# returns the code for the given color string, which is in the format:
|
73
|
+
# foreground* [on background]?
|
74
|
+
#
|
75
|
+
# Note that the foreground and background sections can have modifiers
|
76
|
+
# (attributes).
|
77
|
+
#
|
78
|
+
# Examples:
|
79
|
+
# black
|
80
|
+
# blue on white
|
81
|
+
# bold green on yellow
|
82
|
+
# underscore bold magenta on cyan
|
83
|
+
# underscore red on cyan
|
84
|
+
|
85
|
+
def code(str)
|
86
|
+
fg, bg = str.split(/\s*\bon_?\s*/)
|
87
|
+
(fg ? foreground(fg) : "") + (bg ? background(bg) : "")
|
88
|
+
end
|
89
|
+
|
90
|
+
# Returns the code for the given background color(s).
|
91
|
+
def background(bgcolor)
|
92
|
+
name_to_code("on_" + bgcolor)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Returns the code for the given foreground color(s).
|
96
|
+
def foreground(fgcolor)
|
97
|
+
fgcolor.split(/\s+/).collect { |fg| name_to_code(fg) }.join("")
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
# Highlights using HTML. Fonts are highlighted using <span> tags, not <font>.
|
104
|
+
# Also note that reverse is translated to white on black.
|
105
|
+
# According to http://www.w3.org/TR/REC-CSS2/syndata.html#value-def-color,
|
106
|
+
# valid color keywords are: aqua, black, blue, fuchsia, gray, green, lime,
|
107
|
+
# maroon, navy, olive, purple, red, silver, teal, white, and yellow.
|
108
|
+
# Thus, no magenta or cyan.
|
109
|
+
|
110
|
+
class HTMLHighlighter < Highlighter
|
111
|
+
|
112
|
+
def initialize
|
113
|
+
# we need to know what we're resetting from (bold, font, underlined ...)
|
114
|
+
@stack = []
|
115
|
+
end
|
116
|
+
|
117
|
+
# Returns the start tag for the given name.
|
118
|
+
|
119
|
+
def start_style(name)
|
120
|
+
case name
|
121
|
+
when "reverse"
|
122
|
+
"<span style=\"color: white; background-color: black\">"
|
123
|
+
when /on_(\w+)/
|
124
|
+
"<span style=\"background-color: #{$1}\">"
|
125
|
+
else
|
126
|
+
"<span style=\"color: #{name}\">"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Returns the end tag ("</span>").
|
131
|
+
|
132
|
+
def end_style
|
133
|
+
"</span>"
|
134
|
+
end
|
135
|
+
|
136
|
+
def color_value(cname)
|
137
|
+
case cname
|
138
|
+
when "cyan"
|
139
|
+
"#00FFFF"
|
140
|
+
when "magenta"
|
141
|
+
"#FF00FF"
|
142
|
+
else
|
143
|
+
cname
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# Returns the code for the given name.
|
148
|
+
|
149
|
+
def name_to_code(name)
|
150
|
+
@stack << name
|
151
|
+
|
152
|
+
case name
|
153
|
+
when "none", "reset"
|
154
|
+
@stack.pop
|
155
|
+
str = ""
|
156
|
+
if @stack.length > 0
|
157
|
+
begin
|
158
|
+
prev = @stack.pop
|
159
|
+
case prev
|
160
|
+
when "bold"
|
161
|
+
str << "</b>"
|
162
|
+
when "underscore", "underline"
|
163
|
+
str << "</u>"
|
164
|
+
when "blink"
|
165
|
+
str << "</blink>"
|
166
|
+
when "concealed"
|
167
|
+
str << " -->"
|
168
|
+
else
|
169
|
+
str << end_style
|
170
|
+
end
|
171
|
+
end while @stack.length > 0
|
172
|
+
end
|
173
|
+
str
|
174
|
+
when "bold"
|
175
|
+
"<b>"
|
176
|
+
when "underscore", "underline"
|
177
|
+
"<u>"
|
178
|
+
when "blink"
|
179
|
+
"<blink>"
|
180
|
+
when "concealed"
|
181
|
+
"<!-- "
|
182
|
+
else
|
183
|
+
start_style(name)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
# Highlights using ANSI escape sequences.
|
191
|
+
|
192
|
+
class ANSIHighlighter < Highlighter
|
193
|
+
|
194
|
+
@@ATTRIBUTES = Hash[
|
195
|
+
'none' => '0',
|
196
|
+
'reset' => '0',
|
197
|
+
'bold' => '1',
|
198
|
+
'underscore' => '4',
|
199
|
+
'underline' => '4',
|
200
|
+
'blink' => '5',
|
201
|
+
'reverse' => '7',
|
202
|
+
'concealed' => '8',
|
203
|
+
'black' => '30',
|
204
|
+
'red' => '31',
|
205
|
+
'green' => '32',
|
206
|
+
'yellow' => '33',
|
207
|
+
'blue' => '34',
|
208
|
+
'magenta' => '35',
|
209
|
+
'cyan' => '36',
|
210
|
+
'white' => '37',
|
211
|
+
'on_black' => '40',
|
212
|
+
'on_red' => '41',
|
213
|
+
'on_green' => '42',
|
214
|
+
'on_yellow' => '43',
|
215
|
+
'on_blue' => '44',
|
216
|
+
'on_magenta' => '45',
|
217
|
+
'on_cyan' => '46',
|
218
|
+
'on_white' => '47',
|
219
|
+
]
|
220
|
+
|
221
|
+
# Returns the escape sequence for the given name.
|
222
|
+
|
223
|
+
def name_to_code(nm)
|
224
|
+
"\e[#{@@ATTRIBUTES[nm]}m"
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
|
230
|
+
# Does no highlighting.
|
231
|
+
|
232
|
+
class NonHighlighter < Highlighter
|
233
|
+
|
234
|
+
# Since the NonHighlighter does no highlighting, and thus its name, this
|
235
|
+
# returns an empty string.
|
236
|
+
|
237
|
+
def name_to_code(colorname)
|
238
|
+
""
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
# An object that can be highlighted. This is used by the String class.
|
245
|
+
|
246
|
+
module Highlightable
|
247
|
+
|
248
|
+
# The highlighter for the class in which this module is included.
|
249
|
+
|
250
|
+
@@highlighter = NonHighlighter.new
|
251
|
+
|
252
|
+
Text::Highlighter::ATTRIBUTES.each do |attr|
|
253
|
+
code = <<-EODEF
|
254
|
+
def #{attr}(&blk)
|
255
|
+
@@highlighter.color("#{attr}", self, &blk)
|
256
|
+
end
|
257
|
+
|
258
|
+
EODEF
|
259
|
+
|
260
|
+
eval code
|
261
|
+
end
|
262
|
+
|
263
|
+
alias negative reverse
|
264
|
+
|
265
|
+
# Sets the highlighter for this class. This can be either by type or by
|
266
|
+
# String.
|
267
|
+
|
268
|
+
def highlighter=(hl)
|
269
|
+
$VERBOSE = false
|
270
|
+
@@highlighter = case hl
|
271
|
+
when Text::Highlighter
|
272
|
+
hl
|
273
|
+
when Text::Highlighter::NONE, "NONE", nil
|
274
|
+
Text::NonHighlighter.new unless @@highlighter.kind_of?(Text::NonHighlighter)
|
275
|
+
when Text::Highlighter::HTML, "HTML"
|
276
|
+
Text::HTMLHighlighter.new unless @@highlighter.kind_of?(Text::HTMLHighlighter)
|
277
|
+
when Text::Highlighter::ANSI, "ANSI"
|
278
|
+
Text::ANSIHighlighter.new unless @@highlighter.kind_of?(Text::ANSIHighlighter)
|
279
|
+
else
|
280
|
+
Text::NonHighlighter.new
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
285
|
+
|
286
|
+
$HAVE_TEXT_HIGHLIGHT = true
|
287
|
+
|
288
|
+
end
|
289
|
+
|
290
|
+
|
291
|
+
# String is extended to support highlighting.
|
292
|
+
|
293
|
+
class String
|
294
|
+
include Text::Highlightable
|
295
|
+
extend Text::Highlightable
|
296
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: "0.8"
|
3
|
+
specification_version: 1
|
4
|
+
name: text-highlight
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.2
|
7
|
+
date: 2004-11-08
|
8
|
+
summary: "A Ruby module for highlighting text, using ANSI escape sequences or HTML."
|
9
|
+
require_paths:
|
10
|
+
- "."
|
11
|
+
- text
|
12
|
+
author: Jeff Pace
|
13
|
+
email: jpace@incava.org
|
14
|
+
homepage: http://text-highlight.rubyforge.org/
|
15
|
+
rubyforge_project: text-highlight
|
16
|
+
description: "A Ruby module for highlighting text, using ANSI escape sequences or HTML."
|
17
|
+
autorequire: highlight
|
18
|
+
default_executable:
|
19
|
+
bindir: bin
|
20
|
+
has_rdoc: true
|
21
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
22
|
+
requirements:
|
23
|
+
-
|
24
|
+
- ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.0
|
27
|
+
version:
|
28
|
+
platform: ruby
|
29
|
+
files:
|
30
|
+
- README
|
31
|
+
- install.rb
|
32
|
+
- t
|
33
|
+
- text
|
34
|
+
- text-highlight-1.0.2.gem
|
35
|
+
- text-highlight.gemspec
|
36
|
+
- t/runtests.rb
|
37
|
+
- t/test_ansihighlight.rb
|
38
|
+
- t/test_htmlhighlight.rb
|
39
|
+
- t/test_nonhighlight.rb
|
40
|
+
- t/testcase.rb
|
41
|
+
- text/highlight.rb
|
42
|
+
test_files:
|
43
|
+
- t/runtests.rb
|
44
|
+
rdoc_options:
|
45
|
+
- "--main"
|
46
|
+
- README
|
47
|
+
extra_rdoc_files:
|
48
|
+
- README
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
requirements: []
|
52
|
+
dependencies: []
|