Module:labels: Difference between revisions

From Laenkea
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local export = {}
local export = {}


function export.make_labels(language, labels, nocat)
function export.make_labels(language, labels, nocat, nobrackets, noitalics)
local function make_label(label_id, label, language_name)
    local function make_label(label_id, label, language_name, language_code)
local categories = label["categories"]
        local categories, topics = label["categories"], label["topics"]
local cat_out = ""
        local cat_out = ""
if categories ~= nil then
        if categories ~= nil then
for _, cat in ipairs(categories) do
            for _, cat in ipairs(categories) do
cat_out = cat_out .. "[[Category:" .. language_name .. " " .. cat .. "]]"
                cat_out = cat_out .. "[[Category:" .. language_name .. " " .. cat .. "]]"
end
            end
end
        end
local display = label["display"]
        if topics ~= nil then
local glossary = label["glossary"]
            for _, top in ipairs(topics) do
local wikipedia = label["Wikipedia"]
                cat_out = cat_out .. "[[Category:" .. language_code .. ":" .. top .. "]]"
if display ~= nil then return display, cat_out end
            end
if glossary ~= nil then
        end
local g_url = "Appendix:Glossary#" .. (type(glossary) == "string" and glossary or label_id)
        local display = label["display"]
return "[[" .. g_url .. "|" .. label_id .. "]]", cat_out
        local glossary = label["glossary"]
end
        local wikipedia = label["Wikipedia"]
if wikipedia ~= nil then
        if display ~= nil then return display, cat_out end
return "[[w:" .. (type(wikipedia) == "string" and wikipedia or label_id) .. "|" .. label_id .. "]]", cat_out
        if glossary ~= nil then
end
            local g_url = "Appendix:Glossary#" .. (type(glossary) == "string" and glossary or label_id)
return label_id, cat_out
            return "[[" .. g_url .. "|" .. label_id .. "]]", cat_out
end
        end
local l_data = mw.loadData("Module:labels/data")
        if wikipedia ~= nil then
local out = "(<i>"
            return "[[w:" .. (type(wikipedia) == "string" and wikipedia or label_id) .. "|" .. label_id .. "]]", cat_out
local cats = ""
        end
local skip_comma = false
        return label_id, cat_out
for i, l_id in ipairs(labels) do
    end
l_id = l_data.aliases[l_id] or l_id
    local l_data = mw.loadData("Module:labels/data")
local label = l_data.labels[l_id]
    local out = (nobrackets and "" or "(") .. (noitalics and "" or "<i>")
if i > 1 then
    local cats = ""
local conjunctions = {["with"] = true, ["and"] = true, ["or"] = true, ["/"] = true}
    local skip_comma = false
if conjunctions[l_id] or conjunctions[labels[i-1]] then
 
skip_comma = true
    local conjunctions = {["and"] = true, ["or"] = true, ["/"] = true}
end
 
out = out .. (skip_comma and "" or ",")
    local it = 1
if not (l_id == "/" or labels[i-1] == "/") then
 
out = out .. " "
    -- split parameters at / and ,
end
    while labels[it] do
end
        if mw.ustring.find(labels[it], "^[^,]+,%s.+") then
if label == null then
            local before = mw.ustring.gsub(labels[it], "^([^,]+),%s*(.+)", "%1")
if l_id == "/" then
            local after = mw.ustring.gsub(labels[it], "^([^,]+),%s*(.+)", "%2")
out = out .. "</i>/<i>"
            labels[it] = before
elseif mw.ustring.find(l_id, "^\"[^%[%]]+\"$") then
            table.insert(labels, it + 1, after)
local l_id_link = mw.ustring.match(l_id, "\"([^%[%]]+)\"")
        end
out = out .. "</i>[[" .. l_id_link .. "#" .. language.name .. "|" .. l_id_link .. "]]<i>"
        if mw.ustring.find(labels[it], "^[^/%[%]]+/.+") then
