summaryrefslogtreecommitdiff
path: root/marigold.lua
blob: 37cbdfb32554d0ec279600d7962a5e83acc99b04 (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
-- ANTI-CAPITALIST SOFTWARE LICENSE (v 1.4)
-- 
--  marigold-cgi Copyright (c) 2022 Kate Swanson
-- 
-- This is anti-capitalist software, released for free use by individuals and 
-- organizations that do not operate by capitalist principles.
-- 
-- Permission is hereby granted, free of charge, to any person or organization (
-- the "User") obtaining a copy of this software and associated documentation 
-- files (the "Software"), to use, copy, modify, merge, distribute, and/or sell 
-- copies of the Software, subject to the following conditions:
-- 
-- 1. The above copyright notice and this permission notice shall be included in 
-- all copies or modified versions of the Software.
-- 
-- 2. The User is one of the following:
-- 	a. An individual person, laboring for themselves
-- 	b. A non-profit organization
-- 	c. An educational institution
-- 	d. An organization that seeks shared profit for all of its members, and allows 
-- 	   non-members to set the cost of their labor
-- 
-- 3. If the User is an organization with owners, then all owners are workers and 
--    all workers are owners with equal equity and/or equal vote.
-- 
-- 4. If the User is an organization, then the User is not law enforcement or 
--    military, or working for or under either.
-- 
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY 
-- KIND, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
-- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE 
-- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
-- CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


local marigold = {}

marigold.get_metavars = function()
	local vars = {
		"AUTH_TYPE", "CONTENT_LENGTH",
		"CONTENT_TYPE", "GATEWAY_INTERFACE",
		"PATH_INFO", "PATH_TRANSLATED",
		"QUERY_STRING", "REMOTE_ADDR",
		"REMOTE_HOST", "REMOTE_IDENT",
		"REMOTE_USER", "REQUEST_METHOD",
		"SCRIPT_NAME", "SERVER_NAME",
		"SERVER_PORT", "SERVER_PROTOCOL",
		"SERVER_SOFTWARE"
	}	

	local metavars = {}
	for _, var in ipairs(vars) do
		metavars[string.lower(var)] = os.getenv(var)
	end
	return metavars
end


marigold.h = function(tag_type, content, tbl)
	if type(content) == 'table' and tbl == nil then
		tbl = content
		content = ''
	end

	local tag = {}
	tag.tag = tag_type
	tag.content = content
	tag.attributes = {}
	tag.children = {}

	if tbl then
		-- add attributes
		for k, v in pairs(tbl) do
			if type(k) == 'string' then
				tag.attributes[k] = v
			end
		end
		-- add children
		for _, child in ipairs(tbl) do
			table.insert(tag.children, child)
		end
	end
	return tag
end


marigold.html = function(tbl, indent_level)
	indent_level = indent_level or 0

	-- generate attribute strings
	local attributes = {}
	for k, v in pairs(tbl.attributes) do
		table.insert(attributes, string.format(' %s="%s"', k, v))
	end
	if test then
		-- sort alphabetically for well-defined testing
		table.sort(attributes)
	end
	local a = ''
	for _, attrib in ipairs(attributes) do
		a = a .. attrib
	end

	local open = string.format('<%s%s>', tbl.tag, a)
	local close = string.format('</%s>', tbl.tag)

	return string.format('%s%s%s', open, tbl.content, close)
end


return marigold