X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Ftemplate-helper.js;h=7e51bcfbb9058a25287bf934978d9234b368bf80;hb=c7fb39b664bc0a7eaf310c2ae7666e3cf26824bf;hp=a20d1aec13721cfda098ef7514df175422f51dfa;hpb=e2c0fe7d1ab89eb892cad2f9003391c2e41010e7;p=squeep-html-template-helper diff --git a/lib/template-helper.js b/lib/template-helper.js index a20d1ae..7e51bcf 100644 --- a/lib/template-helper.js +++ b/lib/template-helper.js @@ -6,6 +6,17 @@ const { lazy } = require('@squeep/lazy-property'); + +/** + * Set up expected fields for how we handle error reporting + * and whatnot. + * @param {Object} ctx + */ +const initContext = (ctx) => { + ctx.errors = []; + ctx.notifications = []; +}; + /** * Some fields may have values outside normal dates, handle them here. * @param {Date} date @@ -65,6 +76,33 @@ lazy(dateFormat, '_dtf', () => { }); +/** + * Wrap a Date in a ', + ].join(''); +}; + + /** * Render a duration. * @param {Number} seconds @@ -80,7 +118,7 @@ const secondsToPeriod = (seconds) => { result.push(`${r} ${label}${r != 1 ? 's' : ''}`); } value = factor ? Math.floor(value / factor) : value; - } + }; nextResult(60, 'second'); nextResult(60, 'minute'); @@ -93,6 +131,17 @@ const secondsToPeriod = (seconds) => { }; +/** + * Return array of strings prefixed with tabs. + * @param {Number} indent + * @param {String[]} list + */ +const indented = (indent, list) => { + const spacer = '\t'.repeat(indent); + return list.map((l) => `${spacer}${l}`); +}; + + /** * Render the preamble for an HTML page. * @param {Number} pagePathLevel number of paths below root this page is @@ -111,8 +160,8 @@ function htmlHead(pagePathLevel, ctx, options) { return `\t \t\t \t\t -\t\t -\t\t +\t\t +\t\t ${headElements.map((e) => '\t\t' + e).join('\n')} \t\t${pageTitle} \t`; @@ -354,8 +403,10 @@ function htmlPage(pagePathLevel, ctx, options, main = []) { module.exports = { + initContext, dateOrNot, dateFormat, + timeElement, secondsToPeriod, htmlHead, htmlBody, @@ -363,6 +414,7 @@ module.exports = { htmlHeader, htmlFooter, htmlMessages, + indented, renderNavLink, LI, UL,