site

Website's source files.
Log | Files | Refs | Submodules | LICENSE

commit 830a8300126fdf2790f7d4f04e24556439644585
parent e77471adc91cab46893450b0ab3f3c3de688f28f
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date:   Sat, 22 Jan 2022 00:23:12 -0800

Window templating system

Diffstat:
Mserver.ts | 141+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
Mviews/index.handlebars | 4++--
Aviews/partials/terminalTemplate.handlebars | 21+++++++++++++++++++++
3 files changed, 114 insertions(+), 52 deletions(-)

diff --git a/server.ts b/server.ts @@ -9,6 +9,18 @@ import fs from 'fs'; const app = express(); const port = process.env.PORT || 3000; +function getMonthByNumber(i: number) : string { + const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', + 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; + return (i in months) ? months[i] : ""; +} + +function permissionToString(i: number) : string { + // Unix file permission array. The mode is the index in the array. + const permStrings = ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx']; + return (i in permStrings) ? permStrings[i] : ""; +} + class LSStat { perms: string; numLinks: number; @@ -38,63 +50,92 @@ class LSStat { this.perms = `${prefixChar}${permsResult}`; this.numLinks = stats.nlink; this.fileSize = stats.size; - this.mtime = lsTime(stats.mtimeMs); + this.mtime = LSStat.lsTime(stats.mtimeMs); this.basename = thePath; } -} -function fileExistsIn(thePath: string, statList: LSStat[]) : boolean { - for(let i = 0; i < statList.length; i++) { - if(statList[i].basename == thePath) - return true; + // Get the mtime in the same format that LS would. + static lsTime(timeMS: number) : string { + let fileDate = new Date(timeMS); + let addString = ""; + // If the file was updated this year then set the last column to the + // hour and minute. Else, the last column should be the year. + if((new Date()).getFullYear() != fileDate.getFullYear()) + addString = `${fileDate.getHours()}:${fileDate.getMinutes()}`; + else + addString = ` ${fileDate.getFullYear()}`; + return `${getMonthByNumber(fileDate.getMonth())} ${fileDate.getDate()} ${addString}`; } - return false; -} -function getMonthByNumber(i: number) : string { - const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', - 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; - return (i in months) ? months[i] : ""; -} + static lsList(theDir: string, ext: string, files: string[]) : LSStat[] { + let fileStats: LSStat[] = []; + files.forEach((element: string) => { + fileStats.push(new LSStat(path.join(theDir, element + ext))); + }); + return fileStats; + } -// Get the mtime in the same format that LS would. -function lsTime(timeMS: number) : string { - let fileDate = new Date(timeMS); - let addString = ""; - // If the file was updated this year then set the last column to the - // hour and minute. Else, the last column should be the year. - if((new Date()).getFullYear() != fileDate.getFullYear()) - addString = `${fileDate.getHours()}:${fileDate.getMinutes()}`; - else - addString = ` ${fileDate.getFullYear()}`; - return `${getMonthByNumber(fileDate.getMonth())} ${fileDate.getDate()} ${addString}`; -} + static lsDir(thePath: string) : LSStat[] { + let fileStats: LSStat[] = []; + // TODO error checking. + let files = fs.readdirSync(thePath); -function permissionToString(i: number) : string { - // Unix file permission array. The mode is the index in the array. - const permStrings = ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx']; - return (i in permStrings) ? permStrings[i] : ""; -} + files.forEach((file) => { + fileStats.push(new LSStat(file)); + }); + return fileStats; + } -function lsList(theDir: string, ext: string, ...files: string[]) : LSStat[] { - let fileStats: LSStat[] = []; - files.forEach((element: string) => { - fileStats.push(new LSStat(path.join(theDir, element + ext))); - }); - return fileStats; + static fileExistsIn(thePath: string, statList: LSStat[]) : boolean { + for(let i = 0; i < statList.length; i++) { + if(statList[i].basename == thePath) + return true; + } + return false; + } } -function lsDir(thePath: string) : LSStat[] { - let fileStats: LSStat[] = []; - // TODO error checking. - let files = fs.readdirSync(thePath); +class TerminalWindow { + lsList: LSStat[]; - files.forEach((file) => { - fileStats.push(new LSStat(file)); - }); + where: string; + markup: string; + + constructor() { + } + + static makeDir(path: string) : TerminalWindow { + let newWin = new TerminalWindow(); + // LS if it is a directory, read file if not. + if(fs.lstatSync(path).isDirectory()) { + newWin.where = path; + newWin.lsList = LSStat.lsDir(path); + newWin.markup = ""; + } + else { + newWin.where = ""; + newWin.lsList = []; + newWin.markup = fs.readFileSync(path, 'utf8'); + } + return newWin; + } - return fileStats; + static makeLS(dir: string, ext: string, paths: string[]) : TerminalWindow { + let newWin = new TerminalWindow(); + newWin.where = dir; + newWin.lsList = LSStat.lsList(dir, ext, paths); + newWin.markup = ""; + return newWin; + } + + static makeList(lsList: LSStat[]) : TerminalWindow { + let newWin = new TerminalWindow(); + newWin.lsList = lsList; + newWin.where = ""; + newWin.markup = ""; + return newWin; + } } // App config @@ -107,11 +148,11 @@ app.use(express.static(path.join(process.cwd(), 'public'))); app.use(express.json()); // TODO maybe a system that exports org to handlebars. -const postItems = lsDir('posts'); +const postItems = LSStat.lsDir('posts'); // Get the requested post app.get('/posts/:post', (req, res, next) => { let post = req.params.post.toLowerCase(); - if(fileExistsIn(post, postItems)) { + if(LSStat.fileExistsIn(post, postItems)) { res.status(200).render('writing', { text : fs.readFileSync(post) }); } else { @@ -125,7 +166,7 @@ app.get('/posts', (req, res, next) => { }); // index.html should be before 404 and after everything else // Generate files object. -const files = lsDir('public/files'); +const files = LSStat.lsDir('public/files'); // Server entry for files. app.get('/files', (req, res, next) => { res.status(200).render('files', { @@ -134,11 +175,11 @@ app.get('/files', (req, res, next) => { }); // LS everything. -const frontPageItems = lsList('.', '.html', 'main', 'software', 'sneed'); +const frontPageItems = LSStat.lsList('.', '.html', ['main', 'software', 'sneed']); app.get('/:item', (req, res, next) => { let item = req.params.item.toLowerCase(); - if(fileExistsIn(item, frontPageItems)) { + if(LSStat.fileExistsIn(item, frontPageItems)) { res.status(200).render('post', { text : fs.readFileSync(item) }); } else { @@ -149,7 +190,7 @@ app.get('/:item', (req, res, next) => { app.get('/', (req, res, next) => { res.status(200).render('index', { - entries: frontPageItems + windows: [LSStat.makeLS('.', '.html', ['main', 'software', 'sneed'])] }); }); diff --git a/views/index.handlebars b/views/index.handlebars @@ -1,3 +1,3 @@ -{{#each entries}} - {{> lsTemplate}} +{{#each windows}} + {{> terminalTemplate isTerminal=true}} {{/each}} diff --git a/views/partials/terminalTemplate.handlebars b/views/partials/terminalTemplate.handlebars @@ -0,0 +1,21 @@ +{{#if isTerminal}} + <div class="twin topl"> + <div style="tcontent"> + </div> + <div id="content"> + {{#if lsList}} + <p> + <span class="prompt1">ryan</span><span class="prompt2">@</span><span class="prompt3">themainframe</span><span class="prompt4"></span> ls {{where}} + </p> + {{#each lsList}} + {{> lsTemplate}} + {{/each}} + {{/if}} + + {{#if markup}} + {{{markup}}} + {{/if}} + </div> + </div> +{{/if}} +