ANSI html escape code in Ruby?

Interestingly, the built-in ansi exit code in Ruby .

There is also a more powerful version of the gem .

Unfortunately, these logs are displayed on the console. My text is shown on the page, so I need HTML tags to wrap around my text.

Could you guys think how to do this?

+4
source share
2 answers

I assume that you want to convert from escape characters to HTML.

I did this once, assuming the following code / color hash for escape characters:

{ :reset => 0, :bright => 1, :dark => 2, :underline => 4, :blink => 5, :negative => 7, :black => 30, :red => 31, :green => 32, :yellow => 33, :blue => 34, :magenta => 35, :cyan => 36, :white => 37, :back_black => 40, :back_red => 41, :back_green => 42, :back_yellow => 43, :back_blue => 44, :back_magenta => 45, :back_cyan => 46, :back_white => 47} 

I did the following conversion (far from being so optimized):

 def escape_to_html(data) { 1 => :nothing, 2 => :nothing, 4 => :nothing, 5 => :nothing, 7 => :nothing, 30 => :black, 31 => :red, 32 => :green, 33 => :yellow, 34 => :blue, 35 => :magenta, 36 => :cyan, 37 => :white, 40 => :nothing, 41 => :nothing, 43 => :nothing, 44 => :nothing, 45 => :nothing, 46 => :nothing, 47 => :nothing, }.each do |key, value| if value != :nothing data.gsub!(/\e\[#{key}m/,"<span style=\"color:#{value}\">") else data.gsub!(/\e\[#{key}m/,"<span>") end end data.gsub!(/\e\[0m/,'</span>') return data end 

Well, you will need to fill in the blanks of the colors that I am not considering, or the background. But I think you can get this idea.

Hope this helps

+5
source

Thanks for the link to the coolest stone that I have not seen. I think all you are looking for is called Cascading Style Sheets (CSS). Since this Google search will reveal information about every other page cached on the Internet, here are some links for you that should launch you:

* SASS is a ruby ​​CSS abstraction, used very often with ruby ​​/ rails

+2
source

Source: https://habr.com/ru/post/1338365/


All Articles