From d60a739470dc0d46f5306033b17c1dd27ddc37fc Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Thu, 17 Oct 2013 19:03:48 +0000 Subject: Remove all calls to 'module' in preparation for move to Lua 5.2 Use mymodule parameter for module definition. This was also helpful in revealing places where the code relied on the global environment. --- html.lua | 72 +++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 35 deletions(-) (limited to 'html.lua') diff --git a/html.lua b/html.lua index c2344a2..d220fb4 100644 --- a/html.lua +++ b/html.lua @@ -3,14 +3,14 @@ Copyright (C) 2007 Nathan Angelacos Licensed under the terms of GPL2 ]]-- -module (..., package.seeall) +local mymodule = {} --[[ Cookie functions ]]------------------------------------------------------ -cookie={} +mymodule.cookie={} -- Set a cookie - returns a string suitable for setting a cookie -- if the value is the boolean "false", then set the cookie to expire -cookie.set = function ( name, value, path ) +mymodule.cookie.set = function ( name, value, path ) local expires = "" if name == nil then return ("") @@ -22,20 +22,20 @@ cookie.set = function ( name, value, path ) if path == nil then path = "/" end - return (string.format('Set-Cookie: %s=%s; path=%s; %s\n', html_escape(tostring(name)), - html_escape(tostring(value)), html_escape(path), html_escape(expires))) + return (string.format('Set-Cookie: %s=%s; path=%s; %s\n', mymodule.html_escape(tostring(name)), + mymodule.html_escape(tostring(value)), mymodule.html_escape(path), mymodule.html_escape(expires))) end -- wrapper function to clear a cookie -cookie.unset = function ( name, path) - return cookie.set (name, false, path) +mymodule.cookie.unset = function ( name, path) + return mymodule.cookie.set (name, false, path) end -- escape unsafe html characters -function html_escape (text ) +function mymodule.html_escape (text ) text = text or "" local str = string.gsub (text, "&", "&" ) str = string.gsub (str, "<", "<" ) @@ -57,7 +57,7 @@ local nv_pair = function ( name, value) if ( value == nil ) then return ( "" ) else - return (string.format (' %s="%s" ', html_escape(name) , html_escape(value) )) + return (string.format (' %s="%s" ', mymodule.html_escape(name) , mymodule.html_escape(value) )) end end @@ -89,7 +89,7 @@ local generic_input = function ( field_type, v ) return nil end - local str = string.format ( '" .. html_escape(v.value) .. "" ) + return ( str .. ">" .. mymodule.html_escape(v.value) .. "" ) end -function form.password ( v ) +function mymodule.form.password ( v ) return generic_input ( "password", v ) end -function form.hidden ( v ) +function mymodule.form.hidden ( v ) return generic_input ( "hidden", v ) end -function form.submit ( v ) +function mymodule.form.submit ( v ) return generic_input ( "submit", v ) end -function form.action (v) +function mymodule.form.action (v) return generic_input ("submit", v) end -function form.file ( v ) +function mymodule.form.file ( v ) return generic_input ( "file", v ) end -function form.image ( v ) +function mymodule.form.image ( v ) return generic_input ( "image", v ) end @@ -161,7 +161,7 @@ end -- v.value is the selected item (or an array if multiple) -- v.option is an array of valid options (or an array of value, label) -- NOTE use of value and values (plural) -function form.select ( v ) +function mymodule.form.select ( v ) if ( v.name == nil ) then return nil end @@ -201,22 +201,22 @@ function form.select ( v ) str = str .. " selected" reverseval[val] = nil end - str = str .. nv_pair("value", val) .. ">" .. html_escape(label) .. "" + str = str .. nv_pair("value", val) .. ">" .. mymodule.html_escape(label) .. "" end for val in pairs(reverseval) do - str = str .. '' + str = str .. '' end str = str .. "" return (str) end -function form.checkbox ( v ) +function mymodule.form.checkbox ( v ) return generic_input ( "checkbox", v ) end -- NOTE: VALUE of a form is a table containing the form elements ... -function form.start ( v) +function mymodule.form.start ( v) if ( v.action == nil ) then return nil end @@ -224,30 +224,30 @@ function form.start ( v) local method = v.method or "get" return ( string.format ( '
', - nv_pair ( "class", html_escape(v.class) ), - nv_pair ( "method", html_escape(v.method) ), - nv_pair ( "action", html_escape(v.action) ) + nv_pair ( "class", mymodule.html_escape(v.class) ), + nv_pair ( "method", mymodule.html_escape(v.method) ), + nv_pair ( "action", mymodule.html_escape(v.action) ) ) ) end -function form.stop ( ) +function mymodule.form.stop ( ) return ("
") end -- For "h1, h2, p," etc -- WARNING - Text is printed verbatim - you may want to --- wrap the text in html_escape -function entity (tag, text, class, id) +-- wrap the text in mymodule.html_escape +function mymodule.entity (tag, text, class, id) return ( string.format ( "<%s%s%s>%s", - html_escape(tag), + mymodule.html_escape(tag), nv_pair ("class", class), - nv_pair("id", id), html_escape(text), html_escape(tag)) + nv_pair("id", id), mymodule.html_escape(text), mymodule.html_escape(tag)) ) end -function link ( v ) +function mymodule.link ( v ) if ( v.value == nil ) then return nil end @@ -256,5 +256,7 @@ function link ( v ) str = str .. nv_pair ( k, v[k] ) end - return ( "" .. html_escape(v.label) .. "" ) + return ( "" .. mymodule.html_escape(v.label) .. "" ) end + +return mymodule -- cgit v1.2.3