PandocAnsiFilter/ansi2html.lua

118 lines
2.3 KiB
Lua
Raw Normal View History

2022-09-21 17:06:53 +02:00
-- this is essentially
-- https://github.com/jupyter/nbconvert/blob/main/nbconvert/filters/ansi.py
-- converted to lua
local ANSI_COLORS = {
"ansi-black",
"ansi-red",
"ansi-green",
"ansi-yellow",
"ansi-blue",
"ansi-magenta",
"ansi-cyan",
"ansi-white",
"ansi-black-intense",
"ansi-red-intense",
"ansi-green-intense",
"ansi-yellow-intense",
"ansi-blue-intense",
"ansi-magenta-intense",
"ansi-cyan-intense",
"ansi-white-intense"
}
2022-09-21 21:01:18 +02:00
--[[
2022-09-21 17:06:53 +02:00
local re = require "re"
2022-09-21 13:26:09 +02:00
local codes = {
-- attributes
reset = 0,
bright = 1,
dim = 2,
underscore = 4,
-- blink = 5,
reverse = 7,
-- hidden = 8,
-- foreground
black = 30,
red = 31,
green = 32,
yellow = 33,
blue = 34,
magenta = 35,
cyan = 36,
white = 37,
-- background
onblack = 40,
onred = 41,
ongreen = 42,
onyellow = 43,
onblue = 44,
onmagenta = 45,
oncyan = 46,
onwhite = 47,
}
2022-09-21 17:06:53 +02:00
local ANSI = re.compile [[
'\x1b%[' {.*?} {[@-~]}
]]
2022-09-21 13:26:09 +02:00
2022-09-21 21:01:18 +02:00
--]]
local function LaTeXconverter()
end
local function HTMLconverter(fg, bg, bold, underline, inverse)
if not (fg or bg or bold or underline or inverse) then
return "",""
end
local classes = {}
local styles = {}
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(")
end
end
2022-09-21 17:06:53 +02:00
2022-09-21 21:01:18 +02:00
function CodeBlock(e)
if FORMAT:match 'latex' then
local converter = LaTeXconverter
elseif FORMAT:match 'html' then
local converter = HTMLconverter
elseif FORMAT:match 'native' then
local converter = HTMLconverter
else
return
end
if string.find(e.text, "\x1b%[") then
2022-09-21 17:06:53 +02:00
local text = e.text
local out = ""
2022-09-21 21:01:18 +02:00
-- while text do
local s1, e1, c1, d1 = string.find(text, "\x1b%[(.-)([@-~])")
2022-09-21 17:06:53 +02:00
if s1 then
if d1=="m" then
local numbers={}
for i in string.gmatch(c1, "[^;]") do
table.insert(numbers, tonumber(i))
2022-09-21 21:01:18 +02:00
end
print(s1,numbers[1])
2022-09-21 17:06:53 +02:00
end
end
2022-09-21 21:01:18 +02:00
-- end
2022-09-21 17:06:53 +02:00
end
end