[go: up one dir, main page]

Jump to content

Module:taxlink

From Wiktionary, the free dictionary


local export = {}

function format_text(text, rank, force_italics)

    if force_italics then
        return "''" .. text .. "''"
    end

    if rank == "nothogenus"
        or rank == "subgenus"
        or rank == "subg."
        or rank == "genus"
        or rank == "subsection"
        or rank == "section"
        or rank == "subsect."
        or rank == "sect."
        or rank == "nothospecies"
        or rank == "infraspecies"
        or rank == "subspecies"
        or rank == "subsp."
        or rank == "species"
        or rank == "variety"
        or rank == "var."
        or rank == "forma"
        or rank == "form"
        or rank == "morph"
        or rank == "morpha"
        or rank == "species group"
        or rank == "cultivar"
        or rank == "cvar."
        or rank == "series"
        or rank == "ser."
    then
        text = require("Module:italics").i(text)
    end
    return text
end

function format_result(target, rank, text, cats, warning)
    -- Don't create categories for entries on User: pages
    if cats ~= "" and mw.title.getCurrentTitle().nsText == "User" then
        cats = ""
    end

    if warning ~= "" then
        warning = '<sup class="error previewonly"><small>' .. warning .. '</small></sup>'
    end

    return '<span class="biota">' .. '[[' .. target .. '|' .. text .. ']]' .. warning .. cats .. '</span>'
end


function export.taxlink(frame)
    local warn = ""
    local cats = ""

	local params = {
		[1] = {required = true},
		[2] = {required = true},
		[3] = {},
		["i"] = {type = "boolean"},
		["nocat"] = {type = "boolean"},
		["nomul"] = {},
		["wplink"] = {},
		["wslink"] = {},
		["nover"] = {}, -- nover is allowed but not used
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)

    local taxon = args[1] or ""
    local rank = args[2] or ""
    local text = args[3] or taxon
    text = format_text(text, rank, args["i"])

    local target
    -- if page exists and nomul= is not set, use a local link and add to cleanup category
    if (not args["nomul"]) and mw.title.new( taxon ):getContent() ~= nil then
        cats = cats .. "[[Category:Entries with redundant template: taxlink]]"
        target = taxon .. "#Translingual"
        if warn ~= "" then warn = warn .. "; " end
        warn = warn .. "(Please check if this is already defined at target. Replace <code>{{[[Template:taxlink|taxlink]]}}</code> with <code>{{[[Template:taxfmt|taxfmt]]}}</code> if already defined. Add nomul=1 if not defined.)"
    else
		-- undocumented and unused in existing entries as of 2/24/2024, if rank is epithet use local link
		if rank == "epithet" then
		    target = taxon .. "#Translingual"

		else
		    if args["wplink"] then
		        if args["wplink"] == "0" then
		            target = "w:" .. taxon
		        elseif args["wplink"] == "1" then
		            target = "w:" .. mw.title.getCurrentTitle().subpageText
		        else
		            target = "w:" .. args["wplink"]
		        end
		    else
		        if args["wslink"] then
		            target = "Wikispecies:" .. args["wslink"]
		        else
		            target = "Wikispecies:" .. taxon
		        end
		    end
		end
    end

    if not args["nocat"] then
        label = rank and rank or "unknown"
        cats = cats .. "[[Category:Entries using missing taxonomic name (" .. label .. ")|" .. taxon .. "]]"
    end

    return format_result(target, rank, text, cats, warn)
end

function export.taxfmt(frame)
    local warn = ""

	local params = {
		[1] = {required = true},
		[2] = {required = true},
		[3] = {},
		["i"] = {type = "boolean"},

        -- unused parameters, allowed for easy renaming {{taxlink}} to {{taxfmt}}
		["nocat"] = {type = "boolean"},
		["nomul"] = {},
		["wplink"] = {},
		["wslink"] = {},
	}
	local args = require("Module:parameters").process(frame:getParent().args, params)

    local taxon = args[1] or ""
    local rank = args[2] or ""
    local text = args[3] or taxon
    text = format_text(text, rank, args["i"])

    local target = taxon .. "#Translingual"

    return format_result(target, rank, text, "", warn)
end


return export