site

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

commit 8a91408c81bbc7b938bf03d019308c470a894a0f
parent 2443c59e4fb85be6d2334c5d250149fda8dabe60
Author: Henry Unite <unitehenry@gmail.com>
Date:   Sat, 11 Apr 2026 23:52:41 +0000

refactor: write path

Diffstat:
Msrc/generate.c | 58+++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Dsrc/path_root.c | 37-------------------------------------
Dsrc/path_root.h | 2--
Dsrc/to_write_path.c | 28----------------------------
Dsrc/to_write_path.h | 11-----------
5 files changed, 57 insertions(+), 79 deletions(-)

diff --git a/src/generate.c b/src/generate.c @@ -3,13 +3,47 @@ #include <stdio.h> #include <string.h> #include <stdlib.h> -#include "to_write_path.h" #include "run_pandoc.h" #include <sys/stat.h> +#include "libgen.h" +extern char *BUILD_DIRECTORY; extern char *CONTENT_TAG; extern char *BASE_TEMPLATE; +void path_root(char **root, char *path) { + char *path_copy = strdup(path); + + if (!path_copy) { + *root = NULL; + return; + } + + char *dir = dirname(path_copy); + + char *last_dir = malloc(strlen(dir) + 1); + + strcpy(last_dir, dir); + + while (strcmp(dir, ".") != 0) { + dir = dirname(path_copy); + + if (strcmp(dir, ".") != 0) { + free(last_dir); + + last_dir = malloc(strlen(dir) + 1); + + strcpy(last_dir, dir); + } + } + + *root = strdup(last_dir); + + free(last_dir); + + free(path_copy); +} + void get_content_files(StringList *list, char *content_path) { struct dirent *de; @@ -77,6 +111,28 @@ void create_subdirectories(char *filepath) { free(path_copy); } +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; diff --git a/src/path_root.c b/src/path_root.c @@ -1,37 +0,0 @@ -#include "path_root.h" -#include <libgen.h> -#include <stdlib.h> -#include <string.h> - -void path_root(char **root, char *path) { - char *path_copy = strdup(path); - - if (!path_copy) { - *root = NULL; - return; - } - - char *dir = dirname(path_copy); - - char *last_dir = malloc(strlen(dir) + 1); - - strcpy(last_dir, dir); - - while (strcmp(dir, ".") != 0) { - dir = dirname(path_copy); - - if (strcmp(dir, ".") != 0) { - free(last_dir); - - last_dir = malloc(strlen(dir) + 1); - - strcpy(last_dir, dir); - } - } - - *root = strdup(last_dir); - - free(last_dir); - - free(path_copy); -} diff --git a/src/path_root.h b/src/path_root.h @@ -1 +0,0 @@ -void path_root(char **root, char *path); -\ No newline at end of file diff --git a/src/to_write_path.c b/src/to_write_path.c @@ -1,28 +0,0 @@ -#include "path_root.h" -#include "strings.h" -#include <stdio.h> -#include <string.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); -} diff --git a/src/to_write_path.h b/src/to_write_path.h @@ -1,11 +0,0 @@ -#ifndef TO_WRITE_PATH_H -#define TO_WRITE_PATH_H - -#include "path_root.h" -#include "strings.h" - -extern char *BUILD_DIRECTORY; - -void to_write_path(char **write_path, char *read_path); - -#endif