删除的内容 添加的内容
fix: 去除 Help:替换引用 链接
覆盖为 zhwiki 当前版(修复空参时 {{tlg}} 显示 bug)
 
第1行:
-- This implements Template:Tlg
--[=[
Module:Tlg
改自 https://en.wikipedia.org/w/index.php?title=Module:Tlg&oldid=952327088
{{Tl}}系列模板的模块化
]=]
local getArgs = require('Module:Arguments').getArgs
local p = {}
local mError = require('Module:Error')
local yesno = require('Module:Yesno')
 
-- Is a string non-empty?
local function isempty (s)
local function _ne(s)
return s ~= nil and s ~= ""
end
 
local nowikinw = mw.text.nowiki
 
local function transcludeaddTemplate(s)
local titlei, _ = mw.title.new(s:find(':', 1, true)
if noti title== nil then
return 'Template:' .. s
mw.addWarning(mError.error{[1]="[[Module:Tlg]]错误:无效的页面名<code>" .. nowiki(s) .. '</code>。'})
end
return nowiki(s)
local ns = s:sub(1, i - 1)
elseif title:match("^%:") then
if ns == '' or mw.site.namespaces[ns] then
return tostring(title)
return s
elseif title.namespace == 0 then
return 'Template:' .. tostring(title)
else
return tostring(title)'Template:' .. s
end
end
 
local function p._gettemplatelinktrimTemplate(title, as)
local needle = 'template:'
if not title then
if s:sub(1, needle:len()):lower() == needle then
return '{{{1}}}'
return s:sub(needle:len() + 1)
else
return s
end
return '[[' .. transclude(title) .. '|' .. (a or title) .. ']]'
end
 
local function linkTitle(args)
p._magicword_title = {
if _ne(args.nolink) then
-- [[Help:解析器函数]]
return args['1']
["#expr"] = 'Help:解析器函数#expr',
["#if"] = 'Help:解析器函数#if',
["#ifeq"] = 'Help:解析器函数#ifeq',
["#ifexist"] = 'Help:解析器函数#ifexist',
["#ifexpr"] = 'Help:解析器函数#ifexpr',
["#switch"] = 'Help:解析器函数#switch',
["#babel"] = 'Help:解析器函数#babel',
["#time"] = 'Help:解析器函数#time',
-- [[Help:魔术字]]
["REVISIONDAY"] = "Help:魔术字#REVISIONDAY",
["REVISIONDAY2"] = "Help:魔术字#REVISIONDAY2",
["REVISIONMONTH"] = "Help:魔术字#REVISIONMONTH",
["REVISIONTIMESTAMP"] = "Help:魔术字#REVISIONTIMESTAMP",
["REVISIONUSER"] = "Help:魔术字#REVISIONUSER",
["PAGESIZE"] = "Help:魔术字#PAGESIZE",
["PROTECTIONLEVEL"] = "Help:魔术字#PROTECTIONLEVEL",
["DISPLAYTITLE"] = "Help:魔术字#DISPLAYTITLE",
["DEFAULTSORT"] = "Help:魔术字#DEFAULTSORT",
["PAGESINCAT"] = "Help:魔术字#PAGESINCAT",
["PAGESINCATEGORY"] = "Help:魔术字#PAGESINCATEGORY",
["NUMBERINGROUP"] = "Help:魔术字#NUMBERINGROUP",
["PAGESINNS"] = "Help:魔术字#PAGESINNS",
["PAGESINNAMESPACE"] = "Help:魔术字#PAGESINNAMESPACE",
["NUMBEROFCONTRIBS"] = "Help:魔术字#NUMBEROFCONTRIBS",
["FULLPAGENAME"] = "Help:魔术字#FULLPAGENAME",
["PAGENAME"] = "Help:魔术字#PAGENAME",
["BASEPAGENAME"] = "Help:魔术字#BASEPAGENAME",
["SUBPAGENAME"] = "Help:魔术字#SUBPAGENAME",
["SUBJECTPAGENAME"] = "Help:魔术字#SUBJECTPAGENAME",
["TALKPAGENAME"] = "Help:魔术字#TALKPAGENAME",
["NAMESPACE"] = "Help:魔术字#NAMESPACE",
["SUBJECTSPACE"] = "Help:魔术字#SUBJECTSPACE",
["ARTICLESPACE"] = "Help:魔术字#ARTICLESPACE",
["localurl"] = "Help:魔术字#localurl",
["fullurl"] = "Help:魔术字#fullurl",
["filepath"] = "Help:魔术字#filepath",
["urlencode"] = "Help:魔术字#urlencode",
["anchorencode"] = "Help:魔术字#anchorencode",
["lc"] = "Help:魔术字#lc",
["lcfirst"] = "Help:魔术字#lcfirst",
["uc"] = "Help:魔术字#uc",
["ucfirst"] = "Help:魔术字#ucfirst",
["formatnum"] = "Help:魔术字#formatnum",
["#dateformat"] = "Help:魔术字##dateformat",
["#formatdate"] = "Help:魔术字##formatdate",
["padleft"] = "Help:魔术字#padleft",
["padright"] = "Help:魔术字#padright",
["plural"] = "Help:魔术字#plural",
["int"] = "Help:魔术字#int",
["#language"] = "Help:魔术字##language",
["#special"] = "Help:魔术字##special",
["#tag"] = "Help:魔术字##tag",
["gender"] = "Help:魔术字#gender",
["groupconvert"] = "Help:魔术字#groupconvert",
["PAGEID"] = "Help:魔术字#PAGEID",
["noexternallanglinks"] = "Help:魔术字#noexternallanglinks",
}
 
function p._getmagicwordlink(wt, i)
if not wt then
return '{{{1}}}:{{{2}}}'
end
local s = p.magicword_title[wt]
local titleObj
if wt == "#invoke" then
local titlePart = '[['
return '#invoke:[[' .. transclude('Module:' .. i) .. '|' .. i .. ']]'
elseifif sargs['1'] then
-- This handles :Page and other NS
if i then
titleObj = mw.title.new(args['1'], 'Template')
return '[[' .. s .. '|' .. wt .. ']]:' .. i
else
return '[[' .. s .. '|' .. wt .. ']]'
end
else
titleObj = mw.title.getCurrentTitle()
return s .. (i and ':' .. i)
end
end
titlePart = titlePart .. (titleObj ~= nil and titleObj.fullText or
 
function p._main addTemplate(args['1']))
-- 设定
local nowraptextPart = yesno(args.nowrap) or falsealttext
if not _ne(textPart) then
local code = yesno(args.code) or yesno(args.tt) or false
if titleObj ~= nil then
local kbd = yesno(args.kbd) or false
textPart = titleObj:inNamespace("Template") and args['1'] or titleObj.fullText
local nowrapname = yesno(args.nowrapname) or false
local braceinside = yesno(args.braceinside) or false
local subst = yesno(args.subst) or false
local nosubstlink = yesno(args.nosubstlink) or false
local bold = yesno(args.bold) or yesno(args.boldlink) or yesno(args.boldname) or false
local nolink = yesno(args.nolink) or false
local italic = yesno(args.italic) or yesno(args.italics) or false
local alttext = isempty(args.alttext) or nil
local debug = yesno(args.debug) or false
-- 构建{{tl}}
local wt = '&#123'
if braceinside then
if alttext then
alttext = '&#123;' .. alttext .. '&#125;'
else
-- redlink
alttext = '&#123;' .. title .. '&#125;'
textPart = args['1']
end
wt = wt .. p._gettemplatelink(title, alttext)
else
wt = wt .. '&#123;'
if subst then
if nolink and nosubstlink then
wt = wt .. 'subst:'
else
wt = wt .. 'subst:'
end
end
local t = ''
if nolink then
t = alttext or title
else
t = p._gettemplatelink(title, alttext)
end
if bold then
t = '<span style="font-weight:bold;">' .. t .. '</span>'
end
if nowrapname then
t = '<span class="nowrap">' .. t .. '</span>'
end
wt = wt .. t
local i = 1
local val
local vals = ''
while i < 11 do
val = args[i]
if val ~= nil then
val = mw.text.unstripNoWiki(val)
else
break
end
vals = vals .. '&#124;' .. val
i = i + 1
end
if args[12] then
vals = vals .. '&#124;...'
end
if vals ~= '' and italic then
vals = '<span style="font-style:italic;">' .. vals .. '</span>'
end
wt = wt .. vals
wt = wt .. '&#125;'
end
wt = wt .. '&#125;'
if _ne(args.subst) then
 
-- HACK: the ns thing above is probably broken
if kbd then wt = '<kbd>' .. wt .. '</kbd>' end
textPart = 'subst:' .. textPart
if code then
wt = '<code>' .. wt .. '</code>'
end
if _ne(args.brace) then
if nowrap then wt = '<span class="nowrap">' .. wt .. '</span>' end
textPart = nw('{{') .. textPart .. nw('}}')
elseif _ne(args.braceinside) then
textPart = nw('{') .. textPart .. nw('}')
end
if debug then wttitlePart = wttitlePart .. '\n<pre>|' .. mw.text.encode(mw.dumpObject(args))textPart .. '</pre>]]' end
if _ne(args.braceinside) then
return wt
titlePart = nw('{') .. titlePart .. nw('}')
end
return titlePart
end
 
第204行 ⟶ 第83行:
})
return p._main(args)
end
 
