Div selector added
This commit is contained in:
parent
9242d38ecf
commit
624984fc2a
133
ansi2html.lua
133
ansi2html.lua
@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
-- this is essentially
|
||||
-- https://github.com/jupyter/nbconvert/blob/main/nbconvert/filters/ansi.py
|
||||
-- converted to lua
|
||||
@ -54,7 +52,8 @@ pre .ansi-underline { text-decoration: underline; }
|
||||
|
||||
local latexpreamble = [[
|
||||
\usepackage{fancyvrb}
|
||||
\usepackage{fontspec}
|
||||
\usepackage{fontspec}
|
||||
\usepackage{xcolor}
|
||||
\renewenvironment{verbatim}{%
|
||||
\VerbatimEnvironment
|
||||
\begin{Verbatim}[commandchars=\\\{\}]%
|
||||
@ -68,6 +67,24 @@ local latexpreamble = [[
|
||||
ItalicFont = *-RegularItalic,
|
||||
BoldItalicFont = *-BoldItalic,
|
||||
]
|
||||
\definecolor{ansi-black}{HTML}{3E424D}
|
||||
\definecolor{ansi-black-intense}{HTML}{282C36}
|
||||
\definecolor{ansi-red}{HTML}{E75C58}
|
||||
\definecolor{ansi-red-intense}{HTML}{B22B31}
|
||||
\definecolor{ansi-green}{HTML}{00A250}
|
||||
\definecolor{ansi-green-intense}{HTML}{007427}
|
||||
\definecolor{ansi-yellow}{HTML}{DDB62B}
|
||||
\definecolor{ansi-yellow-intense}{HTML}{B27D12}
|
||||
\definecolor{ansi-blue}{HTML}{208FFB}
|
||||
\definecolor{ansi-blue-intense}{HTML}{0065CA}
|
||||
\definecolor{ansi-magenta}{HTML}{D160C4}
|
||||
\definecolor{ansi-magenta-intense}{HTML}{A03196}
|
||||
\definecolor{ansi-cyan}{HTML}{60C6C8}
|
||||
\definecolor{ansi-cyan-intense}{HTML}{258F8F}
|
||||
\definecolor{ansi-white}{HTML}{C5C1B4}
|
||||
\definecolor{ansi-white-intense}{HTML}{A1A6B2}
|
||||
\definecolor{ansi-default-inverse-fg}{HTML}{FFFFFF}
|
||||
\definecolor{ansi-default-inverse-bg}{HTML}{000000}
|
||||
]]
|
||||
|
||||
local ANSI_COLORS = {
|
||||
@ -99,7 +116,7 @@ local function get_extended_color(numbers)
|
||||
r = table.remove(numbers, 1)
|
||||
g = table.remove(numbers, 1)
|
||||
b = table.remove(numbers, 1)
|
||||
elseif n == 5 and #numbers >= 1 then
|
||||
elseif n == 5 and #numbers >= 1 then
|
||||
-- 256 colors
|
||||
idx = table.remove(numbers, 1)
|
||||
if idx < 16 then
|
||||
@ -108,13 +125,13 @@ local function get_extended_color(numbers)
|
||||
elseif idx < 232 then
|
||||
-- 6x6x6 color cube, see http://stackoverflow.com/a/27165165/500098
|
||||
r = (idx - 16) // 36
|
||||
r = 55 + r * 40
|
||||
r = 55 + r * 40
|
||||
if r < 0 then r = 0 end
|
||||
g = ((idx - 16) % 36) // 6
|
||||
g = 55 + g * 40
|
||||
g = 55 + g * 40
|
||||
if g < 0 then g = 0 end
|
||||
b = (idx - 16) % 6
|
||||
b = 55 + b * 40
|
||||
b = 55 + b * 40
|
||||
if b < 0 then b = 0 end
|
||||
elseif idx < 256 then
|
||||
-- grayscale, see http://stackoverflow.com/a/27165165/500098
|
||||
@ -147,19 +164,46 @@ local function LaTeXconverter(fg, bg, bold, underline, inverse)
|
||||
if inverse then
|
||||
fg, bg = bg, fg
|
||||
end
|
||||
|
||||
|
||||
if type(fg) == "number" then
|
||||
starttag = starttag .. "\\textcolor{" .. ANSI_COLORS[fg+1] .. "}{"
|
||||
starttag = starttag .. [[\textcolor{]] .. ANSI_COLORS[fg+1] .. "}{"
|
||||
endtag = "}" .. endtag
|
||||
elseif type(fg) == "table" then
|
||||
-- See http://tex.stackexchange.com/a/291102/13684
|
||||
starttag = starttag .. "\\def\\tcRGB{\\textcolor[RGB]}\\expandafter"
|
||||
starttag = starttag .. "\\tcRGB\\expandafter{\\detokenize{%s,%s,%s}}{" % fg
|
||||
starttag = starttag .. [[\def\tcRGB{\textcolor[RGB]}\expandafter]]
|
||||
starttag = starttag .. string.format([[\tcRGB\expandafter{\detokenize{%d,%d,%d}}{]], fg[1], fg[2], fg[3])
|
||||
endtag = "}" .. endtag
|
||||
elseif inverse then
|
||||
starttag = starttag .. "\\textcolor{ansi-default-inverse-fg}{"
|
||||
starttag = starttag .. [[\textcolor{ansi-default-inverse-fg}{]]
|
||||
endtag = "}" .. endtag
|
||||
end
|
||||
|
||||
if type(bg) == "number" then
|
||||
starttag = starttag .. [[\setlength{\fboxsep}{0pt}]]
|
||||
starttag = starttag .. [[\colorbox{]] .. ANSI_COLORS[bg+1] .. "}{"
|
||||
endtag = [[\strut}]] .. endtag
|
||||
elseif type(bg) == "table" then
|
||||
-- See http://tex.stackexchange.com/a/291102/13684
|
||||
starttag = starttag .. [[\setlength{\fboxsep}{0pt}]]
|
||||
starttag = starttag .. [[\def\cbRGB{\colorbox[RGB]}\expandafter]]
|
||||
starttag = starttag .. string.format([[\cbRGB\expandafter{\detokenize{%d,%d,%d}}{]], bg[1], bg[2], bg[3])
|
||||
endtag = [[\strut}]] .. endtag
|
||||
elseif inverse then
|
||||
starttag = starttag .. [[\setlength{\fboxsep}{0pt}]]
|
||||
starttag = starttag .. [[\colorbox{ansi-default-inverse-bg}{]]
|
||||
endtag = [[\strut}]] .. endtag
|
||||
end
|
||||
|
||||
if bold then
|
||||
starttag = starttag .. [[\textbf{]]
|
||||
endtag = "}" .. endtag
|
||||
end
|
||||
|
||||
if underline then
|
||||
starttag = starttag .. [[\underline{]]
|
||||
endtag = "}" .. endtag
|
||||
end
|
||||
return starttag, endtag
|
||||
end
|
||||
|
||||
local function HTMLconverter(fg, bg, bold, underline, inverse)
|
||||
@ -185,7 +229,7 @@ local function HTMLconverter(fg, bg, bold, underline, inverse)
|
||||
if type(bg) == "number" then
|
||||
table.insert(classes, ANSI_COLORS[bg+1] .. "-bg")
|
||||
elseif type(bg) == "table" then
|
||||
table.insert(styles, string.format("background-color: rgb(%d,%d,%d)",
|
||||
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")
|
||||
@ -199,16 +243,16 @@ local function HTMLconverter(fg, bg, bold, underline, inverse)
|
||||
table.insert(classes, "ansi-underline")
|
||||
end
|
||||
|
||||
local starttag = "<span"
|
||||
local starttag = "<span"
|
||||
if next(classes) ~= nil then
|
||||
starttag = starttag .. ' class="' .. table.concat(classes, " ") .. '"'
|
||||
starttag = starttag .. ' class="' .. table.concat(classes, " ") .. '"'
|
||||
end
|
||||
|
||||
if next(styles) ~= nil then
|
||||
starttag = starttag .. ' style="' .. table.concat(styles, " ") .. '"'
|
||||
starttag = starttag .. ' style="' .. table.concat(styles, " ") .. '"'
|
||||
end
|
||||
|
||||
return starttag..">","</span>"
|
||||
return starttag..">","</span>"
|
||||
end
|
||||
|
||||
|
||||
@ -249,7 +293,7 @@ local function codeBlockTrans(e)
|
||||
table.insert(numbers, tonumber(i))
|
||||
end
|
||||
else
|
||||
io.stderr:write("Unsupported ANSI sequence ESC["..c1..d1.." ignored\n" )
|
||||
io.stderr:write("Unsupported ANSI sequence ESC["..c1..d1.." ignored\n" )
|
||||
end
|
||||
chunk, text = text:sub(1, s1-1), text:sub(e1+1)
|
||||
else
|
||||
@ -304,34 +348,60 @@ local function codeBlockTrans(e)
|
||||
elseif n >= 100 and n <= 107 then
|
||||
bg = n + 8 - 100
|
||||
else
|
||||
io.stderr:write(string.format("ESC sequence with unknown code %d before:\n",n))
|
||||
io.stderr:write(string.format("ESC sequence with unknown code %d before:\n",n))
|
||||
io.stderr:write(chunk.."\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
-- e.text = out
|
||||
return pandoc.RawBlock(fmt, '<pre class="ansi"><code class="ansi">'..out..'</code></pre>')
|
||||
if fmt == 'html' then
|
||||
return pandoc.RawBlock(fmt, '<pre class="ansi"><code class="ansi">'..out..'</code></pre>')
|
||||
end
|
||||
if fmt == 'latex' then
|
||||
return pandoc.RawBlock(fmt, "\\begin{verbatim}\n"..out.."\n\\end{verbatim}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- traverse='topdown'
|
||||
-- algo from https://github.com/jgm/pandoc/commit/77faccb505992c944cd1b92f50e4e00d2927682b
|
||||
local function divx(e)
|
||||
-- consider only formats html and latex
|
||||
local function divTrans(e)
|
||||
local fmt
|
||||
if FORMAT:match 'latex' then
|
||||
fmt = 'latex'
|
||||
elseif FORMAT:match 'html' then
|
||||
fmt = 'html'
|
||||
elseif FORMAT:match 'native' then
|
||||
fmt = 'html'
|
||||
else
|
||||
return
|
||||
end
|
||||
|
||||
local function cmp(a,b)
|
||||
return a["rank"] < b["rank"]
|
||||
end
|
||||
|
||||
if e.classes[1] == "output" then
|
||||
print("output div entered")
|
||||
io.stderr:write("\noutput div entered\n")
|
||||
local c = e.content -- enhanced pandoc.List table
|
||||
local ranks = {}
|
||||
local best = 0
|
||||
for i, el in pairs(c) do
|
||||
print (i .. el.t)
|
||||
io.stderr:write(i .. el.t)
|
||||
if el.t == "RawBlock" then
|
||||
print(el.format)
|
||||
io.stderr:write(el.format)
|
||||
if el.format == fmt then
|
||||
table.insert(ranks, {rank = 1, el})
|
||||
else
|
||||
table.insert(ranks, {rank = 3, el})
|
||||
end
|
||||
else
|
||||
ranks:insert({rank = 2, i, el})
|
||||
table.insert(ranks, {rank = 2, el})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
table.sort(ranks, cmp)
|
||||
local winner = ranks[1][1]
|
||||
e.content = pandoc.List:new()
|
||||
table.insert(e.content, winner)
|
||||
return e
|
||||
end
|
||||
end
|
||||
|
||||
@ -353,6 +423,7 @@ local function metaAdd(meta)
|
||||
end
|
||||
|
||||
return {
|
||||
{Div = divTrans},
|
||||
{CodeBlock = codeBlockTrans},
|
||||
{Meta = metaAdd}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user