Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:Temp

-- @brief
--  Temporary functions.
-- 
-- @author
--  [[meta:User:Danny B.]]
local _module = {}
----------------------------------------


function kpairs( tbl )
	local keys = {}
	
	for key in pairs( tbl ) do
		table.insert( keys, key )
	end
	table.sort( keys )
	
	local i = 0
	local iterator = function ()
		i = i + 1
		if keys[i] == nil then
			return nil
		else
			return keys[i], tbl[keys[i]]
		end
	end
	
	return iterator
end


function _module.cutRight( frame, count, text )
	count = count or frame.args[1]
	text = text or frame.args[2]
	return mw.ustring.sub( text, 1, mw.ustring.len( text )-count )
end


function _module.cutLeft( frame, count, text )
	count = tonumber ( count or frame.args[1] )
	text = text or frame.args[2]
	return mw.ustring.sub( text, count, -1 )
end


function _module.letterInPosition( frame, position, text )
	position = tonumber ( position or frame.args[1] )
	text = text or frame.args[2]
	return mw.ustring.sub( text, position, position )
end


function _module.checkPriznaky2( frame )
	
	local output = ""
	local parentFrame = frame:getParent()
	local templateArgs = parentFrame.args
	
	for arg, val in pairs( templateArgs ) do
		if string.match( val, '<span class="priznaky">' ) then
			output = output .. "[[Kategorie:Monitoring:Překlady/" .. arg .. "/…Příznaky2…]]"
		end
	end
	
	output = frame:preprocess( output )
	
	return output
	
end


function _module.checkIpa( frame )
	
	return mw.ustring.find( frame.args[1], "[ʣʤʥʦʧʨʩʪʫ:]" ) ~= nil and "[[Kategorie:Monitoring:IPA s chybným znakem]]" or ""
	
end


function _module.checkKana( frame )
	
	local titleObject = mw.title.getCurrentTitle()
	
	if titleObject.namespace ~= 0 then
		return
	end
	
	local output
	
	if frame.args[1] == titleObject.text then
		output = "[[Kategorie:Monitoring:Kana/1/=PAGENAME]]"
	elseif mw.ustring.match( frame.args[1], "^[゠-ヿ]+$" ) ~= nil then
		output = " <small>(katakana)</small>[[Kategorie:Monitoring:Kana/1/= katakana]]"
	elseif mw.ustring.match( frame.args[1], "^[ぁ-ゟ]+$" ) ~= nil then
		output = " <small>(hiragana)</small>[[Kategorie:Monitoring:Kana/1/= hiragana]]"
	elseif mw.ustring.match( frame.args[1], "^[゠-ヿぁ-ゟ]+$" ) ~= nil then
		output = " <small>(mix)</small>[[Kategorie:Monitoring:Kana/1/= mix]]"
	else
		output = " <small>(obsahuje jiné znaky)</small>[[Kategorie:Monitoring:Kana/1/= obsahuje jiné znaky]]"
	end
	
	return output
	
end


function _module.listLangs()
	
	local languages = require( "Module:Languages" )
	
	local output
	local html = mw.html.create()
	local body = mw.html.create()
	
	for lang, data in kpairs( languages ) do
		body
			:tag( "tr" )
				:tag( "td" )
					:wikitext( lang )
					:done()
				:tag( "td")
					:wikitext( data.name )
					:done()
				:done()
			:newline()
		
		for region, name in kpairs( data.regions or {} ) do
			body
				:tag( "tr" )
					:tag( "td" )
						:wikitext( lang .. "-" .. region )
						:done()
					:tag( "td" )
						:wikitext( data.name .. " (" .. name .. ")" )
						:done()
					:done()
				:newline()
		end
		
		for script, name in kpairs( data.scripts or {} ) do
			body
				:tag( "tr" )
					:tag( "td" )
						:wikitext( lang .. "-" .. script )
						:done()
					:tag( "td" )
						:wikitext( data.name .. " (" .. name .. ")" )
						:done()
					:done()
				:newline()
		end
	end
	
	html
		:tag( "table" )
			:addClass( "wikitable" )
			:addClass( "sortable" )
			:newline()
			:tag( "tr" )
				:tag( "th" )
					:wikitext( "kód" )
					:done()
				:tag( "th" )
					:wikitext( "název" )
					:done()
				:done()
			:newline()
			:node( body )
			:allDone()
	
	output = tostring( html )
	
	return output
	
end


local function getEqualityType( a, b )
	if a == b then
		return "≡"
	elseif mw.ustring.lower( a ) == mw.ustring.lower( b ) then
		return "≋"
	else
		return "≢"
	end
end