function p._main(args)
local bold = _ne(args.bold) or _ne(args.boldlink) or _ne(args.boldname)
local italic = _ne(args.italic) or _ne(args.italics)
local dontBrace = _ne(args.brace) or _ne(args.braceinside)
local code = _ne(args.code) or _ne(args.tt)
local show_result = _ne(args._show_result)
local expand = _ne(args._expand)
-- Build the link part
local titlePart = linkTitle(args)
if bold then titlePart = "'''" .. titlePart .. "'''" end
if _ne(args.nowrapname) then titlePart = '<span class="nowrap">' .. titlePart .. '</span>' end
-- Build the arguments
local textPart = ""
local textPartBuffer = "&#124;"
local codeArguments = {}
local codeArgumentsString = ""
local i = 2
local j = 1
while args[i] do
local val = args[i]
if val ~= "" then
if _ne(args.nowiki) then
-- Unstrip nowiki tags first because calling nw on something that already contains nowiki tags will
-- mangle the nowiki strip marker and result in literal UNIQ...QINU showing up
val = nw(mw.text.unstripNoWiki(val))
end
local k, v = string.match(val, "(.*)=(.*)")
if not k then
codeArguments[j] = val
j = j + 1
else
codeArguments[k] = v
end
codeArgumentsString = codeArgumentsString .. textPartBuffer .. val
if italic then
val = '<span style="font-style:italic;">' .. val .. '</span>'
end
textPart = textPart .. textPartBuffer .. val
end
i = i + 1
end
 
