generate.c (6167B)
1 #include "libgen.h" 2 #include "strings.h" 3 #include <dirent.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <sys/stat.h> 8 9 #define BUILD_DIRECTORY "build" 10 #define BASE_TEMPLATE "templates/base.html" 11 #define CONTENT_TAG "{{ content }}" 12 #define TITLE_TAG "{{ title }}" 13 #define DESCRIPTION_TAG "{{ description }}" 14 #define STATIC_DIRECTORY "static" 15 16 void run_pandoc(FILE **fp, char *content_path) { 17 const char *PANDOC_CMD = "pandoc"; 18 19 char *command; 20 21 asprintf(&command, "%s %s", PANDOC_CMD, content_path); 22 23 *fp = popen(command, "r"); 24 } 25 26 void path_root(char **root, char *path) { 27 char *copy = strdup(path); 28 29 if (!copy) { 30 *root = NULL; 31 return; 32 } 33 34 char *dir = dirname(copy); 35 36 char *last = NULL; 37 38 while (strcmp(dir, ".") != 0 && strcmp(dir, "/") != 0) { 39 if (last) 40 free(last); 41 last = strdup(dir); 42 43 char *new_copy = strdup(dir); 44 free(copy); 45 copy = new_copy; 46 dir = dirname(copy); 47 } 48 49 if (last) { 50 *root = last; 51 } else { 52 *root = strdup("."); 53 } 54 55 free(copy); 56 } 57 58 void get_content_files(StringList *list, char *content_path) { 59 struct dirent *de; 60 61 DIR *dr = opendir(content_path); 62 63 if (dr == NULL) { 64 return; 65 } 66 67 while ((de = readdir(dr)) != NULL) { 68 if (strcmp(de->d_name, ".") == 0) { 69 continue; 70 } 71 72 if (strcmp(de->d_name, "..") == 0) { 73 continue; 74 } 75 76 size_t filepath_len = strlen(content_path) + strlen(de->d_name) + 2; 77 78 char filepath[filepath_len]; 79 80 snprintf(filepath, sizeof(filepath), "%s/%s", content_path, de->d_name); 81 82 if (de->d_type == DT_DIR) { 83 get_content_files(list, filepath); 84 } else { 85 string_list_append(list, filepath); 86 } 87 } 88 89 closedir(dr); 90 } 91 92 void create_subdirectories(char *filepath) { 93 char *path_copy = strdup(filepath); 94 95 if (!path_copy) 96 return; 97 98 char *last_slash = strrchr(path_copy, '/'); 99 100 if (last_slash) { 101 *last_slash = '\0'; 102 } else { 103 free(path_copy); 104 105 return; 106 } 107 108 char *p; 109 110 for (p = path_copy + 1; *p; p++) { 111 if (*p == '/') { 112 *p = '\0'; 113 114 mkdir(path_copy, 0755); 115 116 *p = '/'; 117 } 118 } 119 120 mkdir(path_copy, 0755); 121 122 free(path_copy); 123 } 124 125 void to_write_path(char **write_path, char *read_path) { 126 char replace_path[strlen(read_path)]; 127 128 strcpy(replace_path, read_path); 129 130 *write_path = str_replace(replace_path, ".md", ".html"); 131 132 char *root; 133 134 path_root(&root, *write_path); 135 136 char from_dir[strlen(root) + 2]; 137 138 snprintf(from_dir, strlen(root) + 2, "%s/", root); 139 140 char *to_dir; 141 142 asprintf(&to_dir, "%s/", BUILD_DIRECTORY); 143 144 *write_path = str_replace(*write_path, from_dir, to_dir); 145 } 146 147 void get_content_metadata(StringMap **metadata, char *content_path) { 148 FILE *content_fp = fopen(content_path, "r"); 149 150 char *content_line = NULL; 151 152 size_t content_len = 0; 153 154 ssize_t content_read; 155 156 bool metadata_start = false; 157 158 while ((content_read = getline(&content_line, &content_len, content_fp)) != 159 -1) { 160 if (strcmp(trim_whitespace(content_line), "---") == 0) { 161 if (metadata_start) { 162 break; 163 } else { 164 metadata_start = true; 165 continue; 166 } 167 } 168 169 if (metadata_start) { 170 char *token; 171 172 token = strtok(content_line, ":"); 173 174 char *metadata_key = NULL; 175 char *metadata_value = NULL; 176 177 while (token != NULL) { 178 if (metadata_key != NULL && metadata_value != NULL) { 179 break; 180 } 181 182 if (metadata_key == NULL) { 183 asprintf(&metadata_key, "%s", token); 184 token = strtok(NULL, ":"); 185 continue; 186 } 187 188 if (metadata_key != NULL && metadata_value == NULL) { 189 asprintf(&metadata_value, "%s", token); 190 token = strtok(NULL, ":"); 191 continue; 192 } 193 } 194 195 if (metadata_key != NULL && metadata_value != NULL) { 196 string_map_put(*metadata, trim_whitespace(metadata_key), 197 trim_whitespace(metadata_value)); 198 } 199 } 200 } 201 202 fclose(content_fp); 203 } 204 205 void generate_pages(StringList *list) { 206 int i = 0; 207 208 while (list->strings[i] != NULL) { 209 if (strcmp(trim_whitespace(list->strings[i]), "") == 0) { 210 i++; 211 continue; 212 } 213 214 StringMap *metadata = string_map_create(); 215 216 get_content_metadata(&metadata, list->strings[i]); 217 218 char *write_path; 219 220 to_write_path(&write_path, list->strings[i]); 221 222 printf("filepath: %s\n", write_path); 223 224 create_subdirectories(write_path); 225 226 FILE *write_fp = fopen(write_path, "w"); 227 228 FILE *read_fp; 229 230 run_pandoc(&read_fp, list->strings[i]); 231 232 char *template_line = NULL; 233 234 size_t template_len = 0; 235 236 ssize_t template_read; 237 238 FILE *template_fp = fopen(BASE_TEMPLATE, "r"); 239 240 while ((template_read = 241 getline(&template_line, &template_len, template_fp)) != -1) { 242 if (strcmp(trim_whitespace(template_line), CONTENT_TAG) == 0) { 243 char *content_line = NULL; 244 245 size_t content_len = 0; 246 247 ssize_t content_read; 248 249 while ((content_read = getline(&content_line, &content_len, read_fp)) != 250 -1) { 251 fputs(content_line, write_fp); 252 } 253 } else if (strcmp(trim_whitespace(template_line), TITLE_TAG) == 0) { 254 if (string_map_contains(metadata, "title")) { 255 char *title_tag; 256 257 asprintf(&title_tag, "<title>%s</title>\n", 258 string_map_get(metadata, "title")); 259 260 fputs(title_tag, write_fp); 261 } 262 } else if (strcmp(trim_whitespace(template_line), DESCRIPTION_TAG) == 0) { 263 if (string_map_contains(metadata, "description")) { 264 char *description_tag; 265 266 asprintf(&description_tag, 267 "<meta name=\"description\" content=\"%s\" />\n", 268 string_map_get(metadata, "description")); 269 270 fputs(description_tag, write_fp); 271 } 272 } else { 273 fputs(template_line, write_fp); 274 } 275 } 276 277 fclose(write_fp); 278 279 pclose(read_fp); 280 281 i++; 282 } 283 } 284 285 void copy_static() { 286 const char *CP_CMD = "cp"; 287 288 char *command = NULL; 289 290 asprintf(&command, "%s -rf %s/* %s", CP_CMD, STATIC_DIRECTORY, 291 BUILD_DIRECTORY); 292 293 FILE *fp = popen(command, "r"); 294 295 pclose(fp); 296 }