Dlaždice

Z Wiki JU
Verze z 12. 11. 2026, 07:21, kterou vytvořil Ofara (diskuse | příspěvky) (založena nová stránka s textem „-- Module:Dlaždice -- Zjistí, pro koho je cílová stránka zamčená: najde v jejím wikitextu všechny -- kategorie končící na " (skryté)" a vrátí hotový odznak (zámek + názvy skupin). -- Použití: {{#invoke:Dlaždice|odznak|odkaz=Útvar personální/K nástupu}} local p = {} local SUFFIX = ' (skryté)' -- Odstraní části wikitextu, ve kterých se kategorie nepočítají. local function stripIgnored(text) text = text:gsub('<!%-%-.-%…“)
(rozdíl) ← Starší verze | zobrazit aktuální verzi (rozdíl) | Novější verze → (rozdíl)

Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Dlaždice/Dokumentace

-- Module:Dlaždice
-- Zjistí, pro koho je cílová stránka zamčená: najde v jejím wikitextu všechny
-- kategorie končící na " (skryté)" a vrátí hotový odznak (zámek + názvy skupin).
-- Použití: {{#invoke:Dlaždice|odznak|odkaz=Útvar personální/K nástupu}}
local p = {}

local SUFFIX = ' (skryté)'

-- Odstraní části wikitextu, ve kterých se kategorie nepočítají.
local function stripIgnored(text)
	text = text:gsub('<!%-%-.-%-%->', '')
	text = text:gsub('<[nN][oO][wW][iI][kK][iI]>.-</[nN][oO][wW][iI][kK][iI]>', '')
	text = text:gsub('<[pP][rR][eE][^>]*>.-</[pP][rR][eE]>', '')
	return text
end

-- Vrátí seznam skupin (názvy kategorií bez přípony) v pořadí výskytu, bez duplicit.
local function groupsOf(text)
	local groups, seen = {}, {}
	local suffixLen = mw.ustring.len(SUFFIX)
	for prefix, name in stripIgnored(text):gmatch('%[%[%s*([^%]|:]+):%s*([^%]|]-)%s*[|%]]') do
		-- jen odkazy s prefixem jmenného prostoru Kategorie / Category
		local ns = mw.ustring.lower(mw.text.trim(prefix))
		if ns == 'kategorie' or ns == 'category' then
			name = mw.text.trim((name:gsub('_', ' ')))
			if mw.ustring.sub(name, -suffixLen) == SUFFIX then
				local group = mw.text.trim(mw.ustring.sub(name, 1, mw.ustring.len(name) - suffixLen))
				if group ~= '' and not seen[group] then
					seen[group] = true
					table.insert(groups, group)
				end
			end
		end
	end
	return groups
end

function p.odznak(frame)
	local args = frame.args
	local target = mw.text.trim(args.odkaz or '')
	-- externí odkazy a prázdný odkaz neřešíme
	if target == '' or mw.ustring.lower(mw.ustring.sub(target, 1, 4)) == 'http' then
		return ''
	end

	local title = mw.title.new(target)
	if not title or not title.exists then
		return ''
	end
	-- jedno přesměrování následujeme
	if title.isRedirect and title.redirectTarget then
		title = title.redirectTarget
	end
	local content = title:getContent()
	if not content then
		return ''
	end

	local groups = groupsOf(content)
	if #groups == 0 then
		return ''
	end

	return '<span class="dlazdice-odznak dlazdice-odznak-zamek" title="Přístup jen pro: '
		.. mw.text.encode(table.concat(groups, ', ')) .. '">🔒 '
		.. mw.text.encode(table.concat(groups, ', ')) .. '</span>'
end

return p