モジュール:Title with non-BMP

モジュールの解説[作成]
require('strict')

local function getNonBmp(str)
	local nonbmp = {}
	for c in mw.ustring.gcodepoint(str) do
		if c > 0xFFFF then	-- BMP外の文字である
			table.insert(nonbmp, c)
		end
	end
    return nonbmp
end
    
return {
	main = function(frame) 
		local title = mw.title.getCurrentTitle()
		local nonbmp = getNonBmp(title.text)
		if (title.namespace == 2) or (title.namespace == 3) then	-- 利用者名前空間もしくは利用者‐会話名前空間
			return ""	-- 空の文字列を返す
		end
		if #nonbmp == 0 then	-- BMP外の文字が存在しない
			return '<span class="error">ページ名に[[基本多言語面]](U+0000からU+FFFFまで)の範囲外にある文字が含まれていません。</span>'
				.. "[[Category:テンプレート呼び出しエラーのあるページ/Template:Title with non-BMP]]"
		end
		if title.isRedirect then	-- リダイレクトである
			for i = 1, #nonbmp do
				nonbmp[i] = mw.ustring.format(
					'<span style="font-size:large">「[[wikt:&#x%X;|&#x%X;]]」</span>(U+%X)',
					nonbmp[i], nonbmp[i], nonbmp[i])
			end
			return mw.ustring.format(
				"* [[:Category:Unicode基本多言語面外の文字を含むリダイレクト|" ..
					"Unicode基本多言語面外の文字を含むリダイレクト]]: " ..
					"このリダイレクトのタイトルにはUnicodeの[[基本多言語面]]にない文字%sを含んでいます。" .. 
					"[[Category:Unicode基本多言語面外の文字を含むリダイレクト|+]]",
				table.concat(nonbmp, "、"))
		else	-- リダイレクトでない
			return "[[Category:Unicode基本多言語面外の文字を含むページ名|+]]"
		end
    end
}