Difference between revisions of "Module:Find sources/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Pppery
blackwiki>Pppery
(Remove all the /template subpages!)
Line 4: Line 4:
 
-- Define constants
 
-- Define constants
 
local ROOT_PAGE = 'Module:Find sources'
 
local ROOT_PAGE = 'Module:Find sources'
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/' -- for template config modules
 
 
local LINK_ROOT = ROOT_PAGE .. '/links/' -- for link config modules
 
local LINK_ROOT = ROOT_PAGE .. '/links/' -- for link config modules
 
local CONFIG_PAGE = ROOT_PAGE .. '/config' -- for global config
 
local CONFIG_PAGE = ROOT_PAGE .. '/config' -- for global config
Line 10: Line 9:
 
-- Load required modules
 
-- Load required modules
 
local checkType = require('libraryUtil').checkType
 
local checkType = require('libraryUtil').checkType
 +
local yesno = require('Module:yesno')
 
local cfg = mw.loadData(CONFIG_PAGE)
 
local cfg = mw.loadData(CONFIG_PAGE)
  
Line 69: Line 69:
 
end
 
end
  
function p._main(template, args)
+
function p._main(config, args)
 
-- The main access point from Lua.
 
-- The main access point from Lua.
checkType('_main', 1, template, 'string')
+
checkType('_main', 1, config, 'table', true)
 
checkType('_main', 2, args, 'table', true)
 
checkType('_main', 2, args, 'table', true)
args = args or {}
 
 
local title = mw.title.getCurrentTitle()
 
local title = mw.title.getCurrentTitle()
 
-- Get the template config.
 
local templateCfgPage = TEMPLATE_ROOT .. template
 
local templateCfg = maybeLoadData(templateCfgPage)
 
if not templateCfg then
 
error(string.format(
 
"invalid template name '%s'; no template config found at [[%s]]",
 
template, templateCfgPage
 
))
 
end
 
  
 
-- Namespace check.
 
-- Namespace check.
if not templateCfg.isUsedInMainspace and title.namespace == 0 then
+
if title.namespace == 0 and not yesno(config.mainspace) then
 
local formatString = '<strong class="error">%s</strong>'
 
local formatString = '<strong class="error">%s</strong>'
 
if cfg['namespace-error-category'] then
 
if cfg['namespace-error-category'] then
Line 123: Line 112:
  
 
-- Make the intro link
 
-- Make the intro link
local introLink
+
local output = config.introtext or ""
if templateCfg.introLink then
+
local introLink = config.introlink
local code = templateCfg.introLink.code
+
if introLink and #introLink ~= 0 then
 
local display = templateCfg.introLink.display or renderSearchString(
 
local display = templateCfg.introLink.display or renderSearchString(
 
searchTerms,
 
searchTerms,
 
'&nbsp;'
 
'&nbsp;'
 
)
 
)
introLink = p._renderLink(code, searchTerms, display)
+
output = output .. p._renderLink(code, searchTerms, renderSearchString(searchTerms, '&nbsp;'))
else
+
output = output .. (config.introseparator or "")
introLink = ''
 
 
end
 
end
 
 
-- Make the other links
 
-- Make the other links
 
local links = {}
 
local links = {}
local separator = templateCfg.separator or cfg['default-separator']
+
local separator = cfg['default-separator']
 
local sep = ''
 
local sep = ''
for i, t in ipairs(templateCfg.links) do
+
local linkno = 1
links[i] = sep .. p._renderLink(t.code, searchTerms, t.display) ..
+
while true do
(t.afterDisplay or '')
+
local linkArg = config["link"..tostring(linkno)]
sep = t.separator or separator
+
if linkArg == nil then
 +
break
 +
end
 +
links[linkno] = sep .. p._renderLink(linkArg, searchTerms, config["display"..tostring(linkno)]) ..
 +
config["after"..tostring(linkno)]
 +
sep = config["separator"..tostring(linkno)] or separator
 +
linkno = linkno + 1
 
end
 
end
links = table.concat(links)
+
output = output .. table.concat(links)
  
 
-- Make the blurb.
 
-- Make the blurb.
local blurb = substituteParams(templateCfg.blurb, introLink, links)
 
 
local span = mw.html.create('span')
 
local span = mw.html.create('span')
 
span
 
span
 
:addClass('plainlinks')
 
:addClass('plainlinks')
:addClass(templateCfg.class)
+
:wikitext(output)
:cssText(templateCfg.style)
 
:wikitext(blurb)
 
  
 
return tostring(span)
 
return tostring(span)
 
end
 
end
  
