Module:Find sources/sandbox: Difference between revisions

Content deleted Content added
fix test config for real this time
Tag: Reverted
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 5:
local ROOT_PAGE = 'Module:Find sources'
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/' -- for template config modules
local LINK_ROOTLINK_CONFIG = ROOT_PAGE .. '/links/sandbox' -- for link config modules
local CONFIG_PAGE = ROOT_PAGE .. '/config' -- for global config
 
Line 40:
end
 
function p._renderLink(code, searchTerms, display, tooltip)
local function getTemplates(title)
-- This returns a set containing all templates used on a page, including
-- duplicates and commented out transclusions, and following redirects.
if title == nil then return {} end
local content = title:getContent()
if content == nil then return {} end
local templates = {}
for t in mw.ustring.gmatch(content, '{{%s*([^|}]-)%s*[|}]') do
local temp = mw.title.new(t, 'Template')
temp = (temp and temp.redirectTarget) or temp
if temp then
templates[temp.prefixedText] = true
end
end
return templates
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 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 82 ⟶ 66:
url = substituteParams(linkCfg.url, searchString)
end
 
if temptooltip 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
 
Line 92 ⟶ 81:
args = args or {}
local title = mw.title.getCurrentTitle()
local searchTitle = args.title or title.subpageText
 
-- Get the template config.
local templateCfgPage = TEMPLATE_ROOT .. template .. '/sandbox'
if template == 'autoselect' then
-- MOVE THIS TO /config
local autoselectDefault = 'Find sources'
local autoselect = {
{
template = 'Find sources video games',
all = {'Template:WikiProject Video games'},
none = {'Template:WikiProject Biography'}
}
}
-- UNCOMMENT WHEN ABOVE MOVED TO /config
-- local autoselect = cfg['template-autoselect']
-- local autoselectDefault = cfg['template-autoselect-default']
local templates = getTemplates(mw.title.new(searchTitle).talkPageTitle)
for i = 1, #autoselect do
-- Select the first template for which all templates in all and no
-- templates in none are transclued
local match = true
if autoselect[i].all then
for _, v in pairs(autoselect[i].all) do
if not templates[v] then
match = false
break
end
end
end
if autoselect[i].none then
for _, v in pairs(autoselect[i].none) do
if templates[v] then
match = false
break
end
end
end
if match then
template = autoselect[i].template
break
end
end
if template == 'autoselect' then
-- if we haven't found a template use the default
template = autoselectDefault
end
end
local templateCfgPage = TEMPLATE_ROOT .. template
local templateCfg = maybeLoadData(templateCfgPage)
if not templateCfg then
Line 174 ⟶ 115:
-- another title is provided. If the page uses a disambiguator like
-- "Foo (bar)", make "Foo" the first term and "bar" the second.
local searchTitle = args.title or title.subpageText
local term, dab = searchTitle:match('^(.*) (%b())$')
if dab then
Line 195 ⟶ 137:
'&nbsp;'
)
local tooltip = templateCfg.introLink.tooltip
introLink = p._renderLink(code, searchTerms, display, tooltip)
else
introLink = ''
Line 205 ⟶ 148:
local sep = ''
for i, t in ipairs(templateCfg.links) do
links[i] = sep .. p._renderLink(t.code, searchTerms, t.display, t.tooltip) ..
(t.afterDisplay or '')
sep = t.separator or separator
Line 234 ⟶ 177:
return function(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = mw.site.namespaces[10].name .. ':' .. tname,
-- Force getting of args from parent if template = autoselect
frameOnly = template ~= 'autoselect'
})
return t._main(template, args)