else
            local before = mw.ustring.gsub(labels[it], "^([^/]+)/(.+)", "%1")
out = out .. l_id
            local after = mw.ustring.gsub(labels[it], "^([^/]+)/(.+)", "%2")
end
            labels[it] = before
skip_comma = false
            table.insert(labels, it + 1, after)
else
            table.insert(labels, it + 1, "/")
l_out, l_cats = make_label(l_id, label, language.name)
        end
out = out .. l_out
        for conjunction, _ in pairs(conjunctions) do
cats = cats .. l_cats
            if mw.ustring.find(labels[it], "^[^%[%]]+%s+" .. conjunction .. "%s+.+") then
skip_comma = label["omit_comma"] or false
                local before = mw.ustring.gsub(labels[it], "^(.+)%s+" .. conjunction .. "%s+(.+)", "%1")
end
                local after = mw.ustring.gsub(labels[it], "^(.+)%s+" .. conjunction .. "%s+(.+)", "%2")
end
                labels[it] = before
out = out .. "</i>)"
                table.insert(labels, it + 1, after)
return out .. (nocat and "" or cats)
                table.insert(labels, it + 1, conjunction)
            end
        end
        it = it + 1
    end
 
    --generate
 
    for i, l_id in ipairs(labels) do
 
        if mw.ustring.sub(l_id, 1, 1) == "_" then
            skip_comma = true
            l_id = mw.ustring.sub(l_id, 2)
        end
 
        l_id = l_data.aliases[l_id] or mw.ustring.gsub(l_id, "([\"\*])([^\"\*]+)([\"\*])", "</i>[[%2#" .. language.name .. "|%2]]<i>") -- replaces "word" or *word* with [[word#LANG|word]]
 
        local label = l_data.labels[l_id]
        if i > 1 then
            if conjunctions[l_id] or conjunctions[labels[i-1]] or mw.ustring.find(l_id, "^with%s") then
                skip_comma = true
            end
            out = out .. (skip_comma and "" or ",")
            if not (l_id == "/" or labels[i-1] == "/") then
                out = out .. " "
            end
        end
        if label == null then
            if l_id == "/" then
                out = out .. "</i>/<i>"
            else
                out = out .. l_id
            end
            skip_comma = false
        else
            l_out, l_cats = make_label(l_id, label, language.name, language.code)
            out = out .. l_out
            cats = cats .. l_cats
            skip_comma = label["omit_comma"] or false
        end
    end
    out = out .. (noitalics and "" or "</i>") .. (nobrackets and "" or ")")
    return out .. (nocat and "" or cats)
end
end


function export.show(frame)
function export.show(frame)
local params = {
    local params = {
[1] = {required = true},
        [1] = {required = true},
[2] = {required = true, list = true},
        [2] = {required = true, list = true},
["nocat"] = {type = "boolean"}
        ["nocat"] = {type = "boolean"}
}
    }
local args = require("Module:parameters").process(frame:getParent().args, params)
    local args = require("Module:parameters").process(frame:getParent().args, params)
local language = require("Module:languages").get_by_code(args[1])
    local language = require("Module:languages").get_by_code(args[1])
return export.make_labels(language, args[2], args["nocat"])
    return export.make_labels(language, args[2], args["nocat"])
end
end


return export
return export

Latest revision as of 18:45, 26 February 2024

local export = {}

function export.make_labels(language, labels, nocat, nobrackets, noitalics)
    local function make_label(label_id, label, language_name, language_code)
        local categories, topics = label["categories"], label["topics"]
        local cat_out = ""
        if categories ~= nil then
            for _, cat in ipairs(categories) do
                cat_out = cat_out .. "[[Category:" .. language_name .. " " .. cat .. "]]"
            end
        end
        if topics ~= nil then
            for _, top in ipairs(topics) do
                cat_out = cat_out .. "[[Category:" .. language_code .. ":" .. top .. "]]"
            end
        end
        local display = label["display"]
        local glossary = label["glossary"]
        local wikipedia = label["Wikipedia"]
        if display ~= nil then return display, cat_out end
        if glossary ~= nil then
            local g_url = "Appendix:Glossary#" .. (type(glossary) == "string" and glossary or label_id)
            return "[[" .. g_url .. "|" .. label_id .. "]]", cat_out
        end
        if wikipedia ~= nil then
            return "[[w:" .. (type(wikipedia) == "string" and wikipedia or label_id) .. "|" .. label_id .. "]]", cat_out
        end
        return label_id, cat_out
    end
    local l_data = mw.loadData("Module:labels/data")
    local out = (nobrackets and "" or "(") .. (noitalics and "" or "<i>")
    local cats = ""
    local skip_comma = false

    local conjunctions = {["and"] = true, ["or"] = true, ["/"] = true}

    local it = 1

    -- split parameters at / and ,
    while labels[it] do
        if mw.ustring.find(labels[it], "^[^,]+,%s.+") then
            local before = mw.ustring.gsub(labels[it], "^([^,]+),%s*(.+)", "%1")
            local after = mw.ustring.gsub(labels[it], "^([^,]+),%s*(.+)", "%2")
            labels[it] = before
            table.insert(labels, it + 1, after)
        end
        if mw.ustring.find(labels[it], "^[^/%[%]]+/.+") then
            local before = mw.ustring.gsub(labels[it], "^([^/]+)/(.+)", "%1")
            local after = mw.ustring.gsub(labels[it], "^([^/]+)/(.+)", "%2")
            labels[it] = before
            table.insert(labels, it + 1, after)
            table.insert(labels, it + 1, "/")
        end
        for conjunction, _ in pairs(conjunctions) do
            if mw.ustring.find(labels[it], "^[^%[%]]+%s+" .. conjunction .. "%s+.+") then
                local before = mw.ustring.gsub(labels[it], "^(.+)%s+" .. conjunction .. "%s+(.+)", "%1")
                local after = mw.ustring.gsub(labels[it], "^(.+)%s+" .. conjunction .. "%s+(.+)", "%2")
                labels[it] = before
                table.insert(labels, it + 1, after)
                table.insert(labels, it + 1, conjunction)
            end
        end
        it = it + 1
    end

    --generate

    for i, l_id in ipairs(labels) do

        if mw.ustring.sub(l_id, 1, 1) == "_" then
            skip_comma = true
            l_id = mw.ustring.sub(l_id, 2)
        end

        l_id = l_data.aliases[l_id] or mw.ustring.gsub(l_id, "([\"\*])([^\"\*]+)([\"\*])", "</i>[[%2#" .. language.name .. "|%2]]<i>") -- replaces "word" or *word* with [[word#LANG|word]]

        local label = l_data.labels[l_id]
        if i > 1 then
            if conjunctions[l_id] or conjunctions[labels[i-1]] or mw.ustring.find(l_id, "^with%s") then
                skip_comma = true
            end
            out = out .. (skip_comma and "" or ",")
            if not (l_id == "/" or labels[i-1] == "/") then
                out = out .. " "
            end
        end
        if label == null then
            if l_id == "/" then
                out = out .. "</i>/<i>"
            else
                out = out .. l_id
            end
            skip_comma = false
        else
            l_out, l_cats = make_label(l_id, label, language.name, language.code)
            out = out .. l_out
            cats = cats .. l_cats
            skip_comma = label["omit_comma"] or false
        end
    end
    out = out .. (noitalics and "" or "</i>") .. (nobrackets and "" or ")")
    return out .. (nocat and "" or cats)
end

function export.show(frame)
    local params = {
        [1] = {required = true},
        [2] = {required = true, list = true},
        ["nocat"] = {type = "boolean"}
    }
    local args = require("Module:parameters").process(frame:getParent().args, params)
    local language = require("Module:languages").get_by_code(args[1])
    return export.make_labels(language, args[2], args["nocat"])
end

return export