Module:Find sources

From Zoophilia Wiki
Revision as of 04:29, 26 September 2014 by meta>Mr. Stradivarius (start work on a replacement for Template:Find sources multi)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Documentation for this module may be created at Module:Find sources/doc

-- 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