Jump to content

Module:Citation/CS1: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Corrected my typing error.
Tweaked formatting.
Line 130: Line 130:
local URL = args.archiveurl or args.url
local URL = args.archiveurl or args.url
local ChapterURL = args.chapterurl
local ChapterURL = args.chapterurl
local Journal = args.journal or args.newspaper or args.magazine
local Journal = args.journal or args.newspaper or args.magazine or args.work
local Series = args.series
local Series = args.series
local Volume = args.volume
local Volume = args.volume
Line 194: Line 194:
if ( Format ~= nil ) then Format = " (" .. Format .. ")" else Format = "" end
if ( Format ~= nil ) then Format = " (" .. Format .. ")" else Format = "" end
if ( ID ~= nil ) then ID = " " .. ID else ID = "" end
if ( ID ~= nil ) then ID = " " .. ID else ID = "" end
if ( Date ~= nil ) then Date = " (" .. Date .. ")" else Date = "" end
if ( Authors == nil ) then
if ( Authors == nil ) then
Authors = authorprefix(Surname1, Given1, Authorlink1, "")
Authors = authorprefix(Surname1, Given1, Authorlink1, "")
Line 203: Line 204:
Authors = Authors .. authorprefix(Surname7, Given7, Authorlink7, "&#59; ")
Authors = Authors .. authorprefix(Surname7, Given7, Authorlink7, "&#59; ")
Authors = Authors .. authorprefix(Surname8, Given8, Authorlink8, "&#59; ")
Authors = Authors .. authorprefix(Surname8, Given8, Authorlink8, "&#59; ")
Authors = Authors .. authorprefix(Surname9, Given9, Authorlink9, "&#59; ") .. " "
Authors = Authors .. authorprefix(Surname9, Given9, Authorlink9, "&#59; ")
end
if ( "" ~= Authors and "" ~= Date ) then
Date = Date .. "."
end
end
if ( Editors ~= nil ) then
if ( Editors ~= nil ) then
Line 210: Line 214:
Editors = ""
Editors = ""
end
end
local text = Authors .. "(" .. Date .. ")." .. Chapter .. Editors .. Title .. Format .. Edition .. Language .. Volume .. Issue .. Pages .. Series .. PublicationPlace .. Publisher .. ISBN .. DOI .. ID .. URL
local text = Authors .. Date .. Chapter .. Editors .. Title .. Format .. Edition .. Language .. Volume .. Issue .. Pages .. Series .. PublicationPlace .. Publisher .. ISBN .. DOI .. ID .. URL
if ( Ref ~= nil ) then
if ( Ref ~= nil ) then
local id = Ref
local id = Ref

Revision as of 20:41, 25 August 2012

--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 = ""
    if ( "span" == name or "div" == name or "blockquote" == name ) then
        for n,v in pairs(t.params) do
            params = params .. " " .. n .. "=\"" .. v .. "\""
        end
        return "<" .. name .. params .. ">" .. content .. "</" .. name .. ">"
    else
        for n,v in pairs(t.params) do
            params = params .. "|" .. n .. "=" .. v
        end
        return frame:preprocess("{{#tag:" .. name .. "|" .. content .. params .. "}}")
    end
end

function hideinprint(frame, content)
    return content
end

