Modul:Viz

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

Modul Viz je užíván šablonou: Šablona:Viz. V šabloně je bližší nápověda; na diskuzní stránce šablony je odpovídající diskuze. K modulu bylo hlasování: Wikislovník:Hlasování/Zavedení modulových seznamů pro šablonu Viz.


local p = {}

local replacementTable = mw.loadData( 'Modul:Viz/Replacements' )

local function getArgNums(pargs, prefix)
    -- Returns an ordered table containing the numbers (>0) of the arguments
    -- that exist for the specified prefix. Includes 0 if prefix without number
    -- is used as argument. For example, if the prefix was 'seznam', and
    -- 'seznam1', 'seznam', and 'seznam5' exist, it would return {0, 1, 5}.
    local nums = { }
    if pargs[prefix] then table.insert(nums, 0) end
    for k, v in pairs(pargs) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end

local function canonicalizeTitle(title)
	local replacementRegEx = ""
	local regexBuilder = {}
	local idx = 0
	for key, _ in pairs( replacementTable ) do
		idx = idx + 1
		regexBuilder[idx] = (key == '-' or key == '%' or key == '^') and ('%' .. key) or key
	end
	replacementRegEx = "[" .. table.concat( regexBuilder ) .. "]"
	return mw.ustring.gsub( mw.ustring.lower( title ), replacementRegEx, replacementTable )
end

function p.viz(frame)
	pargs = frame:getParent().args
	local title = mw.title.getCurrentTitle()
	local items = {}
	local result = ""
	local listnums = getArgNums(pargs, 'seznam')
	if not (pargs[1] or listnums[1]) then		-- no params, guess the list name
		table.insert( listnums, 0 )
		result = result .. "[[Kategorie:Monitoring:Šablona Viz bez parametrů]]"
	end

	for k, num in ipairs( listnums ) do 		--include lists
		local adr = "Module:Viz/"
		if num == 0 then						--unnumberd guessed list
			if pargs["seznam"] then				--unnumberd
				adr = adr .. pargs["seznam"]
			else								--guess
				adr = adr .. canonicalizeTitle( title.text )
			end
		else
			adr = adr .. pargs["seznam" .. tostring( num )]
		end

		local exists, list = pcall(require, adr)	--read items from existing list
		if exists then
			for i, item in ipairs( list ) do		--and add them
				if item ~= title.fullText and item ~= "" then		--if not empty or identical with the page
					s = "[[" .. item .. "]]"
					for j, link in ipairs( items ) do	--or already included
						if s == link then
							s = nil
							result = result .. "[[Kategorie:Monitoring:Šablona Viz obsahuje duplicity]]"
						end
					end
					table.insert(items, s)
				end
			end
			result = result .. "[[Kategorie:Monitoring:Viz/" .. #list .. "]]"
		else
			result = result .. "[[Kategorie:Monitoring:Šablona Viz odkazuje na neexistující seznam]]"
		end
 	end

	for i, arg in ipairs(pargs) do		--include single items
		if arg ~= title.fullText then
			s = "[[" .. arg .. "]]"
			for j, link in ipairs( items ) do
				if s == link then
					s = nil
					result = result .. "[[Kategorie:Monitoring:Šablona Viz obsahuje duplicity]]"
				end
			end
			table.insert(items, s)
		else
			result = result .. "[[Kategorie:Monitoring:Šablona Viz odkazuje na vlastní stránku]]"
		end
	end

	if #items == 0 then
		result = result .. "[[Kategorie:Monitoring:Šablona Viz bez výstupu]]"
	else
		result = result .. "''Možná hledáte'' " .. mw.text.listToText( items, ", ", " ''nebo'' ")
		if result:sub(-3,-3) ~= "." then result = result .. "." end
	end
	return result
end
 
return p