モジュール:Vau

半永久的に拡張半保護されているモジュール
モジュールの解説[作成]
local p = {}

--[[
使い方:
{{#invoke:Vau|ref|unit=ユニット名|distinction=識別番号|link=リンク先|add=追加説明|group=refタグgroup名}}

unit - 必須
distinction - 任意(既定値 '')
link - 任意(既定値 '')
add - 任意(既定値 '')
group - 任意(既定値 'メンバー')
list - 任意(既定値 '')
]]
function p.ref(frame)
	-- 引数を取得する
	local unit = frame.args.unit
	local distinction = frame.args.distinction
	local link = frame.args.link
	local add = frame.args.add
	local list = frame.args.list
	list = list ~= '' and list ~= '0'
	local ref = {name = '', group = frame.args.group}
	
	-- 非脚注部分を作成する
	local noref = mw.text.nowiki(unit)
	noref = (link == '') and noref or '[[' .. link ..'|' .. noref .. ']]'
	
	-- ユニット名+識別番号をユニット一覧から検索する
	local search = (distinction == '') and unit or (unit .. '|' .. distinction)
	local result = mw.text.unstripNoWiki(frame:expandTemplate{title = 'Vau/ユニット一覧'}):match('(;%s*' .. search:gsub('[().%%%+%-%*?[^$]', '%%%0') .. '%s*\n.-:%s*.-)%s*\n[:;={]')
	
	local member = '' -- メンバー(ref content)
	local error = true
	if result then
		ref.name, member = result:match('.*;%s*(.-)%s+:%s*(.-)%s*[:;={]?$')
		ref.name = 'vau-' .. ref.name .. ((add == "") and "" or "-add-" .. add) .. (list and "-list" or "")
		if not member:find('<strong class=["\']?error["\']?>') then
			-- メンバーがエラーでないとき: 記事の声優を太字にする, addを追加する
			local bold = frame:preprocess('{{PAGENAME}}')
			local avoid_suffix = {'の作品', 'のディスコグラフィ'}
			for k, v in ipairs(avoid_suffix) do
				bold = bold:gsub(v, '')
			end
			member = member:gsub('%[%[' .. bold .. '|(.-)]]', '<b>%1</b>'):gsub('%[%[' .. bold .. ']]', '<b>' .. bold .. '</b>') .. add
			error = false
		elseif member:find('末尾に追加してください') then
			-- メンバーが曖昧さ回避のとき: ビジュアルエディター向けの説明を追加する
			local d_code, i = {}, 0
			for value in member:gmatch('<code>|(.-)</code>') do
				i = i + 1
				d_code[i] = value
			end
			member = '<dl class="hlist" style="display:inline;"><dt>' .. unit .. '</dt><dd style="white-space:normal;">' .. member:gsub('</strong>$', '<small>ビジュアルエディターで編集している場合は「識別番号」フィールドにそれぞれ<code>' .. table.concat(d_code, '</code>、<code>') .. '</code>を指定してください。</small></strong>') .. '</dd></dl>'
		end
	else
		member = '<strong class="error">Vau定義エラー: ユニット<code>' .. mw.text.nowiki(search) .. '</code>は定義されていません。詳しくは[[Template:Vau#エラーが出たときは]]を参照。</strong>'
	end
	
	-- リスト形式
	if list then
		member = '<dl style="display:inline; margin-left:0;"><dt style="display:inline;"><dfn>' .. mw.text.nowiki(unit) .. '</dfn></dt><dd style="display:inline; margin-left:0;"><span style="padding:0 0.5em;">-</span>' .. member .. '</dd></dl>'
	end
	-- 出力する
	if error then
		if frame:preprocess('{{REVISIONID}}') == '' then
			-- プレビュー
			return member, '<div class="previewnote nomobile" style="position:absolute; top:-2em; left:0;"><strong class="error" style="background:#fff; margin:0 ' .. (#tostring(mw.title.getCurrentTitle()) * 0.6 + 11.8) .. 'em;">[[Template:Vau|<span style="color:#c00;">Template:Vau</span>]]の呼び出しエラーが発生しています</strong></div>'
		else
			-- 閲覧
			return '<span class="vau-error">' .. noref .. '</span>', '[[Category:テンプレート呼び出しエラーのあるページ/Template:Vau]]'
		end
	else
		return '<span class="vau">' .. noref .. frame:extensionTag('ref', member, ref) .. '</span>'
	end
end

return p