Module:Find sources/sandbox: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(19 intermediate revisions by 4 users not shown)
Line 4:
-- Define constants
local ROOT_PAGE = 'Module:Find sources'
local LINK_ROOTTEMPLATE_ROOT = ROOT_PAGE .. '/linkstemplates/' -- for linktemplate config modules
local LINK_CONFIG = ROOT_PAGE .. '/links/sandbox' -- 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)
 
Line 40:
end
 
function p._renderLink(code, searchTerms, display, tooltip)
-- Renders the external link wikicode for one link, given the link code,
-- a table of search terms, and an optional display value and tooltip.
 
-- Get link config.
local linkCfglinks = maybeLoadData(LINK_ROOT .. codeLINK_CONFIG)
local linkCfg = links[code]
if not linkCfg then
error(string.format(
"invalid link code '%s'; no link config found at [[%s]]",
code,
LINK_CONFIG
LINK_ROOT .. code
))
end
Line 65 ⟶ 66:
url = substituteParams(linkCfg.url, searchString)
end
 
if tooltip then
return string.format('[%s %s]', url, display or linkCfg.display)
return string.format('<span title="%s" style="border-bottom: 1px dotted;">[%s %s]</span>',
mw.text.encode(tooltip), url, display or linkCfg.display)
else
return string.format('[%s %s]', url, display or linkCfg.display)
end
end
 
function p._main(configtemplate, args)
-- The main access point from Lua.
checkType('_main', 1, configtemplate, 'tablestring', true)
checkType('_main', 2, args, 'table', true)
args = args or {}
local title = mw.title.getCurrentTitle()
 
-- Get the template config.
local templateCfgPage = TEMPLATE_ROOT .. template .. '/sandbox'
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.
if not templateCfg.isUsedInMainspace and title.namespace == 0 and not yesno(config.mainspace) then
local formatString = '<strong class="error">%s</strong>'
if cfg['namespace-error-category'] then
Line 95 ⟶ 112:
end
if not searchTerms[1] then
-- Use the current subpage name as the default search term., Ifunless the page
-- another title is provided. If the page uses a disambiguator like
-- uses a disambiguator like "Foo (bar)", make "Foo" the first term and
-- "Foo (bar)", make "Foo" the first term and "bar" the second.
local term, dabsearchTitle = args.title or title.subpageText:match('^(.*) (%b())$')
local term, dab = searchTitle:match('^(.*) (%b())$')
if dab then
dab = dab:sub(2, -2) -- Remove parens
Line 106 ⟶ 124:
searchTerms[2] = dab
else
searchTerms[1] = title.subpageTextsearchTitle
end
end
Line 112 ⟶ 130:
 
-- Make the intro link
local linkno = 1introLink
local output = config.introtext or ""
localif templateCfg.introLink = config.introlinkthen
if local introLinkcode and= #templateCfg.introLink ~= 0 then.code
outputlocal display = output templateCfg.introLink.display p._renderLink(introLink, searchTerms,or renderSearchString(searchTerms, '&nbsp;'))
searchTerms,
output = output .. (config.introseparator or "")
'&nbsp;'
)
local tooltip = templateCfg.introLink.tooltip
links[linkno]introLink = sep .. p._renderLink(linkArgcode, searchTerms, config["display"..tostring(linkno, tooltip)]) ..
else
introLink = ''
end
 
-- Make the other links
local links = {}
local separator = templateCfg.separator or cfg['default-separator']
local sep = ''
for i, t in ipairs(templateCfg.links) do
local linkno = 1
links[i] = sep .. p._renderLink(t.code, searchTerms, t.display, t.tooltip) ..
while true do
(t.afterDisplay or '')
local linkArg = config["link"..tostring(linkno)]
sep = config["t.separator"..tostring(linkno)] or separator
if linkArg == nil then
break
end
links[linkno] = sep .. p._renderLink(linkArg, searchTerms, config["display"..tostring(linkno)]) ..
(config["after"..tostring(linkno)] or "")
sep = config["separator"..tostring(linkno)] or separator
linkno = linkno + 1
end
outputlinks = output .. table.concat(links)
 
-- Make the blurb.
local blurb = substituteParams(templateCfg.blurb, introLink, links)
local span = mw.html.create('span')
span
:addClass('plainlinks')
:addClass(templateCfg.class)
:wikitext(output)
:cssText(templateCfg.style)
:wikitext(outputblurb)
 
return tostring(span)
end
 
setmetatable(p, { __index = function(t, template)
function p.main(frame)
-- The main access point from #invoke.
return p._main(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 p.main(frame)
local yesnoargs = require('Module:yesnoArguments').getArgs(frame, {
wrappers = mw.site.namespaces[10].name .. ':' .. tname
})
return pt._main(frame.argstemplate, frame:getParent().args)
end
end})
 
return p