Files
su2026rwep/_extensions/drwater/text-align/text-align.lua
T
2026-05-21 13:37:53 +08:00

47 lines
1.4 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--- Align the content of a div (right/center)
--- Copyright: © 2025Present Tom Ben
--- License: MIT License
local function wrap_with(blocks, format, start, finish)
local content = {}
table.insert(content, pandoc.RawBlock(format, start))
for _, block in ipairs(blocks) do
table.insert(content, block)
end
table.insert(content, pandoc.RawBlock(format, finish))
return content
end
local function align_block(el, align)
if FORMAT == "docx" then
if align == "right" then
el.attributes['custom-style'] = 'Right Align'
else
el.attributes['custom-style'] = 'Center Align'
end
return el
elseif FORMAT == "latex" then
if align == "right" then
return wrap_with(el.content, "latex", "\\begin{flushright}", "\\end{flushright}")
else
return wrap_with(el.content, "latex", "\\begin{center}", "\\end{center}")
end
elseif FORMAT == "typst" then
return wrap_with(el.content, "typst", "#align(" .. align .. ")[", "]")
elseif FORMAT == "html" or FORMAT == "epub" then
el.attributes['style'] = "text-align: " .. align .. ";"
return el
else
return el
end
end
function Div(el)
if el.classes:includes("right") then
return align_block(el, "right")
elseif el.classes:includes("center") then
return align_block(el, "center")
end
end