function _module.monitorAudio( frame )
	
	local titleObject = mw.title.getCurrentTitle()
	
	if titleObject.namespace ~= 0 then
		return
	end
	
	local output = ""
	
	local file = mw.text.trim( frame:getParent().args[1] or "" )
	local text = mw.text.trim( frame:getParent().args[2] or "" )
	
	local title = titleObject.text
	local valueType
	local equalityType
	local fileName
	local fileExtension
	local matches
	local entry
	local value
	local parentheses
	
	
	if file == "" then
		output = output .. "[[Kategorie:Monitoring:Audio/1/]]"
	else
		
		file = string.gsub( file, "_", " " )
		
		fileName, fileExtension = string.match( file, "^(.+)%.([^%.]+)$" )
		
		if not fileName then
			output = output .. string.format( "[[Kategorie:Monitoring:Audio/1/?/%s]]", file )
		else
			
			local patterns = {
				{ 2, "^([Ee]n%-%a%a%-)(.+)(%-noun%-adj[1-9]?)$" },
				{ 2, "^([Ee]n%-%a%a%-)(.+)(%-adj[1-9]?)$" },
				{ 2, "^([Ee]n%-%a%a%-)(.+)(%-noun[1-9]?)$" },
				{ 2, "^([Ee]n%-%a%a%-)(.+)(%-verb[1-9]?)$" },
				{ 2, "^([Ee]n%-uk%-to )(.+)$" },
				{ 2, "^([Ee]s%-am%-lat%-)(.+)$" },
				{ 2, "^([Ff]r%-Paris%-%-)(.+)$" },
				{ 2, "^([Ff]r%-)(.+)(%-fr FR%-Paris)$" },
				{ 2, "^([Ff]r%-)(.+)( FR%-Paris)$" },
				{ 2, "^([Ff]r%-)(.+)(%-fr%-Paris)$" },
				{ 2, "^([Ff]r%-le )(.+)(%-fr%-ouest)$" },
				{ 2, "^([Ff]r%-une? )(.+)(%-fr%-ouest)$" },
				{ 2, "^([Ff]r%-)(.+)(%-fr%-ouest)$" },
				{ 2, "^([Ff]r%-la )(.+)(%-fr BE)$" },
				{ 2, "^([Ff]r%-)(.+)(%-fr BE)$" },
				{ 2, "^([Ff]r%-)(.+)(%-fr CA)$" },
				{ 2, "^([Ff]r%-une? )(.+)(%-fr)$" },
				{ 2, "^([Ff]r%-)(.+)(%-fr)$" },
				{ 2, "^([Ff]r%-)(.+)(%-ca%-Montréal)$" },
				{ 2, "^([Ii]t%-il )(.+)$" },
				{ 2, "^([Ii]t%-l['a] )(.+)$" },
				{ 2, "^([Ii]t%-una? )(.+)$" },
				{ 2, "^([Ll]a%-cls%-)(.+)$" },
				{ 2, "^([Ll]v%-riga%-)(.+)$" },
				{ 2, "^([Nn]l%-)(.+)( %(Belgium%))$" },
				{ 2, "^([Ss]v%-en )(.+)$" },
				{ 2, "^([Vv]i%-hanoi%-[mf]%-)(.+)$" },
				{ 2, "^(%a%a%a?%-%a%a%-)(.+)([ %-][1-9])$" },
				{ 2, "^(%a%a%a?%-%a%a%-)(.-)([1-9]?)$" },
				{ 2, "^(%a%a%a?%-)(.+)([ %-][1-9])$" },
				{ 2, "^(%a%a%a?%-)(.-)([1-9]?)$" }
			}
			
			for _, pattern in ipairs( patterns ) do
				matches = { string.match( fileName, pattern[2] ) }
				if next( matches ) ~= nil then
					entry = matches[pattern[1]]
					matches[pattern[1]] = "⌂"
					valueType = table.concat( matches )
					break
				end
			end
			
			entry = entry and entry or fileName
			equalityType = getEqualityType( entry, title )
			
			output = output .. string.format( "[[Kategorie:Monitoring:Audio/1/%s]]", equalityType )
			output = output .. string.format( "[[Kategorie:Monitoring:Audio/1/⌂.%s]]", fileExtension )
			
			if valueType then
				output = output .. string.format( "[[Kategorie:Monitoring:Audio/1/%s]]", valueType )
			end
			
			if equalityType == "≢" then
				output = output .. string.format( "[[Kategorie:Monitoring:Audio/1/≢ */%s]]", entry )
			end
			
			if string.match( fileName, "^%l.*$" ) ~= nil then
				output = output .. "[[Kategorie:Monitoring:Audio/1/!]]"
				output = output .. string.format( "[[Kategorie:Monitoring:Audio/1/!%s]]", string.sub( fileName, 1, 1 ) )
			end
			
		end
		
	end
	
	if text == "" then
		output = output .. "[[Kategorie:Monitoring:Audio/2/]]"
	else
		
		value, parentheses = string.match( text, "(.*) %(([^%)]+)%)$" )
		
		if parentheses then
			equalityType = getEqualityType( value, title )
			valueType = string.format( "%s (%s)", equalityType, parentheses )
		else
			equalityType = getEqualityType( text, title )
			valueType = string.format( "%s *", equalityType )
		end
		
		output = output .. string.format( "[[Kategorie:Monitoring:Audio/2/%s]]", equalityType )
		output = output .. string.format( "[[Kategorie:Monitoring:Audio/2/%s]]", valueType )
		
		if parentheses then
			output = output .. string.format( "[[Kategorie:Monitoring:Audio/2/* (%s)]]", parentheses )
		elseif equalityType == "≢" then
			output = output .. string.format( "[[Kategorie:Monitoring:Audio/2/≢ */%s]]", text )
		end
		
	end
	
	output = frame:preprocess( output )
	
	return output
	
end


----------------------------------------
return _module