Files
PandocAnsiFilter/ansi2html.lua
2022-09-21 17:06:53 +02:00

108 lines
2.2 KiB
Lua

-- 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"
}
local re = require "re"
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,
}
local ANSI = re.compile [[
'\x1b%[' {.*?} {[@-~]}
]]
function CodeBlock(e)
local s1, e1, c1, d1 = string.find(e.text, "\x1b%[(.-)([@-~])")
if s1 then
print(e.text)
print(s1, e1, c1, d1)
local text = e.text
local out = ""
while text do
s1, e1, c1, d1 = string.find(text, "\x1b%[(.-)([@-~])")
if s1 then
if d1=="m" then
local numbers={}
for i in string.gmatch(c1, "[^;]") do
table.insert(numbers, tonumber(i))
end
end
end
end
end
end
function CodeBlockx(e)
if FORMAT:match 'latex' or FORMAT:match 'latex' or FORMAT:match 'native' then
local s1, e1 = string.find(e.text, "\u{001b}\u{005b}", 1, true)
if s1 then
local i = 1
local res = ""
--while i <= #e.text do
--end
-- res = ""
-- for i=1, #e.text) do
-- end
--s1,e1 = string.find(e.text, "\x1b%[") -- "\x1b\\[(.*?)([@-~])"
-- if s1 then
print(#e.text)
print(s1,e1)
end
end
end