Jump to content

Module:string/decode entities

Wiktionary වෙතින්


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
"https://si.wiktionary.org/w/index.php?title=Module:string/decode_entities&oldid=164305" වෙතින් සම්ප්‍රවේශනය කෙරිණි