Ruby 中带有 html 标签的 ANSI 转义码?

发布于 2024-10-15 20:02:26 字数 309 浏览 3 评论 0原文

有趣的是,Ruby中有内置的ansi转义码

还有一个来自 a gem 的更强大的版本。

不幸的是,这些日志输出到控制台。我的文本显示在页面中,因此我需要 HTML 标签来包围我的文本。

你们知道如何去做吗?

Interestingly there are built-in ansi escape code in Ruby.

There is also a more powerful version from a gem.

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

Would you guys have any idea how to go about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骑趴 2024-10-22 20:02:26

我猜你想要的是从转义字符转换为 HTML。

我通过假设以下转义字符的代码/颜色哈希来完成一次:

{ :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}

我所做的是以下转换(远未优化):

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

嗯,您将需要填充我没有考虑的颜色或背景的间隙。但我想你能明白这个想法。

希望有帮助

I guess what you want is to transform from escape characters to HTML.

I did it once by assuming the following code/colour 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}

What I did was the following conversion (far away from being anyhow 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 fill the gaps of the colours I am not considering or backgrounds. But I guess you can get the idea.

Hope it helps

夜无邪 2024-10-22 20:02:26

感谢您提供我从未见过的很酷的宝石的链接。然而,我认为您正在寻找的东西被称为级联样式表(CSS)。因为 Google 搜索将显示互联网上缓存的所有其他页面,所以这里有一些可以帮助您入门的链接:

*SASS 是 CSS 的 ruby​​ 化抽象,经常与 ruby​​/rails 一起使用

Thank you for the link to a cool gem I had not seen. I think what you are looking for, however, is termed Cascading Style Sheets (CSS). Because that google search will bring up about every other page cached on the internet, here are a few links for you that should get you started:

*SASS is a ruby-ized abstraction to CSS used very frequently with ruby/rails

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文