local gsub = string.gsub
local match = string.match
local tonumber = tonumber
local u
local entities
local function decode_numeric_entity(code, pattern, base)
u = u or require("Module:string/char")
local cp = match(code, pattern) and tonumber(code, base)
return cp and cp < 0x110000 and u(cp) or nil
end
local function decode_entity(hash, x, code)
if hash == "#" then
return x == "" and decode_numeric_entity(code, "^%d+$") or
decode_numeric_entity(code, "^%x+$", 16)
end
entities = entities or mw.loadData("Module:data/entities")
return entities[x .. code]
end
-- Non-ASCII characters aren't valid in proper HTML named entities, but MediaWiki uses them in some custom aliases which are also included in [[Module:data/entities]].
return function (this)
return (gsub(this, "&(#?)([Xx]?)([%w\128-\255]+);", decode_entity))
end