Content deleted Content added
Remove all the /template subpages! |
No edit summary |
||
(25 intermediate revisions by 5 users not shown) | |||
Line 4:
-- Define constants
local ROOT_PAGE = 'Module:Find sources'
local
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
local linkCfg = links[code]
if not linkCfg then
error(string.format(
"invalid link code '%s'; no link config found at [[%s]]",
code,
LINK_CONFIG
))
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
end▼
end
function p._main(
-- The main access point from Lua.
checkType('_main', 1,
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
))
-- Namespace check.
if not templateCfg.isUsedInMainspace and title.namespace == 0
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
-- another title is provided. If the page uses a disambiguator like
-- "Foo (bar)", make "Foo" the first term and "bar" the second.
local
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] =
end
end
Line 112 ⟶ 130:
-- Make the intro link
local display = templateCfg.introLink.display or renderSearchString(
searchTerms,
' '
)
local tooltip = templateCfg.introLink.tooltip
output = output .. p._renderLink(code, searchTerms, renderSearchString(searchTerms, ' '))▼
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
▲
(t.afterDisplay or '')
▲ end
▲ links[linkno] = sep .. p._renderLink(linkArg, searchTerms, config["display"..tostring(linkno)]) ..
▲ sep = config["separator"..tostring(linkno)] or separator
end
-- 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)
return tostring(span)
end
setmetatable(p, { __index = function(t, template)
function p.main(frame) ▼
-- The main access point from #invoke.
-- 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
wrappers = mw.site.namespaces[10].name .. ':' .. tname
})
return t._main(template, args)
end
end})
return p
|