Modul:JazykTEST

Z Wikislovníku
Skočit na navigaci Skočit na vyhledávání

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

-- Podporované funkce: 
-- 1) monitoring neexistujících kódů
-- 2) monitoring duplicitního vložení jednoho jazyka
-- 3) monitoring špatné úrovně nadpisu
-- 4) ignorace šablony je-li uvnitř <nowiki></nowiki>, <pre></pre>, <!-- --> 
-- 5) zařazení každého hesla do kategorií Jazyk/vše
-- 6) kategorizace hesel dle délky názvu hesla 
-- 7) kategorizace palindromů
-- 8) kategorizace hesel bez samohlásky v názvu
-- 9) kategorizace dle počtu jazyků
-- TODO:
---- kategorizace hesel bez samohlásky v názvu: založit seznam samohlásek (všechny jazyky dohromady, nebo každý jazyk zvlášť?)
---- kategorizace dle slovních druhů
---- retrográdní kategorizace (jak?)
---- kontrola formátu překladů, synonym, antonym
---- kontrola všech nadpisů
---- 

local p = {}

local function firstToUpper( word )
	return mw.ustring.upper( mw.ustring.sub( word, 1, 1 ) ) .. mw.ustring.sub( word, 2 )
end

local function firstRunJob( Categories, title, Language )
	local content = title:getContent()
	content = mw.ustring.gsub( content, '<!%-%-.-%-%->', '' )
	content = mw.ustring.gsub( content, '<nowiki>.-</nowiki>', '' )
	content = mw.ustring.gsub( content, '<pre[^>]->.-</pre>', '' )

	local curr_code
	local Codes = {}
	local diffCodes = 0
	local levelsOk = true
	local orderOk = true
	local uniqueCodes = true
	local curr_lang, prev_lang
	local SlovniDruhy = require( 'Modul:SlovniDruhy' )
	for line in mw.text.gsplit( content, '\n' ) do
		local level, headline = mw.ustring.match( line, '^(=+)%s*([^=]-)%s*=+%s-$' )
		if level and headline then
			if mw.ustring.match( headline, '%{%{%s*[Jj]azykTEST[^%}]-%}%}' ) then
				curr_code = mw.ustring.match( headline, '%{%{%s*[Jj]azykTEST%s*|%s*([^%|%}]*)' )
				if curr_code and curr_code ~= '' then
					if Codes[curr_code] then
						uniqueCodes = false
					else
						Codes[curr_code] = curr_code
						diffCodes = diffCodes + 1
					end
					if orderOk and prev_lang and curr_code == 'cs' then
						orderOk = false
					end
					if orderOk then
						curr_lang = Language:getFullName( curr_code )
						if curr_lang then
							if prev_lang then
								local Sorter = require( 'Modul:Collation' )
								orderOk = Sorter.sortCompare( prev_lang, curr_lang )
							end
							if orderOk and curr_code ~= 'cs' then
								prev_lang = curr_lang
							end
						end
					end
				end
				if levelsOk and level ~= '==' then
					levelsOk = false
				end
			else
				if curr_lang then
					for _, data in pairs( SlovniDruhy ) do
						if headline == data.name then
							local ok
							if not data.default then
								ok = false
								for _, ok_code in pairs( data.existent_list ) do
									if curr_code == ok_code then
										ok = true
										break
									end
								end
							else
								ok = true
								for _, bad_code in pairs( data.nonexistent_list ) do
									if curr_code == bad_code then
										ok = false
										break
									end
								end
							end
							if not ok then
								table.insert(
									Categories,
									'[[Kategorie:TEST:Přebytečný slovní druh/' .. curr_lang .. ']]'
								)
							end
						end
					end
				end
			end
		end
	end

	table.insert( Categories, '[[Kategorie:TEST:Slova dle počtu jazyků/' .. diffCodes .. ']]' )

	if not levelsOk then
		table.insert( Categories, '[[Kategorie:Monitoring:Šablona Jazyk/Špatná úroveň nadpisu]]' )
	end

	if not orderOk then
		table.insert( Categories, '[[Kategorie:Monitoring:Nesprávné pořadí jazyků]]' )
	end

	if not uniqueCodes then
		table.insert( Categories, '[[Kategorie:Monitoring:Šablona Jazyk/Duplicitní vložení šablony]]' )
	end

	return Categories
end

function p.main( frame )
	local out
	local firstRun = true
	if package.loaded['Modul:SlovniDruhy'] then
		firstRun = false
	end
	local Language = require( 'Modul:Language' )
	local Categories = {}
	local title = mw.title.getCurrentTitle()
	local params = frame:getParent().args
	local code = params[1]
	local language = Language:getFullName( code or '' )
	if language then
		out = language
	else
		if code == '' then
			code = nil
		end
		out = mw.ustring.format( 'neznámý kód jazyka „%s“', code or '[nezadán]' )
		table.insert( Categories, '[[Kategorie:Monitoring:Šablona Jazyk/Špatný kód jazyka]]' )
	end

	if params[2] == "jenjazyk" or title.namespace ~= 0 and title.namespace ~= 2 then
		return out .. table.concat( Categories, '' )
	end

	local word = title.text
	local word_len = mw.ustring.len( word )
	local categoryText = 'Délka názvu hesla/' .. word_len .. ' znaků'
	if word_len == 1 then
		categoryText = 'Délka názvu hesla/' .. word_len .. ' znak'
	elseif word_len < 5 then
		categoryText = 'Délka názvu hesla/' .. word_len .. ' znaky'
	end

	local hasVowels = false
	for letter in mw.text.gsplit( mw.ustring.lower( word ), '' ) do
		if letter == 'a' or letter == 'e' or letter == 'i' or
			letter == 'o' or letter == 'u' or letter == 'y' then -- asi jich je víc
			hasVowels = true
			break
		end
	end

	local withoutSpecial = mw.ustring.lower( mw.ustring.gsub( word, '%W', '' ) )
	local withoutSpecialLen = mw.ustring.len( withoutSpecial )
	local isPalindrome = true
	for i = 1, math.floor( withoutSpecialLen / 2 ) do
		if mw.ustring.sub( withoutSpecial, i, i ) ~= mw.ustring.sub( withoutSpecial, -i, -i ) then
			isPalindrome = false
			break
		end
	end

	if firstRun then
		if params[2] then
			table.insert( Categories, '[[Kategorie:Monitoring:Šablona Jazyk/Více zadaných parametrů]]' )
		end

		table.insert( Categories, mw.ustring.format( '[[Kategorie:TEST:%s/vše]]', categoryText ) )
	
		if not hasVowels then
			table.insert( Categories, '[[Kategorie:TEST:Název hesla neobsahuje samohlásku/vše]]' )
		end
	
		if isPalindrome then
			table.insert( Categories, '[[Kategorie:TEST:Palindromy/vše]]' )
		end

		Categories = firstRunJob( Categories, title, Language )
	end

	if language then
		table.insert( Categories, '[[Kategorie:TEST:' .. firstToUpper( language ) .. '/vše]]' )

		table.insert( Categories, mw.ustring.format( '[[Kategorie:TEST:%s/%s]]', categoryText, language ) )

		if isPalindrome then
			table.insert( Categories, '[[Kategorie:TEST:Palindromy/' .. language .. ']]' )
		end

		if not hasVowels then
			table.insert( Categories, '[[Kategorie:TEST:Název hesla neobsahuje samohlásku/' .. language .. ']]' )
		end
	end

	return out .. table.concat( Categories, '' )
end

return p