Jump to content

Module:Multi-section link/sandbox

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 172.97.141.219 (talk) at 10:00, 9 December 2024 (copied from https://en.wikipedia.org/enwiki/w/index.php?title=Module:Section_link&oldid=1226909451). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module creates a section link with multiple section names.

local p = {}

local function normalizePageName(page)
	local title = mw.title.new(page)
	if not title then
		error(string.format("'%s' is not a valid page name", page), 3)
	elseif title.namespace == 6 or title.namespace == 14 then
		return ':' .. title.prefixedText
	else
		return title.prefixedText
	end
end

function p._main(args)
	local displayParts = {}
	for i, v in ipairs(args) do
		displayParts[i] = v
	end
	if not displayParts[1] then
		displayParts[1] = ""
	else
		displayParts[1] = normalizePageName(displayParts[1])
	end
	local nParts = #displayParts
	if nParts == 1 then
		return string.format('[[%s]]', displayParts[1])
	else
		local display = {}
		for i, s in ipairs(displayParts) do
			table.insert(display, s)
			if i ~= nParts then
				table.insert(display, ' ')
				table.insert(display, string.rep('§', i))
				table.insert(display, ' ')
			end
		end
		display = table.concat(display)
		local page = displayParts[1]
		local fragment = displayParts[nParts]
		return string.format('[[%s#%s|%s]]', page, fragment, display)
	end
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Multi-section link',
		valueFunc = function (key, value)
			value = value:match('^%s*(.-)%s*$') -- Trim whitespace
			-- Allow blank first parameters, as the wikitext template does this.
			if value ~= '' or key == 1 then
				return value
			end
		end
	})
	return p._main(args)
end

return p