From f4f49cd244d2f840cbb79a9286973b5c98d4f534 Mon Sep 17 00:00:00 2001 From: Mhykol Date: Thu, 8 Jan 2026 07:23:16 -0500 Subject: Initial Commit --- src/buildpages.js | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 src/buildpages.js (limited to 'src/buildpages.js') diff --git a/src/buildpages.js b/src/buildpages.js new file mode 100644 index 0000000..89409ec --- /dev/null +++ b/src/buildpages.js @@ -0,0 +1,178 @@ +const fs = require('fs') +const path = require('path') +const logger = require('./logger')() +const template = require('../template/template') + +class BuildPages { + constructor(x) { + this.newTemplate = new Map() + this.template = template({baseURL: x.baseURL}) + this.pages = this.#GetAllPages() + + this.path = [] + this.pages.forEach((value, key) => { + this.path.push(key) + }) + this.routes = x.routes + + this.#AddTemplate({ + name: 'start', + template: ` + + + ` + }) + this.html = { + start: ` + + + `, + end: ` + + ` + } + } + + GetSitemap() {return this.path} + + Build(pageData) { + if (this.routes.has(pageData.path)) { + } else if (this.pages.has(pageData.path)) { + const pages = this.pages.get(pageData.path) + const data = pages({ + req: '', + res: '', + next: '', + baseURL: pageData.baseURL + }) + + this.map = new Map() + for (let i = 0; i < data.page.length; i++) { + this.object = {} + this.object.data = data.page[i] + this.object.name = this.object.data.name + + if (pageData.path === '/') { + if (this.object.name === 'index') { + this.data = this.object.data() + this.output = { + html: this.data.html, + css: this.data.css, + js: this.data.js + } + this.output.js.push('loading.js') + + return this.output + } + } else { + this.data = this.object.data() + this.output = { + html: this.data.html, + css: this.data.css, + js: this.data.js + } + this.output.js.push('loading.js') + return this.output + } + } + } else { + // ! 404 Not Found + const notFound = this.template.NotFound() + //const css = notFound.css + + this.output = { + html: { + header: this.template.Header(), + main: notFound.html, + footer: this.template.Footer() + }, + css: [], + js: ['loading.js'] + } + + notFound.css.forEach(file => this.output.css.push(file)) + notFound.js.forEach(file => this.output.js.push(file)) + + return this.output + } + return false + } + + Template(pageData) { + const html = { + start: ` + + + `, + head: ` + + website + + + `, + headerStart: ` + +
+ `, + headerEnd: ` +
+ `, + mainStart: ` +
+ `, + mainEnd: ` +
+ `, + footerStart: ` + + + + + `, + end: ` + + ` + } + + html.headerEnd = this.template.Loading() + html.headerEnd + + let fullHTML = '' + Object.entries(html).forEach(([key, value]) => { + fullHTML += value + }) + + return this.#RemoveSpaces(fullHTML) + } + + #AddTemplate() { + this.newTemplate.set() + } + #RemoveSpaces(html) {return html.replace(/(\n)\s+/g, '$1')} + #GetAllPages() { + const object = { + modulesDir: path.resolve(`${__dirname}/../pages`), + map: new Map() + } + object.files = fs.readdirSync(object.modulesDir).filter(file => file.endsWith('.js')) + + object.files.forEach(file => { + try { + const name = file.replace('.js', '') + const modulePath = path.join(object.modulesDir, file) + const moduleObject = require(modulePath) + this.path = '/' + if (name !== 'home') this.path = `/${name}` + if (!object.map.has(this.path)) object.map.set(this.path, moduleObject) + } catch (err) { + logger.Error(err) + } + }) + return object.map + } +} + +module.exports = (x) => {return new BuildPages(x)} + -- cgit v1.3.1