Move fillTemplate to Templater class

This commit is contained in:
Robbie Antenesse 2019-01-10 15:31:08 -07:00
parent 646fe6ac14
commit 3564783664
9 changed files with 81 additions and 68 deletions

View File

@ -1,8 +1,8 @@
module.exports = function (app) { module.exports = function (app) {
app.server.get('/about', (req, res) => { app.server.get('/about', (req, res) => {
const resourcePath = (req.url.substr(-1) === '/' ? '../' : './'); const resourcePath = (req.url.substr(-1) === '/' ? '../' : './');
const body = app.fillTemplate('./templates/pages/about.html', { resourcePath }); const body = app.templater.fill('./templates/pages/about.html', { resourcePath });
const html = app.fillTemplate('./templates/htmlContainer.html', { title: 'About', body }); const html = app.templater.fill('./templates/htmlContainer.html', { title: 'About', body });
if (html) { if (html) {
res.send(html); res.send(html);
} else { } else {

View File

@ -1,10 +1,10 @@
module.exports = function (app) { module.exports = function (app) {
app.server.get('/give', (req, res) => { app.server.get('/give', (req, res) => {
const resourcePath = (req.url.substr(-1) === '/' ? '../' : './'); const resourcePath = (req.url.substr(-1) === '/' ? '../' : './');
let body = app.fillTemplate('./templates/pages/uploadForm.html', { resourcePath }); let body = app.templater.fill('./templates/pages/uploadForm.html', { resourcePath });
body = app.replaceBodyWithTooManyBooksWarning(body); body = app.replaceBodyWithTooManyBooksWarning(body);
const html = app.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body }); const html = app.templater.fill('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body });
res.send(html); res.send(html);
}); });
} }

View File

@ -18,10 +18,10 @@ module.exports = function (app) {
const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY'); const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY');
const removed = fecha.format(new Date(parseInt(id)), 'hh:mm:ssA on dddd MMMM Do, YYYY'); const removed = fecha.format(new Date(parseInt(id)), 'hh:mm:ssA on dddd MMMM Do, YYYY');
const removedTag = '<div class="control"><div class="tags has-addons"><span class="tag">Taken</span><span class="tag is-warning">' + removed + '</span></div></div>'; const removedTag = '<div class="control"><div class="tags has-addons"><span class="tag">Taken</span><span class="tag is-warning">' + removed + '</span></div></div>';
const modal = app.fillTemplate('./templates/elements/modalCard.html', { const modal = app.templater.fill('./templates/elements/modalCard.html', {
id, id,
header: '<h2 class="title">' + bookData.title + '</h2><h4 class="subtitle">' + bookData.author + '</h4>', header: '<h2 class="title">' + bookData.title + '</h2><h4 class="subtitle">' + bookData.author + '</h4>',
content: app.fillTemplate('./templates/elements/bookInfo.html', { content: app.templater.fill('./templates/elements/bookInfo.html', {
contributor: bookData.contributor, contributor: bookData.contributor,
fileFormat: bookData.fileType, fileFormat: bookData.fileType,
added, added,
@ -30,7 +30,7 @@ module.exports = function (app) {
}), }),
footer: '<a class="button close">Close</a>', footer: '<a class="button close">Close</a>',
}); });
return app.fillTemplate('./templates/elements/book.html', { return app.templater.fill('./templates/elements/book.html', {
id, id,
title: bookData.title, title: bookData.title,
author: bookData.author, author: bookData.author,
@ -44,7 +44,7 @@ module.exports = function (app) {
} }
const body = '<h2 class="title">History</h2><div class="columns is-multiline">' + history + '</div>'; const body = '<h2 class="title">History</h2><div class="columns is-multiline">' + history + '</div>';
const html = app.fillTemplate('./templates/htmlContainer.html', { const html = app.templater.fill('./templates/htmlContainer.html', {
title: 'History', title: 'History',
resourcePath: (req.url.substr(-1) === '/' ? '../' : './'), resourcePath: (req.url.substr(-1) === '/' ? '../' : './'),
body body

View File

@ -19,25 +19,25 @@ module.exports = function (app) {
const id = fileName.replace('.json', ''); const id = fileName.replace('.json', '');
const confirmId = 'confirm_' + id; const confirmId = 'confirm_' + id;
const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY'); const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY');
const modal = app.fillTemplate('./templates/elements/modalCard.html', { const modal = app.templater.fill('./templates/elements/modalCard.html', {
id, id,
header: '<h2 class="title">' + bookData.title + '</h2><h4 class="subtitle">' + bookData.author + '</h4>', header: '<h2 class="title">' + bookData.title + '</h2><h4 class="subtitle">' + bookData.author + '</h4>',
content: app.fillTemplate('./templates/elements/bookInfo.html', { content: app.templater.fill('./templates/elements/bookInfo.html', {
contributor: bookData.contributor, contributor: bookData.contributor,
fileFormat: bookData.fileType, fileFormat: bookData.fileType,
added, added,
summary: snarkdown(bookData.summary), summary: snarkdown(bookData.summary),
}) })
+ app.fillTemplate('./templates/elements/modal.html', { + app.templater.fill('./templates/elements/modal.html', {
id: confirmId, id: confirmId,
content: app.fillTemplate('./templates/elements/messageBox.html', { content: app.templater.fill('./templates/elements/messageBox.html', {
header: 'Download Your Book', header: 'Download Your Book',
message: app.fillTemplate('./templates/elements/takeConfirm.html', { id }), message: app.templater.fill('./templates/elements/takeConfirm.html', { id }),
}), }),
}), }),
footer: '<a class="button close">Close</a> <a class="button is-success modal-button" data-modal="' + confirmId + '">Take Book</a>', footer: '<a class="button close">Close</a> <a class="button is-success modal-button" data-modal="' + confirmId + '">Take Book</a>',
}); });
return app.fillTemplate('./templates/elements/book.html', { return app.templater.fill('./templates/elements/book.html', {
id, id,
title: bookData.title, title: bookData.title,
author: bookData.author, author: bookData.author,
@ -51,7 +51,7 @@ module.exports = function (app) {
} }
const body = '<h2 class="title">Available Books</h2><div class="columns is-multiline">' + books + '</div>'; const body = '<h2 class="title">Available Books</h2><div class="columns is-multiline">' + books + '</div>';
const html = app.fillTemplate('./templates/htmlContainer.html', { const html = app.templater.fill('./templates/htmlContainer.html', {
title: 'View', title: 'View',
resourcePath: (req.url.substr(-1) === '/' ? '../' : './'), resourcePath: (req.url.substr(-1) === '/' ? '../' : './'),
body body

View File

@ -5,12 +5,12 @@ module.exports = function (app) {
app.server.get('/tools', (req, res) => { app.server.get('/tools', (req, res) => {
if (req.query.pass === settings.toolsPassword) { if (req.query.pass === settings.toolsPassword) {
const templateValues = {}; const templateValues = {};
let html = app.fillTemplate('./templates/pages/tools.html', templateValues); let html = app.templater.fill('./templates/pages/tools.html', templateValues);
if (req.query.do && ['resetVisitors'].includes(req.query.do)) { if (req.query.do && ['resetVisitors'].includes(req.query.do)) {
app.connections = 0; app.connections = 0;
templateValues.resetVisitors = 'Done!'; templateValues.resetVisitors = 'Done!';
html = app.fillTemplate('./templates/pages/tools.html', templateValues); html = app.templater.fill('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
} else if (req.query.dl && ['files', 'history'].includes(req.query.dl)) { } else if (req.query.dl && ['files', 'history'].includes(req.query.dl)) {
const onezip = require('onezip'); const onezip = require('onezip');
@ -25,7 +25,7 @@ module.exports = function (app) {
.on('error', (error) => { .on('error', (error) => {
console.error(error); console.error(error);
templateValues[dl + 'Download'] = 'Something went wrong: ' + JSON.stringify(error); templateValues[dl + 'Download'] = 'Something went wrong: ' + JSON.stringify(error);
html = app.fillTemplate('./templates/pages/tools.html', templateValues); html = app.templater.fill('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
}) })
.on('end', () => { .on('end', () => {
@ -33,7 +33,7 @@ module.exports = function (app) {
let backupLocation = saveLocation.replace(/\\/g, '/'); let backupLocation = saveLocation.replace(/\\/g, '/');
backupLocation = backupLocation.substr(backupLocation.lastIndexOf('/')); backupLocation = backupLocation.substr(backupLocation.lastIndexOf('/'));
templateValues[dl + 'Download'] = '<a download href="' + encodeURI('./files' + backupLocation) + '">Download</a> (This will be removed from the server in 1 hour)'; templateValues[dl + 'Download'] = '<a download href="' + encodeURI('./files' + backupLocation) + '">Download</a> (This will be removed from the server in 1 hour)';
html = app.fillTemplate('./templates/pages/tools.html', templateValues); html = app.templater.fill('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
console.log('Will delete ' + saveLocation + ' in 1 hour'); console.log('Will delete ' + saveLocation + ' in 1 hour');
setTimeout(() => { setTimeout(() => {

View File

@ -8,32 +8,32 @@ module.exports = function (app) {
const { book } = req.files; const { book } = req.files;
const fileType = book.name.substr(book.name.lastIndexOf('.')); const fileType = book.name.substr(book.name.lastIndexOf('.'));
app.addBook({ book, title, author, summary, contributor, fileType }, () => { app.addBook({ book, title, author, summary, contributor, fileType }, () => {
const messageBox = app.fillTemplate('./templates/elements/messageBox.html', { const messageBox = app.templater.fill('./templates/elements/messageBox.html', {
style: 'is-success', style: 'is-success',
header: 'Upload Successful', header: 'Upload Successful',
message: 'Thank you for your contribution!' message: 'Thank you for your contribution!'
}); });
const modal = app.fillTemplate('./templates/elements/modal.html', { const modal = app.templater.fill('./templates/elements/modal.html', {
isActive: 'is-active', isActive: 'is-active',
content: messageBox, content: messageBox,
}); });
let body = app.fillTemplate('./templates/pages/uploadForm.html', { resourcePath }); let body = app.templater.fill('./templates/pages/uploadForm.html', { resourcePath });
body = app.replaceBodyWithTooManyBooksWarning(body); body = app.replaceBodyWithTooManyBooksWarning(body);
const html = app.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, modal }); const html = app.templater.fill('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, modal });
res.send(html); res.send(html);
}, (err) => { }, (err) => {
const messageBox = app.fillTemplate('./templates/elements/messageBox.html', { const messageBox = app.templater.fill('./templates/elements/messageBox.html', {
style: 'is-danger', style: 'is-danger',
header: 'Upload Failed', header: 'Upload Failed',
message: err, message: err,
}); });
const modal = app.fillTemplate('./templates/elements/modal.html', { const modal = app.templater.fill('./templates/elements/modal.html', {
isActive: 'is-active', isActive: 'is-active',
content: messageBox, content: messageBox,
}); });
let body = app.fillTemplate('./templates/pages/uploadForm.html', { resourcePath, title, author, summary, contributor }); let body = app.templater.fill('./templates/pages/uploadForm.html', { resourcePath, title, author, summary, contributor });
body = app.replaceBodyWithTooManyBooksWarning(body); body = app.replaceBodyWithTooManyBooksWarning(body);
const html = app.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, modal }); const html = app.templater.fill('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, modal });
res.send(html); res.send(html);
}); });
} else { } else {
@ -47,14 +47,14 @@ module.exports = function (app) {
if (!req.body.hasOwnProperty('summary') || req.body.summary.trim() === '') { if (!req.body.hasOwnProperty('summary') || req.body.summary.trim() === '') {
errorMessage += (errorMessage.length > 0 ? '<br>' : '') + 'You have not written a summary.'; errorMessage += (errorMessage.length > 0 ? '<br>' : '') + 'You have not written a summary.';
} }
const message = app.fillTemplate('./templates/elements/messageBox.html', { const message = app.templater.fill('./templates/elements/messageBox.html', {
style: 'is-danger', style: 'is-danger',
header: 'Missing Required Fields', header: 'Missing Required Fields',
message: errorMessage, message: errorMessage,
}); });
let body = app.fillTemplate('./templates/pages/uploadForm.html', { resourcePath, title, author, summary, contributor }); let body = app.templater.fill('./templates/pages/uploadForm.html', { resourcePath, title, author, summary, contributor });
body = app.replaceBodyWithTooManyBooksWarning(body); body = app.replaceBodyWithTooManyBooksWarning(body);
const html = app.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, message }); const html = app.templater.fill('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, message });
res.send(html); res.send(html);
} }
}); });

View File

@ -5,7 +5,7 @@ module.exports = function (app) {
app.server.post('/tools', (req, res) => { app.server.post('/tools', (req, res) => {
if (req.query.pass === settings.toolsPassword) { if (req.query.pass === settings.toolsPassword) {
const templateValues = {}; const templateValues = {};
let html = app.fillTemplate('./templates/pages/tools.html', templateValues); let html = app.templater.fill('./templates/pages/tools.html', templateValues);
const { files } = req; const { files } = req;
if (Object.keys(files).length > 0) { if (Object.keys(files).length > 0) {
@ -17,7 +17,7 @@ module.exports = function (app) {
if (err) { if (err) {
console.error(error); console.error(error);
templateValues[backupType + 'UploadSuccess'] = 'Could not upload the file.'; templateValues[backupType + 'UploadSuccess'] = 'Could not upload the file.';
html = app.fillTemplate('./templates/pages/tools.html', templateValues); html = app.templater.fill('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
} else { } else {
onezip.extract(uploadPath, path.resolve('./public', backupType)) onezip.extract(uploadPath, path.resolve('./public', backupType))
@ -27,12 +27,12 @@ module.exports = function (app) {
.on('error', (error) => { .on('error', (error) => {
console.error(error); console.error(error);
templateValues[backupType + 'UploadSuccess'] = 'Something went wrong: ' + JSON.stringify(error); templateValues[backupType + 'UploadSuccess'] = 'Something went wrong: ' + JSON.stringify(error);
html = app.fillTemplate('./templates/pages/tools.html', templateValues); html = app.templater.fill('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
}) })
.on('end', () => { .on('end', () => {
templateValues[backupType + 'UploadSuccess'] = 'Uploaded Successfully!'; templateValues[backupType + 'UploadSuccess'] = 'Uploaded Successfully!';
html = app.fillTemplate('./templates/pages/tools.html', templateValues); html = app.templater.fill('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
fs.unlink(uploadPath, (err) => { fs.unlink(uploadPath, (err) => {
if (err) { if (err) {
@ -46,7 +46,7 @@ module.exports = function (app) {
}); });
} else { } else {
templateValues['generalError'] = '<p>' + backupType + ' is not a valid backup type.</p>'; templateValues['generalError'] = '<p>' + backupType + ' is not a valid backup type.</p>';
html = app.fillTemplate('./templates/pages/tools.html', templateValues); html = app.templater.fill('./templates/pages/tools.html', templateValues);
res.send(html); res.send(html);
} }
} else { } else {

View File

@ -13,6 +13,8 @@ const privateKey = settings.sslPrivateKey ? fs.readFileSync(settings.sslPrivateK
const certificate = settings.sslCertificate ? fs.readFileSync(settings.sslCertificate, 'utf8') : null; const certificate = settings.sslCertificate ? fs.readFileSync(settings.sslCertificate, 'utf8') : null;
const ca = settings.sslCertificateAuthority ? fs.readFileSync(settings.sslCertificateAuthority, 'utf8') : null; const ca = settings.sslCertificateAuthority ? fs.readFileSync(settings.sslCertificateAuthority, 'utf8') : null;
const Templater = require('./templates/Templater');
function Server () { function Server () {
this.server = express(); this.server = express();
this.http = http.Server(this.server); this.http = http.Server(this.server);
@ -28,7 +30,7 @@ function Server () {
this.fileLocation = path.resolve(settings.fileLocation); this.fileLocation = path.resolve(settings.fileLocation);
this.historyLocation = path.resolve(settings.historyLocation); this.historyLocation = path.resolve(settings.historyLocation);
this.templateCache = {}; this.templater = new Templater(this);
this.connections = 0; this.connections = 0;
this.takenBooks = []; this.takenBooks = [];
@ -50,42 +52,11 @@ function Server () {
require('./routes/socketio')(this); require('./routes/socketio')(this);
} }
Server.prototype.fillTemplate = function (file, templateVars = {}) {
let data;
if (this.templateCache.hasOwnProperty(file)) {
data = this.templateCache[file];
} else {
data = fs.readFileSync(path.resolve(file), 'utf8');
}
if (data) {
if (!this.templateCache.hasOwnProperty(file)) {
this.templateCache[file] = data;
}
let filledTemplate = data.replace(/\{\{siteTitle\}\}/g, settings.siteTitle)
.replace(/\{\{titleSeparator\}\}/g, settings.titleSeparator)
.replace(/\{\{allowedFormats\}\}/g, settings.allowedFormats.join(','))
.replace(/\{\{maxFileSize\}\}/g, (settings.maxFileSize > 0 ? settings.maxFileSize + 'MB' : 'no'));
for (let templateVar in templateVars) {
const regExp = new RegExp('\{\{' + templateVar + '\}\}', 'g')
filledTemplate = filledTemplate.replace(regExp, templateVars[templateVar]);
}
// If any template variable is not provided, don't even render them.
filledTemplate = filledTemplate.replace(/\{\{[a-zA-Z0-9\-_]+\}\}/g, '');
return filledTemplate;
}
return data;
}
Server.prototype.replaceBodyWithTooManyBooksWarning = function (body) { Server.prototype.replaceBodyWithTooManyBooksWarning = function (body) {
if (settings.maxLibrarySize > 0) { if (settings.maxLibrarySize > 0) {
const numberOfBooks = fs.readdirSync(this.fileLocation).filter(fileName => fileName.includes('.json')).length; const numberOfBooks = fs.readdirSync(this.fileLocation).filter(fileName => fileName.includes('.json')).length;
if (numberOfBooks >= settings.maxLibrarySize) { if (numberOfBooks >= settings.maxLibrarySize) {
body = this.fillTemplate('./templates/elements/messageBox.html', { body = this.templater.fill('./templates/elements/messageBox.html', {
style: 'is-danger', style: 'is-danger',
title: 'Library Full', title: 'Library Full',
message: 'Sorry, the library has reached its maximum capacity for books! You will need to wait until a book is taken before a new one can be added.', message: 'Sorry, the library has reached its maximum capacity for books! You will need to wait until a book is taken before a new one can be added.',

42
templates/Templater.js Normal file
View File

@ -0,0 +1,42 @@
const path = require('path');
const fs = require('fs');
const settings = require('../settings.json');
module.exports = class {
constructor (app) {
this.app = app;
this.cache = {};
}
fill (file, templateVars = {}) {
let data;
if (this.cache.hasOwnProperty(file)) {
data = this.cache[file];
} else {
data = fs.readFileSync(path.resolve(file), 'utf8');
}
if (data) {
if (!this.cache.hasOwnProperty(file)) {
this.cache[file] = data;
}
let filledTemplate = data.replace(/\{\{siteTitle\}\}/g, settings.siteTitle)
.replace(/\{\{titleSeparator\}\}/g, settings.titleSeparator)
.replace(/\{\{allowedFormats\}\}/g, settings.allowedFormats.join(','))
.replace(/\{\{maxFileSize\}\}/g, (settings.maxFileSize > 0 ? settings.maxFileSize + 'MB' : 'no'));
for (let templateVar in templateVars) {
const regExp = new RegExp('\{\{' + templateVar + '\}\}', 'g')
filledTemplate = filledTemplate.replace(regExp, templateVars[templateVar]);
}
// If any template variable is not provided, don't even render them.
filledTemplate = filledTemplate.replace(/\{\{[a-zA-Z0-9\-_]+\}\}/g, '');
return filledTemplate;
}
return data;
}
}