Module:Coinage

From The Languages of David J. Peterson
Jump to navigation Jump to search

Documentation for this module may be created at Module:Coinage/documentation

local export = {}
local m_utilities = require("Module:utilities")
local m_languages = require("Module:languages")

function export.coinage(frame)
	local args = frame:getParent().args
	
	local params = {
		[1] = {required = true, default = "und"},
		[2] = {required = true, default = "unknown"},
		
		["in"] = {},		-- date: "in"
		["w"] = {default = nil}, 		-- Wikipedia link target
		["nationality"] = {},		-- nationality
		["occupation"] = {list = true, default = nil},
		["alt"] = {default = nil},		-- alt text
		
		["nat"] = {},
		["occ"] = {list = true, default = nil},

		["exnihilo"] = {type = "boolean"},

		["sort"] = {},
		["nobycat"] = {type = "boolean"},
		["nocat"] = {type = "boolean"},
		["nocap"] = {type = "boolean"},
		["notext"] = {type = "boolean"},
	}
	
	args = require("Module:parameters").process(args, params)
	
	local lang = m_languages.getByCode(args[1]) or m_languages.err(args[1], 1)

    local coiner = args[2]
    local wikilink = args["w"] or coiner
    local date = args["in"]
    
	local result = ""
	
	if not args["notext"] then
		if args["exnihilo"] then
			result = result .. "[[Appendix:Glossary#coinage|" .. (args["nocap"] and "c" or "C") .. "oined]] [[w:Ex nihilo lexical enrichment|''ex nihilo'']] by "
		else
			result = result .. "[[Appendix:Glossary#coinage|" .. (args["nocap"] and "c" or "C") .. "oined]] by "
		end
	end

    local nationality = args["nationality"] or args["nat"] or nil
	local occupation = args["occupation"]
	if not occupation or #occupation < 1 then
		occupation = args["occ"]
	end
    local nat_occ = ""
    
	if nationality then
		nat_occ = nat_occ .. nationality .. " "
	end
	if occupation and #occupation > 0 then
		nat_occ = nat_occ .. mw.text.listToText(occupation)
	end
    
    -- use Wikidata to get label and Wikipedia link if coiner looks like a QID
    if mw.wikibase and mw.ustring.match(coiner, "^Q%d+$") then
		wikilink = mw.wikibase.sitelink(coiner, 'enwiki')
		if nat_occ == "" then
			-- fall back to Wikidata description if nothing specified
			nat_occ = mw.wikibase.getDescription(coiner) or ""
			-- remove biographical "(born-died)" information often present in Wikidata descriptions
			nat_occ = mw.ustring.gsub(nat_occ, "%([%d-–]+%)$", "")
			-- remove everything after the first semicolon
			nat_occ = string.gsub(nat_occ, ";.+$", "")
			
		end
		coiner = mw.wikibase.getLabel(coiner)
	end
		
	local coiner_text = args["alt"] or coiner
	local link = coiner_text
    if wikilink and wikilink ~= "-" then
        -- create Wikipedia link
        -- default to title, or interpret as language code if a colon is present

        if mw.ustring.match(wikilink, ":$") then
        	-- just language code and a colon?
            wikilink = wikilink .. coiner
        end

        link = "[[w:" .. wikilink .. "|" .. coiner_text .. "]]"
    end
	
	result = result .. nat_occ .. " " .. link
	
	if date then
		if mw.ustring.match(date, "^[0-9]+$") -- year only
		or mw.ustring.match(date, "^[A-Z]")   -- month and year?
		or mw.ustring.match(date, "^the ")
		then
			result = result .. " in " .. date
		else -- assume alternative preposition was given
			result = result .. " " .. date
		end
	end
	
	local categories = {}
	if not args["nocat"] then
		if not args["nobycat"] then
			table.insert(categories, lang:getCanonicalName() .. " terms coined by " .. coiner)
		end
		table.insert(categories, lang:getCanonicalName() .. " coinages")
		if args["exnihilo"] then
			table.insert(categories, lang:getCanonicalName() .. " terms coined ex nihilo")
		end
	end
    categories = m_utilities.format_categories(categories, lang, args["sort"])
	
	return result .. categories
end

return export