Springe nei ynhâld

Module:puzel-top

Ut Wikiwurdboek
De neikommende dokumintaasje stiet op Module:puzel-top/dokumintaasje. [bewurkje]

Generearet in part fan 'e tekst mei opmaak fan Berjocht:puzel-top.


p = {}

function p.main(frame)
	local args = frame:getParent().args
	local count = args["kolom"] or "3"
	
	if count == "" then
		count = "3"
	end
	
	local m_table = require("Module:table")
	local sparseIpairs = m_table.sparseIpairs
	local size = m_table.size
	local sortedPairs = m_table.sortedPairs
	local getByCode = require("Module:languages").getByCode
	local lang = getByCode("fy")
	local lang2 = getByCode(args["lang"] or "fy") or lang
	local language_link = require("Module:links").language_link
	local format_categories = require("Module:utilities").format_categories
	local gsub = mw.ustring.gsub
	local head1 = ""
	local head2 = ""
	local cat = {"Wurden yn it Frysk"}
	local text = ""
	
	local param = {[1] = "cruci", [2] = "crypto", [3] = "anagr"}
	
	for key, value in ipairs(param) do
		if args[value] and args[value] ~= "" then
			if key == 1 then
				head1 = "Krúswurd"
				table.insert(cat, "Fryske krúswurden")
			elseif key == 2 then
				head1 = "Kryptogram"
				table.insert(cat, "Fryske kryptogrammen")
			elseif key == 3 then
				head1 = "Anagram"
				table.insert(cat, "Fryske anagrammen")
			end
			
			text = text .. '<div><p><span title="' .. value .. '">' .. head1 .. '</span>:</p></div>'
			
			local data = {}
			
			for k, v in ipairs(mw.text.split(args[value], "%s*,%s*")) do
				local term = v:gsub("i/j", "ij")
				local alt = mw.ustring.upper(v:gsub("#.*", ""):gsub("IJ", "IJ"):gsub("ij", "ij"):gsub("i/j", "ij"):gsub("[%p%s]", ""))
				local length = mw.ustring.len(alt)
				
				if data[length] then
					if data[length][alt] then
						error('It puzelwurd "' .. alt .. '" yn "' .. value .. '" is dûbeld.')
					else
						data[length][alt] = term
					end
				else
					if length == 0 then
						error('De parameter "' .. value .. '" befettet ûnjildige ynfier.')
					elseif length > 25 then
						error('It puzelwurd "' .. alt .. '" yn "' .. value .. '" is te lang.')
					end
					
					data[length] = {[alt] = term}
				end
			end
			
			for k, v in sparseIpairs(data) do
				if k ~= nil then
					local test = "row"
					local linkpfx = ", "
					local linksfx = ""
					local links = ""
					
					if size(data[k]) > 5 then
						test = "cols"
						linkpfx = "<dd>"
						linksfx = "</dd>"
					end
					
					local function sort(item1, item2)
						local sort1, sort2 = lang:makeSortKey(item1), lang:makeSortKey(item2)
						
						return sort1 < sort2
					end
					
					for k, v in sortedPairs(data[k], sort) do
						if mw.ustring.match(v, "#") then
							links = links .. linkpfx ..
									language_link({ term = v:gsub("#.*", ""), alt = k, lang = getByCode(v:gsub(".*#", "")) or lang }, false) ..
									linksfx
						else
							links = links .. linkpfx ..
									language_link({ term = v, alt = k, lang = lang2 }, false) ..
									linksfx
						end
					end
					
					local cases = {
						cols = ':</li></ul><div style="column-count:' .. count .. ';-moz-column-count:' .. count ..
								';-webkit-column-count:' .. count .. ';"><dl>' .. links .. '</dl></div>',
						default = ': ' .. links:gsub("^, ", "") .. '.</li></ul>',
					}
					
					if k == 1 then
						head2 = "1 letter"
					else
						head2 = k .. " letters"
					end
					
					table.insert(cat, head1 .. "oplossings fan " .. head2)
					table.insert(cat, "Fryske puzelwurden fan " .. head2)
					text = text .. '<div><ul><li>' .. head2 .. (cases[test] or cases["default"]) .. '</div>'
				end
			end
		end
	end
	
	return text .. format_categories(cat, lang)
end

return p