Эчтәлеккә күчү

Модуль:authors

Wiktionary проектыннан

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

local data = mw.loadData("Module:authors/data")
local p = {}

local function getAuthors ()
    local result = {};
    for key,v in pairs(data) do
        local firstname = v[1];
        local secondname = v[2];
        local thirdname = v[3];
        if v.alias ~= nil then
            result[key] = { alias = v.alias };
        else
            result[key] = { link = v.link, cat = v.cat}; 
        end
        if v.cat ~= nil then
            result[key].cat = "[[Төркем:Өзекләр/" .. v.cat .. "]]";
        end
        if firstname ~= nil and firstname ~= "" then
            if firstname ~= key then
                result[firstname] = { alias = key };
            end
            if secondname ~= nil and secondname ~= "" then
                local a = secondname .. " " .. firstname;
                result[a] = { alias = key };
                if thirdname ~= nil and thirdname ~= "" then
                    local sn = mw.ustring.sub(secondname,1,1);
                    local tn = mw.ustring.sub(thirdname,1,1);
                    a = sn .. ". " .. tn .. ". " .. firstname;
                    result[a] = { alias = key };
                    if v.link == nil then
                        result[key].link =  mw.text.decode("[[w:" .. firstname .. ", " .. secondname .. " " .. thirdname .. "|" ..
                          sn ..". " .. tn .. ". " .. firstname .. "]]");
                    end
                    if v.cat == nil then
                        result[key].cat = "[[Төркем:Өзекләр/" .. firstname .. " " .. sn .. ". " .. tn .. ".]]";
                    end;
                end
            end
        end
    end
    return result;
end;

function p.list(frame)
    local authors = getAuthors();
    local list_type = frame.args["type"];
    local result = "";
    local names = {};
    local aliases = {};
    for key, value in pairs(authors) do
        if value.alias == nil then
            table.insert(names, key);
        else
            if aliases[value.alias] == nil then
                aliases[value.alias] = { key };
            else
                table.insert(aliases[value.alias], key);
            end
        end
    end
    table.sort(names);
    for i, key in ipairs(names) do
        local catlink = authors[key].cat; 
        if catlink ~= nil then
            catlink = mw.ustring.gsub(catlink, "Төркем", ":Төркем");
        else 
            catlink = "nil"
        end
        local wikilink = authors[key].link; 
        if list_type == "table" then
            local  code = "<code>{{автор|" .. key .. "}}</code>";
            if aliases[key] ~= nil then
                for j, alias in ipairs(aliases[key]) do 
                   code = code .. "<br/><code>{{автор|" .. alias .. "}}</code>" 
                end
            else
            end
            result = result .. mw.text.tag("tr", nil, 
                table.concat( 
                {   mw.text.tag("td", nil, code), 
                    mw.text.tag("td", nil, authors[key].link or "nil"), 
                    mw.text.tag("td", nil, catlink)
                }, "")) .. "\n";
        elseif list_type == "list" then
            result = result .. "* <code>{{автор|" .. key .. "}}</code> → " .. (authors[key].link or "nil") .. " - " .. catlink .. "\n";
        else
            result = result .. table.concat( {key, authors[key].link or "nil", catlink}, ", ") .. "\n";
        end
    end
    if result ~= "" and list_type == "table" then
        result = mw.text.tag("tr", nil, 
            table.concat( 
                {   mw.text.tag("th", nil, "Код"), 
                    mw.text.tag("th", nil, "Сылтама"), 
                    mw.text.tag("th", nil, "Төркем")
                }, "")) .. "\n" .. result;
        result = mw.text.tag("table", {class="standard"}, result) .. "\n";
    end
    return result;
end

function p.find(frame)
    local result = frame.args[1];
    if result ~= nil and type(result)=='string' and result ~= "" then
        local authors = getAuthors();
        local name = mw.ustring.gsub(result, mw.ustring.char(160), " "); -- замена неразрывных пробелов
        local f = authors[name];
        if f ~= nil then
            if f.alias ~= nil then
                local af = authors[f.alias];
                if af ~= nil then
                    f = af
                end
            end;
            if f.link ~= nil then
                result = f.link;
            end
            if mw.title.getCurrentTitle().namespace == 0 and f.cat ~= nil then
                result = result .. f.cat;
            end    
        end;
    end
    return result;
end

--
return p;