From 6d7d5982906b6c2bf89a86afab91834b8fd5dee1 Mon Sep 17 00:00:00 2001 From: Moutonjr Geoff Date: Sat, 21 Nov 2020 15:08:16 +0100 Subject: [PATCH] Init commit --- .gitignore | 3 +++ README.md | 30 ++++++++++++++++++++++++++++++ build.js | 23 +++++++++++++++++++++++ package.json | 19 +++++++++++++++++++ settings.json | 3 +++ templates/index.njk | 8 ++++++++ 6 files changed, 86 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 build.js create mode 100644 package.json create mode 100644 settings.json create mode 100644 templates/index.njk diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb4efac --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +public/ +package-lock.json +node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..808766d --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Test static site generation + +## Introduction +This small snippet aims to convince that you can render all static pages at once using SSG-scheme. + +## OK, what should I see? +Scheme is as follows: +- Aim is to deliver plain ol' HTML pages by web server. No, no application server accepted; This is best for most of services! +- Problem: You have redundant content, and you don't want the hassle to maintain HTML files: + - No functions, so redundant code; + - Can't properly handle modular breakdown (header, footer, sections); + - When you made it work once, shall you re-understand everything 6 months later to thange stubs? + +Here is how it works: +- We store relevant data in a YAML file, say `settings.yml`. +- We Use [Nunjucks](https://mozilla.github.io/nunjucks/templating.html), the millenials way to code, to generate Jinja2-styled webpages on steroids. +- We Then compile the whole _once_, and after we got static HTML for basically every use. + +What repo doesn't contain: +- All Nunjucks killer features. + +## Dependencies +Install NodeJS and npm + +## Build +```bash + npm run build +``` + +Serve your webserver to display the `public` folder as webroot. diff --git a/build.js b/build.js new file mode 100644 index 0000000..6e824d0 --- /dev/null +++ b/build.js @@ -0,0 +1,23 @@ +const nunjucks = require('nunjucks'); +const fs = require('fs'); +const yaml = require('js-yaml'); + +const ENTRY_POINT='templates/index.njk'; +const OUTPUT_POINT='public/index.html'; +const GLOBAL_SETTINGS='./settings.yml'; + +try { + let fileContents = fs.readFileSync(GLOBAL_SETTINGS, 'utf8'); + let settings = yaml.safeLoad(fileContents); + + console.log(data); +} catch (e) { + console.log(e); +} + +var data = nunjucks.render(ENTRY_POINT, settings); + +fs.writeFile(OUTPUT_POINT, data, 'utf-8', function(err){ + if (err) return console.log(err); +}); + diff --git a/package.json b/package.json new file mode 100644 index 0000000..09a73d6 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "test", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "prebuild": "rm -rf public/ && mkdir public/", + "install": "npm run build", + "build": "node build.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "fs": "0.0.1-security", + "js-yaml": "^3.14.0", + "nunjucks": "^3.2.2" + } +} diff --git a/settings.json b/settings.json new file mode 100644 index 0000000..1be7914 --- /dev/null +++ b/settings.json @@ -0,0 +1,3 @@ +{ + "dinner": "ready" +} diff --git a/templates/index.njk b/templates/index.njk new file mode 100644 index 0000000..a7698f1 --- /dev/null +++ b/templates/index.njk @@ -0,0 +1,8 @@ + + + +

+ Dinner is {{ dinner }} +

+ +