summaryrefslogtreecommitdiffstats
path: root/filters/macros.lua
blob: cb7975b3fa59f2f98af0ab2ab0fc14e8113bbfa1 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function wide(el)
    local els = pandoc.List()

    els:insert(pandoc.RawBlock("html", "</div>"))
    els:insert(el)
    els:insert(pandoc.RawBlock("html", "<div class=\"l-container l-container--narrow u-ml-0\">"))

    return els
end

function alert(el)
    el.classes = pandoc.List()
    el.classes:insert("o-alert")
    el.classes:insert("o-alert--primary")
    el.classes:insert("s-alerts__alert")

    return wide(el)
end

function figure(el)
    local images = pandoc.List()

    pandoc.walk_block(el, { Image = function(iel) images:insert(iel) end })

    el = pandoc.RawBlock("html",
        "<figure class=\"c-figure js-lightbox__image\">\
            <a class=\"c-figure__link\" href=\"" .. images[1].src .. "\">\
                <img class=\"c-figure__image\" src=\"" .. images[1].src .. "\" title=\"" .. images[1].title .. "\" />\
            </a>\
        </figure>")

    return wide(el)
end

function Div(el)
    if el.classes:includes("alert") then
        el = alert(el)
    elseif el.classes:includes("figure") then
        el = figure(el)
    end

    return el
end

function CodeBlock(el)
    if el.classes[1] == "plain" then
        el = pandoc.Div({ el }, { class = 's-code' })
    elseif el.classes[1] then
        local formatted = pandoc.pipe('pygmentize', {
            '-l', el.classes[1], '-f', 'html', '-O', 'cssclass=s-code s-code--highlight',
        }, el.text)

        if formatted then el = pandoc.RawBlock('html', formatted) end
    end

    return wide(el)
end

function Header(el)
    if el.level <= 3 then
        local newchildren = pandoc.List()
        newchildren:insert(pandoc.Span(el.content, { class = 's-headings__primary' }))
        el.content = newchildren
    end

    return el
end

function Table(el)
    return wide(el)
end

function Str(el)
    local icon = el.text:match('^:(.*):$')

    if icon then
        return pandoc.RawInline('html',
            '<svg class="o-icon o-icon--inline" width="1em" height="1em" aria-hidden="true"><use href="/icons.svg#' ..
                icon .. '"></use></svg>')
    end

    return el
end