site

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

commit c4935cd754d0fea2af8d1ca26f0754cd6179ec81
parent ee48b9dea4f8cc6cad5dcb40f10d7eabd08f93b5
Author: Henry Unite <unitehenry@gmail.com>
Date:   Thu,  9 Apr 2026 04:52:32 +0000

refactor: c files

Diffstat:
Dbuild/first-post.html | 2--
Dbuild/index.html | 2--
Dbuild/nested-1.html | 1-
Asrc/generate_pages.c | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/generate_pages.h | 12++++++++++++
Asrc/get_content_files.c | 39+++++++++++++++++++++++++++++++++++++++
Asrc/get_content_files.h | 9+++++++++
Msrc/main.c | 117+++----------------------------------------------------------------------------
Asrc/run_pandoc.c | 15+++++++++++++++
Asrc/run_pandoc.h | 9+++++++++
Msrc/string_list.h | 5+++++
Asrc/to_write_path.c | 29+++++++++++++++++++++++++++++
Asrc/to_write_path.h | 12++++++++++++
13 files changed, 176 insertions(+), 118 deletions(-)

diff --git a/build/first-post.html b/build/first-post.html @@ -1,2 +0,0 @@ -<h1 id="first-post">First Post</h1> -<p>This is my first blog post.</p> diff --git a/build/index.html b/build/index.html @@ -1,2 +0,0 @@ -<h2 id="welcome-to-my-site">Welcome to my Site</h2> -<p>This is my website.</p> diff --git a/build/nested-1.html b/build/nested-1.html @@ -1 +0,0 @@ -<p>nested content</p> diff --git a/src/generate_pages.c b/src/generate_pages.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include "string_list.h" +#include "to_write_path.h" +#include "create_subdirectories.h" +#include "run_pandoc.h" + +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 *line = NULL; + + size_t len = 0; + + ssize_t read; + + while ((read = getline(&line, &len, read_fp)) != -1) { + fputs(line, write_fp); + } + + fclose(write_fp); + + fclose(read_fp); + + i++; + } +} +\ No newline at end of file diff --git a/src/generate_pages.h b/src/generate_pages.h @@ -0,0 +1,11 @@ +#ifndef GENERATE_PAGES_H +#define GENERATE_PAGES_H + +#include "string_list.h" +#include "to_write_path.h" +#include "create_subdirectories.h" +#include "run_pandoc.h" + +void generate_pages(StringList *list); + +#endif +\ No newline at end of file diff --git a/src/get_content_files.c b/src/get_content_files.c @@ -0,0 +1,38 @@ +#include <dirent.h> +#include <stdio.h> +#include <string.h> +#include "string_list.h" + +void get_content_files(StringList *list, char *content_path) { + struct dirent *de; + + DIR *dr = opendir(content_path); + + if (dr == NULL) { + return; + } + + while ((de = readdir(dr)) != NULL) { + if (strcmp(de->d_name, ".") == 0) { + continue; + } + + if (strcmp(de->d_name, "..") == 0) { + continue; + } + + size_t filepath_len = strlen(content_path) + strlen(de->d_name) + 2; + + char filepath[filepath_len]; + + snprintf(filepath, sizeof(filepath), "%s/%s", content_path, de->d_name); + + if (de->d_type == DT_DIR) { + get_content_files(list, filepath); + } else { + string_list_append(list, filepath); + } + } + + closedir(dr); +} +\ No newline at end of file diff --git a/src/get_content_files.h b/src/get_content_files.h @@ -0,0 +1,8 @@ +#ifndef GET_CONTENT_FILES_H +#define GET_CONTENT_FILES_H + +#include "string_list.h" + +void get_content_files(StringList *list, char *content_path); + +#endif +\ No newline at end of file diff --git a/src/main.c b/src/main.c @@ -2,124 +2,15 @@ #include "path_root.h" #include "string_list.h" #include "string_replace.h" -#include <dirent.h> +#include "get_content_files.h" +#include "run_pandoc.h" +#include "to_write_path.h" +#include "generate_pages.h" #include <stdio.h> #include <stdlib.h> -#include <string.h> char *BUILD_DIRECTORY = "build"; -/* - * - For each markdown file in `content` (command line arg) - * - Generate html with `pandoc` - * - Load `templates/base.html` (from command line arg) - * - For each `content` html, replace sections - * - Generate output html file equivalent - */ -void get_content_files(StringList *list, char *content_path) { - struct dirent *de; - - DIR *dr = opendir(content_path); - - if (dr == NULL) { - return; - } - - while ((de = readdir(dr)) != NULL) { - if (strcmp(de->d_name, ".") == 0) { - continue; - } - - if (strcmp(de->d_name, "..") == 0) { - continue; - } - - size_t filepath_len = strlen(content_path) + strlen(de->d_name) + 2; - - char filepath[filepath_len]; - - snprintf(filepath, sizeof(filepath), "%s/%s", content_path, de->d_name); - - if (de->d_type == DT_DIR) { - get_content_files(list, filepath); - } else { - string_list_append(list, filepath); - } - } - - closedir(dr); -} - -void run_pandoc(FILE **fp, char *content_path) { - char *PANDOC_CMD = "pandoc "; - - size_t command_len = strlen(content_path) + strlen(PANDOC_CMD) + 2; - - char command[command_len]; - - snprintf(command, sizeof(command), "%s %s", PANDOC_CMD, content_path); - - *fp = popen(command, "r"); -} - -void to_write_path(char **write_path, char *read_path) { - char replace_path[strlen(read_path)]; - - strcpy(replace_path, read_path); - - *write_path = str_replace(replace_path, ".md", ".html"); - - char *root; - - path_root(&root, *write_path); - - char from_dir[strlen(root) + 2]; - - snprintf(from_dir, strlen(root) + 2, "%s/", root); - - char to_dir[strlen(BUILD_DIRECTORY) + 2]; - - snprintf(to_dir, strlen(BUILD_DIRECTORY) + 2, "%s/", BUILD_DIRECTORY); - - *write_path = str_replace(*write_path, from_dir, to_dir); -} - -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 *line = NULL; - - size_t len = 0; - - ssize_t read; - - while ((read = getline(&line, &len, read_fp)) != -1) { - fputs(line, write_fp); - } - - fclose(write_fp); - - fclose(read_fp); - - i++; - } -} - int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "Must provide content directory\n"); diff --git a/src/run_pandoc.c b/src/run_pandoc.c @@ -0,0 +1,14 @@ +#include <stdio.h> +#include <string.h> + +void run_pandoc(FILE **fp, char *content_path) { + char *PANDOC_CMD = "pandoc "; + + size_t command_len = strlen(content_path) + strlen(PANDOC_CMD) + 2; + + char command[command_len]; + + snprintf(command, sizeof(command), "%s %s", PANDOC_CMD, content_path); + + *fp = popen(command, "r"); +} +\ No newline at end of file diff --git a/src/run_pandoc.h b/src/run_pandoc.h @@ -0,0 +1,8 @@ +#ifndef RUN_PANDOC_H +#define RUN_PANDOC_H + +#include <stdio.h> + +void run_pandoc(FILE **fp, char *content_path); + +#endif +\ No newline at end of file diff --git a/src/string_list.h b/src/string_list.h @@ -1,3 +1,6 @@ +#ifndef STRING_LIST_H +#define STRING_LIST_H + #include <stddef.h> typedef struct { @@ -8,3 +11,5 @@ typedef struct { StringList *init_string_list(size_t size); void string_list_append(StringList *string_list, char *string); + +#endif diff --git a/src/to_write_path.c b/src/to_write_path.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <string.h> +#include "string_replace.h" +#include "path_root.h" + +extern char *BUILD_DIRECTORY; + +void to_write_path(char **write_path, char *read_path) { + char replace_path[strlen(read_path)]; + + strcpy(replace_path, read_path); + + *write_path = str_replace(replace_path, ".md", ".html"); + + char *root; + + path_root(&root, *write_path); + + char from_dir[strlen(root) + 2]; + + snprintf(from_dir, strlen(root) + 2, "%s/", root); + + char to_dir[strlen(BUILD_DIRECTORY) + 2]; + + snprintf(to_dir, strlen(BUILD_DIRECTORY) + 2, "%s/", BUILD_DIRECTORY); + + *write_path = str_replace(*write_path, from_dir, to_dir); +} +\ No newline at end of file diff --git a/src/to_write_path.h b/src/to_write_path.h @@ -0,0 +1,11 @@ +#ifndef TO_WRITE_PATH_H +#define TO_WRITE_PATH_H + +#include "string_replace.h" +#include "path_root.h" + +extern char *BUILD_DIRECTORY; + +void to_write_path(char **write_path, char *read_path); + +#endif +\ No newline at end of file