Launch a Web Page (code snippet)

Description

Launches a web page using fhShellExecute() after encoding the URL string before sending.

Requires: None

Code Version 1

This version copes with all character byte codes 00 (%00) to 255 (%FF).
However, some URL reject non-ASCII byte codes 128 (%80) to 255 (%FF) encoded this way.

LaunchWebPage.fh_lua
--[[
@Title:		Launch a Web Page
@Author:	Calico Pie & tatewise
@LastUpdated:	December 2011
@Description:	Sample to Launch a Selected Web Page 
]]
 
-- Encode each URL non-alphanumeric ASCII char to %XX code, and space to "+"
 
function strURL_encode(strURL)
	if strURL then
		strURL = strURL:gsub("\n", "\r\n")
		strURL = strURL:gsub("[^0-9A-Za-z ]", function(char) return string.format("%%%02X", string.byte(char)) end)
		strURL = strURL:gsub(" ", "+")
	else
		strURL = ""
	end
	return strURL	
end
 
strPlace = strURL_encode("Kingsbury Episcopi, Somerset \n\t\f ' £ € © ª «")
 
fhShellExecute("http://maps.google.co.uk?q="..strPlace)
 
print(strPlace)

Code Version 2

This version copes with all LUA characters with byte codes 0 to 255 (FF).
Non-ASCII codes 128 (80) to 255 (FF) are UTF-8 encoded as %XX%XX or %XX%XX%XX.
This is sometimes required by certain URL such as Google Maps Geocoder services.
It also % encodes all except the RFC 3986 section 2.3 Unreserved Characters.

LaunchWebPage.fh_lua
--[[
@Title:		Launch a Web Page
@Author:	tatewise
@LastUpdated:	January 2012
@Description:	Sample to Launch a Selected Web Page 
]]
 
-- Encode each URL non-alphanumeric LUA char to %XX or %XX%XX or %XX%XX%XX codes
 
function strURL_encode(strURL)
	if strURL then
		strURL = strURL:gsub("[^0-9A-Za-z_~%-%.]",		-- 0-9 A-Z a-z _ ~ - . are the RFC 3986 section 2.3 Unreserved Characters
		function(strChar)
			if	strChar == "€" then strChar = "%E2%82%AC" -- Encoding of some chars 80 to 9F
			elseif	strChar == "™" then strChar = "%E2%84%A2"
			elseif	strChar == "Š" then strChar = "%C5%A0"
			elseif	strChar == "Ž" then strChar = "%C5%BD"
			elseif	strChar == "š" then strChar = "%C5%A1"
			elseif	strChar == "ž" then strChar = "%C5%BE"
			elseif	strChar == "Ÿ" then strChar = "%C5%B8"
			else
				local intChar = string.byte(strChar)
				if intChar < 0x80 then			-- ASCII chars 00-7F become %XX code	NUL-US ! " # $ % & ' ( ) * + , / : ; < = > ? @ [ \ ] ^ ` { | }
					strChar = string.format("%%%02X", intChar)
				elseif intChar < 0xA0 then		-- Characters 80-9F become "?" %3F code	 €  ‚ ƒ „ … † ‡ ˆ ‰ Š ‹ Œ  Ž   ‘ ’ “ ” • – — ˜ ™ š › œ  ž Ÿ 
					strChar = string.format("%%%02X", string.byte("?"))
				else					-- Characters A0-FF become UTF-8 %XX%XX	 ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ...thru... ý þ ÿ
					local intFloor = math.floor(intChar/0x40)
					strChar = string.format("%%%02X", 0xC0 + intFloor)..string.format("%%%02X", 0x80 + intChar - intFloor*0x40)
				end
			end
			return strChar
		end)
	else
		strURL = ""
	end
	return strURL	
end
 
strPlace = strURL_encode("Kingsbury Episcopi, Somerset \n\t\f ' £ ƒ © ª «")
 
fhShellExecute("http://maps.google.co.uk?q="..strPlace)
 
print(strPlace)