مندرجات کا رخ کریں

ماڈیول:Footnotes

آزاد دائرۃ المعارف، ویکیپیڈیا سے
Documentation icon دستاویز [تخلیق]
require('strict');
local getArgs = require ('Module:Arguments').getArgs;

local f = {
    args_default = {
        bracket_left = "",
        bracket_right = "",
        bracket_year_left = "",
        bracket_year_right = "",
        postscript = "",
        page = "",
        pages = "",
        location = "",
        page_sep = ", p. ",
        pages_sep = ", pp. ",
        ref = "",
        P1 = "",
        P2 = "",
        P3 = "",
        P4 = "",
        P5 = ""
    }
};

local function trim(str)
    if str == nil then
        return nil;
    end
    return str:match("^%s*(.-)%s*$");
end    

local function core(args)
    local result;
    
    if args.P5 ~= "" then
        result = args.P1 .. ' et al. ' .. args.bracket_year_left .. args.P5 .. args.bracket_year_right;
    elseif args.P4 ~= "" then
        result = args.P1 .. ', ' .. args.P2 .. ' & ' .. args.P3 .. ' ' .. args.bracket_year_left .. args.P4 .. args.bracket_year_right;
    elseif args.P3 ~= "" then
        result = args.P1 .. ' & ' .. args.P2 .. ' ' .. args.bracket_year_left .. args.P3 .. args.bracket_year_right;
    else
        result = trim(args.P1 .. ' ' .. args.bracket_year_left .. args.P2 .. args.bracket_year_right)
    end
    
    if args.ref ~= 'none' then
        if args.ref ~= "" then
            result = "[[#" .. args.ref .. "|" .. result .. "]]";
        else
            result = "[[#CITEREF" .. args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5 .. "|" .. result .. "]]";
        end
    end
    
    if args.page ~= "" then
        result = result .. args.page_sep .. args.page;
    elseif args.pages ~= "" then
        result = result .. args.pages_sep .. args.pages;
    end      
    
    if args.location ~= "" then
        result = result .. "، " .. args.location;
    end
    
    result = args.bracket_left .. result .. args.bracket_right .. args.postscript;
    return result;
end

function f.harvard_core(frame)
    local args = {};
    local pframe = frame:getParent();
    
    args.bracket_left = pframe.args.BracketLeft or "";
    args.bracket_right = pframe.args.BracketRight or "";
    args.bracket_year_left = pframe.args.BracketYearLeft or "";
    args.bracket_year_right = pframe.args.BracketYearRight or "";
    args.postscript = pframe.args.Postscript or "";
    args.page = pframe.args.Page or "";
    args.pages = pframe.args.Pages or "";
    args.location = pframe.args.Location or "";
    args.page_sep = pframe.args.PageSep or "";
    args.pages_sep = pframe.args.PagesSep or "";
    args.ref = pframe.args.REF or "{{{REF}}}";
    args.P1 = trim(pframe.args.P1) or "";
    args.P2 = trim(pframe.args.P2) or "";
    args.P3 = trim(pframe.args.P3) or "";
    args.P4 = trim(pframe.args.P4) or "";
    args.P5 = trim(pframe.args.P5) or "";
    
    return core(args);
end

function f.harvard_citation(frame)
    local args = f.args_default;
    local pframe = frame:getParent();
    
    args.bracket_left = "(";
    args.bracket_right = ")";
    args.page = pframe.args.p or pframe.args.page or "";
    args.pages = pframe.args.pp or pframe.args.pages or "";
    args.location = pframe.args.loc or "";
    args.ref = pframe.args.ref or pframe.args.Ref or "";
    args.P1 = trim(pframe.args[1]) or "";
    args.P2 = trim(pframe.args[2]) or "";
    args.P3 = trim(pframe.args[3]) or "";
    args.P4 = trim(pframe.args[4]) or "";
    args.P5 = trim(pframe.args[5]) or "";
    
    return core(args);
end
    
function f.harvard_citation_no_bracket(frame)
    local args = f.args_default;
    local pframe = frame:getParent();
    
    args.page = pframe.args.p or pframe.args.page or "";
    args.pages = pframe.args.pp or pframe.args.pages or "";
    args.location = pframe.args.loc or "";
    args.ref = pframe.args.ref or pframe.args.Ref or "";
    args.P1 = trim(pframe.args[1]) or "";
    args.P2 = trim(pframe.args[2]) or "";
    args.P3 = trim(pframe.args[3]) or "";
    args.P4 = trim(pframe.args[4]) or "";
    args.P5 = trim(pframe.args[5]) or "";
    
    return core(args);
end

