Module:User:Theknightwho/encode entities

local codepoint = require("Module:string/codepoint") local decode_entities local find = string.find local format = string.format local gsub = string.gsub local match = string.match

local function encode_entity(ch) return "&#x" .. format("%X", codepoint(ch)) .. ";" end

return function(text, set, raw) if not raw then decode_entities = decode_entities or require("Module:string/decode entities") text = decode_entities(text) end if set and not match(set, "[\128-\255]") then return (gsub(text, "[" .. set .. "]", encode_entity)) end set = set or "\"&'<>\194\160"	return (gsub(text, "[%z\1-\127\194-\244][\128-\191]*", function(ch)		return find(set, ch, 1, true) and encode_entity(ch) or nil	end)) end