<commit_before>local st = require "util.stanza";
local datetime = require"util.datetime";
local jid_split = require"util.jid".split;
local nodeprep = require"util.encodings".stringprep.nodeprep;
local uuid = require"util.uuid".generate;
local it = require"util.iterators";
local gettime = require"socket".gettime;

local archive = module:open_store("archive2", "archive");

-- Support both old and new MUC code
local mod_muc = module:depends"muc";
local rooms = rawget(mod_muc, "rooms");
local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end;
local new_muc = not rooms;
if new_muc then
	rooms = module:shared"muc/rooms";
end
local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
	function (jid)
		return rooms[jid];
	end

local function get_room(name)
	local jid = name .. '@' .. module.host;
	return get_room_from_jid(jid);
end

module:depends"http";

local function template(data)
	--[[ DOC
	Like util.template, but deals with plain text
	Returns a closure that is called with a table of values
	{name} is substituted for values["name"] and is XML escaped
	{name!} is substituted without XML escaping
	{name?} is optional and is replaced with an empty string if no value exists
	]]
	return function(values)
		return (data:gsub("{([^}]-)(%p?)}", function (name, opt)
			local value = values[name];
			if value then
				if opt ~= "!" then
					return st.xml_escape(value);
				end
				return value;
			elseif opt == "?" then
				return "";
			end
		end));
	end
end

