cgi-bin/loona.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Sat, 10 Mar 2007 21:28:27 +0100
changeset 129 99b3008f0c77
parent 124 326b29cf2a7c
child 131 b8973657ca52
permissions -rw-r--r--
design is now object-oriented; added unrolling to static HTML
tmueller@0
     1
tmueller@0
     2
--
tmueller@0
     3
--	loona - tiny CMS
tmueller@0
     4
--	Written by Timm S. Mueller <tmueller at neoscientists.org>
tmueller@0
     5
--	See copyright notice in COPYRIGHT
tmueller@0
     6
--
tmueller@0
     7
tmueller@68
     8
local tek = require "tek"
tmueller@68
     9
local cgi = require "tek.cgi"
tmueller@68
    10
local posix = require "tek.posix"
tmueller@0
    11
require "tek.cgi.request"
tmueller@0
    12
require "tek.cgi.request.args"
tmueller@0
    13
require "tek.cgi.session"
tmueller@0
    14
require "tek.web"
tmueller@0
    15
require "tek.web.markup"
tmueller@0
    16
require "tek.util"
tmueller@0
    17
tmueller@23
    18
tmueller@0
    19
local boxed_G = { 
tmueller@0
    20
	string = string, table = table,
tmueller@0
    21
	assert = assert, collectgarbage = collectgarbage, dofile = dofile,
tmueller@0
    22
	error = error, getfenv = getfenv, getmetatable = getmetatable,
tmueller@0
    23
	ipairs = ipairs, load = load, loadfile = loadfile, loadstring = loadstring,
tmueller@0
    24
	next = next, pairs = pairs, pcall = pcall, print = print,
tmueller@0
    25
	rawequal = rawequal, rawget = rawget, rawset = rawset, require = require,
tmueller@0
    26
	select = select, setfenv = setfenv, setmetatable = setmetatable,
tmueller@0
    27
	tonumber = tonumber, tostring = tostring, type = type, unpack = unpack,
tmueller@0
    28
	xpcall = xpcall
tmueller@0
    29
}
tmueller@0
    30
tmueller@68
    31
local table, string, assert, unpack, ipairs, pairs, type, require =
tmueller@68
    32
	table, string, assert, unpack, ipairs, pairs, type, require
tmueller@0
    33
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv
tmueller@0
    34
local open, remove, rename, getenv, time =
tmueller@0
    35
	io.open, os.remove, os.rename, os.getenv, os.time
tmueller@0
    36
tmueller@0
    37
tmueller@0
    38
module "loona"
tmueller@0
    39
tmueller@0
    40
tmueller@129
    41
_VERSION = 3
tmueller@129
    42
_REVISION = 0
tmueller@0
    43
tmueller@0
    44
tmueller@20
    45
local function lookupname(tab, val)
tmueller@0
    46
	if tab then
tmueller@0
    47
		for i, v in ipairs(tab) do
tmueller@20
    48
			if v.name == val then
tmueller@0
    49
				return i
tmueller@0
    50
			end
tmueller@0
    51
		end
tmueller@0
    52
	end
tmueller@0
    53
end
tmueller@0
    54
tmueller@0
    55
tmueller@129
    56
local atom = { }
tmueller@20
    57
tmueller@129
    58
function atom:new(o)
tmueller@129
    59
	o = o or { }
tmueller@129
    60
	setmetatable(o, self)
tmueller@129
    61
	self.__index = self
tmueller@129
    62
	return o
tmueller@129
    63
end
tmueller@129
    64
tmueller@129
    65
local loona = atom:new(getfenv())
tmueller@129
    66
tmueller@129
    67
tmueller@129
    68
function loona:dbmsg(msg, detail)
tmueller@129
    69
 	return (msg and detail and self.authuser) and
tmueller@129
    70
 		("%s : %s"):format(msg, detail) or msg
tmueller@129
    71
end
tmueller@129
    72
tmueller@129
    73
tmueller@129
    74
function loona:checkprofilename(n)
tmueller@129
    75
	assert(n:match("^%w+$") and n ~= "current",
tmueller@129
    76
		self:dbmsg("Invalid profile name", n))
tmueller@129
    77
	return n
tmueller@129
    78
end
tmueller@129
    79
tmueller@129
    80
tmueller@129
    81
function loona:checkbodyname(s)
tmueller@129
    82
	s = s or "main"
tmueller@129
    83
	assert(s:match("^[%w_]*%w+[%w_]*$"), self:dbmsg("Invalid body name", s))
tmueller@129
    84
	return s
tmueller@129
    85
end
tmueller@129
    86
tmueller@129
    87
tmueller@129
    88
function loona:deleteprofile(p, lang)
tmueller@129
    89
	p = self.config.contentdir .. "/" .. p .. "_" .. (lang or self.lang)
tmueller@129
    90
	for e in tek.util.readdir(p) do
tmueller@129
    91
 		local success, msg = remove(p .. "/" .. e)
tmueller@129
    92
		assert(success, self:dbmsg("Error removing entry in profile", msg))
tmueller@129
    93
	end
tmueller@129
    94
	return remove(p)
tmueller@129
    95
end
tmueller@129
    96
tmueller@129
    97
tmueller@129
    98
function loona:copyprofile(newprofile, srcprofile, lang)
tmueller@129
    99
	srcprofile = srcprofile or self.profile
tmueller@129
   100
	lang = lang or self.lang
tmueller@129
   101
	local contentdir = self.config.contentdir
tmueller@129
   102
	local src = ("%s/%s_%s"):format(contentdir, srcprofile or self.profile, lang)
tmueller@129
   103
	assert(posix.stat(src, "mode") == "directory",
tmueller@129
   104
		self:dbmsg("Not a directory", src))
tmueller@129
   105
	local dst = ("%s/%s_%s"):format(contentdir, newprofile, lang)
tmueller@129
   106
	local success, msg = posix.mkdir(dst)
tmueller@129
   107
	assert(success, self:dbmsg("Error creating profile directory", msg))
tmueller@129
   108
	for e in tek.util.readdir(src) do
tmueller@129
   109
		local ext = e:match("^[^.].*%.([^.]*)$")
tmueller@129
   110
		if ext ~= "LOCK" then
tmueller@129
   111
			local f = src .. "/" .. e
tmueller@129
   112
			if posix.stat(f, "mode") == "file" then
tmueller@129
   113
				success, msg = tek.copyfile(f, dst .. "/" .. e)
tmueller@129
   114
				assert(success, self:dbmsg("Error copying file", msg))
tmueller@129
   115
			end
tmueller@129
   116
		end
tmueller@129
   117
	end
tmueller@129
   118
end
tmueller@129
   119
tmueller@129
   120
tmueller@129
   121
function loona:publishprofile(profile, lang)
tmueller@129
   122
	lang = lang or self.lang
