summaryrefslogtreecommitdiff
path: root/script/provider/markdown.lua
blob: c5765571cae64492acce85494218bf92c14d1929 (plain)
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
local mt = {}
mt.__index = mt
mt.__name = 'markdown'

mt._splitLine = false

local function checkSplitLine(self)
    if not self._splitLine then
        return
    end
    self._splitLine = nil
    if #self == 0 then
        return
    end

    self[#self+1] = '\n---'
end

function mt:add(language, text)
    if not text then
        return
    end
    text = tostring(text)
    if #text == 0 then
        return
    end

    checkSplitLine(self)

    if language == 'md' then
        if self._last == 'md' then
            self[#self+1] = ''
        end
        self[#self+1] = text
    else
        if #self > 0 then
            self[#self+1] = ''
        end
        self[#self+1] = ('```%s\n%s\n```'):format(language, text)
    end

    self._last = language
end

function mt:string()
    return table.concat(self, '\n')
end

function mt:splitLine()
    self._splitLine = true
end

return function ()
    return setmetatable({}, mt)
end