31 lines
1.2 KiB
Lua
31 lines
1.2 KiB
Lua
-- inline-media.lua — pandoc filter that rewrites every image to a self-contained
|
|
-- base64 data: URI, pulling the bytes from pandoc's mediabag (populated when
|
|
-- reading DOCX, or fetched for HTML). Used by the docx→md / html→md conversions
|
|
-- so the resulting markdown carries its images inline (markdown output has no
|
|
-- native --embed-resources equivalent).
|
|
|
|
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
|
|
local function base64(data)
|
|
return ((data:gsub('.', function(x)
|
|
local r, byte = '', x:byte()
|
|
for i = 8, 1, -1 do r = r .. (byte % 2 ^ i - byte % 2 ^ (i - 1) > 0 and '1' or '0') end
|
|
return r
|
|
end) .. '0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
|
|
if #x < 6 then return '' end
|
|
local c = 0
|
|
for i = 1, 6 do c = c + (x:sub(i, i) == '1' and 2 ^ (6 - i) or 0) end
|
|
return b:sub(c + 1, c + 1)
|
|
end) .. ({ '', '==', '=' })[#data % 3 + 1])
|
|
end
|
|
|
|
function Image(img)
|
|
local mt, data = pandoc.mediabag.lookup(img.src)
|
|
if not data then
|
|
mt, data = pandoc.mediabag.fetch(img.src)
|
|
end
|
|
if data then
|
|
img.src = 'data:' .. (mt or 'application/octet-stream') .. ';base64,' .. base64(data)
|
|
end
|
|
return img
|
|
end
|