-- TODO Move templates into files
local base = template(template[[
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="canonical" href="{canonical}">
<title>{title}</title>
<style>
body{background-color:#eeeeec;margin:1ex 0;padding-bottom:3em;font-family:Arial,Helvetica,sans-serif;}
header,footer{margin:1ex 1em;}
footer{font-size:smaller;color:#babdb6;}
.content{background-color:white;padding:1em;list-style-position:inside;}
nav{font-size:x-large;margin:1ex 2em;}
nav a{text-decoration:none;}
nav a.up{font-size:smaller;}
nav a.next{float:right;}
a:link,a:visited{color:#2e3436;text-decoration:none;}
a:link:hover,a:visited:hover{color:#3465a4;}
ul,ol{padding:0;}
li{list-style:none;}
hr{display:none;}
li time{float:right;font-size:small;opacity:0.2;}
li:hover time{opacity:1;}
.room-list .name{font-size:larger;}
q.body::before,q.body::after{content:"";}
.presence .verb{font-style:normal;color:#30c030;}
.presence.unavailable .verb{color:#c03030;}
</style>
</head>
<body>
<header>
<h1>{title}</h1>
{header!}
</header>
<hr>
<div class="content">
{body!}
</div>
<hr>
<footer>
{footer!}
<div class="powered-by">Prosody {prosody_version?}</div>
</footer>
</body>
</html>
]] { prosody_version = prosody.version });

local dates_template = template(base{
	title = "Logs for room {room}";
	header = [[
<nav>
<a href=".." class="up">Back to room list</a>
</nav>
]];
	body = [[
<nav>
<ul class="dates">
{lines!}</ul>
</nav>
]];
	footer = "";
})

local date_line_template = template[[
<li><a href="{date}">{date}</a></li>
]];

local page_template = template(base{
	title = "Logs for room {room} on {date}";
	header = [[
<nav>
<a class="up" href=".">Back to date list</a>
<br>
<a class="prev" href="{prev}">← {prev}</a>
<a class="next" href="{next}">{next} →</a>
</nav>
]];
	body = [[
<ol class="chat-logs">
{logs!}</ol>
]];
	footer = [[
<nav>
<div>
<a class="prev" href="{prev}">← {prev}</a>
<a class="next" href="{next}">{next} →</a>
</div>
</nav>
<script>
(function () {
	var timeTags = document.getElementsByTagName("time");
	var i = 0;
	var date;
	while(timeTags[i]) {
		date = new Date(timeTags[i].getAttribute("datetime"));
		timeTags[i].textContent = date.toLocaleTimeString();
		timeTags[i].setAttribute("title", date.toString());
		i++;
	}
})();
</script>
]];
});

local line_template = template[[
<li class="{st_name} {st_type?}" id="{key}"><commit_msg>mod_http_muc_log: Render arrows in prev/next links using CSS<commit_after>local st = require "util.stanza";
local datetime = require"util.datetime";
local jid_split = require"util.jid".split;
local nodeprep = require"util.encodings".stringprep.nodeprep;
local uuid = require"util.uuid".generate;
local it = require"util.iterators";
local gettime = require"socket".gettime;

local archive = module:open_store("archive2", "archive");

-- Support both old and new MUC code
local mod_muc = module:depends"muc";
local rooms = rawget(mod_muc, "rooms");
local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end;
local new_muc = not rooms;
if new_muc then
	rooms = module:shared"muc/rooms";
end
local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
	function (jid)
		return rooms[jid];
	end

local function get_room(name)
	local jid = name .. '@' .. module.host;
	return get_room_from_jid(jid);
end

module:depends"http";

local function template(data)
	--[[ DOC
	Like util.template, but deals with plain text
	Returns a closure that is called with a table of values
	{name} is substituted for values["name"] and is XML escaped
	{name!} is substituted without XML escaping
	{name?} is optional and is replaced with an empty string if no value exists
	]]
	return function(values)
		return (data:gsub("{([^}]-)(%p?)}", function (name, opt)
			local value = values[name];
			if value then
				if opt ~= "!" then
					return st.xml_escape(value);
				end
				return value;
			elseif opt == "?" then
				return "";
			end
		end));
	end
end

-- TODO Move templates into files
local base = template(template[[
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="canonical" href="{canonical}">
<title>{title}</title>
<style>
body{background-color:#eeeeec;margin:1ex 0;padding-bottom:3em;font-family:Arial,Helvetica,sans-serif;}
header,footer{margin:1ex 1em;}
footer{font-size:smaller;color:#babdb6;}
.content{background-color:white;padding:1em;list-style-position:inside;}
nav{font-size:x-large;margin:1ex 2em;}
nav a{text-decoration:none;}
nav a.up{font-size:smaller;}
nav a.prev{float:left;}
nav a.next{float:right;}
nav a.next::after{content:" →";}
nav a.prev::before{content:"← ";}
a:link,a:visited{color:#2e3436;text-decoration:none;}
a:link:hover,a:visited:hover{color:#3465a4;}
ul,ol{padding:0;}
li{list-style:none;}
hr{display:none;}
li time{float:right;font-size:small;opacity:0.2;}
li:hover time{opacity:1;}
.room-list .name{font-size:larger;}
q.body::before,q.body::after{content:"";}
.presence .verb{font-style:normal;color:#30c030;}
.presence.unavailable .verb{color:#c03030;}
</style>
</head>
<body>
<header>
<h1>{title}</h1>
{header!}
</header>
<hr>
<div class="content">
{body!}
</div>
<hr>
<footer>
{footer!}
<div class="powered-by">Prosody {prosody_version?}</div>
</footer>
</body>
</html>
]] { prosody_version = prosody.version });

local dates_template = template(base{
	title = "Logs for room {room}";
	header = [[
<nav>
<a href=".." class="up">Back to room list</a>
</nav>
]];
	body = [[
<nav>
<ul class="dates">
{lines!}</ul>
</nav>
]];
	footer = "";
})

local date_line_template = template[[
<li><a href="{date}">{date}</a></li>
]];

local page_template = template(base{
	title = "Logs for room {room} on {date}";
	header = [[
<nav>
<a class="up" href=".">Back to date list</a>
<br>
<a class="prev" href="{prev}">{prev}</a>
<a class="next" href="{next}">{next}</a>
</nav>
]];
	body = [[
<ol class="chat-logs">
{logs!}</ol>
]];
	footer = [[
<nav>
<div>
<a class="prev" href="{prev}">{prev}</a>
<a class="next" href="{next}">{next}</a>
</div>
</nav>
<script>
(function () {
	var timeTags = document.getElementsByTagName("time");
	var i = 0;
	var date;
	while(timeTags[i]) {
		date = new Date(timeTags[i].getAttribute("datetime"));
		timeTags[i].textContent = date.toLocaleTimeString();
		timeTags[i].setAttribute("title", date.toString());
		i++;
	}
})();
</script>
]];
});

local line_template = template[[
<li class="{st_name} {st_type?}" id="{key}">
{'fraction_non_alphanumeric': 0.19425461908377625, 'fraction_numerical': 0.00961781827385472, 'mean_word_length': 5.708828522920204, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6185639', 'n_tokens_mistral': 3086, 'n_tokens_neox': 2901, 'n_words': 675}