6 ms·
Using the C preprocessor as an HTML templating engine
- cozzyd 2y agoOr there's the opposite choice of using PHP as a C preprocessors. Not sure which is more frightening.
- cedric_h 2y agoThey're both pretty chill-inducing.
- _benj 2y agoI was considering doing something like this just the other day. It had a gnuplot script and it seemed to me that it would be so much more versatile if I could use #ifdefine and #include in it. I ended up using a Perl preprocessor, but it seems like cpp would work ok-ish for certain use cases that don’t require, say, strict tabs like Python among a few other things mentioned on the cpp manual.
- webstrand 2y agoCheck out https://github.com/logological/gpp https://github.com/logological/gpp it's what I use when I feel like I need a preprocessor. It's more flexible in syntax and operation than cpp. I've used it in the past for wrangling the definitions of a multitude of SQL triggers I was using to generate a real-time materialized view. The fact that you can modify its syntax is very helpful for making it work with non-C syntaxes (like SQL or even LaTeX). It even has an HTML mode built-in, which you can customize to your needs, but looks like: <#define x|y> <#macro arg|...> which works better for certain WYSIWYG editors. You could modify the syntax to be <!--#define x y--> <!--#macro(arg, ...)-->
- _benj 2y agoThanks for sharing! Looks just like what I need :-)
- woodruffw 2y agoSeems like a bad idea, given that the CPP and HTML have a fair number of overlapping tokens (e.g. `#` for both URL fragments and macro beginnings). Why not use SSI[1]? It's inspired by the CPP's syntax, but is supported directly by most HTTP servers (and is more powerful, to boot). [1]: https://en.wikipedia.org/wiki/Server_Side_Includes https://en.wikipedia.org/wiki/Server_Side_Includes
- ksherlock 2y ago# is only processed by cpp when it's the first non-whitespace character in a line so that shouldn't be an issue in the real world. But, yeah, SSI.
- woodruffw 2y agoTrue. Maybe < and > would have been better examples, although one could argue those are optional :-)
- zzo38computer 2y agoYes, although if you use #define or C comments or several other stuff then it can potentially cause a problem due to it is not a C syntax and is not expected.
- tiagod 2y ago>Why not use SSI[1]? Because hacking is fun, and it's still fun if it's a bad idea
- beej71 2y agoThank you for saying this!
- Alifatisk 2y agoCompletely forgot about SSI! What a cool feature
- sour-taste 2y agoWhy not go all out? Use -DUSERNAME=$USERNAME on the command line to pass in data? Define macros with parameters to affect rendering in complicated ways. Call cpp for every request to generate the html. Could be fun!
- cedric_h 2y agoI've gotten a series of deranged preprocessor hacks from friends since sharing that repo, the possibilities are ... terrifyingly limitless.
- defrost 2y agoIIRC the earliest version of the C++ language, before a C++ compiler existed, was implemented with C and the M4 preprocessor. https://en.wikipedia.org/wiki/M4_(computer_language) https://en.wikipedia.org/wiki/M4_(computer_language) https://www.gnu.org/software/m4/ https://www.gnu.org/software/m4/
- cedric_h 2y agowoah, that's a really fun tidbit.
- imglorp 2y agoYep, it was called cfront. https://en.wikipedia.org/wiki/Cfront https://en.wikipedia.org/wiki/Cfront
- kragen 2y agocfront was the actual compiler; it was written in c++, not m4
- aodonnell2536 2y agocfront was a C++ => C transpiler
- ninjin 2y agoThank you, I did not know this. I was about to comment and say: Why not go with m4(1)? I have used it for a personal homepage and while it certainly will try to stab you in the back like any old macro processor will, it is more full-fledged that its C counterpart and will also allow to to smugly state that your homepage is POSIX compliant [1]. There are a few guides out there from around 2000 to get you started, but at some point one needs to read the original paper [2] and let your imagination run wild. [1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/m4.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/m... [2]: https://maibriz.de/unix/ultrix/etc/m4.pdf https://maibriz.de/unix/ultrix/etc/m4.pdf
- kragen 2y ago
- litoE 2y agoThere's also the htp processor that handles the same problem. It has macros (equivalent to #define) file inclusion (equivalent to #include) and conditionals (equivalent to #ifxxx) but is better suited for HTML than the C preprocessor. https://sourceforge.net/projects/htp/ https://sourceforge.net/projects/htp/
- torstenvl 2y agohttps://news.ycombinator.com/item?id=39863492 https://news.ycombinator.com/item?id=39863492
- cedric_h 2y agonice!
- rkeene2 2y agoI do this for some very small static sites like: [0] [0] http://rbl.oc9.org/ http://rbl.oc9.org/
- AstroJetson 2y agoVery cool, I like the simple mark up.
- kragen 2y agoi've used a linux setup for wireless access points that used the unix shell as its html templating engine. (openwrt 20 years ago, maybe?) the web server would run a shell script and send its output to the browser, cgi-style
- liama 2y ago[flagged]
- arp242 2y agoPlease don't post AI generated text.
- katzenversteher 2y agoQuite a long time ago I worked at a company developing games for mobile phones in J2ME. The J2ME runtime had a lot of (different) quirks issues on different phones and the phones where also quite different. We used the C preprocessor a lot to make porting the games for different models easier.
- babydevkute 2y agohi
- tzone22 2y ago[flagged]