Jump to content

Module:Citation/CS1

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Uncle G (talk | contribs) at 13:16, 25 August 2012 (Added skeleton citation function.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--require "mw.text"

local p = {}

-- This can be removed when mw.text.tag appears.
function tag(frame, t)
    local name = t.name or "!--"
    local content = t.contents or ""
    local params = ""
    for n,v in pairs(t.params) do
        params = params .. "|" .. n .. "=" .. v
    end
    return frame:preprocess("{{#tag:" .. name .. "|" .. content .. params .. "}}")
end

function doi(frame, i)
    return frame:preprocess("{{doi|" .. i .. "}}")
end

function authorprefix(Surname, Given, Authorlink, ampersand)
    if (Surname == nil) then return "" end
    local prefix = ampersand .. Surname
    if (Given ~= nil) then prefix = prefix .. ", " .. Given end
    if (Authorlink ~= nil) then prefix = "[[" .. Authorlink .. "|" .. prefix .. "]]" end
    return prefix 
end

function anchorid(frame)
    local P1 = frame.args[1] or ""
    local P2 = frame.args[2] or ""
    local P3 = frame.args[3] or ""
    local P4 = frame.args[4] or ""
    local P5 = frame.args[5] or ""
    return "CITEREF" .. P1 .. P2 .. P3 .. P4 .. P5
end

function name(frame)
    local P1 = frame.args[1] or ""
    if ( frame.args[5] ~= nil) then
        return P1 .. " et al."
    else
        local P2 = frame.args[2] or ""
        local P3 = frame.args[3] or "" 
        local P4 = frame.args[4] or ""
        if ( frame.args[4] ~= nil ) then
            P4 = " " .. P4
            P3 = " & " .. P3
            P2 = " & " .. P2
        elseif ( frame.args[3] ~= nil ) then
            P3 = " " .. P3
            P2 = " & " .. P2
        elseif ( frame.args[2] ~= nil ) then
            P2 = " " .. P2            
        end
        return P1 .. P2 .. P3 .. P4
    end 
end

function crossref(pframe, frame)
    local LB = frame.BracketLeft or ""
    local RB = frame.BracketRight or ""
    local anchor = pframe.ref or pframe.Ref or anchorid(pframe)
    local text = name(pframe)
    local loc = pframe.loc or ""
    local page = pframe.p or pframe.page or nil
    local pages = pframe.pp or pframe.pages or nil
    if ( page ~= nil ) then
        local pagesep = frame.PageSep or ""
        text = text .. pagesep .. page
    end
    if ( pages ~= nil ) then
        local pagessep = frame.PagesSep or ""
        text = text .. pagessep .. pages
    end        
    local ps = pframe.Postscript or ""
    return LB .. "[[#" .. anchor .. "|" .. text .. "]]" .. RB .. ps
end

function citation(pframe, frame)
    local AuthorMask = pframe.authormask or pframe.author-mask or ""
    local Surname1 = pframe.last or pframe.surname or pframe.last1 or pframe.surname1 or pframe.author1 or pframe.authors or pframe.author or ""
    local Surname2 = pframe.last2 or pframe.surname2 or pframe.author2 or nil
    local Surname3 = pframe.last3 or pframe.surname3 or pframe.author3 or nil
    local Surname4 = pframe.last4 or pframe.surname4 or pframe.author4 or nil
    local Surname5 = pframe.last5 or pframe.surname5 or pframe.author5 or nil
    local Surname6 = pframe.last6 or pframe.surname6 or pframe.author6 or nil
    local Surname7 = pframe.last7 or pframe.surname7 or pframe.author7 or nil
    local Surname8 = pframe.last8 or pframe.surname8 or pframe.author8 or nil
    local Surname9 = pframe.last9 or pframe.surname9 or pframe.author9 or nil
    local Given1 = pframe.first1 or pframe.given1 or pframe.first or pframe.given or nil
    local Given2 = pframe.first2 or pframe.given2 or nil
    local Given3 = pframe.first3 or pframe.given3 or nil
    local Given4 = pframe.first4 or pframe.given4 or nil
    local Given5 = pframe.first5 or pframe.given5 or nil
    local Given6 = pframe.first6 or pframe.given6 or nil
    local Given7 = pframe.first7 or pframe.given7 or nil
    local Given8 = pframe.first8 or pframe.given8 or nil
    local Given9 = pframe.first9 or pframe.given9 or nil
    local Authorlink1 = pframe.authorlink or pframe.author1link or pframe.authorlink1 or nil
    local Authorlink2 = pframe.author2link or pframe.authorlink2 or nil
    local Authorlink3 = pframe.author3link or pframe.authorlink3 or nil
    local Authorlink4 = pframe.author4link or pframe.authorlink4 or nil
    local Authorlink5 = pframe.author5link or pframe.authorlink5 or nil
    local Authorlink6 = pframe.author6link or pframe.authorlink6 or nil
    local Authorlink7 = pframe.author7link or pframe.authorlink7 or nil
    local Authorlink8 = pframe.author8link or pframe.authorlink8 or nil
    local Authorlink9 = pframe.author9link or pframe.authorlink9 or nil
    local Coauthors = pframe.coauthor or pframe.coauthors or nil
    local Date = pframe.date or ((pframe.day or "") .. (pframe.month or "") .. (pframe.year or pframe.publicationdate or ""))
    local Title = pframe.title or ""
    local URL = pframe.archiveurl or pframe.url or nil
    local Series = pframe.series or nil
    local Volume = pframe.volume or nil
    local Issue = pframe.issue or pframe.number or nil
    local Edition = pframe.edition or nil
    local Place = pframe.place or pframe.location or nil
    local PublicationPlace = pframe.publicationplace or pframe.place or pframe.location or nil
    local Publisher = pframe.publisher or ""
    local Language = pframe.language or nil
    local Format = pframe.format or ""
    local ISBN= pframe.isbn or pframe.ISBN or nil
    local DOI= pframe.doi or pframe.DOI or nil
    local text = "(" .. Date .. "). ''" .. Title .. "''. " .. Publisher
    text = authorprefix(Surname1, Given1, Authorlink1, "") .. text
    text = authorprefix(Surname2, Given2, Authorlink2, "; ") .. text
    text = authorprefix(Surname3, Given3, Authorlink3, "; ") .. text
    text = authorprefix(Surname4, Given4, Authorlink4, "; ") .. text
    text = authorprefix(Surname5, Given5, Authorlink5, "; ") .. text
    text = authorprefix(Surname6, Given6, Authorlink6, "; ") .. text
    text = authorprefix(Surname7, Given7, Authorlink7, "; ") .. text
    text = authorprefix(Surname8, Given8, Authorlink8, "; ") .. text
    text = authorprefix(Surname9, Given9, Authorlink9, "; ") .. text
    if ( ISBN ~= nil ) then text = text .. "ISBN " .. ISBN end
    if ( DOI ~= nil ) then text = text .. doi(DOI) end
    return text
end

-- This is used by templates such as {{SfnRef}} to create the (encoded) anchor name for a Harvard cross-reference hyperlink.
function p.SFNID(frame)
    local pframe = frame:getParent()
    return anchorid(pframe)
end

-- This is used by templates such as {{Harvard citation}} to create the Harvard cross-reference text.
function p.Harvard(frame)
    local pframe = frame:getParent()
    return crossref(pframe, frame)
end

-- This is used by templates such as {{cite book}} to create the actual citation text.
function p.citation(frame)
    local pframe = frame:getParent()
    return citation(pframe, frame)
end

-- This is used by templates such as {{sfn}} to create the entire cross-reference.
function p.sfn(frame)
    local pframe = frame:getParent()
    local content = crossref(pframe, frame)
    local args = { name = anchorid(pframe) }
--  return mw.text.tag{name = "ref", contents = content, params = args}
    return tag(frame, {name = "ref", contents = content, params = args})
end

return p