Building a Static Site Generator in C++ (Because Why Not?)

The Problem That Wasn't Really a Problem

I needed a portfolio. Simple enough, right? Hugo exists. Jekyll exists. Eleventy exists. Hell, even WordPress exists.

Since I am learning C++, I wanted to challenge myself by building a static site generator from scratch using C++. I wanted to understand the full stack of how content becomes a website.

Why C++ Though?

Many would ask, "Why not use an existing tool?"

Probably, there are better-suited languages for this task, but I wanted to challenge myself and explore C++'s capabilities in web development. Plus, I was curious about the performance benefits C++ could bring to the table. And honestly, I just wanted to see if I could do it.

Here were my actual reasons:

  • Performance curiosity: How fast could I make the build process?
  • Learning experience: I am just looking to learn new things as I build projects and this was a great one.
  • Because I could: Sometimes that's reason enough

The Core Challenges

1. Markdown Parsing

First I started with something basic, parsing a markdown file and turning it into a HTML page using m4dclibrary. Which was pretty straightforward, lightweight and easy to use.

2. Template Engine

As I continued building it, I felt the need for a template engine to avoid repeating HTML structure across pages. So I built a simple one with variable interpolation and basic control flow (if, for loops). And it worked surprisingly well for my needs. Also it has utilities like filters to manipulate strings (like uppercase, lowercase, etc).

3. File System Operations

I used standard C++ filesystem library to handle file operations. It made traversing directories and reading/writing files quite straightforward. I also used efswlibrary for file watching during development, which allowed me to automatically rebuild the site on file changes during development.

4. Metadata Handling

I implemented YAML frontmatter parsing using yaml-cpplibrary. This allowed me to add metadata to my markdown files, which I could then access in my templates.

What I Learned

The Good

  • Building a static site generator in C++ is entirely feasible with the right libraries.
  • The C++ standard library has matured significantly, making file system operations and string manipulations much easier.
  • Libraries like md4c and yaml-cpp are powerful tools that can handle complex parsing tasks with ease.
  • Templating can be simple yet effective without needing a full engine. While my template engine is basic, it meets my needs perfectly.
  • The build times are impressively fast, around 100 milliseconds for my site.

The Bad

  • Fast reloading during development was tricky to implement efficiently. Since I used http-lib for the development server, integrating it with file watching and hot-reloading took some effort. Because It would have been cooler or more efficent to use a more robust web server library or framework that has built-in support websockets.

  • Template engine limitations: My custom template engine is quite basic. Adding more features like nested loops, advanced conditionals, or partials would require significant effort.

  • Error handling: C++'s error handling can be verbose, and ensuring robust error checking throughout the codebase was a challenge.

  • I would have liked to implement websockets without needing to use a different and more complex library than http-lib. But for now, it works fine.

  • Also, I would like to have made it easier to add custom configuration options without needing to deal with too much refactoring. As right now, adding new config options requires too much efforts.

The "Wait, That's Actually Useful?"

  • Gained a deeper understanding of how static site generators work under the hood.

  • Learned about threading and asynchronous programming in C++ while implementing the development server.

  • An useful feature I ended up adding by "mistake" was allowing to use markdown files as pages. And since markdown can also contain HTML, it gives a lot of flexibility to create custom layouts without needing to modify the template engine.

  • Also, I noticed how easy it is to deploy a static site generated by C++. The output is just plain HTML/CSS/JS files, which can be hosted anywhere... Vercel CLI made it super easy to deploy this site.

The Tech Stack

  • Language: C++17
  • Markdown Parsing: m4dc
  • YAML Parsing: yaml-cpp
  • HTTP Server: custom minimalist server
  • File Watching: efsw
  • Build System: CMake

Try It Yourself

The repo is still private for now, but I plan to open source it soon!

Live site: You're looking at it!

Final Thoughts

I enjoyed building this project and I want to continue improving it over time. When I make it open source, I hope others find it useful or at least interesting from a C++ perspective.