Jump to content

Module:Find sources

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 04:29, 26 September 2014 (start work on a replacement for Template:Find sources multi). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module implements {{find sources}} and other similar templates, and also
-- provides a mechanism to easily create new source-finding templates.
--
-- Template settings are found in subpages of [[Module:Find sources/templates]].
-- Link functions are defined in subpages of [[Module:Find sources/links]].
-- Functions shared between the link modules are stored at
-- [[Module:Find sources/shared]].

local ROOT_PAGE = 'Module:Find sources'
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/'
local LINK_ROOT = ROOT_PAGE .. '/links/'

local p = {}

local function maybeRequire(page)
	local success, module = pcall(require, page)
	if success then
		return module
	else
		return nil
	end
end

local function maybeLoadData(page)
	local success, data = pcall(mw.loadData, page)
	if success then
		return data
	else
		return nil
	end
end

function p.main(template, args)
	local templateCfg = maybeLoadData(TEMPLATE_ROOT .. template)
	if not templateCfg then
		error(string.format(
			"invalid template name '%s'; no template config found at [[%s]]'",
			template,
			TEMPLATE_ROOT .. template
		))
	end
end

setmetatable(p, { __index = function(t, template)
	return function(frame)
		local args = require('Module:Arguments').getArgs(frame, {
			wrappers = mw.site.namespaces[10].name .. ':' .. template
		})
		return t.main(template, args)
	end
end })

return p