site

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

commit 2443c59e4fb85be6d2334c5d250149fda8dabe60
parent fab07ffbc1c4da23bafef4a43a4086b3c206c9c7
Author: Henry Unite <unitehenry@gmail.com>
Date:   Sat, 11 Apr 2026 23:50:01 +0000

refactor: generate pages

Diffstat:
Msrc/generate.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/generate.h | 3+++
Dsrc/generate_pages.c | 61-------------------------------------------------------------
Dsrc/generate_pages.h | 5-----
Msrc/main.c | 1-
5 files changed, 60 insertions(+), 67 deletions(-)

diff --git a/src/generate.c b/src/generate.c @@ -3,8 +3,13 @@ #include <stdio.h> #include <string.h> #include <stdlib.h> +#include "to_write_path.h" +#include "run_pandoc.h" #include <sys/stat.h> +extern char *CONTENT_TAG; +extern char *BASE_TEMPLATE; + void get_content_files(StringList *list, char *content_path) { struct dirent *de; @@ -71,3 +76,55 @@ void create_subdirectories(char *filepath) { free(path_copy); } + +void generate_pages(StringList *list) { + int i = 0; + + while (list->strings[i] != NULL) { + char *write_path; + + to_write_path(&write_path, list->strings[i]); + + printf("filepath: %s\n", write_path); + + create_subdirectories(write_path); + + FILE *write_fp = fopen(write_path, "w"); + + FILE *read_fp; + + run_pandoc(&read_fp, list->strings[i]); + + char *template_line = NULL; + + size_t template_len = 0; + + ssize_t template_read; + + 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_TAG) == 0) { + char *content_line = NULL; + + size_t content_len = 0; + + ssize_t content_read; + + 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); + + fclose(read_fp); + + i++; + } +} diff --git a/src/generate.h b/src/generate.h @@ -1,7 +1,10 @@ #ifndef GENERATE_H #define GENERATE_H +#include "strings.h" + void get_content_files(StringList *list, char *content_path); void create_subdirectories(char *filepath); +void generate_pages(StringList *list); #endif diff --git a/src/generate_pages.c b/src/generate_pages.c @@ -1,61 +0,0 @@ -#include "run_pandoc.h" -#include "strings.h" -#include "to_write_path.h" -#include "generate.h" -#include <stdio.h> -#include <string.h> - -extern char *CONTENT_TAG; -extern char *BASE_TEMPLATE; - -void generate_pages(StringList *list) { - int i = 0; - - while (list->strings[i] != NULL) { - char *write_path; - - to_write_path(&write_path, list->strings[i]); - - printf("filepath: %s\n", write_path); - - create_subdirectories(write_path); - - FILE *write_fp = fopen(write_path, "w"); - - FILE *read_fp; - - run_pandoc(&read_fp, list->strings[i]); - - char *template_line = NULL; - - size_t template_len = 0; - - ssize_t template_read; - - 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_TAG) == 0) { - char *content_line = NULL; - - size_t content_len = 0; - - ssize_t content_read; - - 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); - - fclose(read_fp); - - i++; - } -} diff --git a/src/generate_pages.h b/src/generate_pages.h @@ -1,5 +0,0 @@ -#include "run_pandoc.h" -#include "strings.h" -#include "to_write_path.h" - -void generate_pages(StringList *list); diff --git a/src/main.c b/src/main.c @@ -1,4 +1,3 @@ -#include "generate_pages.h" #include "generate.h" #include <stdio.h> #include <stdlib.h>