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