-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathansi2html
executable file
·80 lines (72 loc) · 1.89 KB
/
ansi2html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env ruby
# Usage: ansi2html [-w] [--css]
#
# Convert terminal color ANSI escape sequences to HTML.
#
# -w : wrap HTML output in PRE tags
# -css : include CSS styles
#
# Examples:
#
# $ git log --oneline --decorate -10 --color | ansi2html
#
# $ ruby -e '30.upto(37) {|n| puts "\e[#{n}mHi \e[1;#{n}mho\e[m" }' | ansi2html
#
# Author: Mislav Marohnić
require 'cgi'
if ARGV.include?('-h') || ARGV.include?('--help')
File.open(__FILE__, 'r') do |source|
source.each do |line|
case line
when /^#!/ then next
when /^#/ then puts line.sub(/^# ?/, '')
else break
end
end
end
exit 0
end
wrap = ARGV.delete('-w')
css = ARGV.delete('--css')
data = ARGF.read
out = $stdout
COLORS = [
%w[black black #818383],
%w[red #BA3521 #F9391F],
%w[green #25BC22 #31E722],
%w[yellow #ADAD27 #EAEC23],
%w[blue #3D2EE2 #562FE2],
%w[magenta #AE38D4 #F935F7],
%w[cyan #2BB9C9 #14F0F0],
%w[white #DEDFE1 #E9EBEB],
]
def classnames(code)
codes = code.split(';').map { |c| c.to_i }
codes.delete(0)
if color_code = codes.find { |c| c.between?(30, 37) }
codes.delete(color_code)
codes << COLORS[color_code - 30][0]
end
codes << 'bright' if codes.delete(1)
codes.map { |c| "ansi-#{c}" }.join(' ')
end
open = false
out << "<pre class='ansi'>" if wrap
# replace escape sequences with SPAN tags
out << CGI.escapeHTML(data.rstrip).gsub(/\e\[ ([\d;]*)m /x) {
opentag = $1.empty?? nil : "<span class='#{classnames($1)}'>"
closetag = open ? '</span>' : nil
open = !!opentag
"#{closetag}#{opentag}"
}
out << "</pre>" if wrap
out << "\n"
if css
out.puts "\n<style type=\"text/css\">"
out.puts " pre.ansi { color: #DEDFE1; background-color: #010101; }"
COLORS.map do |name, normal, bright|
out.puts " pre.ansi .ansi-#{name} { color: #{normal}; }"
out.puts " pre.ansi .ansi-#{name}.ansi-bright { color: #{bright}; }"
end
out.puts "</style>"
end