💾 Archived View for koyu.space › aartaka › public › this-post-is-cpp.gmi captured on 2024-05-10 at 10:48:40. Gemini links have been rewritten to link to archived content

View Raw

More Information

➡️ Next capture (2024-05-26)

-=-=-=-=-=-=-

📎 I Generated This Post With C Preprocessor

By Artyom Bologov

It's a new phase, so I'm re-evaluating my life and tech choices yet again.

This time, I identify as a C programmer.

I'm moving to C-based software and trying to script everything with C.

So why not move my website from Lisp to C too?

To C preprocessor, actually.

There are several inspirations for this idea:

Quake hack for raw file injection.

An anecdote about preprocessor-based website.

One attempt to generate HTML with C,

And another HTML generation library.

So, technically, I'm not the first one to generate a website with C preprocessor

(let's call it

CPP.

But I'm up to the challenge of making it actually usable and pretty!

The page you're looking at is generated with C preprocessor, so consider that a success.

C Preprocessor Is a Templating Engine, Actually (#cpp-is-templating-engine)

CPP is quite dumb: it operates on code that's not even parsed yet.

Which is bad if you want to make Lisp-like macros.

And good if you want to embed some text or files into arbitrary text.

Even if this text is HTML.

So preprocessor is a templating engine of sorts.

But CPP possesses several advantages over tools like

Mustache:

C compiler with a preprocessor is available for every OS and every toolset.

Every C programmer knows how CPP works.

Almost every programmer knows HTML.

If they don't, they can easily learn both preprocessor and HTML.

Again, C compilers are everywhere.

And you don't need any third-party libraries to build a website with preprocessor.

Preprocessor also has built-in recursive file inclusion.

One can write HTML files with preprocessor directives in them.

Here's how a template file (say, <code>head.template</code>) might look like:

<head>
  <title> PAGE_TITLE </title>
 #ifdef PAGE_DESCRIPTION
  <meta name="description" content=PAGE_DESCRIPTION/>;
 #endif
</head>

You can then #include "head.template" from another file.

Problematic Chars (#problematic-chars)

Preprocessor, as any templating engine, has some special chars that you have to handle.

And preprocessor doesn't make it easy to work with these.

The most obvious preprocessor offender is the hash sign.

Preprocessor interprets hash sign as a directive.

And fails silently if it cannot interpret the directive properly.

GCC in particular is bad at it. It expands 😃 to <code>U0001f603</code>, for example.

That's why I use Clang.

Compilation stages before preprocessor remove comments, unless you instruct preprocessor not to.

You simply have to provide a -C flag.

GCC is making it hard again: it's adding its own comments to the output.

That's why I use Clang.

Is That Worth It? (#worth-it)

Given all these problems with chars and the fact that preprocessor is scary, is it worth it?

Actually, yes.

The preprocessor-based setup abstracts away the repetitive parts, while keeping things simple and portable enough.

And, however painful it is to acknowledge, it's simpler than

my previous Lispy setup.

You can review all of my build code here:

Makefile that builds blog posts.

head,

header,

copyright footer.

this-post-is-cpp.h

This website is

Designed to Last

and generated with the help of

C preprocessor.

You can view page sources by appending .h to the page URL.

Copyright 2022-2024 Artyom Bologov (aartaka).

Any and all opinions listed here are my own and not representative of my employers; future, past and present.

Back to home page

About me

My projects