site

A custom static site generator written in C.
git clone https://unitehenry.com/git/site.git
Log | Files | Refs | README

commit 187bffc23343d3f60d3e91b28529c35fa7ffd97f
parent a9f7736a2de5b6774b7287aa07526611ce207be4
Author: Henry Unite <unitehenry@gmail.com>
Date:   Thu,  9 Apr 2026 05:21:36 +0000

feat: generate pages

Diffstat:
Msrc/generate_pages.c | 32+++++++++++++++++++++++++-------
Msrc/main.c | 1+
Asrc/trim_whitespace.c | 41+++++++++++++++++++++++++++++++++++++++++
Asrc/trim_whitespace.h | 9+++++++++
Atemplates/base.html | 9+++++++++
5 files changed, 85 insertions(+), 7 deletions(-)

diff --git a/src/generate_pages.c b/src/generate_pages.c @@ -2,7 +2,11 @@ #include "run_pandoc.h" #include "string_list.h" #include "to_write_path.h" +#include "trim_whitespace.h" #include <stdio.h> +#include <string.h> + +extern char *BASE_TEMPLATE; void generate_pages(StringList *list) { int i = 0; @@ -22,14 +26,29 @@ void generate_pages(StringList *list) { run_pandoc(&read_fp, list->strings[i]); - char *line = NULL; + char *template_line = NULL; + + size_t template_len = 0; + + ssize_t template_read; + + // begin writing template + FILE *template_fp = fopen(BASE_TEMPLATE, "r"); + + while ((template_read = getline(&template_line, &template_len, template_fp)) != -1) { + if (strcmp(trim_whitespace(template_line), "{{ content }}") == 0) { + char *content_line = NULL; - size_t len = 0; + size_t content_len = 0; - ssize_t read; + ssize_t content_read; - while ((read = getline(&line, &len, read_fp)) != -1) { - fputs(line, write_fp); + while ((content_read = getline(&content_line, &content_len, read_fp)) != -1) { + fputs(content_line, write_fp); + } + } else { + fputs(template_line, write_fp); + } } fclose(write_fp); @@ -38,4 +57,4 @@ void generate_pages(StringList *list) { i++; } -} -\ No newline at end of file +} diff --git a/src/main.c b/src/main.c @@ -4,6 +4,7 @@ #include <stdlib.h> char *BUILD_DIRECTORY = "build"; +char *BASE_TEMPLATE = "templates/base.html"; int main(int argc, char **argv) { if (argc < 2) { diff --git a/src/trim_whitespace.c b/src/trim_whitespace.c @@ -0,0 +1,40 @@ +#include "trim_whitespace.h" +#include <ctype.h> +#include <stdlib.h> +#include <string.h> + +char *trim_whitespace(const char *str) { + if (!str) + return NULL; + + size_t len = strlen(str); + + if (len == 0) + return strdup(""); + + size_t start = 0; + + while (start < len && isspace(str[start])) + start++; + + if (start == len) + return strdup(""); + + size_t end = len - 1; + + while (end > start && isspace(str[end])) + end--; + + size_t new_len = end - start + 1; + + char *result = malloc(new_len + 1); + + if (!result) + return NULL; + + memcpy(result, str + start, new_len); + + result[new_len] = '\0'; + + return result; +} +\ No newline at end of file diff --git a/src/trim_whitespace.h b/src/trim_whitespace.h @@ -0,0 +1,8 @@ +#ifndef TRIM_WHITESPACE_H +#define TRIM_WHITESPACE_H + +#include <stddef.h> + +char *trim_whitespace(const char *str); + +#endif +\ No newline at end of file diff --git a/templates/base.html b/templates/base.html @@ -0,0 +1,9 @@ +<html> + <head> + <title>Page Title</title> + </head> + + <body> + {{ content }} + </body> +</html>