Jump to content

Module:Wp/isv/Jezyky

From Wikimedia Incubator

Documentation for this module may be created at Module:Wp/isv/Jezyky/doc

local p = {}

local stylesPage = 'Module:Wp/isv/Jezyky/styles.css'

-- Data for module
local dataPage = 'Module:Wp/isv/Jezyky/data'
local data = mw.loadData( dataPage )

-- Gets arguments from the template
local getArgs = require('Module:Arguments').getArgs
local cyrl = require('Module:Wp/isv/Alphabet')._global_cyrl

-- Checks if variable is blank
local function isBlank(value)
	return value == nil or value == ''
end

-- Return abbreviation for language
function p._abbr(code)
	if data[code] == nil then
		return nil
	end
	
	return (data[code]['abbr'] or data[code]['name'])
end

-- Return locative for language
function p._locative(code)
	if data[code] == nil then
		return nil
	end
	
	return (data[code]['locative'] or data[code]['name'])
end

-- Return code if it exists in data file
function p._code(code)
	return (data[code] ~= nil and code or nil)
end

-- Return formatted link to language
function p._link(code, separator, after, title)
	local lang = data[code]
	separator = (separator and separator or '')
	after = (after and after or '')
	
	if lang ~= nil then
		local name = cyrl(lang['name'])
		local abbr = cyrl(lang['abbr'] or lang['name'])
		
		if mw.ustring.lower(name) == mw.ustring.lower(title) then
			return string.format('%s%s%s', abbr, separator, after)
		end
		return string.format('[[Wp/isv/%s|%s%s]]%s', name, abbr, separator, after)
	else
		return ''
	end
end

-- Return language link title if it ěists in data file
function p._linkTitle(code)
	local lang = data[code]
	
	if lang ~= nil then
		return lang['name']
	end
	
	return ''
end

-- Return formatted text in other language
function p._text(code, text, class)
	text = mw.text.trim(text)
	local lang = data[code]
	local addClass = (class and 'lang' .. (lang and lang['dir'] and ' lang-normal' or '') or '')
	if not lang or (lang and lang['italic'] == false) then
		addClass = addClass .. ' lang-normal'
	end
	local dir = (lang and lang['dir'] and string.format(' dir="%s"', lang['dir']) or '')
	
	local frame = mw.getCurrentFrame()
	local styles = frame:extensionTag{
		name = 'templatestyles',
		args = { src = stylesPage }
	}
	if lang ~= nil then
		return styles .. string.format('<span class="%s"%s lang="%s">%s</span>', addClass, dir, code, text)
	else
		return styles .. string.format('<sup>[[%s|%s?]]</sup><span lang="%s" class="%s">%s</span>', dataPage, code, code, addClass, text)
	end
end

-- Format list from the table
function p._list(t, sep)
	local prevLang = nil
	local currentLang = nil
	local sameLang = false
	local langDone = false
	local result = ''
	
	local title = mw.title.getCurrentTitle().text
	
	for index, value in pairs(t) do
		value = mw.text.trim(value)
		if currentLang == nil then
			
			-- First, third, fifth occurrence: it's language
			currentLang = value
			
			sameLang = false
			if prevLang == currentLang then
				sameLang = true
			end
			
			langDone = true
			
		elseif langDone == true and not isBlank(currentLang) then
			
			-- Second, fourth etc. occurrences: it's text
			local text = ''
			local separator = sep
			
			-- If language is the same, go with comma
			if sameLang then
				separator = ','
			end
			
			-- If it is first language, go with nothing
			if prevLang == nil then
				separator = nil
			end
			
			text = (separator and separator .. ' ' or '')
			
			-- Display language name
			if sameLang == false then
				text = text .. p._link(currentLang, ':', ' ', title)
			end
			text = text .. p._text(currentLang, value, true)
			
			result = result .. text
			
			prevLang = currentLang
			currentLang = nil
			langDone = false
		end
	end
	
	local frame = mw.getCurrentFrame()
	return result
end

-- Return abbreviation for language
function p.abbr(frame)
	local code = frame.args[1]
	
	if isBlank(code) then
		return
	end
	
	return p._abbr(code)
end

-- Return locative for language
function p.locative(frame)
	local code = frame.args[1]
	
	if (isBlank(code)) then
		return
	end
	
	return p._locative(code)
end

-- Format list from the frame
function p.list(frame)
	local args = getArgs(frame)
	args.cyrl = nil
	
	return p._list(args, ';')
end

-- Get language link title
function p.linkTitle(frame)
	local code = frame.args[1]
	
	if (isBlank(code)) then
		return
	end
	
	return p._linkTitle(code)
end

-- Format text
function p.text(frame)
	local code = frame.args[1]
	local text = frame.args[2]
	
	if isBlank(code) or isBlank(text) then
		return
	end
	
	code = mw.text.trim(code)
	text = mw.text.trim(text)
	return p._text(code, text, true)
end

return p