Moduł:Data

Z Nonsensopedii, polskiej encyklopedii humoru

local p = {}

local monthMap = {
	I = 'styczeń',
	II = 'luty',
	III = 'marzec',
	IV = 'kwiecień',
	V = 'maj',
	VI = 'czerwiec',
	VII = 'lipiec',
	VIII = 'sierpień',
	IX = 'wrzesień',
	X = 'październik',
	XI = 'listopad',
	XII = 'grudzień'
}

local function cleanThings(text)
	text = mw.ustring.gsub(text, 'r%.', '')
	local split = mw.text.split(text, '[%s%.%-/]')
	local result = ''
	
	for i,s in ipairs(split) do
		if i > 1 then result = result .. ' ' end
		local changed = false
		
		for key, val in pairs(monthMap) do
			if key == s then
				result = result .. val
				changed = true
				break
			end
		end
		
		if not changed then result = result .. s end
	end
	
	return result
end

-- przetwarza datę, zwraca tekst z adnotacjami SMW i wyczyszczony tekst bez ozdobników
function p.processToSMW(text)
	text = text or ''
	if mw.ustring.find(mw.ustring.lower(text), 'nieznan', 1, true) then
		return text, text
	end
	
	local matched = false
	local clean = mw.ustring.gsub(mw.ustring.gsub(text, '>>', ''), '<<', '')
	
	local function regexCallback(m)
		matched = true
		m = mw.text.trim(mw.ustring.sub(m, 3, -3))
		local original = m
		m = cleanThings(m)
		return '[[Ma datę::' .. m .. '|' .. original .. ']]'
	end
	
	text = mw.ustring.gsub(text, '<<.->>', regexCallback)
	
	if not matched then
		local original = text
		text = cleanThings(text)
		-- adnotacja tekstowa, by pokazało się ładnie ostrzeżenie
		text = '[[Ma datę::' .. text .. '|' .. original .. ']]'
	end
	
	return text, clean
end

return p