tmueller@129
   123
	local contentdir = self.config.contentdir
tmueller@129
   124
	local newpath = ("%s/current_%s"):format(contentdir, lang)
tmueller@129
   125
	local tmppath = newpath .. "." .. self.session.name
tmueller@129
   126
	local success, msg = posix.symlink(profile .. "_" .. lang, tmppath)
tmueller@129
   127
	assert(success, self:dbmsg("Cannot create symlink", msg))
tmueller@129
   128
	success, msg = rename(tmppath, newpath)
tmueller@129
   129
	assert(success, self:dbmsg("Cannot put symlink in place", msg))
tmueller@129
   130
	
tmueller@129
   131
	-- Unroll site to static HTML
tmueller@129
   132
	
tmueller@129
   133
	self:recursesections(self.sections, function(self, s, e, path)
tmueller@129
   134
		path = path and path .. "/" .. e.name or e.name
tmueller@129
   135
 		loona:dumphtml { requestpath = path }
tmueller@129
   136
		return path
tmueller@129
   137
	end)
tmueller@129
   138
end
tmueller@129
   139
tmueller@129
   140
tmueller@129
   141
function loona:recursesections(s, func, ...)
tmueller@0
   142
	for _, e in ipairs(s) do
tmueller@129
   143
		local udata = { func(self, s, e, unpack(arg)) }
tmueller@0
   144
		if e.subs then
tmueller@129
   145
			self:recursesections(e.subs, func, unpack(udata))
tmueller@0
   146
		end
tmueller@129
   147
	end
tmueller@129
   148
end
tmueller@129
   149
tmueller@129
   150
tmueller@129
   151
function loona:indexsections()
tmueller@129
   152
	self:recursesections(self.sections, function(self, s, e)
tmueller@129
   153
		e.notvalid = (not self.secure and e.secure) or 
tmueller@129
   154
			(not self.authuser and e.secret) or nil
tmueller@129
   155
		e.notvisible = e.notvalid or not self.authuser and e.hidden or nil
tmueller@16
   156
		s[e.name] = e
tmueller@129
   157
	end)
tmueller@0
   158
end
tmueller@0
   159
tmueller@0
   160
tmueller@20
   161
--	Decompose section path into a stack of sections, returning only up to
tmueller@0
   162
--	the last valid element in the path. additionally returns the table of
tmueller@0
   163
--	the last section path element (or the default section)
tmueller@0
   164
tmueller@129
   165
function loona:getsection(path)
tmueller@129
   166
	local default = not self.authuser and self.config.defname
tmueller@129
   167
	local tab = { { entries = self.sections, name = default } }
tmueller@129
   168
	local ss = self.sections
tmueller@0
   169
	local sectionpath
