Jump to content

Module:TOC link

Permanently protected module
From Wikisource

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs

local function _toc_link(args)
	local current_title = mw.title.getCurrentTitle()
	
	local scan = args[1]
	if not scan then
		return require('Module:Error')['error']({message = '[[Module:TOC link]] error: First argument is mandatory'})
	end
	local fragment = 'pageindex_' .. scan
	
	local link_display = args[4] or args[3]
	if not link_display then
		return require('Module:Error')['error']({message = '[[Module:TOC link]] error: Third argument is mandatory'})
	end
	
	-- Page or Index
	local ns_lookups = {
		['page'] = true,
		['index'] = true,
		['page talk'] = true,
		['index talk'] = true
	}
	if current_title:inNamespaces(104, 105, 106, 107) or (args.demospace and ns_lookups[string.lower(args.demospace)]) then
		return '[[Page:' .. table.concat({current_title.rootText, scan}, '/') .. '|' .. link_display .. ']]'
	else
		return '[[' .. mw.title.makeTitle(current_title.nsText, table.concat({current_title.rootText, args[2]}, '/'), fragment).fullText .. '|' .. link_display .. ']]'
	end
end

function p.toc_link(frame)
	return _toc_link(getArgs(frame))
end

return p