function doi(frame, id)
    local text = hideinprint(frame, "[[Digital object identifier|doi]]:[http://dx.doi.org/{{urlencode:" .. id .. "}} " .. tag(frame, {name="nowiki",contents=id,params={}}) .. "]")
    if ( string.sub(id,1,3) ~= "10." ) then
        text = text .. "[[Category:Pages with DOI errors]]"
    end
    return text
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
    local pages = args.pp or args.pages
    if ( page ~= nil ) then
        local pagesep = config.PageSep or ", p. "
        loc = loc .. pagesep .. page
    end
    if ( pages ~= nil ) then
        local pagessep = config.PagesSep or ", pp. "
        loc = loc .. pagessep .. pages
    end        
    local ps = args.Postscript or ""
    return LB .. "[[#" .. anchor .. "|" .. text .. "]]" .. loc .. 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.author or args.last1 or args.surname1 or args.author1
    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 Year = args.year or args.publicationdate or ""
    local Date = args.date
    local Title = args.title or args.encyclopedia
    local Chapter = args.chapter or args.article
    local URL = args.archiveurl or args.url
    local ChapterURL = args.chapterurl
    local Journal = args.journal or args.newspaper or args.magazine or args.work
    local Series = args.series
    local Volume = args.volume
    local Issue = args.issue or args.number
    local Pages = args.pages or args.page
    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
    local ID = args.id or args.ID or args.pmid or args.PMID
    local Authors = args.authors 
    local Editors = args.editors 
    local Ref = args.ref or args.Ref
    if ( Date == nil ) then
        Date = Year
        if ( Date ~= nil ) then
            if ( args.month ~= nil ) then 
                Date = args.month .. " " .. Date 
                if ( args.day ~= nil ) then Date = args.day .. " " .. Date end
            end
        end        
    end
    if ( Journal ~= nil ) then
        Chapter = Title
        Title = Journal
    end
    if ( Chapter ~= nil ) then 
        Chapter = "\"" .. Chapter .. "\""
        if ( ChapterURL ~= nil ) then
            Chapter = "[" .. ChapterURL .. " " .. Chapter .. "]"
        elseif ( URL ~= nil ) then 
            Chapter = "[" .. URL .. " " .. Chapter .. "]"
            URL = nil
        end
        Chapter = " " .. Chapter .. "."
    else 
        Chapter = "" 
    end    
    if ( Title ~= nil ) then
        if ( URL ~= nil ) then 
            Title = "[" .. URL .. " " .. Title .. "]"
            URL = nil
        end
        Title = " ''" .. Title .. "''."
    else 
        Title = "" 
    end
    if ( Publisher ~= nil ) then Publisher = " " .. Publisher .. "." else Publisher = "" end
    if ( PublicationPlace ~= nil ) then PublicationPlace = " " .. PublicationPlace .. ":" else PublicationPlace = "" end
    if ( Language ~= nil ) then Language = " (in " .. Language .. ")" else Language = "" end
    if ( ISBN ~= nil ) then ISBN = " ISBN " .. ISBN else ISBN = "" end
    if ( DOI ~= nil ) then DOI = " " .. doi(frame, DOI) else DOI = "" end
    if ( URL ~= nil ) then URL = " " .. URL else URL = "" end
    if ( Edition ~= nil ) then Edition = " " .. Edition .. " edition" else Edition = "" end
    if ( Volume ~= nil ) then Volume = " '''" .. Volume .. "'''" else Volume = "" end
    if ( Issue ~= nil ) then Issue = " (" .. Issue .. ")" else Issue = "" end
    if ( Pages ~= nil ) then Pages = " :" .. Pages else Pages = "" end
    if ( Series ~= nil ) then Series = " " .. Series .. "." else Series = "" end
    if ( Format ~= nil ) then Format = " (" .. Format .. ")" else Format = "" end
    if ( ID ~= nil ) then ID = " " .. ID else ID = "" end
    if ( Date ~= nil ) then Date = " (" .. Date .. ")" else Date = "" end
    if ( Authors == nil ) then
        Authors = authorprefix(Surname1, Given1, Authorlink1, "")
        Authors = Authors .. authorprefix(Surname2, Given2, Authorlink2, "&#59; ")
        Authors = Authors .. authorprefix(Surname3, Given3, Authorlink3, "&#59; ")
        Authors = Authors .. authorprefix(Surname4, Given4, Authorlink4, "&#59; ")
        Authors = Authors .. authorprefix(Surname5, Given5, Authorlink5, "&#59; ")
        Authors = Authors .. authorprefix(Surname6, Given6, Authorlink6, "&#59; ")
        Authors = Authors .. authorprefix(Surname7, Given7, Authorlink7, "&#59; ")
        Authors = Authors .. authorprefix(Surname8, Given8, Authorlink8, "&#59; ")
        Authors = Authors .. authorprefix(Surname9, Given9, Authorlink9, "&#59; ")       
    end
    if ( "" ~= Authors and "" ~= Date ) then
        Date = Date .. "."
    end
    if ( Editors ~= nil ) then
        Editors = " in " .. Editors .. "."
    else
        Editors = ""    
    end
    local text = Authors .. Date .. Chapter .. Editors .. Title .. Format .. Edition .. Language .. Volume .. Issue .. Pages .. Series .. PublicationPlace .. Publisher .. ISBN .. DOI .. ID .. URL
    if ( Ref ~= nil ) then 
        local id = Ref
        if ( "harv" == Ref ) then
            local P1 = Surname1
            local P2 = Surname2
            local P3 = Surname3
            local P4 = Surname4
            local P5
            if ( Surname2 == nil ) then 
                P2 = Year 
            elseif ( Surname3 == nil ) then
                P3 = Year
            elseif ( Surname4 == nil ) then
                P4 = Year
            else
                P5 = Year
            end
            id = anchorid({P1,P2,P3,P4,P5})
        end
        local args = { class="citation " .. config.CitationClass, id=id }
        text = tag(frame, {name="span", contents=text, params=args}) 
    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 citation0(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