模組:Reaction
外观
-- implement {{Reaction}}
local p = {}
local mIfexist
local function mayMakeFile(iconInput)
local success, title = pcall(mw.title.new, iconInput)
if success and title and title.namespace == 6 then
if not mIfexist then
mIfexist = require('Module:Ifexist')
end
-- if title.file.exists then
if mIfexist._pfFileExists(title) then
return string.format('[[File:%s|x20px|link=]]', title.text)
end
end
return false
end
local function stripInputCount(inputCount, realCount)
if inputCount ~= nil then
inputCount = mw.text.trim(inputCount)
if inputCount == "" then
return "0"
else
-- 示例使用了 99+ 所以這裡也允許尾隨 + 號
-- 順便把前導 0 也丟掉
local num = mw.ustring.match(inputCount, "^0*(%d+%+?)$")
if num then
return num
end
end
end
return tostring(realCount)
end
function p.main(frame)
local parent = frame:getParent()
if not parent then
-- 不是模板被引用
return ''
end
local args = parent.args
local iconInput = args[0] or "👍"
iconInput = mw.text.trim(iconInput)
if mw.ustring.find(iconInput, "\n") then
-- TODO 這種情況按照 MediaWiki 舊解析器鐵定是炸的
end
local iconDisplay = mayMakeFile(iconInput) or iconInput
local iconData = mw.text.unstrip(iconInput)
local reactions = {}
while true do
local currentItem = args[1 + #reactions + 1] -- 反應者從第二個參數開始
if currentItem == nil then
break
end
currentItem = mw.text.trim(currentItem)
if currentItem == '' then
break
end
table.insert(reactions, currentItem)
end
local realRectionCount = #reactions -- 真實計數
local reactionCount = stripInputCount(args.num, realRectionCount) -- 顯示的計數
local out = mw.html.create('span')
:addClass('reactionable')
:addClass('template-reaction')
:attr('title', mw.text.listToText(reactions, '、', '和') .. '回应了这条留言')
:attr('data-reaction-commentors', table.concat(reactions, '/'))
:attr('data-reaction-icon', iconData)
:attr('data-reaction-count', rectionCount)
:attr('data-reaction-real-count', realRectionCount)
local content = out:tag('span')
:addClass('reaction-content')
-- icon
content:tag('span')
:addClass('reaction-icon-container')
:tag('span')
:addClass('reaction-icon')
:wikitext(iconDisplay)
-- counter
content:tag('span')
:addClass('reaction-counter-container')
:tag('span')
:addClass('reaction-counter')
:wikitext(rectionCount)
return frame:extensionTag({ name = 'templatestyles', args = { src = 'Template:Reaction/styles.css' } }) .. tostring(out)
end
return p