-/**
- * Render the preamble for an HTML page, up through body.
- * @param {Number} pagePathLevel number of paths below root this page is
- * @param {String} pageTitle
- * @param {String[]} headElements
- * @returns
- */
-function htmlHead(pagePathLevel, pageTitle, headElements = []) {
- const rootPathPfx = '../'.repeat(pagePathLevel);
- return `<!DOCTYPE html>
-<html lang="en">
- <head>
- <meta charset="utf-8">` +
- headElements.map((e) => `${' '.repeat(2)}${e}`).join('\n') + `
- <title>${pageTitle}</title>
- <link rel="stylesheet" href="${rootPathPfx}static/theme.css">
- </head>
- <body>`;
-}
-
-
-/**
- * Closes remainder of HTML page body.
- * @returns {String}
- */
-function htmlTail() {
- return ` </body>
-</html>`;
-}
-
-
-/**
- * Render a navigation link for the header section.
- * @param {Object} nav
- * @param {String} nav.href
- * @param {String} nav.class
- * @param {String} nav.text
- * @returns {String}
- */
-function renderNavLink(nav) {
- return `<li>
- <a href="${nav.href}"${nav.class ? (' class="' + nav.class + '"') : ''}>${nav.text}</a>
-</li>`;
-}
-
-
-/**
- * Render the navigation header, and open the main section.
- * @param {String} pageTitle
- * @param {Object[]} navLinks
- * @returns {String}
- */
-function htmlHeader(pageTitle, navLinks = []) {
- return ` <header>
- <h1>${pageTitle}</h1>
- <nav>` +
- (navLinks.length ? `
- <ol>
- ${navLinks.map((l) => renderNavLink(l)).join('\n')}
- </ol>`
- : '') + `
- </nav>
- </header>
- <main>`;
-}
-
-
-/**
- * Close the main section and finish off with boilerplate.
- * @param {String[]} footerEntries
- * @returns {String}
- */
-function htmlFooter(footerEntries = []) {
- return ` </main>
- <footer>` +
- (footerEntries.length ? `
- <ol>` + footerEntries.map((f) => ` <li>${f}</li>`).join('\n') + `
- </ol>`
- : '') + `
- </footer>`;
-}
-
-
-/**
- * Render all parts of an HTML page. Adds user logout nav link automatically.
- * @param {Object} ctx
- * @param {Number} pagePathLevel
- * @param {String} pageTitle
- * @param {String[]} headElements
- * @param {Object[]} navLinks
- * @param {String[]} main
- * @param {String[]} footerEntries
- * @returns {String}
- */
-function htmlTemplate(ctx, pagePathLevel, pageTitle, headElements = [], navLinks = [], main = [], footerEntries = []) {
- const user = (ctx && ctx.session && ctx.session.authenticatedProfile) || (ctx && ctx.session && ctx.session.authenticatedIdentifier);
- if (user) {
- let logoutPath;
- if (pagePathLevel > 0) {
- logoutPath = `${'../'.repeat(pagePathLevel - 1)}`;
- } else {
- logoutPath = 'admin/';
- }
- navLinks.push({
- text: `Logout (${user})`,
- href: `${logoutPath}logout`,
- });
- }
- return [
- htmlHead(pagePathLevel, pageTitle, headElements),
- htmlHeader(pageTitle, navLinks),
- ...main,
- htmlFooter(footerEntries),
- htmlTail(),
- ].join('\n');
-}
-
-
-module.exports = {
- dateOrNot,
- secondsToPeriod,
- htmlHeader,
- htmlFooter,
- htmlHead,
- htmlTail,
- renderNavLink,