Module:Headnote

From Wikipedia
Jump to: navigation, search

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

--require "mw.text"
 
local z = {
    mw = require "Module:mw",
    wikitext = require "Module:Wikitext"
}
 
-- This function makes an ordinary array copy of a "special" arguments array taken from a frame.
function clonenumberedargs(args)
    local newargs = {}
    local index = 1
    while args[index] ~= nil do
        local arg = args[index]
        newargs[index] = arg
        index = index + 1
    end
    return newargs
end
 
-- This encloses the text in the necessary CSS for a headnote link.
function rellink(text,classes) 
    local params = { class = table.concat(classes, " ") }
    return z.mw.text.tag({name="div",contents=text,params=params})
end
 
function simple_unlinked(frame,phrase,classes)
    local pframe = frame:getParent()
    local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
    local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself    
    local newargs = clonenumberedargs(args)
    local text = z.wikitext.oxfordlist(newargs)
    local Phrase = args.altphrase or phrase
    return rellink(Phrase .. ": " .. text,classes)
end
 
function simple_labelled_linked(frame,phrase,classes)
    local pframe = frame:getParent()
    local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
    local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
    local newargs = {}
    -- This would be shorter using ipairs(), but that doesn't work on an arguments table supplied to a template.
    local index = 1
    while args[index] ~= nil do
        local arg = args[index]
        local pipe = args["label" .. index] or args["l" .. index]
        if ( pipe ~= nil ) then pipe = "|" .. pipe else pipe = "" end
        newargs[index] = "[[:" .. arg .. pipe .. "]]"
        index = index + 1
    end
    local text = z.wikitext.oxfordlist(newargs)
    local Phrase = args.altphrase or phrase
    return rellink(Phrase .. ": " .. text,classes)
end
 
-- This is used by template {{see also}}.
function z.seealso(frame)
    return simple_labelled_linked(frame,"See also",{"rellink","boilerplate","seealso"})
end
 
-- This is used by template {{see also2}}.
function z.seealso2(frame)
    return simple_unlinked(frame,"See also",{"rellink","boilerplate","seealso"})
end
 
-- This is used by template {{main}}.
function z.main(frame)
    return simple_labelled_linked(frame,"Main article",{"rellink","relarticle","mainarticle"})
end
 
-- This is used by template {{main2}}.
function z.main2(frame)
    return simple_unlinked(frame,"Main article",{"rellink","relarticle","mainarticle"})
end
 
-- This is used by template {{further}}.
function z.further(frame)
    return simple_labelled_linked(frame,"Further information",{"rellink","relarticle","further"})
end
 
-- This is used by template {{further2}}.
function z.further2(frame)
    return simple_unlinked(frame,"Further information",{"rellink","relarticle","further"})
end
 
-- This is used by templates {{about}}, {{for}}, {{other uses}} and brethren, {{redirect}} and brethren, {{other people}} and brethren, {{other ships}}, {{other hurricanes}}, and {{other places}}
function z.about(frame)
    local pframe = frame:getParent()
    local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
    local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
 
    local classes = { "dablink" }
 
    local thistype = config["this-type"] or config.thistype or "about"
    local others = config.others or "other uses"
    local this1 = config.this1 or config.pagename or ""
    local of = config.of
    local thiscount = tonumber(config["this-count"] or config.thiscount) or 0
 
    local result = {}
 
    -- Construct the list of referents as the "these" arguments.
    local theseargs = {}
    local tempargs = clonenumberedargs(args)    
    if ( thiscount > 0 ) then        
        for i=1,thiscount do 
            local arg = table.remove(tempargs,1)
            table.insert(theseargs,arg) 
        end
        if ( "" == theseargs[1] ) then theseargs[1] = this1 end
        -- Put quotation marks around "these" arguments if necessary.
        if ( "redirect" == thistype ) then
            for i,arg in ipairs(theseargs) do theseargs[i] = "\"" .. arg .. "\"" end
        end
        local these = z.wikitext.oxfordlist(theseargs)
        if ( "redirect" == thistype ) then
            local singular = "" 
            if ( #theseargs == 1 ) then singular = "s" end
            these = these .. " redirect" .. singular .. " here."
        else
            these = "This article is about " .. these .. "."
        end
        table.insert(result, these)
    end
 
    -- In the case that there are no remaining arguments, construct a load of "other uses","THIS (disambiguation)" pairs.
    if ( #tempargs < 1 ) then
        if ( #theseargs > 0 ) then
            for i,arg in ipairs(theseargs) do 
                table.insert(tempargs,"other uses") 
                table.insert(tempargs,arg .. " (disambiguation)") 
            end
        else
            table.insert(tempargs,"other uses") 
            table.insert(tempargs,this1 .. " (disambiguation)")
        end
    end
 
    -- Process remaining arguments in tempargs as LABEL,ARTICLE pairs.
    if #tempargs % 2 == 1 then 
        table.insert(tempargs,this1 .. " (disambiguation)") 
    end
    local argscount = #tempargs
    for i=1,argscount,2 do
        local referent = theseargs[(i + 1) / 2] or this1
        local label = table.remove(tempargs,1)
        local article = table.remove(tempargs,1)
        if ( nil ~= of ) then
            table.insert(result, "For " .. label .. " of " .. referent .. ", see [[" .. article .. "]].")
        else
            table.insert(result, "For " .. label .. ", see [[" .. article .. "]].")
        end
    end
 
    return rellink(table.concat(result, " "),classes)
end
 
return z