Module:Citation/CS1: Difference between revisions

From BitProjects
Jump to navigation Jump to search
Infobox>Uncle G
Modified generation of citations.
Infobox>Uncle G
Fixed some argument handling.
Line 26: Line 26:
end
end


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


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


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


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


Line 148: Line 150:
function p.Harvard(frame)
function p.Harvard(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     return crossref(pframe, frame)
     return crossref(frame, pframe.args)
end
end


Line 154: Line 156:
function p.citation(frame)
function p.citation(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     return citation0(pframe, frame)
     return citation(frame, pframe.args)
end
end


Line 160: Line 162:
function p.sfn(frame)
function p.sfn(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     local content = crossref(pframe, frame)
     local content = crossref(frame, pframe.args)
     local args = { name = anchorid(pframe) }
     local args = { name = anchorid(pframe.args) }
--  return mw.text.tag{name = "ref", contents = content, params = args}
--  return mw.text.tag{name = "ref", contents = content, params = args}
     return tag(frame, {name = "ref", contents = content, params = args})
     return tag(frame, {name = "ref", contents = content, params = args})

Revision as of 18:10, 25 August 2012

Documentation for this module may be created at Module:Citation/CS1/doc

--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(args)
    local P1 = args[1] or ""
    local P2 = args[2] or ""
    local P3 = args[3] or ""
    local P4 = args[4] or ""
    local P5 = args[5] or ""
    return "CITEREF" .. P1 .. P2 .. P3 .. P4 .. P5
end

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

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

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

return p