Module:User:Saph/sandbox

Hello, you have come here looking for the meaning of the word Module:User:Saph/sandbox. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:User:Saph/sandbox, but we will also tell you about its etymology, its characteristics and you will know how to say Module:User:Saph/sandbox in singular and plural. Everything you need to know about the word Module:User:Saph/sandbox you have here. The definition of the word Module:User:Saph/sandbox will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:User:Saph/sandbox, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.


local export = {}

-- Automatically generate an entry using a Wikidata lexeme.

local languages_module = "Module:languages"

	local function get_by_canonical_name(...)
		get_by_canonical_name = require(languages_module).getByCanonicalName
		return get_by_canonical_name(...)
	end

local concat = table.concat
local error = error
local find = string.find
local get_current_title = mw.title.getCurrentTitle
local get_entity = mw.wikibase.getEntity
local get_label = mw.wikibase.getLabel
local ipairs = ipairs
local match = string.match
local pairs = pairs
local sort = table.sort
local sub = string.sub
local tonumber = tonumber
local upper = string.upper

-- Helper functions.

local function sentence_case(text)
	return upper(sub(text, 1, 1)) .. sub(text, 2, -1)
end

local function grab_id(i)
	local id = i.mainsnak.datavalue.value.id
	if find(id, "%-") then
		return match(id, "^(L%d+)%-.+$")
	end
	return id
end

-- Handlers.

local function ety_handler(entity, langcode)
	local ety = ''
	
	if entity.claims.P5238 then
		ety = 'From {{af|' .. langcode
		for _, i in ipairs(entity.claims.P5238) do
			local entity = get_entity(grab_id(i))
			ety = ety .. '|' .. entity.lemmas.value
			if entity.senses.glosses and entity.senses.glosses.en then
				ety = ety .. '<t:' .. entity.senses.glosses.en .. '>'
			end
		end
		ety = ety .. '}}.'
	elseif entity.claims.P5191 then
		ety = 'From {{der|' .. langcode
		local _, lemmas = next(get_entity(grab_id(entity.claims.P5191)).lemmas)
		ety = ety .. '|' .. lemmas.language .. '|' .. lemmas.value .. '}}.'
	else
		return ''
	end
	
	return '\n\n===Etymology===\n' .. ety
end

local function trans_handler(entity, gloss)
	if entity.senses.claims and entity.senses.claims.P5972 then
		local ret = '\n\n====Translations====\n{{trans-top}}\n'
		local translations = {}
		
		local n = 1
		for _, i in ipairs(entity.senses.claims.P5972) do
			local entity = get_entity(grab_id(i))
			local langname = get_label(entity.language)
			local langcode = get_by_canonical_name(langname)
			
			if langcode then
				langcode = langcode:getCode()
			end
			
			local ok = pcall(function() -- Protect this, because Wikidata codes are sometimes different from ours.
				translations = '* ' .. langname .. ': {{t|' .. langcode .. '|' .. entity.lemmas.value .. '}}\n'
			end)
			
			if ok then
				n = n + 1
			end
		end
		
		sort(translations)
		return ret .. concat(translations) .. '{{trans-bottom}}'
	end
	
	return ''
end

local function def_handler(entity, langcode, head)
	local pos = get_label(entity.lexicalCategory)
	local ret = ''
	
	local function add(pos, entity)
		ret = ret .. '\n\n===' .. sentence_case(pos) .. '===\n{{head|' .. langcode .. '|' .. pos
		
		if head then
			ret = ret .. '|head=' .. head
		end
		
		ret = ret .. '}}\n\n'
		
		local gloss
		if entity.senses and entity.senses.glosses.en then
			gloss = sentence_case(entity.senses.glosses.en.value)
			ret = ret .. '# ' .. gloss .. (not find(gloss, 'rfdef') and '.' or '')
			if langcode == "en" then
				ret = ret .. trans_handler(entity, gloss)
			end
		end
	end
	
	if entity.senses and entity.senses then
		add(pos, entity)
		
		if entity.claims and entity.claims.P5402 then
			for _, i in ipairs(entity.claims.P5402) do
				local entity = get_entity(grab_id(i))
				local pos = get_label(entity.lexicalCategory)
				if entity.senses and entity.senses then
					add(pos, entity)
				else
					add(pos, {senses = {{glosses = {en = {value = '{{rfdef|' .. langcode .. '}}'}}}}})
				end
			end
		end
	else
		add(pos, {senses = {{glosses = {en = {value = '{{rfdef|' .. langcode .. '}}'}}}}})
	end
	
	return ret
end

-- Main entry point.

function export.make(frame)
	local args = frame.args
	
	if not match(args, "^L%d+$") then
		error(args .. " is not a lexeme")
	end
	
	local entity = get_entity(args)
	
	local langname = get_label(entity.language)
	local ok, obj = pcall(function()
		return get_by_canonical_name(langname)
	end)
	
	if not ok and args.langname then
		obj = get_by_canonical_name(args.langname)
	elseif not ok then
		error('Could not find language code for language ' .. langname .. '. '
			.. 'You can override the Wikidata language with a |langname= argument.')
	end
	
	langname = args.langname or langname
	local langcode = obj:getCode()
	
	local ret = '==' .. langname .. '=='
	
	local forms = { lemma = nil, alternative = {} }
	local title = get_current_title()
	
	args.head = args.head 
			 or entity.claims.P12420 and entity.claims.P12420.mainsnak.datavalue.value
			 or entity.forms.claims.P2440 and entity.forms.claims.P2440.mainsnak.datavalue.value
	
	do
		local n = 1
		for _, v in pairs(entity.lemmas) do
			if v.value == (args.head or title.rootText) then
				forms.lemma = v.value
			else
				forms.alternative = v.value
				n = n + 1
			end
		end
	end
	
	if forms.alternative then
		ret = ret .. '\n\n===Alternative forms===' .. '\n* {{alt|' .. langcode
		for _, i in ipairs(forms.alternative) do
			ret = ret .. '|' .. i
		end
		ret = ret .. '}}'
	end
	
	if not title.isContentPage then
		args.head = args.head or forms.lemma
	end
	
	ret = ret .. ety_handler(entity, langcode)
			  .. def_handler(entity, langcode, args.head)
	
	mw.log(ret)
	
	return frame:preprocess(ret)
end

return export