Zenith

Introduction

In the era of dynamic web applications, static site generators (SSGs) have regained popularity for their simplicity, speed, and security. This blog post explores my journey in building a custom static site generator using C++, designed to power this very website. Unlike popular tools like Jekyll or Hugo, this project offers a lightweight, hands-on approach to generating static HTML pages from Markdown files, with support for themes, tags, and RSS feeds.

Why C++?

Choosing C++ for an SSG might seem unconventional, given the dominance of languages like Python or JavaScript. However, C++ brings unique advantages:

  1. Performance: Fast compilation and generation, ideal for large sites.
  2. Learning: A personal challenge to leverage C++ for a web-related task.

Project Overview

This static site generator is a command-line tool that transforms Markdown files in a content directory into a public directory of HTML pages. Key features include:

  1. Markdown Parsing: Converts Markdown to HTML using a custom parser.
  2. Template System: Uses base templates with placeholders for dynamic content.
  3. Tag Support: Categorizes posts with tags with clickable tag pages.
  4. Theme Switching: Supports light and dark modes, toggled via JavaScript.
  5. RSS Generation: Creates an RSS feed for blog updates.

How It Works

  1. Parsing Markdown: The MarkdownParser class extracts front matter (title, date, description, tags) and converts the body to HTML.
  2. Page Generation: The PageGenerator class loads templates, replaces placeholders, and writes output files.
  3. Build Process: A Makefile compiles the C++ code, and a script regenerates the site on file changes.
  4. Deployment: The public folder is served via a simple HTTP server or deployed to a host like Netlify.

Example Workflow

  1. Add a new post: content/blogs/new-post.md.
  2. Run ./build/zenith to generate public/blogs/new-post.html.
  3. Test locally with http-server public -p 8080.
  4. Deploy with scp -r public/* user@server:/var/www/html/.

Explore the source code on my GitHub and try building your own SSG!

Happy coding!