模組:Template:KeepFake
模块文档[创建]
您可能想要创建本Scribunto模块的文档。 编者可以在本模块的沙盒 (创建 | 镜像)和测试样例 (创建)页面进行实验。 请在/doc子页面中添加分类。本模块的子页面。 |
local data = require( 'Module:Template:KeepFake/data' )
local z = {}
function extractAliases(item)
allnames = {item['code']}
for j, alias in ipairs(item['aliases']) do
table.insert(allnames, alias)
end
return allnames
end
function extractShortDesc(item)
if item['description'] then
return item['description']:gsub('{', '{'):gsub('|', '|'):gsub('}', '}')
else
return ''
end
end
function extractLongDesc(item)
if (item['description_old'] == nil) then
if item['description'] then
return ':' .. item['description']:gsub('{', '{'):gsub('{{subst:', '{{tls|'):gsub('{{', '{{tl|')
else
return ''
end
else
return item['description_old']
end
end
function extractDeleteReason(item)
return item['deletereason'] or item['criteria'] or ''
end
function shortDesc(frame, name)
name = mw.text.trim(name):upper()
wt = {}
for i, item in ipairs(data) do
if name == '' or #name == 1 and item['code']:sub(1, 1) == name or item['code'] == name then
para = extractShortDesc(item)
if para ~= '' then
table.insert(wt, para)
end
end
end
if short then
return table.concat(wt, '\n')
else
return frame:preprocess(table.concat(wt, '\n'))
end
end
function z.shortDesc(frame)
return shortDesc(frame, frame.args[1])
end
function desc( frame, name, short )
name = mw.text.trim( name ):upper()
wt = {}
for i, item in ipairs( data ) do
if name == '' or #name == 1 and item['code']:sub( 1, 1 ) == name or item['code'] == name then
if short then
para = extractShortDesc( item )
if para ~= '' then
table.insert( wt, para )
end
else
allnames = extractAliases( item )
para = extractLongDesc( item )
tinfo = item['usage']
if tinfo == nil then
tusage = {}
for k, aname in ipairs( allnames ) do
table.insert( tusage, '{{tl|KeepFake|' .. aname .. '}}' )
end
tinfo = '\n*使用模板' .. mw.text.listToText( tusage, '、', '或' ) .. '。'
else
tinfo = ''
end
snippet = '; {{anchor|' .. table.concat( allnames, '|' ) .. '}} ' .. item['code'] .. '. ' .. item['criteria'] .. '\n' .. para .. tinfo
table.insert( wt, snippet )
end
end
end
if short then
return table.concat( wt, '\n' )
else
return frame:preprocess( table.concat( wt, '\n' ) )
end
end
function z.desc(frame)
return desc(frame, frame.args[1])
end
function z.reasons(frame)
wt = {}
for i, item in ipairs(data) do
allnames = extractAliases(item)
table.insert(wt, '|-\n|' .. mw.text.listToText(allnames, '、', '或') .. '||<span title="' .. extractShortDesc(item) .. '">' .. item['criteria'] .. '</span>\n')
end
wt = '{|class="wikitable\n'.. table.concat(wt) .. '\n|}'
return wt
end
function z.reasons2(frame)
local wt = [=[
{|class="wikitable
|-
!速留編號!!可使用的代碼!!簡介!!詳細說明!!使用模板
]=]
for i, item in ipairs(data) do
allnames = extractAliases(item)
tinfo = item['usage']
if tinfo == nil then
tusage = {}
for k, aname in ipairs( allnames ) do
table.insert( tusage, '{{tl|KeepFake|' .. aname .. '}}' )
end
tinfo = '使用模板' .. mw.text.listToText( tusage, '、', '或' ) .. '。'
end
wt = wt .. string.format([=[
|-
|%s
|
* %s
|%s
|
%s
|%s
]=]
, item['code'], mw.text.listToText(allnames, '\n* ', '\n* '), item['criteria'], extractLongDesc(item), tinfo)
end
wt = wt .. '|}'
return frame:preprocess(wt)
end
function z.input(frame)
if frame.args.parent then
args = frame:getParent().args
else
args = frame.args
end
-- precache
map = {}
for i, item in ipairs(data) do
map[item['code']:lower()] = i
for j, alias in ipairs(item['aliases']) do
map[alias:lower()] = i
end
end
-- parse
i = 1
rows = {}
pretext = {}
deletelinks = {}
while i < 10 do
arg = args[i]
if arg and map[mw.text.trim(arg:lower())] then
item = data[map[mw.text.trim(arg:lower())]]
if frame.args.reasoncode then
return item['code']
end
title = mw.title.getCurrentTitle()
checkfunc = item['check']
if checkfunc then
check = checkfunc(title)
else
check = nil
end
-- special case for F1
rowsuffix2 = ''
deletesuffix = ''
if check then
rowsuffix = '<br><span class="error">' .. check .. '</span>' .. (args.cat or args.cate or args.category or '[[Category:快速保留候选|错]]')
else
if frame.args.deletelink then
table.insert(deletelinks, '[[WP:CSK#' .. item['code'] .. '|' .. item['code'] .. ']]: ' .. extractDeleteReason(item) .. deletesuffix)
end
rowsuffix = args.cat or args.cate or args.category or ('[[Category:快速保留候选|' .. (item['category'] or '速') .. ']]')
end
if deletesuffix then
row = '* <strong><span id="speedy-delete-' .. item['code'] .. '" title="' .. extractShortDesc(item) .. '">' .. item['criteria'] .. '([[WP:CSXK#' .. item['code'] .. '|CSK ' .. item['code'] .. ']]' .. deletesuffix .. ')' .. rowsuffix .. rowsuffix2 .. '</span></strong>'
else
row = '* <strong><span id="speedy-delete-' .. item['code'] .. '" title="' .. extractShortDesc(item) .. '">' .. item['criteria'] .. '([[WP:CSXK#' .. item['code'] .. '|CSK ' .. item['code'] .. ']])' .. rowsuffix .. rowsuffix2 .. '</span></strong>'
end
table.insert(rows, row)
elseif arg and mw.text.trim(arg) ~= '' then
if frame.args.reasoncode then
return ''
end
-- try to read it as a title
title = mw.title.new(mw.text.trim(arg))
cat = args.cat or args.cate or args.category or '[[Category:快速保留候选|速]]'
if title and title.exists then
table.insert(rows, '*<strong>' .. cat .. '[[:' .. arg .. ']]</strong>')
else
if frame.args.deletelink then
table.insert(deletelinks, arg)
end
arg = string.gsub(arg, '^([*:#]*)(.*)', '%1<strong>%2</strong>')
table.insert(rows, '*' .. cat .. arg)
end
end
arg = args['c' .. i]
if arg and mw.text.trim(arg) ~= '' then
table.insert(rows, '*' .. arg)
end
i = i + 1
end
if #rows > 0 then
return mw.text.trim(table.concat(pretext) .. '\n' .. table.concat(rows, '\n'))
else
return '<span style="font-weight:bold;color:red;">(請填寫理由)</span>' .. (args.cat or args.cate or args.category or '[[Category:快速保留候选|错]]')
end
end
return z