site

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

commit 7da272c6bf097d76d996fbe75e587749f7f9a70e
parent 84dcd483c6a91b82e8506ac435fb584006645b73
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date:   Wed, 19 Jan 2022 23:42:07 -0800

Basic handlebars config.

Diffstat:
Mserver.js | 80++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mviews/index.handlebars | 4+---
Aviews/layouts/main.handlebars | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mviews/partials/lsTemplate.handlebars | 2+-
4 files changed, 151 insertions(+), 29 deletions(-)

diff --git a/server.js b/server.js @@ -1,6 +1,9 @@ // Cropyright (C) Ryan Jeffrey 2022 // A simple express server that uses handlebars. + +// TODO convert to typescript + const path = require('path'); const express = require('express'); const { engine } = require('express-handlebars'); @@ -22,51 +25,76 @@ function lsTime(timeMS) { let fileDate = new Date(timeMS); // 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(Date.now().getFullYear() != fileDate.getFullYear()) + if((new Date()).getFullYear() != fileDate.getFullYear()) return `${getMonthByNumber(fileDate.getMonth())} ${fileDate.getDate()} ${fileDate.getHours()}:${fileDate.getMinutes()}`; return `${getMonthByNumber(fileDate.getMonth())} ${fileDate.getDate()} ${fileDate.getFullYear()}`; } function ls(thePath) { - let result = ""; + let result = {}; - fs.stat(thePath, (err, stats) => { - // ls file permissions. - if(err) { - return console.error("Could not stat ", thePath, ": ", err); - } - // Convert mode to string. - let unixFilePermissions = (stats.mode & parseInt('777', 8)).toString(8); - // Unix file permission array. The mode is the index in the array. - const permStrings = ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx']; - let permsResult = permStrings[parseInt(unixFilePermissions[0])]; - permsResult += permStrings[parseInt(unixFilePermissions[1])]; - permsResult += permStrings[parseInt(unixFilePermissions[2])]; - - let prefixChar = '-'; - if(stats.isDirectory()) { - prefixChar = 'd'; - } + let stats = fs.statSync(thePath); - result = `${prefixChar}${permsResult} ${stats.nlink} ryan ryan ${stats.size} ${lsTime(stats.mtimeMS)} ${path.basename(thePath)}`; - }); + // ls file permissions. Convert into an easier format to use. + if(!stats) { + return console.error("Could not stat", thePath, ":", err); + } + // Convert mode to string. + let unixFilePermissions = (stats.mode & parseInt('777', 8)).toString(8); + // Unix file permission array. The mode is the index in the array. + const permStrings = ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx']; + let permsResult = permStrings[parseInt(unixFilePermissions[0])]; + permsResult += permStrings[parseInt(unixFilePermissions[1])]; + permsResult += permStrings[parseInt(unixFilePermissions[2])]; + + let prefixChar = '-'; + if(stats.isDirectory()) { + prefixChar = 'd'; + } + result = { + perms: `${prefixChar}${permsResult}`, + numLinks: stats.nlink, + fileSize: stats.size, + mtime: lsTime(stats.mtimeMS), + basename: path.parse(path.basename(thePath)).name, + }; return result; } +// Problem: the site is dynamically generated... so the files we're trying +// to ls don't exist. + +function lsDynamicList(theDir, ext, ...files) { + let fileStats = []; + files.forEach((element) => { + fileStats.push(ls(path.join(theDir, element + ext))); + }); + console.log(fileStats); + return fileStats; +} + +function lsList(theDir, ...files) { + let fileStats = []; + files.forEach((element) => { + fileStats.push(ls(path.join(theDir, element))); + }); + return fileStats; +} + function lsDir(thePath) { - let lines = []; + let fileStats = []; fs.readdir(thePath, (err, files) => { if(err) { return console.log('Cannot scane directory ', thePath, ": ", err); } files.forEach((file) => { - lines.push(ls(file)); + fileStats.push(ls(file)); }); }); - return lines; + return fileStats; } // App config @@ -99,8 +127,10 @@ app.get('/posts', (req, res, next) => { app.get('/', (req, res, next) => { + let test = lsDynamicList('public', '.html', 'main', 'software', 'sneed'); + console.log(test); res.status(200).render('index', { - entries: lsDir('.'), + entries: test }); }); diff --git a/views/index.handlebars b/views/index.handlebars @@ -1,5 +1,3 @@ - - {{#each entries}} - {{{> lsTemplate}}} + {{> lsTemplate}} {{/each}} diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars @@ -0,0 +1,94 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" +"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> +<head> +<!-- 2021-04-22 Thu 02:37 --> +<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> +<meta name="viewport" content="width=device-width, initial-scale=1" /> +<title>Ryan's Homepage</title> +<meta name="generator" content="Org mode" /> +<meta name="author" content="Ryan Jeffrey" /> +<link rel="stylesheet" type="text/css" href="css/terminal.css" /> +<script defer src="scripts/main.js"></script> +<link rel="shortcut icon" type="image/x-icon" href="res/favicon-32x32.ico"> +</head> +<body> +<div id="preamble" class="status"> +<script defer src="scripts/site-bkgs/main.js"></script> +<div id="task-meta" class="topl"> + <h4 style="margin-left:1em; margin-top:0.5em;">Links</h4> + <hr> + + <div id="taskbar"> + <div id="home"> + <a href="index.html"><img src="res/user-home.png" alt="Homepage" class="taskimg"></a> + <h4>Home</h4> + </div> + + <div id="blog"> + <a href="posts/"><img src="res/folder-documents.png" alt="Blog" class="taskimg"></a> + <h4>Blog</h4> + </div> + + <div id="files"> + <a href="files/"><img src="res/ktorrent.png" alt="Files" class="taskimg"></a> + <h4>Files</h4> + </div> + + <div id="git-server"> + <a href="https://git.ryanmj.xyz"><img src="res/folder-git.png" alt="git" class="taskimg"></a> + <h4>Git Server</h4> + </div> + + <div id="contact-me"> + <a href="mailto:ryan@ryanmj.xyz"><img alt="Email me" src="res/kopete.png"/></a> + <h4>Contact Me</h4> + </div> + </div> +</div> + +<!-- + _____ _ ___ _ _ _ _ +|_ _| |__ ___ |_ _|_ __ __| |_ _ ___| |_ _ __(_) __ _| | + | | | '_ \ / _ \ | || '_ \ / _` | | | / __| __| '__| |/ _` | | + | | | | | | __/ | || | | | (_| | |_| \__ \ |_| | | | (_| | | + |_| |_| |_|\___| |___|_| |_|\__,_|\__,_|___/\__|_| |_|\__,_|_| + + ____ _ _ _ _ +| _ \ _____ _____ | |_ _| |_(_) ___ _ __ __ _ _ __ __| | +| |_) / _ \ \ / / _ \| | | | | __| |/ _ \| '_ \ / _` | '_ \ / _` | +| _ < __/\ V / (_) | | |_| | |_| | (_) | | | | | (_| | | | | (_| | +|_| \_\___| \_/ \___/|_|\__,_|\__|_|\___/|_| |_| \__,_|_| |_|\__,_| + + _ _ +(_) |_ ___ ___ ___ _ __ ___ ___ __ _ _ _ ___ _ __ ___ ___ ___ +| | __/ __| / __/ _ \| '_ \/ __|/ _ \/ _` | | | |/ _ \ '_ \ / __/ _ \/ __| +| | |_\__ \ | (_| (_) | | | \__ \ __/ (_| | |_| | __/ | | | (_| __/\__ \ +|_|\__|___/ \___\___/|_| |_|___/\___|\__, |\__,_|\___|_| |_|\___\___||___/ + |_| + _ _ +| |__ __ ___ _____ | |__ ___ ___ _ __ __ _ +| '_ \ / _` \ \ / / _ \ | '_ \ / _ \/ _ \ '_ \ / _` | +| | | | (_| |\ V / __/ | |_) | __/ __/ | | | | (_| | +|_| |_|\__,_| \_/ \___| |_.__/ \___|\___|_| |_| \__,_| + + _ _ _ __ _ _ + __| (_)___ __ _ ___| |_ ___ _ __ / _| ___ _ __ | |_| |__ ___ + / _` | / __|/ _` / __| __/ _ \ '__| | |_ / _ \| '__| | __| '_ \ / _ \ +| (_| | \__ \ (_| \__ \ || __/ | | _| (_) | | | |_| | | | __/ + \__,_|_|___/\__,_|___/\__\___|_| |_| \___/|_| \__|_| |_|\___| + + _ +| |__ _ _ _ __ ___ __ _ _ __ _ __ __ _ ___ ___ +| '_ \| | | | '_ ` _ \ / _` | '_ \ | '__/ _` |/ __/ _ \ +| | | | |_| | | | | | | (_| | | | | | | | (_| | (_| __/_ +|_| |_|\__,_|_| |_| |_|\__,_|_| |_| |_| \__,_|\___\___(_) +--> + +{{{body}}} + +</div> + +</body> +</html> diff --git a/views/partials/lsTemplate.handlebars b/views/partials/lsTemplate.handlebars @@ -1 +1 @@ -<p>{{line}}</p> +<p>{{perms}} {{numLinks}} ryan ryan {{fileSize}} {{mtime}} <a href="{{basename}}">{{basename}}</a></p>