build system (saki) done

This commit is contained in:
jetstream0
2023-07-30 23:23:20 -07:00
parent 003f4eec72
commit 17baac0640
8 changed files with 155 additions and 14 deletions

View File

@@ -1,11 +1,26 @@
## Hedgeblog # Hedgeblog
My personal blog. My [personal blog](https://www.prussiafan.club), because what the world needs is yet another semi-abandoned blog.
# Makoto ## Technical Goals
Makoto is the markdown-to-html parser. - Completely rewrite the blog, and get it working
- Be able to be served statically (so it can be deployed on Github Pages or Cloudflare Pages for no dinero)
- No dependencies - or basically, I want to write every line of code
- No Javascript served to client - the web pages should be pure HTML and CSS
# Ryuji ## Non-Technical Goals
Ryuji is a simple templating system. - Make two things I can call "Ryuji" and "Saki" to go along with "Makoto" (those are the three main characters of one of the best manga series ever)
- Move over some of the old blog posts (only the stuff I like), after rewriting them
- Start writing stuff on the blog again, at least semi-regularly
# Saki ## Makoto
Saki is the build system that puts it all together and creates the blog's static html. Makoto is the markdown-to-html parser, made with no dependencies. It was made around two months before Ryuji and Saki, and is meant to be more of a standalone thing. This is the sole npm dependency of the project (because I published makoto to npm and wanted to make sure it worked).
It also has a very cool warnings feature, that isn't used in this project, but can be seen in action if you use the [Makoto Web Editor](https://makoto.prussia.dev).
## Ryuji
Ryuji is a simple templating system. It's Jinja/Nunjucks inspired but has less features. On the upside, Ryuji is less than 200 lines of code and supports if statements, for loops, components and inserting variables.
I didn't write any docs for it, but you can see the syntax if you look in the `templates` directory or look in `tests.ts`.
## Saki
Saki is the build system that puts it all together and outputs the blog's static html.

View File

@@ -0,0 +1,18 @@
import { Renderer } from './ryuji.js';
import { BlogBuilder, PostMetadata } from './saki.js';
import _posts_metadata from './posts/_metadata.json';
let renderer: Renderer = new Renderer("templates", "components");
let builder: BlogBuilder = new BlogBuilder(renderer);
let posts_metadata: PostMetadata[] = Object.values(_posts_metadata);
builder.serve_static_folder("static");
//home page
builder.serve_template("/", "index.html", {
posts: posts_metadata,
});
//blog posts
builder.serve_markdowns("/posts", "/posts", "post.html", posts_metadata, true);

View File

@@ -4,7 +4,7 @@
"description": "blog", "description": "blog",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"build": "tsc -p . && node saki.js", "build": "tsc -p . && node index.js",
"test": "tsc -p . && node tests.js" "test": "tsc -p . && node tests.js"
}, },
"repository": { "repository": {

114
saki.ts
View File

@@ -1,7 +1,73 @@
import { readFileSync, writeFileSync } from 'fs'; import * as path from 'path';
import { Renderer } from './ryuji'; import { copyFileSync, existsSync, readdirSync, rmSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import type { Renderer } from './ryuji.js';
import { parse_md_to_html } from 'makoto'; import { parse_md_to_html } from 'makoto';
export class Builder {
build_dir: string;
constructor(build_dir: string="/build") {
this.build_dir = path.join(__dirname, build_dir);
if (existsSync(this.build_dir)) {
//wipe the build directory
rmSync(this.build_dir, {
recursive: true,
});
}
mkdirSync(this.build_dir);
}
static copy_folder(folder_path: string, dest_path: string) {
let children: string[] = readdirSync(folder_path);
for (let i=0; i < children.length; i++) {
let child: string = children[i];
let child_path: string = path.join(folder_path, child);
let copy_path: string = path.join(dest_path, child);
if (child.includes(".")) {
//file
copyFileSync(child_path, copy_path);
} else {
//directory, make directory and recursively copy
mkdirSync(copy_path);
Builder.copy_folder(child_path, path.join(dest_path, child));
}
}
}
serve_static_folder(static_dir: string, serve_from: string="/") {
let static_path: string = path.join(__dirname, static_dir);
let dest_path: string = path.join(this.build_dir, serve_from);
Builder.copy_folder(static_path, dest_path);
}
serve_content(content: string, serve_path: string) {
let dest_path: string = path.join(this.build_dir, serve_path);
if (!serve_path.includes(".")) {
//serve as index.html in serve_path directory
if (dest_path !== this.build_dir && dest_path !== path.join(this.build_dir, "/")) {
//will not make a new directory if `serve_path` is "/", since the build directory already exists
mkdirSync(dest_path);
}
writeFileSync(path.join(dest_path, "index.html"), content);
} else {
//serve as file regularly
writeFileSync(dest_path, content);
}
}
serve_file(file_path: string, serve_path: string) {
let file_content: string = readFileSync(path.join(__dirname, file_path), "utf-8");
this.serve_content(file_content, serve_path);
}
_serve_template(renderer: Renderer, serve_path: string, template_name: string, vars: any) {
let content: string = renderer.render_template(template_name, vars);
this.serve_content(content, serve_path);
}
}
//this code is more or less specific to my blog
export interface PostMetadata { export interface PostMetadata {
title: string, title: string,
slug: string, slug: string,
@@ -16,4 +82,46 @@ export interface Post extends PostMetadata {
html: string, html: string,
} }
// export class BlogBuilder extends Builder {
renderer: Renderer;
constructor(renderer: Renderer, build_dir: string="/build") {
super(build_dir);
this.renderer = renderer;
}
serve_template(serve_path: string, template_name: string, vars: any) {
super._serve_template(this.renderer, serve_path, template_name, vars);
}
serve_markdown(serve_path: string, template_name: string, markdown_post: Post, additional_vars: any={}) {
additional_vars.post = markdown_post;
this.serve_template(serve_path, template_name, {
post: markdown_post,
author_expected: markdown_post.author.toLowerCase().startsWith("jetstream0") || markdown_post.author.toLowerCase().startsWith("prussia"),
});
}
serve_markdowns(serve_path: string, posts_path: string, template_name: string, posts_metadata: PostMetadata[], own_dir: boolean=true) {
let posts_dir_path: string = path.join(this.build_dir, posts_path);
if (!existsSync(posts_dir_path)) {
mkdirSync(posts_dir_path);
}
for (let i=0; i < posts_metadata.length; i++) {
let post_metadata: PostMetadata = posts_metadata[i];
let post_md_path: string = path.join(__dirname, posts_path, `${post_metadata.slug}.md`);
let post_md: string = readFileSync(post_md_path, "utf-8");
let post_html: string = parse_md_to_html(post_md);
let post: Post = {
...post_metadata,
md: post_md,
html: post_html,
};
if (own_dir) {
this.serve_markdown(path.join(serve_path, post.slug), template_name, post);
} else {
this.serve_markdown(path.join(serve_path, `${post.slug}.html`), template_name, post);
}
}
}
}

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

0
static/styles/index.css Normal file
View File

0
static/styles/post.css Normal file
View File

View File

@@ -1,5 +1,5 @@
import { Renderer } from './ryuji'; import { Renderer } from './ryuji.js';
import { test_assert_equal, log_test_results } from './endosulfan'; import { test_assert_equal, log_test_results } from './endosulfan.js';
//tests for ryuji //tests for ryuji