Module:Smartlink: Difference between revisions
From WWII Archives
(guard against script errors in case of whitespace-only or invalid titles as well (e.g. Template:Soutěž Československo 1948 - 1989/layout uses an unpiped parameter in this template’s parameter, which resulted in a script error on that very page)) |
Paul Sidle (talk | contribs) m (1 revision imported) |
||
(No difference)
|
Latest revision as of 17:57, 9 May 2023
Documentation for this module may be created at Module:Smartlink/doc
--[[ __ __ _ _ ____ _ _ _ _ | \/ | ___ __| |_ _| | ___ _/ ___| _ __ ___ __ _ _ __| |_| (_)_ __ | | __ | |\/| |/ _ \ / _` | | | | |/ _ (_)___ \| '_ ` _ \ / _` | '__| __| | | '_ \| |/ / | | | | (_) | (_| | |_| | | __/_ ___) | | | | | | (_| | | | |_| | | | | | < |_| |_|\___/ \__,_|\__,_|_|\___(_)____/|_| |_| |_|\__,_|_| \__|_|_|_| |_|_|\_\ Authors and maintainers: * User:Jarekt - original version ]] --[[ This function is the core part of the Smartlink template. Usage from a template: {{#invoke:Smartlink|Smartlink|1=|2= }} Parameters: frame.args[1] - base page name frame.args[2] - desired language (often user's native language) Error Handling: ]] local p = {} function p.Smartlink(frame) -- get base page and language fallback list local base = frame.args[1] if not mw.title.new(base) then -- missing, empty/whitespace-only or invalid title return '' end local lang = frame.args[2] if not lang or not mw.language.isSupportedLanguage(lang) then lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language end local langList = mw.language.getFallbacksFor(lang) table.insert(langList,1,lang) -- find base template language subpage local page = nil for _, language in ipairs(langList) do if mw.title.new(base .. '/' .. language).exists then page = base .. '/' .. language -- returns only the page return page end end return base end return p