summaryrefslogtreecommitdiffstats
path: root/html.lua
blob: dceb8938251cfba4d818d76ab2b19f9bdd193255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
--[[ 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, "<", "&lt;" )
	str = string.gsub (str, ">", "&gt;" )
	str = string.gsub (str, "'", "&#39;" )
	return (string.gsub (str, '"', "&quot;" ))
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


--[[
	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
		for n, val in ipairs(vals) do
			v.value = val
			table.insert(ret, generic_input(field_type, v))
		end
		v.value = vals
		return table.concat(ret)
	end
	if ( field_type == nil ) then 
		return nil
	end
	
	local str = string.format ( '<input class="%s %s" type="%s" ', mymodule.html_escape(v.class), mymodule.html_escape(field_type), mymodule.html_escape(field_type) )

	for i,k in ipairs ( {
			"name", "size", "checked", "maxlength", 
			"value", "length", "id", "src",
			"align", "alt", "contenteditable", "readonly", 
			"tabindex", "accesskey", "onfocus", "onblur", "title"
			} ) do
		str = str .. nv_pair ( k, v[k] )
	end

	if ( v.disabled ~= nil ) then 
		str = str .. " disabled"
	end

	return ( str .. ">" )
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 = "<textarea"
	for i,k in ipairs ( {
				"name", "rows", "cols",
				"class", "id", "tabindex", "accesskey", 
				"onfocus", "onblur", "readonly", "title"
			} ) do
		str = str .. nv_pair ( k, v[k] )
	end
	str = str .. nv_pair (nil, v.disabled)
	return ( str .. ">" .. mymodule.html_escape(v.value) .. "</textarea>" )
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 )
	return generic_input ( "file", v )
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 = "<select"
	for i,k in ipairs ( {
			"name", "size", "tabindex", "accesskey", 
			"onfocus", "onblur", "onchange", "id", 
			"class", "multiple", "title"
			} ) do
		str = str .. nv_pair ( k, v[k] )
	end
	
	if ( v.disabled ~= nil ) then
		str = str .. " disabled"
	end
	str = str .. ">"
	-- now the options
	local reverseval = {}
	if type(v.value) == "table" then
		for x,val in ipairs(v.value) do
			reverseval[val]=x
		end
	else
		reverseval[v.value]=1
	end
	for i, k in ipairs ( v.option ) do
		local val, label
		if type(k) == "string" then
			val = k
			label = k
		else
			val = k.value
			label = k.label
		end
		str = str .. "<option "
		if reverseval[val] then
			str = str .. " selected"
			reverseval[val] = nil
		end
		str = str .. nv_pair("value", val) .. ">" .. mymodule.html_escape(label) .. "</option>"
	end
	for val in pairs(reverseval) do
		str = str .. '<option selected value="' .. mymodule.html_escape(val) ..'">[' .. mymodule.html_escape(val) .. ']</option>'
	end
	str = str .. "</select>"
	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 (
			'<form %s%s%s>',
			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 mymodule.form.stop ( )
	return ("</form>")
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 ( "<a " .. str .. ">" .. mymodule.html_escape(v.label) .. "</a>" )
end

return mymodule