summaryrefslogtreecommitdiffstats
path: root/filters/vars.lua
blob: e60019e14d42497ba9ac3e92aa73340e342326e0 (plain) (blame)
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
local vars = {}

function string.split(str, sep)
    sep = sep or '%s'

    local parts = pandoc.List()

    for field, s in str:gmatch("([^" .. sep .. "]*)(" .. sep .. "?)") do
        parts:insert(field)
        if s == "" then return parts end
    end
end

function meta(meta) vars = meta end

function str(el)
    local prefix, varref, suffix = el.text:match('^(.*)%%(.*)%%(.*)$')

    if varref then
        local parts = varref:split(".")
        local var = vars

        for i = 1, #parts do
            local part = parts[i]
            local v = var[part]

            if not v then return el end

            var = v
        end

        if var then return pandoc.Str(prefix .. var .. suffix) end
    end

    return el
end

return { { Meta = meta }, { Str = str } }