Modul:Transclude

Z Wikipedie, otevřené encyklopedie

Transclude může být použit k vložení jedné konkrétní sekce z vybrané stránky. Zatím je povoleno použití pouze mimo hlavní jmenný prostor, pro použití v hlavním jmenném prostoru je třeba získat konsensus pod lípou.

Použití[editovat zdroj]

{{#invoke:Transclude|section|<název stránky>|<název sekce>}}

Alternativně funguje také rozšíření MediaWiki, které umožňuje zobrazit jakoukoliv část stránky, nejenom sekci. Ta ale musí být označená pomocí speciálních značek <section>.

{{#section:<název stránky>|<název sekce>}}
local p = {}

function p.section(context)
	if mw.title.new(mw.getCurrentFrame():getParent():getTitle()).namespace == 0 then
		error("Modul Transclude zatím není možné použít v hlavním jmenném prostoru. Pro použití tam je třeba získat konsensus pod lípou.")
	end
	local a = require('Modul:Arguments')
	local parameters = a.getArgs(context)
	-- Get (and parse) a title and a section from the input argument(s)
	local title = ""
	local section = ""
	if parameters[2] then
		title = parameters[1]
		section = parameters[2]
	elseif parameters[1] then
		local link = parameters[1]
		title = mw.text.split(link, "#")[1]
		section = mw.text.split(link, "#")[2]
	else
		error("Žádná stránka ani sekce nebyla zadána")
	end
	if not title then
		error("Žádná stránka nebyla zadána")
	elseif not section then
		error("Žádná sekce nebyla zadána")
	end
	-- Find a page and get its content if any
	local page = mw.title.new(title)
	local text = page:getContent()
	if not text then
		error("Zadaná stránka nebyla nalezena")
	end
	-- Get a position of the section in the page
	local heading = nil
	local content = nil
	local level = ""
	heading, content, level = string.find(text, "\n(==+) *" .. section .. " *%1 *\n")
	if not heading then
		error("Zadaná sekce nebyla nalezena")
	end
	-- Get a position of a next section if any
	local next_heading = string.find(text, "\n" .. level .. " *[^\n=]* *" .. level .. " *\n", content)
	-- Get a section text, trim it and parse it
	text = string.sub(text, parameters['heading'] == "no" and content or heading, next_heading or string.len(text))
	text = mw.text.trim(text)
	text = mw.getCurrentFrame():preprocess(text)
	-- Print output
	return text
end

return p