模組:TemplateExist
外观
local p = {}
function p.getTemplateTitle(input_data)
local template_name = input_data
local is_lua = true
if type(input_data) == type({}) and type(input_data.args) == type({}) then
template_name = input_data.args[1] or input_data.args['1'] or ''
end
local function _return(_value)
if is_lua then return _value
else return _value and tostring(_value) or '' end
end
template_name = mw.text.trim(template_name or '')
if template_name == '' then return _return() end
local success, template_obj = pcall(mw.title.new, template_name, "Template")
if not success then return _return() end
local page_name = tostring(template_obj)
local template_title = template_obj.redirectTarget or template_obj
local redircct_name = tostring(template_title)
if type(template_title.getContent) ~= type(tostring)then return _return() end
if (template_title:getContent() or ''):len() > 0 then
return _return(template_title)
end
local lib_zhcvt = require('Module:ZhConversion') --繁簡轉換
for _, check_title in ipairs({
lib_zhcvt.to_hant(page_name), lib_zhcvt.to_hans(page_name),
lib_zhcvt.to_hant(redircct_name), lib_zhcvt.to_hans(redircct_name),
}) do
local _success, check_page = pcall(mw.title.new, check_title, "Template")
if _success then
check_page = check_page.redirectTarget or check_page
return _return(check_page)
end
end
return _return()
end
return p