Module:Cite message

From Irony Wiki
Revision as of 09:14, 29 April 2023 by Saucy (talk | contribs) (fix regex)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Cite message/doc

-- Implements {{Cite message}}
-- {{cite message|author=|date=|content=|server=|channel=|link=}}
-- Author (Date): "Content". ''Server'' #Channel. [Link]

local getArgs = require("Module:Arguments").getArgs
local p = {}


-- Surrounds middle with left on the left and right on the right, unless middle is nil, in which case it returns nil.
local function surround(middle, left, right)
    if middle then
        return left .. middle .. right
    else
        return nil
    end
end

-- If neither left or right are nil, then returns left concatenated with right with sep as a separator. If only one of left and right is nil, the other is returned. If both are nil, nil is returned.
local function combine2(left, right, sep)
    if left and right then
        return left .. sep .. right
    elseif left then
        return left
    elseif right then
        return right
    else
        return nil
    end
end


function p.main(frame)
    local args = getArgs(frame)
    return p._main(args)
end


function p._main(args)
    local ftable = {}

    local authordate = combine2(args.author, surround(args.date, '(', ')'), " ")
    local authordatecontent = combine2(authordate, surround(args.content, '"', '"'), ": ")
    table.insert(ftable, authordatecontent)

    local channel = args.channel
    if channel then
        local channelmatch = mw.ustring.match(channel, "^(#?)[^%sA-Z]+$")
        if channelmatch then
            if channelmatch[1] ~= "#" then
                channel = "#" .. channel
            end
        end 
    end

    local serverchannel = combine2(surround(args.server, "''", "''"), channel, " ")
    table.insert(ftable, serverchannel)

    local link = surround(args.link, "[", " Link]")
    table.insert(ftable, link)

    return table.concat(ftable, ". ")
end

return p