mirror of
https://github.com/Spengreb/sync.git
synced 2026-05-15 03:52:06 +00:00
We now allow server operators to customize the /r/ part of the channel links The new config option in the template is commented and the config module validates and will terminate with status 78 if an improper value is used. We've also dropped some old cruft and uses a more elegant method to assign CHANNEL.name Resolves #668
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
var pug = require("pug");
|
|
var fs = require("fs");
|
|
var path = require("path");
|
|
var Config = require("../config");
|
|
var templates = path.join(__dirname, "..", "..", "templates");
|
|
var cache = {};
|
|
|
|
/**
|
|
* Merges locals with globals for pug rendering
|
|
*/
|
|
function merge(locals, res) {
|
|
var _locals = {
|
|
siteTitle: Config.get("html-template.title"),
|
|
siteDescription: Config.get("html-template.description"),
|
|
siteAuthor: "Calvin 'calzoneman' 'cyzon' Montgomery",
|
|
loginDomain: Config.get("https.enabled") ? Config.get("https.full-address")
|
|
: Config.get("http.full-address"),
|
|
csrfToken: typeof res.req.csrfToken === 'function' ? res.req.csrfToken() : '',
|
|
baseUrl: getBaseUrl(res),
|
|
channelPath: Config.get("channel-path"),
|
|
};
|
|
if (typeof locals !== "object") {
|
|
return _locals;
|
|
}
|
|
for (var key in locals) {
|
|
_locals[key] = locals[key];
|
|
}
|
|
return _locals;
|
|
}
|
|
|
|
function getBaseUrl(res) {
|
|
var req = res.req;
|
|
return req.realProtocol + "://" + req.header("host");
|
|
}
|
|
|
|
/**
|
|
* Renders and serves a pug template
|
|
*/
|
|
function sendPug(res, view, locals) {
|
|
if (!locals) {
|
|
locals = {};
|
|
}
|
|
locals.loggedIn = locals.loggedIn || !!res.user;
|
|
locals.loginName = locals.loginName || res.user ? res.user.name : false;
|
|
locals.superadmin = locals.superadmin || res.user ? res.user.global_rank >= 255 : false;
|
|
if (!(view in cache) || Config.get("debug")) {
|
|
var file = path.join(templates, view + ".pug");
|
|
var fn = pug.compile(fs.readFileSync(file), {
|
|
filename: file,
|
|
pretty: !Config.get("http.minify")
|
|
});
|
|
cache[view] = fn;
|
|
}
|
|
var html = cache[view](merge(locals, res));
|
|
res.send(html);
|
|
}
|
|
|
|
module.exports = {
|
|
sendPug: sendPug
|
|
};
|