function f.sfn(frame)
    local args = f.args_default;
    local pframe = frame:getParent();

    args.postscript = pframe.args.postscript or pframe.args.ps or "";
    args.page = pframe.args.p or pframe.args.page or "";
    args.pages = pframe.args.pp or pframe.args.pages or "";
    args.location = pframe.args.loc or "";
    args.ref = pframe.args.ref or pframe.args.Ref or "";
    args.P1 = trim(pframe.args[1]) or "";
    args.P2 = trim(pframe.args[2]) or "";
    args.P3 = trim(pframe.args[3]) or "";
    args.P4 = trim(pframe.args[4]) or "";
    args.P5 = trim(pframe.args[5]) or "";

    -- Remove "ء" from all year-related fields (P2, P3, P4, P5)
    args.P2 = args.P2:gsub("ء", "");
    args.P3 = args.P3:gsub("ء", "");
    args.P4 = args.P4:gsub("ء", "");
    args.P5 = args.P5:gsub("ء", "");

    local result = core(args);
    local name = "FOOTNOTE" .. args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5 .. args.page .. args.pages .. args.location;

    result = frame:extensionTag{ name = "ref", args = { name = name }, content = result };

    return result;
end

function f.sfnm(frame)
    local args = f.args_default;
    local pframe = frame:getParent();
    
    local n = 1;
    local first_pnum = 1;
    local second_pnum = 2;
    local last_ps = 0;
    local last_index = 0;
    local out = {};
    local footnote = {'FOOTNOTE'};
    
    for k, v in pairs(frame.args) do
        args[k] = v;
    end
    
    while true do
        if not pframe.args[table.concat({n, 'a1'})] and not pframe.args[first_pnum] then
            break;
        end
        
        if pframe.args[table.concat({n, 'a1'})] then
            for _, v in ipairs({'P1', 'P2', 'P3', 'P4', 'P5'}) do
                args[v] = '';
            end

            for i, v in ipairs({'P1', 'P2', 'P3', 'P4', 'P5'}) do
                args[v] = pframe.args[table.concat({n, 'a', i})] or '';
                if '' == args[v] then
                    args[v] = pframe.args[table.concat({n, 'y'})] or '';
                    break;
                end
            end
        else
            args.P1 = trim(pframe.args[first_pnum]);
            args.P2 = (pframe.args[second_pnum] and trim(pframe.args[second_pnum])) or '';

            for _, v in ipairs({'P3', 'P4', 'P5'}) do
                args[v] = '';
            end

            first_pnum = first_pnum + 2;
            second_pnum = first_pnum + 1;
        end
        
        args.postscript = pframe.args[table.concat({n, 'ps'})] or '';
        if 'none' == args.postscript then
            args.postscript = '';
        end

        args.ref = pframe.args[table.concat({n, 'ref'})] or '';
        args.page = pframe.args[table.concat({n, 'p'})] or '';
        args.pages = pframe.args[table.concat({n, 'pp'})] or '';
        args.pages = ('' ~= args.pages) and args.pages:gsub('%-', '–') or '';
        args.location = pframe.args[table.concat({n, 'loc'})] or pframe.args[table.concat({n, 'at'})] or '';
        args.ignore = ('yes' == pframe.args[table.concat({n, 'ignore-false-positive'})]) or ('yes' == pframe.args[table.concat({n, 'ignore-err'})]);

        table.insert(out, core(args));
        
        for k, v in ipairs({'P1', 'P2', 'P3', 'P4', 'P5'}) do
            if '' ~= args[v] then
                table.insert(footnote, args[v]);
            end
        end
        for k, v in ipairs({'page', 'pages', 'location'}) do
            if '' ~= args[v] then
                local cleaned_value = args[v]:gsub('[%%%-]', '');
                table.insert(footnote, cleaned_value);
            end
        end
        
        last_index = n;
        if '' ~= args.postscript then
            last_ps = n;
        end
        
        n = n + 1;
    end
    
    local name = table.concat(footnote):gsub('%s+', ' ');
    args.end_ps = pframe.args.postscript or pframe.args.ps or '.';
    if 'none' == args.end_ps then
        args.end_ps = '';
    end

    local result = table.concat({table.concat(out, '; '), (last_index == last_ps) and '' or args.end_ps});
    return frame:extensionTag({name='ref', args={name=name}, content=result});
end

local patterns_date = {
    "^%d%d%d%d$",
    "^%d%d%d%d%-$",
    "^%d%d%d%d%-%d%d%-%d%d$",
    "^%d%d%d%d%-%d%d%-%d%d%-%d%d$"
};

local function is_year(param, args)
    args.year = '';  -- used for harv error
    for _, pattern in ipairs(patterns_date) do
        if mw.ustring.match(param, pattern) then
            args.year = param;  -- used for harv error
            return true;
        end
    end
    return false;
end

function f.sfnref(frame)
    local args = getArgs(frame);
    local out = {};
    
    for i = 1, 5 do
        if args[i] then
            out[i] = args[i];
        else
            break;
        end
    end
    
    if 5 == #out then
        local i = 6;
        while args[i] do
            if is_year(args[i], args) then
                out[5] = args[i];
                break;
            end
            i = i + 1;
        end
    end
    
    return mw.uri.anchorEncode('CITEREF' .. table.concat(out));
end

return f;