9 changed files with 81 additions and 68 deletions
@ -1,10 +1,10 @@
@@ -1,10 +1,10 @@
|
||||
module.exports = function (app) { |
||||
app.server.get('/give', (req, res) => { |
||||
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); |
||||
|
||||
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); |
||||
}); |
||||
} |
@ -0,0 +1,42 @@
@@ -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; |
||||
} |
||||
} |
Loading…
Reference in new issue