Модуль:Loc/Marking

Материал из Space Station 14 Вики

Для документации этого модуля может быть создана страница Модуль:Loc/Marking/doc

local p = {}

local function apply_tag(tag, param, inner)
    local t = mw.ustring.lower(tag or "")
    if t == "bold" then
        return "<b>" .. inner .. "</b>"
    elseif t == "italic" then
        return "<i>" .. inner .. "</i>"
    elseif t == "bolditalic" then
        return "<i><b>" .. inner .. "</b></i>"
	elseif t == "head" then
	    local level = math.min(math.max(param, 1), 3) -- clamp(levelParam, 1, 3)
	    local defaultSize = 12
	    local size = math.ceil(defaultSize * 2 / math.sqrt(level))
	    return '<span style="font-weight:bold; font-size:' .. size .. 'px;">' .. inner .. '</span>'
    elseif t == "color" then
        if not param or param == "" then
            return '<span>' .. inner .. '</span>'
        end
        return '<span style="color:' .. param .. ';">' .. inner .. '</span>'
    elseif t == "bullet" then
        return "· " .. inner
    else
        return inner
    end
end

local function transform(s)
    s = s or ""
    while true do
        local c_s, c_e, c_tag = mw.ustring.find(s, "%[/([%w_]+)%]")
        if not c_s then break end

        local last_o_s, last_o_e = nil, nil
        local search_pos = 1
        while true do
            local a, b = mw.ustring.find(s, "%[" .. c_tag .. "=?[^%]]*%]", search_pos)
            if not a or a > c_s then break end
            last_o_s, last_o_e = a, b
            search_pos = b + 1
        end

        if not last_o_s then
            s = mw.ustring.sub(s, 1, c_s - 1) .. mw.ustring.sub(s, c_e + 1)
        else
            local inner = mw.ustring.sub(s, last_o_e + 1, c_s - 1)
            local _, _, param = mw.ustring.find(s, "%[" .. c_tag .. "=([^%]]+)%]", last_o_s)
            local replacement = apply_tag(c_tag, param, inner)

            s = mw.ustring.sub(s, 1, last_o_s - 1) .. replacement .. mw.ustring.sub(s, c_e + 1)
        end
    end

    s = mw.ustring.gsub(s, "%[/?[%w_]+[^%]]*%]", "")

    return s
end

function p.main(frame)
    local args = frame.args or {}
    local text = args[1] or args.text or ""
    return frame:preprocess(transform(text))
end

return p