モジュールの解説[表示] [編集] [履歴] [キャッシュを破棄]

関連テンプレート

編集

local p = {}
local htmlerror = require('Module:Error').error

local template_reply_to = "Template:Reply to/ja"
local string_and = ''       -- args['c'] en:"and", ja:"および"
local string_to = ''        -- args['p'] en:":",   ja:"へ"
local user_separator = "、" -- args['s'] en:",",   ja:"、"
local user_title = "さん"   -- args['t'] en:nil,   ja:"さん"

local function err(msg)
	return htmlerror{mw.ustring.format("エラー: [[%s]]: %s",template_reply_to,msg)}
end

function p.replyto(frame)
	local origArgs = frame:getParent().args
	local args = {}
	local maxArg = 0
	for k, v in pairs(origArgs) do
		if type(k) == 'number' then
			if v:match('^%s*(.-)%s*$') ~= '' then
				if k > maxArg then maxArg = k end
				args[k] = mw.title.new(v).baseText
			end
		else
			args[k] = v
		end
	end

	if maxArg == 0 then
		if frame.args.example then 
			args[1] = "ウィキ助" 
		else 
			return err('ユーザー名が指定されていません。')
		end
	end

	local maxArgLimit = tonumber(frame.args.max) or 50
	args['c'] = args['c'] or string_and
	args['p'] = args['p'] or string_to
	args['s'] = args['s'] or user_separator
	args['t'] = args['t'] or user_title
	
	if maxArg > maxArgLimit then
		return err(mw.ustring.format("%d以上のユーザー数は指定できません。",maxArgLimit))
	else
		local outStr = '<span class="template-ping">'..(args['prefix'] or '@')..'[[:User:'..args[1]..'|'..((args['label1'] or args['label']) or args[1])..']]'..args['t']
		for i = 2, maxArg do
			if ( (maxArg > 2) or ((maxArg == 2) and (args['c'] == '')) ) then outStr = outStr..args['s'] end
			if i == maxArg then outStr = outStr..args['c'] end
			outStr = outStr.."[[:User:"..args[i].."|"..(args['label'..tostring(i)] or args[i]).."]]"..args['t']
		end
		outStr = outStr..args['p'].."</span>"
		return outStr
	end
end

return p