Module:Tlg:修订间差异
外观
删除的内容 添加的内容
创建 Module:Tlg(来源于 zhwiki,去除 Wikipedia:Lua 链接) |
覆盖为 zhwiki 当前版(修复空参时 {{tlg}} 显示 bug) |
||
| (未显示同一用户的1个中间版本) | |||
| 第1行: | 第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 getArgs = require('Module:Arguments').getArgs |
||
local p = {} |
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 ~= "" |
return s ~= nil and s ~= "" |
||
end |
end |
||
local |
local nw = mw.text.nowiki |
||
local function |
local function addTemplate(s) |
||
local |
local i, _ = s:find(':', 1, true) |
||
if |
if i == 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 |
else |
||
return |
return 'Template:' .. s |
||
end |
end |
||
end |
end |
||
function |
local function trimTemplate(s) |
||
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 |
end |
||
return '[[' .. transclude(title) .. '|' .. (a or title) .. ']]' |
|||
end |
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 |
end |
||
local s = p.magicword_title[wt] |
|||
local titleObj |
|||
if wt == "#invoke" then |
|||
local titlePart = '[[' |
|||
return '#invoke:[[' .. transclude('Module:' .. i) .. '|' .. i .. ']]' |
|||
if args['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 |
else |
||
titleObj = mw.title.getCurrentTitle() |
|||
return s .. (i and ':' .. i) |
|||
end |
end |
||
end |
|||
titlePart = titlePart .. (titleObj ~= nil and titleObj.fullText or |
|||
addTemplate(args['1'])) |
|||
-- 设定 |
|||
local |
local textPart = args.alttext |
||
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 = '{' |
|||
if braceinside then |
|||
if alttext then |
|||
alttext = '{' .. alttext .. '}' |
|||
else |
else |
||
-- redlink |
|||
alttext = '{' .. title .. '}' |
|||
textPart = args['1'] |
|||
end |
end |
||
wt = wt .. p._gettemplatelink(title, alttext) |
|||
else |
|||
wt = wt .. '{' |
|||
if subst then |
|||
if nolink and nosubstlink then |
|||
wt = wt .. 'subst:' |
|||
else |
|||
wt = wt .. '[[Help:替换引用|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 .. '|' .. val |
|||
i = i + 1 |
|||
end |
|||
if args[12] then |
|||
vals = vals .. '|...' |
|||
end |
|||
if vals ~= '' and italic then |
|||
vals = '<span style="font-style:italic;">' .. vals .. '</span>' |
|||
end |
|||
wt = wt .. vals |
|||
wt = wt .. '}' |
|||
end |
end |
||
wt = wt .. '}' |
|||
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 |
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 |
|||
titlePart = titlePart .. '|' .. textPart .. ']]' |
|||
if _ne(args.braceinside) then |
|||
return wt |
|||
titlePart = nw('{') .. titlePart .. nw('}') |
|||
end |
|||
return titlePart |
|||
end |
end |
||
| 第204行: | 第83行: | ||
}) |
}) |
||
return p._main(args) |
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 = "|" |
|||
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('*') .. ' ' .. 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 |
end |
||
2026年6月21日 (日) 22:11的最新版本
-- This implements Template:Tlg
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- Is a string non-empty?
local function _ne(s)
return s ~= nil and s ~= ""
end
local nw = mw.text.nowiki
local function addTemplate(s)
local i, _ = s:find(':', 1, true)
if i == nil then
return 'Template:' .. s
end
local ns = s:sub(1, i - 1)
if ns == '' or mw.site.namespaces[ns] then
return s
else
return 'Template:' .. s
end
end
local function trimTemplate(s)
local needle = 'template:'
if s:sub(1, needle:len()):lower() == needle then
return s:sub(needle:len() + 1)
else
return s
end
end
local function linkTitle(args)
if _ne(args.nolink) then
return args['1']
end
local titleObj
local titlePart = '[['
if args['1'] then
-- This handles :Page and other NS
titleObj = mw.title.new(args['1'], 'Template')
else
titleObj = mw.title.getCurrentTitle()
end
titlePart = titlePart .. (titleObj ~= nil and titleObj.fullText or
addTemplate(args['1']))
local textPart = args.alttext
if not _ne(textPart) then
if titleObj ~= nil then
textPart = titleObj:inNamespace("Template") and args['1'] or titleObj.fullText
else
-- redlink
textPart = args['1']
end
end
if _ne(args.subst) then
-- HACK: the ns thing above is probably broken
textPart = 'subst:' .. textPart
end
if _ne(args.brace) then
textPart = nw('{{') .. textPart .. nw('}}')
elseif _ne(args.braceinside) then
textPart = nw('{') .. textPart .. nw('}')
end
titlePart = titlePart .. '|' .. textPart .. ']]'
if _ne(args.braceinside) then
titlePart = nw('{') .. titlePart .. nw('}')
end
return titlePart
end
function p.main(frame)
local args = getArgs(frame, {
trim = true,
removeBlanks = false
})
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 = "|"
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('*') .. ' ' .. 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
return p