tmueller@129
   170
	(path or ""):gsub("(%w+)/?", function(a)
tmueller@27
   171
		if ss then
tmueller@27
   172
			local s = ss[a]
tmueller@20
   173
			if s and not s.notvalid then
tmueller@20
   174
				sectionpath = s
tmueller@0
   175
				tab[#tab].name = a
tmueller@27
   176
				ss = s.subs
tmueller@27
   177
				if ss then
tmueller@27
   178
					table.insert(tab, { entries = ss })
tmueller@0
   179
				end
tmueller@0
   180
			else
tmueller@27
   181
				ss = nil -- stop.
tmueller@0
   182
			end
tmueller@0
   183
		end
tmueller@0
   184
	end)
tmueller@129
   185
	if not self.section and not sectionpath then
tmueller@129
   186
		sectionpath = self.sections[default]
tmueller@0
   187
		if sectionpath then
tmueller@0
   188
			table.insert(tab, { entries = sectionpath.subs })
tmueller@0
   189
		end
tmueller@0
   190
	end
tmueller@0
   191
	return tab, sectionpath
tmueller@0
   192
end
tmueller@0
   193
tmueller@0
   194
tmueller@129
   195
function loona:getpath(delimiter)
tmueller@0
   196
	local t = { }
tmueller@129
   197
	for _, menu in ipairs(self.submenus) do
tmueller@0
   198
		if menu.name then
tmueller@0
   199
			table.insert(t, menu.name)
tmueller@0
   200
		end
tmueller@0
   201
	end
tmueller@0
   202
	return table.concat(t, delimiter or "/")
tmueller@0
   203
end
tmueller@0
   204
tmueller@0
   205
tmueller@129
   206
function loona:deletesection(fname, all_bodies)
tmueller@129
   207
	local fullname = self.contentdir .. "/" .. fname
tmueller@0
   208
	local success, msg = remove(fullname)
tmueller@129
   209
	if all_bodies then
tmueller@0
   210
		local pat = "^" .. 
tmueller@0
   211
			fname:gsub("%^%$%(%)%%%.%[%]%*%+%-%?", "%%%1") .. "%..*$"
tmueller@129
   212
		for e in tek.util.readdir(self.contentdir) do
tmueller@0
   213
			if e:match(pat) then
tmueller@129
   214
				remove(self.contentdir .. "/" .. e)
tmueller@0
   215
			end
tmueller@0
   216
		end
tmueller@0
   217
	end
tmueller@0
   218
	return success, msg
tmueller@0
   219
end
tmueller@0
   220
tmueller@0
   221
tmueller@129
   222
function loona:addpath(path, e)
tmueller@129
   223
	local tab = self.sections
tmueller@0
   224
	path:gsub("(%w+)/?", function(a)
tmueller@20
   225
		if tab then
tmueller@20
   226
			local s = tab[a]
tmueller@20
   227
			if s then
tmueller@20
   228
				if not s.subs then
tmueller@20
   229
					s.subs = { }
tmueller@0
   230
				end
tmueller@20
   231
				tab = s.subs
tmueller@0
   232
			else
tmueller@20
   233
				table.insert(tab, e)
tmueller@20
   234
				tab[a] = e
tmueller@20
   235
 				tab = nil -- stop
tmueller@0
   236
			end
tmueller@0
   237
		end
tmueller@0
   238
	end)
tmueller@129
   239
	return e
tmueller@0
   240
end
tmueller@0
   241
tmueller@0
   242
tmueller@129
   243
function loona:rmpath(path)
tmueller@20
   244
	local parent
tmueller@129
   245
	local tab = self.sections
tmueller@0
   246
	path:gsub("(%w+)/?", function(a)
tmueller@20
   247
		if tab then
tmueller@20
   248
			local idx = lookupname(tab, a)
tmueller@20
   249
			if idx then
tmueller@20
   250
				if tab[idx].subs then
tmueller@20
   251
					parent = tab[idx]
tmueller@20
   252
					tab = tab[idx].subs
tmueller@0
   253
				else
tmueller@20
   254
					table.remove(tab, idx)
tmueller@43
   255
					tab[a] = nil
tmueller@20
   256
					if #tab == 0 and parent then
tmueller@20
   257
						parent.subs = nil
tmueller@0
   258
					end
tmueller@20
   259
					tab = nil
tmueller@0
   260
				end
tmueller@0
   261
			end
tmueller@0
   262
		end
tmueller@0
   263
	end)
tmueller@0
   264
end
tmueller@0
   265
tmueller@0
   266
tmueller@129
   267
function loona:checkpath(path)
tmueller@129
   268
	if path ~= "index" then -- "index" is reserved
tmueller@129
   269
		local res, idx
tmueller@129
   270
		local tab = self.sections
tmueller@129
   271
		path:gsub("(%w+)/?", function(a)
tmueller@129
   272
			if tab then
tmueller@129
   273
				local i = lookupname(tab, a)
tmueller@129
   274
				if i then
tmueller@129
   275
					res, idx = tab, i
tmueller@129
   276
					tab = tab[i].subs
tmueller@129
   277
				else
tmueller@129
   278
					res, idx = nil, nil
tmueller@129
   279
				end
tmueller@129
   280
			end
tmueller@129
   281
		end)
tmueller@129
   282
		return res, idx
tmueller@129
   283
	end
tmueller@8
   284
end
tmueller@8
   285
tmueller@8
   286
tmueller@129
   287
function loona:title()
tmueller@129
   288
	return self.section and (self.section.title or self.section.label or
tmueller@129
   289
		self.section.name) or ""
tmueller@0
   290
end
tmueller@0
   291
tmueller@0
   292
tmueller@0
   293
--	Run a site function snippet, with full error recovery
tmueller@0
   294
--	(also recovers from errors in error handling function)
tmueller@0
   295
tmueller@129
   296
function loona:dosnippet(func, errfunc)
tmueller@0
   297
	local ret = { tek.catch(func) }
tmueller@0
   298
	if ret[1] == 0 or (errfunc and tek.catch(errfunc) == 0) then
tmueller@0
   299
		return unpack(ret)
tmueller@0
   300
	end
tmueller@129
   301
	self:out("<h2>Error</h2>")
tmueller@129
   302
	self:out("<h3>" .. self:encodeform(ret[2] or "") .. "</h3>")
tmueller@129
   303
	if self.authuser then
tmueller@0
   304
		if type(ret[3]) == "string" then
tmueller@129
   305
			self:out("<p>" .. self:encodeform(ret[3]) .. "</p>")
tmueller@0
   306
		end
tmueller@129
   307
		if ret[4] then
tmueller@129
   308
			self:out("<pre>" .. self:encodeform(ret[4]) .. "</pre>")
tmueller@0
   309
		end
tmueller@0
   310
	end
tmueller@0
   311
end	
tmueller@0
   312
tmueller@0
   313
tmueller@129
   314
function loona:lockfile(file)
tmueller@129
   315
	return not self.session and true or 
tmueller@129
   316
		posix.symlink(self.session.filename, file .. ".LOCK")
tmueller@0
   317
end
tmueller@0
   318
tmueller@0
   319
tmueller@129
   320
function loona:unlockfile(file)
tmueller@129
   321
	return not self.session and true or remove(file .. ".LOCK")
tmueller@0
   322
end
tmueller@0
   323
tmueller@0
   324
tmueller@129
   325
function loona:saveindex()
tmueller@129
   326
	local tempname = self.indexfname .. "." .. self.session.name
tmueller@124
   327
	local f, msg = open(tempname, "wb")
tmueller@129
   328
	assert(f, self:dbmsg("Error opening section file for writing", msg))
tmueller@129
   329
	tek.dump(self.sections, function(...)
tmueller@124
   330
		f:write(unpack(arg))
tmueller@124
   331
	end)
tmueller@124
   332
	f:close()
tmueller@129
   333
	local success, msg = rename(tempname, self.indexfname)
tmueller@129
   334
	assert(success, self:dbmsg("Error renaming section file", msg))
tmueller@124
   335
end
tmueller@124
   336
tmueller@124
   337
tmueller@129
   338
function loona:savebody(fname, content)
tmueller@129
   339
	fname = self.contentdir .. "/" .. fname
tmueller@0
   340
	local f, msg = open(fname, "wb")
tmueller@129
   341
	assert(f, self:dbmsg("Could not open file for writing", msg))
tmueller@0
   342
	f:write(content or "")
tmueller@0
   343
	f:close()
tmueller@0
   344
end
tmueller@0
   345
tmueller@23
   346
tmueller@129
   347
function loona:runboxed(func, envitems, ...)
tmueller@129
   348
	local fenv = {
tmueller@129
   349
 		arg = arg,
tmueller@129
   350
 		loona = self
tmueller@129
   351
 	}
tmueller@129
   352
 	if envitems then
tmueller@129
   353
	 	for k, v in pairs(envitems) do
tmueller@129
   354
 			fenv[k] = v
tmueller@129
   355
 		end
tmueller@129
   356
 	end
tmueller@129
   357
 	setmetatable(fenv, { __index = boxed_G }) -- boxed global environment
tmueller@129
   358
	setfenv(func, fenv)
tmueller@129
   359
	return func()
tmueller@129
   360
end
tmueller@0
   361
tmueller@129
   362
tmueller@129
   363
function loona:include(fname, ...)
tmueller@129
   364
	assert(not fname:match("%W"), self:dbmsg("Invalid include name", fname))
tmueller@129
   365
	local fname2 = ("%s/%s.lua"):format(self.config.extdir, fname)
tmueller@0
   366
	local f, msg = open(fname2)
tmueller@129
   367
	assert(f, self:dbmsg("Cannot open file", msg))
tmueller@129
   368
	local parsed, msg = self:loadhtml(f, "loona:out", fname2)
tmueller@129
   369
	assert(parsed, self:dbmsg("Syntax error", msg))
tmueller@129
   370
	return self:runboxed(parsed)
tmueller@0
   371
end
tmueller@0
   372
tmueller@0
   373
tmueller@68
   374
--	produce link target, propagate lang, profile, session
tmueller@68
   375
tmueller@129
   376
function loona:href(section, ...)
tmueller@129
   377
	local target = self:getdocname(section)
tmueller@129
   378
-- 	if self.session or self.profile ~= self.pubprofile then
tmueller@129
   379
	if self.session then
tmueller@68
   380
		return tek.web.gethref(target, { "profile", "session", "lang", 
tmueller@68
   381
			unpack(arg) })
tmueller@0
   382
	end
tmueller@68
   383
	return tek.web.gethref(target, { "lang", unpack(arg) })
tmueller@0
   384
end
tmueller@0
   385
tmueller@129
   386
function loona:ilink(target, text, extra)
tmueller@68
   387
	return ('<a href="%s"%s>%s</a>'):format(target, extra or "", text)
tmueller@0
   388
end
tmueller@0
   389
tmueller@68
   390
--	internal link, propagation of lang, profile, session
tmueller@0
   391
tmueller@129
   392
function loona:link(section, text, ...)
tmueller@129
   393
	return self:ilink(self:href(section, unpack(arg)), text or section)
tmueller@0
   394
end
tmueller@0
   395
tmueller@68
   396
--	external link (opens in a new window), no argument propagation
tmueller@0
   397
tmueller@129
   398
function loona:elink(target, text)
tmueller@129
   399
	return self:ilink(target, text or target, not self.config.extlinksamewindow and
tmueller@83
   400
		' class="extlink" onclick="void(window.open(this.href, \'\', \'\')); return false;"')
tmueller@0
   401
end
tmueller@0
   402
tmueller@68
   403
--	plain link, no argument propagation
tmueller@68
   404
tmueller@129
   405
function loona:plink(section, text, ...)
tmueller@129
   406
	return self:ilink(tek.web.gethref(section, arg), text or section)
tmueller@68
   407
end
tmueller@68
   408
tmueller@68
   409
--	user interface link, propagation of lang, profile, session
tmueller@68
   410
tmueller@129
   411
function loona:uilink(section, text, ...)
tmueller@129
   412
	return self:ilink(self:href(section, unpack(arg)), text or section)
tmueller@68
   413
end
tmueller@68
   414
tmueller@68
   415
--	produce a hidden input value in forms
tmueller@0
   416
tmueller@129
   417
function loona:hidden(name, value)
tmueller@68
   418
	return not value and "" or
tmueller@68
   419
		('<input type="hidden" name="%s" value="%s" />'):format(name, value)
tmueller@0
   420
end
tmueller@0
   421
tmueller@0
   422
tmueller@129
   423
function loona:getprofiles(lang)
tmueller@129
   424
	lang = lang or self.lang
tmueller@129
   425
	local dir = self.config.contentdir
tmueller@0
   426
	local t = { }
tmueller@129
   427
	for f in tek.util.readdir(dir) do
tmueller@129
   428
		if posix.lstat(dir .. "/" .. f, "mode") == "directory" then
tmueller@0
   429
			local e = f:match("^(%w+)_" .. lang .. "$")
tmueller@0
   430
 			if e then
tmueller@0
   431
	 			t[e] = e
tmueller@0
   432
 	 		end
tmueller@0
   433
		end
tmueller@0
   434
	end
tmueller@0
   435
	return t
tmueller@0
   436
end
tmueller@0
   437
tmueller@0
   438
tmueller@80
   439
--	Functions to produce a hierarchical navigation menu
tmueller@80
   440
tmueller@129
   441
local newent = { name = "new", label = "[+]", action="actionnew=true" }
tmueller@124
   442
tmueller@129
   443
function loona:rmenu(level, linkf, path)
tmueller@129
   444
	local sub = (self.authuser and level == #self.submenus + 1) and
tmueller@129
   445
		{ name = "new", entries = { }} or self.submenus[level]
tmueller@124
   446
 	if sub and sub.entries then
tmueller@80
   447
		local visible = { }
tmueller@80
   448
		for _, e in ipairs(sub.entries) do
tmueller@80
   449
			if not e.notvisible then
tmueller@80
   450
				table.insert(visible, e)
tmueller@80
   451
			end
tmueller@80
   452
		end
tmueller@129
   453
		if self.authuser then
tmueller@124
   454
			table.insert(visible, newent)
tmueller@124
   455
		end
tmueller@80
   456
		if #visible > 0 then
tmueller@129
   457
			self:out('<ul id="menulevel' .. level .. '">\n')
tmueller@80
   458
			for _, e in ipairs(visible) do
tmueller@129
   459
				local label = self:encodeform(e.label or e.name)
tmueller@80
   460
				local newpath = path and path .. "/" .. e.name or e.name
tmueller@80
   461
				local active = (e.name == sub.name)
tmueller@129
   462
				self:out('<li>\n')
tmueller@129
   463
				linkf(self, newpath, label, active, e.action)
tmueller@80
   464
				if active then
tmueller@129
   465
					self:rmenu(level + 1, linkf, newpath)
tmueller@80
   466
				end
tmueller@129
   467
				self:out('</li>\n')
tmueller@80
   468
			end
tmueller@129
   469
			self:out('</ul>\n')
tmueller@80
   470
		end
tmueller@80
   471
	end
tmueller@80
   472
end
tmueller@80
   473
tmueller@129
   474
function loona:menulink(path, label, active, ...)
tmueller@129
   475
	self:out(('<a %shref="%s">%s</a>\n'):format(active and 'class="active" ' or "",
tmueller@129
   476
		self:href(path, unpack(arg)), label))
tmueller@80
   477
end
tmueller@80
   478
tmueller@129
   479
function loona:menu(level, linkf)
tmueller@129
   480
	self:rmenu(level or 1, linkf or menulink)
tmueller@80
   481
end
tmueller@80
   482
tmueller@80
   483
tmueller@129
   484
function loona:init()
tmueller@0
   485
	
tmueller@0
   486
	-- get list of languages, in order of preference
tmueller@0
   487
	
tmueller@129
   488
	self.langs = { self.args.lang and self.args.lang:match("^%w+$") }
tmueller@129
   489
	if self.config.browserlang then
tmueller@0
   490
		local s = getenv("HTTP_ACCEPT_LANGUAGE")
tmueller@0
   491
		while s do
tmueller@0
   492
			local l, r = s:match("^([%w.=]+)[,;](.*)$")
tmueller@0
   493
			l = l or s
tmueller@0
   494
			s = r
tmueller@0
   495
			if l:match("^%w+$") then
tmueller@129
   496
				table.insert(self.langs, l)
tmueller@0
   497
			end
tmueller@0
   498
		end
tmueller@0
   499
	end
tmueller@129
   500
	table.insert(self.langs, self.config.deflang)
tmueller@0
   501
	
tmueller@0
   502
	-- get list of possible profiles
tmueller@0
   503
	
tmueller@0
   504
	local profiles = { }
tmueller@129
   505
	for e in tek.util.readdir(self.config.contentdir) do
tmueller@0
   506
		profiles[e] = e
tmueller@0
   507
	end
tmueller@0
   508
	
tmueller@0
   509
	-- get pubprofile
tmueller@0
   510
	
tmueller@129
   511
	for _, lang in ipairs(self.langs) do
tmueller@129
   512
		local p = posix.readlink(self.config.contentdir .. "/current_" .. lang)
tmueller@0
   513
		p = p and p:match("^(%w+)_" .. lang .. "$")
tmueller@0
   514
		if p then
tmueller@129
   515
			self.pubprofile = p
tmueller@0
   516
			break
tmueller@0
   517
		end
tmueller@0
   518
	end
tmueller@0
   519
	
tmueller@0
   520
	-- get profile
tmueller@0
   521
	
tmueller@129
   522
	local checkprofile = self.authuser and self.args.profile or self.pubprofile or "default"
tmueller@129
   523
	for _, lang in ipairs(self.langs) do
tmueller@129
   524
		if profiles[checkprofile .. "_" .. lang] then
tmueller@129
   525
			self.profile = checkprofile
tmueller@129
   526
			self.lang = lang
tmueller@0
   527
			break
tmueller@0
   528
		end
tmueller@0
   529
	end
tmueller@0
   530
	
tmueller@129
   531
	assert(self.profile and self.lang, "Invalid profile or language")
tmueller@0
   532
	
tmueller@47
   533
	
tmueller@129
   534
	-- write back language and profile
tmueller@0
   535
tmueller@129
   536
	self.args.lang = self.lang ~= self.config.deflang and self.lang or nil
tmueller@129
   537
	self.args.profile = self.profile
tmueller@0
   538
	
tmueller@47
   539
	
tmueller@0
   540
	-- determine content directory pathname and section filename
tmueller@0
   541
	
tmueller@129
   542
	self.contentdir = ("%s/%s_%s"):format(self.config.contentdir, self.profile, self.lang)
tmueller@129
   543
 	self.indexfname = self.contentdir .. "/.sections"
tmueller@0
   544
	
tmueller@0
   545
	-- load sections
tmueller@0
   546
	
tmueller@129
   547
 	self.sections = tek.source(self.indexfname)
tmueller@0
   548
	
tmueller@20
   549
	-- index sections, determine visibility in menu
tmueller@0
   550
	
tmueller@129
   551
	self:indexsections()
tmueller@0
   552
	
tmueller@129
   553
	-- decompose request path, produce a stack of sections
tmueller@0
   554
	
tmueller@129
   555
	self.submenus, self.section = self:getsection(self.requestpath)
tmueller@0
   556
tmueller@0
   557
	-- handle redirects if not logged on
tmueller@0
   558
	
tmueller@129
   559
	if not self.authuser and self.section and self.section.redirect then
tmueller@129
   560
		self.submenus, self.section = self:getsection(self.section.redirect)
tmueller@0
   561
	end
tmueller@0
   562
			
tmueller@0
   563
	-- section path and document name (refined)
tmueller@0
   564
	
tmueller@129
   565
	self.sectionpath = self:getpath()
tmueller@129
   566
	self.sectionname = self:getpath("_")
tmueller@0
   567
tmueller@0
   568
end
tmueller@0
   569
tmueller@0
   570
tmueller@129
   571
function loona:handlechanges()
tmueller@129
   572
	
tmueller@129
   573
	local save
tmueller@0
   574
tmueller@129
   575
	if self.args.editkey == "main" then
tmueller@23
   576
		
tmueller@23
   577
		-- In main editable section:
tmueller@23
   578
		
tmueller@129
   579
		if self.args.actioncreate then
tmueller@129
   580
			
tmueller@45
   581
			-- Create new section
tmueller@129
   582
			
tmueller@129
   583
			local editname = self.args.editname:lower()
tmueller@20
   584
			assert(not editname:match("%W"),
tmueller@47
   585
				dbmsg("Invalid section name", editname))
tmueller@20
   586
			if not (section and (section.subs or section)[editname]) then
tmueller@20
   587
				local newpath = 
tmueller@129
   588
					(self.sectionpath and (self.sectionpath .. "/")) .. editname
tmueller@129
   589
				local s = self:addpath(newpath, { name = editname,
tmueller@129
   590
					label = self.args.editlabel ~= "" and self.args.editlabel or nil,
tmueller@129
   591
					title = self.args.edittitle ~= "" and self.args.edittitle or nil,
tmueller@129
   592
					redirect = self.args.editredirect ~= "" and self.args.editredirect or nil,
tmueller@129
   593
					hidden = self.args.editvisibility and true,
tmueller@129
   594
					secret = self.args.editsecrecy and true,
tmueller@129
   595
					secure = self.args.editsecure and true,
tmueller@129
   596
					creator = self.authuser,
tmueller@20
   597
					creationdate = time() })
tmueller@23
   598
				save = true
tmueller@0
   599
			end
tmueller@20
   600
		
tmueller@129
   601
		elseif self.args.actionsave then
tmueller@129
   602
			
tmueller@45
   603
			-- Save section
tmueller@129
   604
			
tmueller@129
   605
			self.section.revisiondate = time()
tmueller@129
   606
			self.section.revisioner = self.authuser
tmueller@129
   607
			save = true
tmueller@129
   608
 		
tmueller@129
   609
		elseif self.args.actionsaveprops then
tmueller@129
   610
			
tmueller@129
   611
			-- Save properties
tmueller@129
   612
			
tmueller@129
   613
			self.section.hidden = self.args.editvisibility and true
tmueller@129
   614
			self.section.secret = self.args.editsecrecy and true
tmueller@129
   615
			self.section.secure = self.args.editsecure and true
tmueller@129
   616
			self.section.label = self.args.editlabel ~= "" and self.args.editlabel or nil
tmueller@129
   617
			self.section.title = self.args.edittitle ~= "" and self.args.edittitle or nil
tmueller@129
   618
			self.section.redirect =
tmueller@129
   619
				self.args.editredirect ~= "" and self.args.editredirect or nil
tmueller@23
   620
			save = true
tmueller@20
   621
		
tmueller@129
   622
		elseif self.args.actionup then
tmueller@129
   623
			
tmueller@45
   624
			-- Move section up
tmueller@129
   625
			
tmueller@129
   626
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
   627
			if t and i > 1 then
tmueller@129
   628
				if self.profile == self.pubprofile and not self.args.actionconfirm then
tmueller@115
   629
					useralert = {
tmueller@129
   630
						text = self.locale.ALERT_MOVE_SECTION_IN_PUBLISHED_PROFILE,
tmueller@115
   631
						confirm =
tmueller@115
   632
							'<input type="submit" name="actionup" value="' .. 
tmueller@129
   633
							self.locale.MOVE .. '" /> ' ..
tmueller@129
   634
							self:hidden("actionconfirm", "true")
tmueller@115
   635
					}
tmueller@115
   636
				else
tmueller@115
   637
					local item = table.remove(t, i)
tmueller@115
   638
					table.insert(t, i - 1, item)
tmueller@115
   639
					save = true
tmueller@115
   640
				end
tmueller@0
   641
			end
tmueller@20
   642
		
tmueller@129
   643
		elseif self.args.actiondown then
tmueller@129
   644
			
tmueller@45
   645
			-- Move section down
tmueller@129
   646
			
tmueller@129
   647
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
   648
			if t and i < #t then
tmueller@129
   649
				if self.profile == self.pubprofile and not self.args.actionconfirm then
tmueller@115
   650
					useralert = {
tmueller@129
   651
						text = self.locale.ALERT_MOVE_SECTION_IN_PUBLISHED_PROFILE,
tmueller@115
   652
						confirm =
tmueller@115
   653
							'<input type="submit" name="actiondown" value="' .. 
tmueller@129
   654
							self.locale.MOVE .. '" /> ' ..
tmueller@129
   655
							self:hidden("actionconfirm", "true")
tmueller@115
   656
					}
tmueller@115
   657
				else
tmueller@115
   658
					local item = table.remove(t, i)
tmueller@115
   659
					table.insert(t, i + 1, item)
tmueller@115
   660
					save = true
tmueller@115
   661
				end
tmueller@0
   662
			end
tmueller@20
   663
		
tmueller@129
   664
		elseif self.args.actioncreateprofile and self.args.createprofile then
tmueller@129
   665
			
tmueller@23
   666
			-- Create profile
tmueller@129
   667
			
tmueller@129
   668
			local c = self:checkprofilename(self.args.createprofile:lower())
tmueller@0
   669
			if c == profile then
tmueller@129
   670
				useralert = { text = self.locale.ALERT_CANNOT_COPY_PROFILE_TO_SELF }
tmueller@0
   671
			else
tmueller@129
   672
				local profiles = self:getprofiles()
tmueller@129
   673
				if profiles[c] and not self.args.actionconfirm then
tmueller@20
   674
					useralert = {
tmueller@129
   675
						text = c == self.pubprofile and 
tmueller@129
   676
							self.locale.ALERT_OVERWRITE_PUBLISHED_PROFILE or
tmueller@129
   677
							self.locale.ALERT_OVERWRITE_EXISTING_PROFILE,
tmueller@0
   678
						confirm =
tmueller@20
   679
							'<input type="submit" name="actioncreateprofile" value="' .. 
tmueller@129
   680
							self.locale.OVERWRITE .. '" /> ' ..
tmueller@129
   681
							self:hidden("actionconfirm", "true") .. 
tmueller@129
   682
							self:hidden("createprofile", c)
tmueller@20
   683
					}
tmueller@0
   684
				else
tmueller@0
   685
					if profiles[c] then
tmueller@129
   686
						self:deleteprofile(c)
tmueller@0
   687
					end
tmueller@129
   688
					self:copyprofile(c)
tmueller@0
   689
				end
tmueller@0
   690
			end
tmueller@20
   691
		
tmueller@129
   692
		elseif self.args.actiondeleteprofile and self.args.deleteprofile then
tmueller@129
   693
			
tmueller@23
   694
			-- Delete profile
tmueller@129
   695
			
tmueller@129
   696
			local c = self:checkprofilename(self.args.deleteprofile:lower())
tmueller@129
   697
			assert(c ~= self.pubprofile, self:dbmsg("Cannot delete published profile", c))
tmueller@129
   698
			if self.args.actionconfirm then
tmueller@129
   699
				self:deleteprofile(c)
tmueller@129
   700
				self.profile = nil
tmueller@129
   701
				self.args.profile = nil
tmueller@129
   702
				self:init()
tmueller@23
   703
				save = true
tmueller@0
   704
			else
tmueller@20
   705
				useralert = { 
tmueller@129
   706
					text = self.locale.ALERT_DELETE_PROFILE,
tmueller@20
   707
					confirm = 
tmueller@20
   708
						'<input type="submit" name="actiondeleteprofile" value="' .. 
tmueller@129
   709
						self.locale.DELETE .. '" /> ' ..
tmueller@129
   710
						self:hidden("actionconfirm", "true") ..
tmueller@129
   711
						self:hidden("deleteprofile", c)
tmueller@20
   712
				}
tmueller@0
   713
			end
tmueller@20
   714
		
tmueller@129
   715
		elseif self.args.actionchangeprofile and self.args.changeprofile then
tmueller@129
   716
			
tmueller@23
   717
			-- Change profile
tmueller@129
   718
			
tmueller@129
   719
			local c = self:checkprofilename(self.args.changeprofile:lower())
tmueller@129
   720
			self.profile = c
tmueller@129
   721
			self.args.profile = c
tmueller@23
   722
			save = true
tmueller@20
   723
		
tmueller@129
   724
		elseif self.args.actionpublishprofile and self.args.publishprofile then
tmueller@129
   725
			
tmueller@23
   726
			-- Publish profile
tmueller@129
   727
			
tmueller@129
   728
			local c = self:checkprofilename(self.args.publishprofile:lower())
tmueller@129
   729
			if c ~= self.publicprofile then
tmueller@129
   730
				if self.args.actionconfirm then
tmueller@129
   731
					self:publishprofile(c)
tmueller@23
   732
					save = true
tmueller@0
   733
				else
tmueller@20
   734
					useralert = {
tmueller@129
   735
						text = self.locale.ALERT_PUBLISH_PROFILE,
tmueller@20
   736
						confirm =
tmueller@20
   737
							'<input type="submit" name="actionpublishprofile" value="' ..
tmueller@129
   738
							self.locale.PUBLISH .. '" /> ' ..
tmueller@129
   739
							self:hidden("actionconfirm", "true") ..
tmueller@129
   740
							self:hidden("publishprofile", c)
tmueller@20
   741
					}
tmueller@20
   742
				end
tmueller@20
   743
			end
tmueller@0
   744
		end
tmueller@0
   745
		
tmueller@129
   746
	end
tmueller@0
   747
	
tmueller@129
   748
	if self.args.actiondelete then
tmueller@129
   749
		
tmueller@129
   750
		-- Delete section
tmueller@129
   751
		
tmueller@129
   752
		if not self.args.actionconfirm then
tmueller@129
   753
			useralert = {
tmueller@129
   754
				text = self.profile == self.pubprofile and
tmueller@129
   755
					self.locale.ALERT_DELETE_SECTION_IN_PUBLISHED_PROFILE or
tmueller@129
   756
					self.locale.ALERT_DELETE_SECTION,
tmueller@129
   757
				confirm =
tmueller@129
   758
					'<input type="submit" name="actiondelete" value="' .. 
tmueller@129
   759
					self.locale.DELETE .. '" /> ' ..
tmueller@129
   760
					self:hidden("actionconfirm", "true")
tmueller@129
   761
			}
tmueller@129
   762
		else
tmueller@129
   763
			local key = self.args.editkey
tmueller@129
   764
			if key == "main" and not self.section.subs then
tmueller@129
   765
				self:deletesection(self.sectionname, true) -- all bodies
tmueller@129
   766
				self:rmpath(self.sectionpath) -- and node
tmueller@129
   767
			else
tmueller@129
   768
				local ext = (key == "main" and "") or "." .. key
tmueller@129
   769
				self:deletesection(self.sectionname .. ext) -- only text
tmueller@129
   770
				if self.section.dynamic then
tmueller@129
   771
					self.section.dynamic[key] = nil
tmueller@129
   772
				end
tmueller@129
   773
			end
tmueller@129
   774
			save = true
tmueller@0
   775
		end
tmueller@0
   776
	end
tmueller@129
   777
		
tmueller@129
   778
	if save then
tmueller@129
   779
		self:saveindex()
tmueller@129
   780
		self:init()
tmueller@129
   781
	end
tmueller@129
   782
	
tmueller@0
   783
end
tmueller@0
   784
tmueller@0
   785
tmueller@129
   786
function loona:encodeform(s)
tmueller@129
   787
	return cgi.encodeform(s)
tmueller@129
   788
end
tmueller@0
   789
tmueller@80
   790
tmueller@129
   791
function loona:loadhtml(src, outfunc, chunkname)
tmueller@129
   792
	return tek.web.include.load(src, outfunc, chunkname)
tmueller@129
   793
end
tmueller@80
   794
tmueller@80
   795
tmueller@129
   796
function loona:domarkup(s)
tmueller@129
   797
	return tek.web.markup.main(s)
tmueller@129
   798
end
tmueller@0
   799
tmueller@0
   800
tmueller@129
   801
function loona:expire(dir, pat, maxage)
tmueller@129
   802
	return tek.util.expire(dir, pat, maxage)
tmueller@129
   803
end
tmueller@129
   804
tmueller@129
   805
--
tmueller@129
   806
--	Get pathname of an existing content file that
tmueller@129
   807
--	the current path is determined by or defaults to
tmueller@129
   808
--
tmueller@129
   809
tmueller@129
   810
function loona:getsectionpath(bodyname, requestpath)
tmueller@129
   811
	local ext = (not bodyname or bodyname == "main") and "" or "." .. bodyname
tmueller@129
   812
	local t, path, section = { }
tmueller@129
   813
	for _, menu in ipairs(self.submenus) do
tmueller@129
   814
		if menu.entries and menu.entries[menu.name] then
tmueller@129
   815
			table.insert(t, menu.name)
tmueller@129
   816
			local fn = table.concat(t, "_")
tmueller@129
   817
			if posix.stat(self.contentdir .. "/" .. fn .. ext, "mode") == "file" then
tmueller@129
   818
				path, section = fn, menu
tmueller@129
   819
			end
tmueller@0
   820
		end
tmueller@0
   821
	end
tmueller@129
   822
	return path, ext, section
tmueller@0
   823
end
tmueller@0
   824
tmueller@20
   825
tmueller@129
   826
function loona:body(name)
tmueller@129
   827
	name = self:checkbodyname(name)
tmueller@129
   828
	local path, ext = self:getsectionpath(name)
tmueller@129
   829
	self:dosnippet(self.editable(name, path and path .. ext, self.sectionname .. ext))
tmueller@0
   830
end
tmueller@0
   831
tmueller@23
   832
tmueller@129
   833
function loona:new(o)
tmueller@0
   834
tmueller@129
   835
	local parsed, msg
tmueller@129
   836
	
tmueller@129
   837
	o = o or { }
tmueller@129
   838
	o = atom.new(self, o)
tmueller@129
   839
	
tmueller@129
   840
	o.out = o.out or function(self, s) tek.web.out(s) end
tmueller@129
   841
	o.setheader = o.setheader or function(self, s) tek.web.setheader(s) end
tmueller@129
   842
	
tmueller@129
   843
	-- Get configuration
tmueller@129
   844
	
tmueller@129
   845
	o.config = o.config or tek.source(o.conffile or "../etc/config.lua") or { }
tmueller@129
   846
	o.config.defname = o.config.defname or "home"
tmueller@129
   847
	o.config.deflang = o.config.deflang or "en"
tmueller@129
   848
	o.config.sessionmaxage = o.config.sessionmaxage or 600
tmueller@129
   849
	o.config.secureport = o.config.secureport or 443
tmueller@129
   850
	o.config.passwdfile = posix.abspath(o.config.passwdfile or "../etc/passwd.lua")
tmueller@129
   851
	o.config.sessiondir = posix.abspath(o.config.sessiondir or "../var/sessions")
tmueller@129
   852
	o.config.extdir = posix.abspath(o.config.extdir or "../extensions")
tmueller@129
   853
	o.config.contentdir = posix.abspath(o.config.contentdir or "../content")
tmueller@129
   854
	o.config.localedir = posix.abspath(o.config.localedir or "../locale")
tmueller@129
   855
	o.config.htdocsdir = posix.abspath(o.config.htdocsdir or "../htdocs")
tmueller@129
   856
	
tmueller@129
   857
	-- Create proxy for on-demand loading of locales
tmueller@129
   858
	
tmueller@129
   859
	o.locale = { }
tmueller@129
   860
	local locmt = { }
tmueller@129
   861
	locmt.__index = function(_, key)
tmueller@129
   862
		for _, l in ipairs(o.langs) do
tmueller@129
   863
			locmt.__locale = tek.source(o.config.localedir .. "/" .. l)
tmueller@129
   864
			if locmt.__locale then
tmueller@129
   865
				break
tmueller@129
   866
			end
tmueller@129
   867
		end
tmueller@129
   868
		locmt.__index = function(tab, key)
tmueller@129
   869
			return locmt.__locale[key] or key
tmueller@129
   870
		end
tmueller@129
   871
		return locmt.__locale[key] or key
tmueller@129
   872
	end
tmueller@129
   873
	setmetatable(o.locale, locmt)
tmueller@129
   874
	
tmueller@129
   875
	-- Get request, args, document, script name, request path
tmueller@129
   876
	
tmueller@129
   877
	o.request = o.request or cgi.request
tmueller@129
   878
	o.args = o.args or cgi.request.args
tmueller@129
   879
	o.session = o.session or cgi.session
tmueller@129
   880
 	
tmueller@129
   881
 	o.scriptpath = o.scriptpath or cgi.document.Path
tmueller@129
   882
	o.requesthandler = o.requesthandler or cgi.document.Handler
tmueller@129
   883
 	o.requestdocument = o.requestdocument or cgi.document.Name
tmueller@129
   884
	o.requestpath = o.requestpath or cgi.document.VirtualPath
tmueller@129
   885
tmueller@129
   886
	-- Manage login and establish session
tmueller@129
   887
	
tmueller@129
   888
	o.session.init(o.config.sessiondir, o.args.session, o.config.sessionmaxage)
tmueller@129
   889
	if o.args.login then
tmueller@129
   890
		if o.args.login == "false" then
tmueller@129
   891
			o.session.delete()
tmueller@129
   892
			o.session = nil
tmueller@129
   893
		elseif o.args.password then
tmueller@129
   894
			o.loginfailed = true
tmueller@129
   895
			local pwddb = tek.source(o.config.passwdfile)
tmueller@129
   896
			local pwdentry = pwddb[o.args.login]
tmueller@129
   897
			if pwdentry and pwdentry.password == o.args.password then
tmueller@129
   898
				o.session.data.authuser = pwdentry.username
tmueller@129
   899
				o.session.data.id = o.session.id
tmueller@129
   900
				o.loginfailed = nil
tmueller@129
   901
			end
tmueller@129
   902
		end
tmueller@129
   903
	end
tmueller@129
   904
	
tmueller@129
   905
	o.secure = o.request.Port == o.config.secureport
tmueller@129
   906
	o.authuser = o.session and o.session.data.authuser
tmueller@129
   907
	
tmueller@129
   908
	if o.nologin or not o.authuser then
tmueller@129
   909
		o.authuser = nil
tmueller@129
   910
		o.session = nil
tmueller@129
   911
-- 		o.args.session = nil -- TODO?
tmueller@129
   912
	end
tmueller@129
   913
	
tmueller@129
   914
	-- Get lang, locale, profile, section
tmueller@129
   915
	
tmueller@129
   916
	o:init()
tmueller@129
   917
	if o.authuser then
tmueller@129
   918
		o:handlechanges()
tmueller@129
   919
	end
tmueller@129
   920
	
tmueller@129
   921
	-- Current document
tmueller@129
   922
	
tmueller@129
   923
	o.document = o.requestdocument .. "/" .. o.sectionpath
tmueller@129
   924
	if o.authuser then
tmueller@129
   925
		o.getdocname = function(self, path)
tmueller@129
   926
			return path and self.requestdocument .. "/" .. path or self.requestdocument
tmueller@129
   927
		end
tmueller@129
   928
	else
tmueller@129
   929
		o.getdocname = function(self, path)
tmueller@129
   930
			path = path or self.config.defname
tmueller@129
   931
			if self:isdynamic(path) then
tmueller@129
   932
				return self.requestdocument .. "/" .. path
tmueller@129
   933
			end
tmueller@129
   934
			path = path == self.config.defname and "index" or path
tmueller@129
   935
			return "/" .. path:gsub("/", "_") .. ".html"
tmueller@129
   936
		end
tmueller@129
   937
	end
tmueller@129
   938
	
tmueller@129
   939
	-- Create "editable section" function
tmueller@129
   940
	
tmueller@129
   941
	local func, msg = o:loadhtml(open("loona/editable.lua"),
tmueller@129
   942
		"loona:out", "loona/editable.lua")
tmueller@129
   943
 	assert(func, o:dbmsg("Syntax error", msg))
tmueller@129
   944
	o.editable = o:runboxed(func)
tmueller@129
   945
	
tmueller@129
   946
	-- Save session state
tmueller@129
   947
	
tmueller@129
   948
	if o.session then
tmueller@129
   949
		o.session.save()
tmueller@129
   950
	end
tmueller@129
   951
tmueller@129
   952
	return o
tmueller@0
   953
end
tmueller@0
   954
tmueller@23
   955
tmueller@129
   956
function loona:execute(fname)
tmueller@129
   957
	self:indexdynamic() -- TODO: this a solution?
tmueller@129
   958
	fname = fname or self.requesthandler
tmueller@129
   959
	local parsed, msg = self:loadhtml(open(fname), "loona:out", fname)
tmueller@129
   960
	assert(parsed, self:dbmsg("HTML/Lua parsing failed", msg))
tmueller@129
   961
	self:runboxed(parsed)
tmueller@129
   962
	return self
tmueller@129
   963
end
tmueller@80
   964
tmueller@80
   965
tmueller@129
   966
function loona:indexdynamic()
tmueller@129
   967
	self:recursesections(self.sections, function(self, s, e, path, dynamic)
tmueller@129
   968
		path = path and path .. "_" .. e.name or e.name
tmueller@129
   969
		dynamic = dynamic or { }
tmueller@129
   970
		for k in pairs(e.dynamic or { }) do
tmueller@129
   971
			dynamic[k] = true
tmueller@129
   972
		end
tmueller@129
   973
		for k in pairs(dynamic) do
tmueller@129
   974
			local ext = (k == "main" and "") or "." .. k
tmueller@129
   975
			if posix.stat(self.contentdir .. "/" .. path .. ext, "mode") == "file" then
tmueller@129
   976
				dynamic[k] = e.dynamic and e.dynamic[k]
tmueller@129
   977
			end
tmueller@129
   978
		end
tmueller@129
   979
		local n = 0
tmueller@129
   980
		for k in pairs(dynamic) do
tmueller@129
   981
			n = n + 1
tmueller@129
   982
		end
tmueller@129
   983
		if n > 0 then
tmueller@129
   984
			e.dynamic = { }
tmueller@129
   985
			for k in pairs(dynamic) do
tmueller@129
   986
				e.dynamic[k] = true
tmueller@129
   987
			end
tmueller@129
   988
		else
tmueller@129
   989
			e.dynamic = nil
tmueller@129
   990
		end
tmueller@129
   991
		return path, dynamic
tmueller@129
   992
	end)
tmueller@129
   993
end
tmueller@80
   994
tmueller@0
   995
tmueller@129
   996
function loona:isdynamic(path)
tmueller@129
   997
	path = path or self.sectionpath
tmueller@129
   998
	local t, i = self:checkpath(path)
tmueller@129
   999
	return t and t[i].dynamic
tmueller@129
  1000
end
tmueller@0
  1001
tmueller@0
  1002
tmueller@129
  1003
function loona:dumphtml(o)
tmueller@129
  1004
	local outbuf = { }
tmueller@129
  1005
	o = o or { }
tmueller@129
  1006
	o.nologin = true
tmueller@129
  1007
	o.out = function(self, s) table.insert(outbuf, s) end
tmueller@129
  1008
	o.setheader = function(self, s) end
tmueller@129
  1009
	o = self:new(o):execute()
tmueller@129
  1010
	if not o:isdynamic() then
tmueller@129
  1011
		local path = o.sectionname
tmueller@129
  1012
		path = path == o.config.defname and "index" or path
tmueller@129
  1013
		local fname = o.config.htdocsdir .. "/" .. path .. ".html"
tmueller@129
  1014
		local fh = open(fname, "wb")
tmueller@129
  1015
		fh:write(unpack(outbuf))
tmueller@129
  1016
		fh:close()
tmueller@0
  1017
	end
tmueller@0
  1018
end