Skip to content

Instantly share code, notes, and snippets.

@yuters
Created March 17, 2013 20:52
Show Gist options
  • Save yuters/5183603 to your computer and use it in GitHub Desktop.
Save yuters/5183603 to your computer and use it in GitHub Desktop.
From 2645e8296145ffd7dbbb40dbbba2660a8f1da717 Mon Sep 17 00:00:00 2001
From: gitlab-ci system <gitlab-ci@localhost>
Date: Sun, 17 Mar 2013 16:47:31 -0400
Subject: [PATCH] change ansi2html.rb
---
app/assets/stylesheets/main.scss | 24 ++++++++++++++++++
lib/ansi2html.rb | 55 ++++++++++++++++++++++------------------
2 files changed, 55 insertions(+), 24 deletions(-)
diff --git a/app/assets/stylesheets/main.scss b/app/assets/stylesheets/main.scss
index e3a1856..b37d3d9 100644
--- a/app/assets/stylesheets/main.scss
+++ b/app/assets/stylesheets/main.scss
@@ -178,6 +178,30 @@ h3.error { color: #B94A48; }
.grey {
color: grey;
}
+.bg-red {
+ background-color: red;
+}
+.bg-green {
+ background-color: #0E0;
+}
+.bg-yellow {
+ background-color: yellow;
+}
+.bg-blue {
+ background-color: blue;
+}
+.bg-magenta {
+ background-color: magenta;
+}
+.bg-cyan {
+ background-color: cyan;
+}
+.bg-white {
+ background-color: white;
+}
+.bg-grey {
+ background-color: grey;
+}
.bordered {
border: 1px solid #DDD;
diff --git a/lib/ansi2html.rb b/lib/ansi2html.rb
index 59491f1..6692d24 100644
--- a/lib/ansi2html.rb
+++ b/lib/ansi2html.rb
@@ -9,36 +9,43 @@ module Ansi2html
'35' => 'magenta',
'36' => 'cyan',
'37' => 'white',
+ '40' => 'bg-black',
+ '41' => 'bg-red',
+ '42' => 'bg-green',
+ '43' => 'bg-yellow',
+ '44' => 'bg-blue',
+ '45' => 'bg-magenta',
+ '46' => 'bg-cyan',
+ '47' => 'bg-white',
'90' => 'grey'
}
def self.convert(ansi)
+
out = ""
- tag_open = false
- s = StringScanner.new(ansi.gsub("<", "&lt;"))
- while(!s.eos?)
- if s.scan(/\e\[(3[0-7]|90)m/) || s.scan(/\e\[1;(3[0-7])m/)
- if tag_open
- out << %{</span>}
- end
- out << %{<span class="#{COLOR[s[1]]}">}
- tag_open = true
- elsif s.scan(/\e\[1m/)
- # Just ignore bold style
- else
- if s.scan(/\e\[0m/)
- if tag_open
- out << %{</span>}
- end
- tag_open = false
- else
- out << s.scan(/./m)
- end
+
+ ansi.each_line{|line|
+
+ line.chomp!
+
+ # process text color/decoration
+ a = line.partition(/\e\[.*?m/) # split at first escape
+ aline = a[0]
+ while a[1] != ""
+ # interpret each numerical code
+ a[1][2..-2].split(';').each{|code|
+ aline += "<span class=\"#{COLOR[code]}\">"
+ }
+ a = a[2].partition(/\e\[.*?m/)
+ aline += a[0]
end
- end
- if tag_open
- out << %{</span>}
- end
+
+ aline += "\r\n"
+ # close tags
+ n = aline.scan("<span").length
+ out << aline + "</span>"*n
+ }
+
out
end
end
--
1.8.1.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment