1 Star 0 Fork 0

风不会停息/pandoc-plug

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
plantuml.lua 1.90 KB
一键复制 编辑 原始数据 按行查看 历史
风不会停息 提交于 2024-09-02 16:41 +08:00 . first commit
-- filter_plantuml.lua
local function base64Encode(data)
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
return ((data:gsub('.', function(x)
local r, b = '', x:byte()
for i = 8, 1, -1 do
r = r .. (b % 2 ^ i - b % 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
local function convertToHex(str)
local hexString = {}
for i = 1, #str do
local byte = string.byte(str, i)
table.insert(hexString, string.format("%02X", byte))
end
hexString = table.concat(hexString)
return hexString
end
-- pandoc lua 语法
function CodeBlock(block)
-- 检查代码块是否为 PlantUML 代码
if block.classes[1] == "plantuml" then
-- 提取 PlantUML 代码
local code = block.text
--print((code))
--print(convertToHex(code))
local url = "https://www.plantuml.com/plantuml/svg/~h" .. convertToHex(code)
-- 通过 Pandoc 的 mediabag.fetch() 将图片嵌入到文档中
print("plantUml & request URL: "..url)
local img, contents = pandoc.mediabag.fetch(url, ".svg")
print("request URL end ...")
--print("plantUml contents: "..contents)
-- 将二进制数据转换为 Base64 编码的字符串
local base64_contents = base64Encode(contents)
-- 构建 HTML 标签
local img_html = pandoc.RawBlock('html', string.format('<img src="data:image/svg+xml;base64,%s">', base64_contents))
print("return ing...")
return img_html
end
return block
end
return {
{ CodeBlock = CodeBlock }
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Lua
1
https://gitee.com/handcontent/pandoc-plug.git
git@gitee.com:handcontent/pandoc-plug.git
handcontent
pandoc-plug
pandoc-plug
master

搜索帮助