trphoenix
2025-11-27 671e8fe727be08651992bb5f49b2680731543fc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- minimum supported version for full environment
-- support is 3.8.unkown yet to be released but probably 3
local environment_fully_supported_version = pandoc.types.Version('3.8.3')
local environment_partially_supported_version = pandoc.types.Version('3.8.0')
local is_partially_supported = PANDOC_VERSION >= environment_partially_supported_version
local problamatic_environments = {
    displaymath = true,
    math = true,
    equation = true,
    ["equation*"] = true,
    gather = true,
    ["gather*"] = true,
    multline = true,
    ["multline*"] = true,
    eqnarray = true,
    ["eqnarray*"] = true,
    align = true,
    ["align*"] = true,
    alignat = true,
    ["alignat*"] = true,
    flalign = true,
    ["flalign*"] = true,
}
if is_partially_supported then
    return {
        {
            Math = function(elem)
                if elem.text:find("^%s*\\begin{") ~= nil then
                    local replacement = pandoc.text:gsub(elem.text, "^%s*\\begin{(.-)}", "\\begin{%1}"):gsub("\\end{(.-)}%s*$", "\\end{%1}")
                    return pandoc.Math(replacement, elem.mathtype)
                else
                    return elem
                end
            end,
        }
    }
elseif not environment_fully_supported_version then
    return {
        {
            Math = function(elem)
                local result = elem.text:match("^%s*\\begin{(%a+%*?)}")
                if result ~= nil and problamatic_environments[result] ~= nil then
                    return pandoc.RawInline('tex', elem.text)
                else
                    return elem
                end
            end,
        }
    }
end