From 69c2e0545f008ae66835d9db86b01d49a01b1c0d Mon Sep 17 00:00:00 2001 From: Meik Hellmund Date: Thu, 22 Sep 2022 13:11:06 +0200 Subject: [PATCH] some progress --- ansi2html.lua | 65 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/ansi2html.lua b/ansi2html.lua index d1a5f67..38549c7 100644 --- a/ansi2html.lua +++ b/ansi2html.lua @@ -77,41 +77,90 @@ local function HTMLconverter(fg, bg, bold, underline, inverse) if inverse then fg, bg = bg, fg end + if type(fg) == "number" then table.insert(classes, ANSI_COLORS[fg] .. "-fg") elseif fg then - table.insert("background-color: rgb(") + table.insert(styles, string.format("color: rgb(%d,%d,%d)", fg[1], fg[2], fg[3])) + elseif inverse then + table.insert(classes,"ansi-default-inverse-fg") end + if type(bg) == "number" then + table.insert(classes, ANSI_COLORS[bg] .. "-bg") + elseif bg then + table.insert(styles, string.format("background-color: rgb(%d,%d,%d)", bg[1], bg[2], bg[3])) + elseif inverse then + table.insert(classes,"ansi-default-inverse-bg") + end + if bold then + table.insert(classes, "ansi-bold") + end + + if underline then + table.insert(classes, "ansi-underline") + end + + local starttag = "" end function CodeBlock(e) + local converter if FORMAT:match 'latex' then - local converter = LaTeXconverter + converter = LaTeXconverter elseif FORMAT:match 'html' then - local converter = HTMLconverter + converter = HTMLconverter elseif FORMAT:match 'native' then - local converter = HTMLconverter + converter = HTMLconverter else return end if string.find(e.text, "\x1b%[") then + local bold = false + local underline = false + local inverse = false local text = e.text local out = "" - -- while text do + local chunk = "" + local fg = {} + local bg = {} + local starttag = "" + local endtag = "" + + while text do local s1, e1, c1, d1 = string.find(text, "\x1b%[(.-)([@-~])") if s1 then - if d1=="m" then + if d1 == "m" then local numbers={} for i in string.gmatch(c1, "[^;]") do table.insert(numbers, tonumber(i)) end - print(s1,numbers[1]) end + chunk, text = text:sub(1, s1-1), text:sub(e1+1) + else + chunk, text = text, "" end - -- end + + if chunk then + if bold and type(fg)=="number" and fg<8 then + starttag, endtag = converter(fg+8, bg, bold, underline, inverse) + else + starttag, endtag = converter(fg, bg, bold, underline, inverse) + end + out = out .. starttag .. chunk .. endtag + end + end end end