Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

73
LINES

< > BotCompany Repo | #35 // Escape HTML entities

Lua code

-- Convert accented characters from a given string and return HTML entities in the output string.
-- by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr
-- v. 2.0 -- 2003/06/10 -- Better algorithm, using regular expression
-- v. 1.0 -- 2003/04/19 -- Naive implementation
-- from http://lua-users.org/files/wiki_insecure/users/PhiLho/3EncodeEntities.lua

local entities =
{
--	['&'] = "&amp;",
	['<'] = "&lt;",
	['>'] = "&gt;",
	-- French entities (the most common ones)
	['à'] = "&agrave;",
	['â'] = "&acirc;",
	['é'] = "&eacute;",
	['è'] = "&egrave;",
	['ê'] = "&ecirc;",
	['ë'] = "&euml;",
	['î'] = "&icirc;",
	['ï'] = "&iuml;",
	['ô'] = "&ocirc;",
	['ö'] = "&ouml;",
	['ù'] = "&ugrave;",
	['û'] = "&ucirc;",
	['ÿ'] = "&yuml;",
	['À'] = "&Agrave;",
	['Â'] = "&Acirc;",
	['É'] = "&Eacute;",
	['È'] = "&Egrave;",
	['Ê'] = "&Ecirc;",
	['Ë'] = "&Euml;",
	['Î'] = "&Icirc;",
	['Ï'] = "&Iuml;",
	['Ô'] = "&Ocirc;",
	['Ö'] = "&Ouml;",
	['Ù'] = "&Ugrave;",
	['Û'] = "&Ucirc;",
	['ç'] = "&ccedil;",
	['Ç'] = "&Ccedil;",
	['Ÿ'] = "&Yuml;",
	['«'] = "&laquo;",
	['»'] = "&raquo;",
	['©'] = "&copy;",
	['®'] = "&reg;",
	['æ'] = "&aelig;",
	['Æ'] = "&AElig;",
	['Œ'] = "&OElig;", -- Not understood by all browsers
	['œ'] = "&oelig;", -- Not understood by all browsers
}

function EncodeEntities3(toEncode)
	if toEncode == nil or type(toEncode) ~= "string" then
		return ''
	end

	local EncodeToEntities = function (char)
		local entity = entities[char]
		if entity == nil then
			local code = string.byte(char)
			if code > 127 then
				entity = string.format("&#%d;", code)
			end
		end
		return entity or char
	end

	entities['&'] = "&amp;"
	-- I will replace '(.)' with '([^%c%s%w%p])'
	encodedString = string.gsub(toEncode, '(.)', EncodeToEntities)
	return encodedString
end

return EncodeEntities3(input)

test run  test run with input  download  show line numbers   

Relations

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
963 #1000604 (pitcher) 2015-08-20 15:28:24
962 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
-- Convert accented characters from a given string and return HTML entities in the output string.
-- by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr
-- v. 2.0 -- 2003/06/10 -- Better algorithm, using regular expression
-- v. 1.0 -- 2003/04/19 -- Naive implementation
-- from http://lua-users.org/files/wiki_insecure/users/PhiLho/3EncodeEntities.lua

local entities =
{
-- ['&'] = "&amp;",
['<'] = "&lt;",
['>'] = "&gt;",
-- French entities (the most common ones)
['à'] = "&agrave;",
['â'] = "&acirc;",
['é'] = "&eacute;",
['è'] = "&egrave;",
['ê'] = "&ecirc;",
['ë'] = "&euml;",
['î'] = "&icirc;",
['ï'] = "&iuml;",
['ô'] = "&ocirc;",
['ö'] = "&ouml;",
['ù'] = "&ugrave;",
['û'] = "&ucirc;",
['ÿ'] = "&yuml;",
['Ã?'] = "&Agrave;",
['Ã?'] = "&Acirc;",
['Ã?'] = "&Eacute;",
['Ã?'] = "&Egrave;",
['Ã?'] = "&Ecirc;",
['Ã?'] = "&Euml;",
['Ã?'] = "&Icirc;",
['Ï'] = "&Iuml;",
['Ã?'] = "&Ocirc;",
['Ã?'] = "&Ouml;",
['Ã?'] = "&Ugrave;",
['Ã?'] = "&Ucirc;",
['ç'] = "&ccedil;",
['Ã?'] = "&Ccedil;",
['Ÿ'] = "&Yuml;",
['«'] = "&laquo;",
['»'] = "&raquo;",
['©'] = "&copy;",
['®'] = "&reg;",
['æ'] = "&aelig;",
['Ã?'] = "&AElig;",
['Å?'] = "&OElig;", -- Not understood by all browsers
['Å?'] = "&oelig;", -- Not understood by all browsers
}

function EncodeEntities3(toEncode)
if toEncode == nil or type(toEncode) ~= "string" then
return ''
end

local EncodeToEntities = function (char)
local entity = entities[char]
if entity == nil then
local code = string.byte(char)
if code > 127 then
entity = string.format("&#%d;", code)
end
end
return entity or char
end

entities['&'] = "&amp;"
-- I will replace '(.)' with '([^%c%s%w%p])'
encodedString = string.gsub(toEncode, '(.)', EncodeToEntities)
return encodedString
end

return EncodeEntities3(input)
}}
2015-08-20 08:33:07  delete 

add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 1915 [visualize]

Snippet ID: #35
Snippet name: Escape HTML entities
Eternal ID of this version: #35/1
Text MD5: 59b10f4085073c01b1696819367af1a7
Author: stefan
Category: tools
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2013-12-13 23:00:48
Source code size: 1915 bytes / 73 lines
Pitched / IR pitched: Yes / Yes
Views / Downloads: 1806 / 287
Referenced in: [show references]