-- final wrap
local ret = titlePart .. textPart
if not dontBrace then ret = nw('{{') .. ret .. nw('}}') end
if _ne(args.a) then ret = nw('*') .. '&nbsp;' .. ret end
if _ne(args.kbd) then ret = '<kbd>' .. ret .. '</kbd>' end
if code then
ret = '<code>' .. ret .. '</code>'
elseif _ne(args.plaincode) then
ret = '<code style="border:none;">' .. ret .. '</code>'
end
if _ne(args.nowrap) then ret = '<span class="nowrap">' .. ret .. '</span>' end
--[[ Wrap as html??
local span = mw.html.create('span')
span:wikitext(ret)
--]]
if _ne(args.debug) then ret = ret .. '\n<pre>' .. mw.text.encode(mw.dumpObject(args)) .. '</pre>' end
 
if show_result then
local result = mw.getCurrentFrame():expandTemplate{title = addTemplate(args[1]), args = codeArguments}
ret = ret .. " → " .. result
end
 
if expand then
local query = mw.text.encode('{{' .. addTemplate(args[1]) .. string.gsub(codeArgumentsString, textPartBuffer, "|") .. '}}')
local url = mw.uri.fullUrl('special:ExpandTemplates', 'wpInput=' .. query)
mw.log()
ret = ret .. " [" .. tostring(url) .. "]"
end
 
return ret
end