--[[ lowlevel html functions Written for Alpine Configuration Framework (ACF) -- see www.alpinelinux.org Copyright (C) 2007 Nathan Angelacos Licensed under the terms of GPL2 ]]-- local mymodule = {} --[[ Cookie functions ]]------------------------------------------------------ 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 mymodule.cookie.set = function ( name, value, path ) local expires = "" if name == nil then return ("") end if value == false then expires = 'expires=Thu Jan 1 00:00:00 EST 1970' value = "" end if path == nil then path = "/" end 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 mymodule.cookie.unset = function ( name, path) return mymodule.cookie.set (name, false, path) end -- escape unsafe html characters function mymodule.html_escape (text ) text = text or "" local str = string.gsub (text, "&", "&" ) str = string.gsub (str, "<", "<" ) str = string.gsub (str, ">", ">" ) str = string.gsub (str, "'", "'" ) return (string.gsub (str, '"', """ )) end -- percent-encode reserved characters according to RFC3986 (except space to '+') function mymodule.url_encode ( text ) local str = text or "" str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") return str end -- return a name,value pair as a string. local nv_pair = function ( name, value) if ( name == nil ) then return ( value or "" ) end if ( type(value) == "boolean" ) then value = tostring(value) end if ( value == nil ) then return ( "" ) else return (string.format (' %s="%s" ', mymodule.html_escape(name) , mymodule.html_escape(value) )) end end local boolean_attribute = function ( name, value ) if ( name and value ) then return name.." " else return "" end end --[[ each of these functions take a table that has an associative array of the values we might care about: value -- this is the value in the form element, or the selected element name -- this is the name of the element cols, rows class id etc. ]]-- local generic_input generic_input = function ( field_type, v ) if type(v.value) == "table" then ret = {} local vals = v.value local name = v.name for n, val in ipairs(vals) do v.value = val v.name = name.."."..n table.insert(ret, generic_input(field_type, v)) end v.value = vals v.name = name return table.concat(ret) end if ( field_type == nil ) then return nil end local str = string.format ( '" ) end --[[ Form functions ]]------------------------------------------------------ -- These expect something like a cfe to work (see mvc.lua) mymodule.form = {} mymodule.form.text = function ( v ) return generic_input ( "text", v ) end mymodule.form.longtext = function ( v ) local str = "" ) end function mymodule.form.password ( v ) return generic_input ( "password", v ) end function mymodule.form.hidden ( v ) return generic_input ( "hidden", v ) end function mymodule.form.submit ( v ) return generic_input ( "submit", v ) end function mymodule.form.action (v) return generic_input ("submit", v) end function mymodule.form.file ( v ) -- CFE must contain value, but file cannot have value local value = v.value v.value = nil local retval = generic_input ( "file", v ) v.value = value return retval end function mymodule.form.image ( v ) return generic_input ( "image", v ) 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 mymodule.form.select ( v ) if ( v.name == nil ) then return nil end local str = "" return (str) end function mymodule.form.checkbox ( v ) return generic_input ( "checkbox", v ) end -- NOTE: VALUE of a form is a table containing the form elements ... function mymodule.form.start ( v) if ( v.action == nil ) then return nil end local method = v.method or "get" return ( string.format ( '
") end -- For "h1, h2, p," etc -- WARNING - Text is printed verbatim - you may want to -- wrap the text in mymodule.html_escape function mymodule.entity (tag, text, class, id) return ( string.format ( "<%s%s%s>%s%s>", mymodule.html_escape(tag), nv_pair ("class", class), nv_pair("id", id), mymodule.html_escape(text), mymodule.html_escape(tag)) ) end function mymodule.link ( v ) if ( v.value == nil ) then return nil end local str = nv_pair ( "href", v.value ) for i,k in ipairs( { "class", "id" }) do str = str .. nv_pair ( k, v[k] ) end return ( "" .. mymodule.html_escape(v.label) .. "" ) end return mymodule