setmetatable(p, { __index = function(t, template)
+
function p.main(frame)  
-- The main access point from #invoke.
+
return p._main(frame, frame.args, frame:getParent().args)
-- Invocations will look like {{#invoke:Find sources|template name}},
+
end
-- where "template name" is a subpage of [[Module:Find sources/templates]].
 
local tname = template
 
if tname:sub(-8) == '/sandbox' then
 
-- This makes {{Find sources/sandbox|Albert Einstein}} work.
 
tname = tname:sub(1, -9)
 
end
 
return function(frame)
 
local args = require('Module:Arguments').getArgs(frame, {
 
wrappers = mw.site.namespaces[10].name .. ':' .. tname
 
})
 
return t._main(template, args)
 
end
 
end})
 
 
 
 
return p
 
return p

Revision as of 02:51, 15 February 2019

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

-- This module implements {{find sources}} and other similar templates, and
-- also provides a mechanism to easily create new source-finding templates.

-- Define constants
local ROOT_PAGE = 'Module:Find sources'
local LINK_ROOT = ROOT_PAGE .. '/links/' -- for link config modules
local CONFIG_PAGE = ROOT_PAGE .. '/config' -- for global config

-- Load required modules
local checkType = require('libraryUtil').checkType
local yesno = require('Module:yesno')
local cfg = mw.loadData(CONFIG_PAGE)

local p = {}

local function maybeLoadData(page)
	local success, data = pcall(mw.loadData, page)
	return success and data
end

local function substituteParams(msg, ...)
	return mw.message.newRawMessage(msg, ...):plain()
end

local function renderSearchString(searchTerms, separator, transformFunc)
	-- This takes a table of search terms and turns it into a search string
	-- that can be used in a URL or in a display value. The transformFunc
	-- parameter can be used to transform each search term in some way (for
	-- example, URL-encoding them).
	local searchStrings = {}
	for i, s in ipairs(searchTerms) do
		searchStrings[i] = s
	end
	if transformFunc then
		for i, s in ipairs(searchStrings) do
			searchStrings[i] = transformFunc(s)
		end
	end
	return table.concat(searchStrings, separator)
end

function p._renderLink(code, searchTerms, display)
	-- Renders the external link wikicode for one link, given the link code,
	-- a table of search terms, and an optional display value.

	-- Get link config.
	local linkCfg = maybeLoadData(LINK_ROOT .. code)
	if not linkCfg then
		error(string.format(
			"invalid link code '%s'; no link config found at [[%s]]",
			code,
			LINK_ROOT .. code
		))
	end

	-- Make URL.
	local url
	do
		local separator = linkCfg.separator or "+"
		local searchString = renderSearchString(
			searchTerms,
			separator,
			mw.uri.encode
		)
		url = substituteParams(linkCfg.url, searchString)
	end

	return string.format('[%s %s]', url, display or linkCfg.display)
end

function p._main(config, args)
	-- The main access point from Lua.
	checkType('_main', 1, config, 'table', true)
	checkType('_main', 2, args, 'table', true)
	local title = mw.title.getCurrentTitle()

	-- Namespace check.
	if title.namespace == 0 and not yesno(config.mainspace) then
		local formatString = '<strong class="error">%s</strong>'
		if cfg['namespace-error-category'] then
			formatString = formatString .. '[[%s:%s]]'
		end
		return string.format(
			formatString,
			cfg['namespace-error'],
			mw.site.namespaces[14].name,
			cfg['namespace-error-category']
		)
	end

	-- Get the search terms from the arguments.
	local searchTerms = {}
	for i, s in ipairs(args) do
		searchTerms[i] = s
	end
	if not searchTerms[1] then
		-- Use the current subpage name as the default search term. If the page
		-- uses a disambiguator like "Foo (bar)", make "Foo" the first term and
		-- "bar" the second.
		local term, dab = title.subpageText:match('^(.*) (%b())$')
		if dab then
			dab = dab:sub(2, -2) -- Remove parens
		end
		if term and dab then
			searchTerms[1] = term
			searchTerms[2] = dab
		else
			searchTerms[1] = title.subpageText
		end
	end
	searchTerms[1] = '"' .. searchTerms[1] .. '"'

	-- Make the intro link
	local output = config.introtext or ""
	local introLink = config.introlink
	if introLink and #introLink ~= 0 then
		local display = templateCfg.introLink.display or renderSearchString(
			searchTerms,
			'&nbsp;'
		)
		output = output .. p._renderLink(code, searchTerms, renderSearchString(searchTerms, '&nbsp;'))
		output = output .. (config.introseparator or "")
	end
	-- Make the other links
	local links = {}
	local separator = cfg['default-separator']
	local sep = ''
	local linkno = 1
	while true do
		local linkArg = config["link"..tostring(linkno)]
		if linkArg == nil then
			break
		end
		links[linkno] = sep .. p._renderLink(linkArg, searchTerms, config["display"..tostring(linkno)]) ..
			config["after"..tostring(linkno)]
		sep = config["separator"..tostring(linkno)] or separator
		linkno = linkno + 1
	end
	output = output .. table.concat(links)

	-- Make the blurb.
	local span = mw.html.create('span')
	span
		:addClass('plainlinks')
		:wikitext(output)

	return tostring(span)
end

function p.main(frame) 
	return p._main(frame, frame.args, frame:getParent().args)
end
return p