Moduł:Przypisy

Z PrePedia
Skocz do: nawigacja, szukaj

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Przypisy/opis

return {
	
["Przypisy"] = function(frame)
	--mw.logObject(frame:getParent():getTitle(), "parent:title")
	
	local result = {}
	
	local patternX = frame.args["wzór"]
	local pattern0 = frame.args["wzór0"] or patternX
	local section = frame.args["nagłówek"]
	local category = frame.args["kategoria"]
	local defaultGroup = frame.args["grupa"]

	local hasIgnoredFields = false

	-- grupa  przekazywana w polu o pustej nazwie
	-- aby docelowo zwolnić wszystkie nazwy na przypisy
	local group = frame:getParent().args[""]
	if group and string.match(group, "^=*$") then
		-- z wyjątkiem błednych wywołań w stylu "=="
		hasIgnoredFields = true
		group = nil
	end
	
	-- alternatywnie do czasu przebotowania z pola o nazwie "grupa"
	if not group then
		group = frame:getParent().args["grupa"]
		if group and (#group == 0) then
			group = nil
		end
	end
	
	local function noArgs()
		for k, v in pairs(frame:getParent().args) do
			return false
		end
		
		return true
	end

	local function missingHeader(pattern)
		return
			not mw.title.getCurrentTitle().isTalkPage and
			not group -- jawnie przekazana grupa musi wyłączać nagłówek
			and pattern -- wymagany wzór na wyszukiwanie wywołania z nagłówkiem
			and section -- wymagany nagłówek do wstawienia
			and not mw.ustring.find(mw.title.getCurrentTitle():getContent(), pattern)
	end
	
	local pattern = noArgs() and pattern0 or patternX
	--mw.logObject(pattern, "pattern")
	if missingHeader(pattern) then
		table.insert(result, section)
		table.insert(result, "\n")
	end
	
	table.insert(result, '<div class="do-not-make-smaller refsection')
	if group or defaultGroup then
		local groupClass, _ = (group or defaultGroup) == "uwaga" and "uwagi ll-script ll-script-uwagi" or string.gsub(group or defaultGroup, ' ', '_')
		table.insert(result, " refsection-")
		table.insert(result, groupClass)
	end
	table.insert(result, '">')
	
	-- to jest lista powszechnie występujących pól
	-- w wywołaniach szablonów przypisów i/lub uwag
	local exceptions = {
		"", -- to jest docelowo jedyne wyjątkowe pole
		"colwidth", -- kopia z enwiki?
		"group", -- kopia z enwiki?
		"grupa",
		"poziom",
		"przypis",
		"przypisy",
		"Przypisy",
		"refs", -- kopia z enwiki?
		"status",
		"stopien",
		"stopień",
		"Stopień",
		"stopnień",
		"uwagi",
	}
	local ignore = {}
	for i, v in ipairs(exceptions) do
		ignore[v] = true
	end
	
	local contents = {}
	for k, v in pairs(frame:getParent().args) do
		if (type(k) == "number") and (k == 1) then
			--mw.logObject( {k, v}, "pole {{{1}}} puste lub z przypisami w stylu <ref name=...>")
			table.insert(contents, v)
		elseif ignore[k] then
		-- !!! TO WYŁĄCZĘ PO PRZEBOTOWANIU !!!
			hasIgnoredFields = k ~= ""
			-- pole ignorowane
			--mw.logObject( {k, v}, "ignorowane pole z potencjalnymi przypisami w stylu <ref name=...>")
			table.insert(contents, v)
		elseif (type(k) == "string") then
			-- pole podanej nazwie tłumaczymy na przypis o takiej samej nazwie
			local ref = frame:extensionTag('ref', v, {name=k, group=group or defaultGroup})
			--mw.logObject( {k, v, ref}, "indywidualny przypis")
			table.insert(contents, ref)
		else
			-- pole indeksowane
			--mw.logObject( {k, v}, "indeksowane pole z potencjalnymi przypisami w stylu <ref name=...>")
			table.insert(contents, v)
			hasIgnoredFields = true
		end
	end
	
	local references = frame:extensionTag('references', table.concat(contents), {group=group or defaultGroup, responsive="1"})
	
	table.insert(result, references)
	table.insert(result, '</div>')
	if hasIgnoredFields and category then
		--mw.logObject({hasIgnoredFields, category}, "hasIgnoredFields and category")
		table.insert(result, category)
	end
	
	--mw.logObject(table.concat(result), "result")
	return table.concat(result)
end,
}