2023-01-17 01:13:12 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-01-20 18:37:11 +00:00
|
|
|
#include <stdarg.h>
|
2023-01-17 01:13:12 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <cmark.h>
|
|
|
|
|
|
|
|
// Structure defining the content and metadata of a single page
|
|
|
|
struct Page {
|
|
|
|
char title[80];
|
|
|
|
char *permalink;
|
|
|
|
struct Page *next;
|
2023-01-20 17:23:19 +00:00
|
|
|
struct PageList *incoming;
|
2023-01-17 01:13:12 +00:00
|
|
|
char *content;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*** Helper functions ***/
|
|
|
|
int min(int x, int y) {
|
|
|
|
return x > y ? y : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * read_file(char *filename) {
|
|
|
|
FILE *file = fopen(filename, "rb");
|
|
|
|
|
|
|
|
if (file == NULL) {
|
|
|
|
printf("Warning: Failed to open %s\n", filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, we calculate the length of the file
|
|
|
|
fseek(file, 0, SEEK_END); // Traverse to the end of the file
|
|
|
|
int fileLength = ftell(file); // Get the current position in the file
|
|
|
|
rewind(file); // Go back to the beginning of the file
|
|
|
|
|
|
|
|
// Allocate enough space in our buffer to hold the entire file.
|
|
|
|
char *buffer = malloc(fileLength);
|
|
|
|
|
|
|
|
if (buffer == NULL) {
|
|
|
|
printf("Warning: Failed to allocate enough memory for %s\n", filename);
|
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fread(buffer, 1, fileLength, file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** Hash map implementation ***/
|
|
|
|
int helper_hash_polynomial(
|
|
|
|
char string[],
|
|
|
|
int i,
|
|
|
|
int length,
|
|
|
|
int tableSize,
|
|
|
|
unsigned long long acc
|
|
|
|
) {
|
|
|
|
if (i == length) {
|
|
|
|
return acc % tableSize;
|
|
|
|
} else {
|
|
|
|
return helper_hash_polynomial(string, i + 1, length, tableSize,
|
|
|
|
acc + string[i] * pow(33, length-i-1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int hash_polynomial(int mapSize, char key[]) {
|
|
|
|
return helper_hash_polynomial(key, 0, strlen(key), mapSize, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void to_lower_case(char dest[], char str[]) {
|
|
|
|
int i = 0;
|
|
|
|
for (; str[i] != '\0'; i++) {
|
|
|
|
dest[i] = tolower(str[i]);
|
|
|
|
}
|
|
|
|
dest[i] = 0;
|
|
|
|
}
|
|
|
|
|
2023-01-19 23:33:16 +00:00
|
|
|
void map_put(struct Page **map, int mapSize, char title[], struct Page *page) {
|
2023-01-17 01:13:12 +00:00
|
|
|
char lowercased[80];
|
|
|
|
to_lower_case(lowercased, title);
|
|
|
|
|
|
|
|
int index = hash_polynomial(mapSize, lowercased);
|
2023-01-19 23:33:16 +00:00
|
|
|
map[index] = page;
|
2023-01-17 01:13:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 23:33:16 +00:00
|
|
|
struct Page * map_get(struct Page **map, int mapSize, char title[]) {
|
2023-01-17 01:13:12 +00:00
|
|
|
char lowercased[80];
|
|
|
|
to_lower_case(lowercased, title);
|
|
|
|
|
|
|
|
int index = hash_polynomial(mapSize, lowercased);
|
2023-01-19 23:33:16 +00:00
|
|
|
struct Page *page = map[index];
|
|
|
|
|
|
|
|
return page;
|
2023-01-17 01:13:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 17:23:19 +00:00
|
|
|
/*** Dynamic Array Implementation ***/
|
|
|
|
struct PageList {
|
|
|
|
struct Page **pages;
|
|
|
|
int length;
|
|
|
|
int capacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PageList * page_list(int capacity) {
|
|
|
|
struct PageList *pageList = malloc(sizeof(struct PageList));
|
|
|
|
pageList->pages = malloc(sizeof(struct Page *) * capacity);
|
|
|
|
pageList->capacity = capacity;
|
|
|
|
pageList->length = 0;
|
|
|
|
|
|
|
|
return pageList;
|
|
|
|
}
|
|
|
|
|
|
|
|
void page_list_insert(struct PageList *list, struct Page *page) {
|
|
|
|
// If the page is already present in the list, we don't bother adding it again
|
|
|
|
for (int i = 0; i < list->length; i++) {
|
|
|
|
if (page == list->pages[i]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increase the length of the array if necessary
|
|
|
|
if (list->length >= list->capacity) {
|
|
|
|
list->pages = realloc(list->pages, (list->capacity + 1)*sizeof(struct Page *));
|
|
|
|
}
|
|
|
|
|
|
|
|
list->pages[list->length] = page;
|
|
|
|
list->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void page_list_free(struct PageList *list) {
|
|
|
|
free(list->pages);
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
2023-01-17 01:13:12 +00:00
|
|
|
/*** Templating ***/
|
2023-01-20 18:37:11 +00:00
|
|
|
char * concat_strings(int n, ...) {
|
|
|
|
va_list list;
|
|
|
|
va_start(list, n);
|
|
|
|
|
|
|
|
int length = 0;
|
|
|
|
char **strings = malloc(n * sizeof(char *));
|
|
|
|
memset(strings, 0, n * sizeof(char *));
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
strings[i] = va_arg(list, char *);
|
|
|
|
length += strlen(strings[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
char * joinedString = malloc((length + 1) * sizeof(char *));
|
|
|
|
memset(joinedString, 0, (length + 1) * sizeof(char *));
|
|
|
|
|
|
|
|
strcpy(joinedString, strings[0]);
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
|
|
strcat(joinedString, strings[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(strings);
|
|
|
|
va_end(list);
|
|
|
|
return joinedString;
|
|
|
|
}
|
|
|
|
|
2023-01-17 01:13:12 +00:00
|
|
|
char * substitute_string(char dest[], char sub[], char *start, char *end) {
|
|
|
|
int startIndex = start - dest;
|
|
|
|
|
|
|
|
int newLength = strlen(dest) - (end - start) + strlen(sub) + 1;
|
|
|
|
|
2023-01-20 17:23:19 +00:00
|
|
|
char *compiled = malloc(newLength * sizeof(char));
|
|
|
|
memset(compiled, 0, newLength * sizeof(char));
|
2023-01-17 01:13:12 +00:00
|
|
|
strncpy(compiled, dest, startIndex);
|
|
|
|
strcat(compiled, sub);
|
|
|
|
strcat(compiled, end);
|
|
|
|
|
|
|
|
return compiled;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
char pagesLocation[256] = "./pages";
|
|
|
|
int mapSize = 1000;
|
2023-01-20 17:23:19 +00:00
|
|
|
int initialInboundCapacity = 2;
|
2023-01-17 01:13:12 +00:00
|
|
|
char templateFileName[256] = "./template.html";
|
|
|
|
char outputDirectoryName[256] = "./build";
|
|
|
|
|
|
|
|
/*** Argument Parsing ***/
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "--pages") == 0) {
|
|
|
|
// We only want to do this if a directory was actually supplied
|
|
|
|
if (i + 1 < argc) {
|
|
|
|
i++;
|
|
|
|
strcpy(pagesLocation, argv[i]);
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i], "--table-size") == 0) {
|
|
|
|
if (i + 1 < argc) {
|
|
|
|
i++;
|
|
|
|
mapSize = atoi(argv[i]);
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i], "--template") == 0) {
|
|
|
|
if (i + 1 < argc) {
|
|
|
|
i++;
|
|
|
|
strcpy(templateFileName, argv[i]);
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[i], "--output-dir") == 0) {
|
|
|
|
if (i + 1 < argc) {
|
|
|
|
i++;
|
|
|
|
strcpy(outputDirectoryName, argv[i]);
|
|
|
|
}
|
2023-01-20 17:23:19 +00:00
|
|
|
} else if (strcmp(argv[i], "--incoming-cap") == 0) {
|
|
|
|
if (i + 1 < argc) {
|
|
|
|
i++;
|
|
|
|
initialInboundCapacity = atoi(argv[i]);
|
|
|
|
}
|
2023-01-17 01:13:12 +00:00
|
|
|
} else {
|
|
|
|
printf("Unknown argument: %s\n", argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *templateContent = read_file(templateFileName);
|
|
|
|
|
|
|
|
if (templateContent == NULL) {
|
|
|
|
// If no template is given, we'll just dump the rendered markdown into a
|
|
|
|
// plaintext file
|
|
|
|
templateContent = malloc(sizeof(char) * 12);
|
|
|
|
strcpy(templateContent, "{{content}}\0");
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *pagesDir = opendir(pagesLocation);
|
|
|
|
|
|
|
|
if (pagesDir == NULL) {
|
|
|
|
fprintf(stderr, "Unable to open directory: %s\n", pagesLocation);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-19 23:33:16 +00:00
|
|
|
struct Page ** pageMap = malloc(mapSize * sizeof(struct Page *));
|
|
|
|
memset(pageMap, 0, mapSize * sizeof(struct Page *));
|
2023-01-17 01:13:12 +00:00
|
|
|
|
|
|
|
// Contains some information about the current file picked from pagesDir
|
|
|
|
struct dirent *fileEntry = readdir(pagesDir);
|
|
|
|
struct Page *currentPage = malloc(sizeof(struct Page));
|
|
|
|
struct Page *firstPage = currentPage;
|
|
|
|
|
|
|
|
while (fileEntry != NULL) {
|
|
|
|
// Ignore hidden files, ".", and ".." on Unix
|
|
|
|
if (fileEntry->d_name[0] == '.') {
|
|
|
|
fileEntry = readdir(pagesDir);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the base name of the file
|
|
|
|
int filenameLength = strlen(fileEntry->d_name);
|
|
|
|
|
|
|
|
char fileBasename[filenameLength];
|
|
|
|
memset(fileBasename, 0, filenameLength);
|
|
|
|
|
|
|
|
unsigned char foundPoint = 0;
|
|
|
|
|
|
|
|
for (int i = filenameLength - 1; i >= 0; i--) {
|
|
|
|
if (foundPoint) {
|
|
|
|
// Start writing the base name into the string
|
|
|
|
fileBasename[i] = fileEntry->d_name[i];
|
|
|
|
} else if (fileEntry->d_name[i] == '.') {
|
|
|
|
// Start writing on the next iteration
|
|
|
|
foundPoint = 1;
|
|
|
|
} else {
|
|
|
|
// Write zeros where the extension would have been
|
|
|
|
fileBasename[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 17:23:19 +00:00
|
|
|
currentPage->incoming = page_list(initialInboundCapacity);
|
|
|
|
|
2023-01-17 01:13:12 +00:00
|
|
|
// Build the page's permalink
|
2023-01-20 18:37:11 +00:00
|
|
|
currentPage->permalink = concat_strings(2, fileBasename, ".html");
|
2023-01-17 01:13:12 +00:00
|
|
|
|
|
|
|
// Construct the relative path
|
|
|
|
// The two accounts for the slash and the terminal zero
|
2023-01-20 18:37:11 +00:00
|
|
|
char *relativePath = concat_strings(3, pagesLocation, "/", fileEntry->d_name);
|
2023-01-17 01:13:12 +00:00
|
|
|
|
|
|
|
char *buffer = read_file(relativePath);
|
2023-01-20 18:37:11 +00:00
|
|
|
free(relativePath);
|
2023-01-17 01:13:12 +00:00
|
|
|
|
|
|
|
if (buffer == NULL) {
|
|
|
|
fileEntry = readdir(pagesDir);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a pointer to the start of the content part of the page
|
|
|
|
char *endOfFirstLine = strchr(buffer, '\n');
|
|
|
|
|
|
|
|
if (endOfFirstLine == NULL) {
|
|
|
|
printf("Warning: First line in %s/%s must be the title\n",
|
|
|
|
pagesLocation, fileEntry->d_name);
|
|
|
|
free(buffer);
|
|
|
|
fileEntry = readdir(pagesDir);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We subtract the buffer pointer from the pointer to the end of the
|
|
|
|
// first line to get the length of the title
|
|
|
|
int titleLength = (endOfFirstLine - buffer)/sizeof(char);
|
|
|
|
|
|
|
|
// Save the content string for later by mallocing it
|
|
|
|
char *contentBuffer = endOfFirstLine;
|
|
|
|
currentPage->content = malloc(
|
|
|
|
sizeof(char) * (strlen(buffer) - titleLength + 1)
|
|
|
|
);
|
|
|
|
strcpy(currentPage->content, contentBuffer);
|
|
|
|
|
|
|
|
// Copy the first line (title) into its respective field
|
|
|
|
strncpy(currentPage->title, buffer, min(titleLength, 80));
|
|
|
|
currentPage->title[min(titleLength, 80)] = 0;
|
|
|
|
|
|
|
|
// Insert it into the hash map for lookup later
|
2023-01-19 23:33:16 +00:00
|
|
|
map_put(pageMap, mapSize, currentPage->title, currentPage);
|
2023-01-17 01:13:12 +00:00
|
|
|
|
|
|
|
// Get ready to process the next page
|
|
|
|
fileEntry = readdir(pagesDir);
|
|
|
|
|
|
|
|
if (fileEntry != NULL) {
|
|
|
|
struct Page *nextPage = malloc(sizeof(struct Page));
|
|
|
|
|
|
|
|
// Swap the pages
|
|
|
|
currentPage->next = nextPage;
|
|
|
|
currentPage = nextPage;
|
|
|
|
} else {
|
|
|
|
currentPage->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the directory if it doesn't exist
|
2023-01-20 18:37:11 +00:00
|
|
|
char *createOutputDir = concat_strings(2, "mkdir ", outputDirectoryName);
|
2023-01-17 01:13:12 +00:00
|
|
|
system(createOutputDir);
|
|
|
|
free(createOutputDir);
|
|
|
|
|
2023-01-20 17:23:19 +00:00
|
|
|
/*** Link Processing ***/
|
2023-01-17 01:13:12 +00:00
|
|
|
currentPage = firstPage;
|
|
|
|
while (currentPage != NULL) {
|
|
|
|
// Scan the file for links
|
|
|
|
// This pointer is updated upon each iteration
|
|
|
|
char *nextLinkStart = strstr(currentPage->content, "[[");
|
|
|
|
while (nextLinkStart != NULL) {
|
|
|
|
char *nextLinkEnd = strstr(nextLinkStart, "]]");
|
|
|
|
|
|
|
|
if (nextLinkEnd == NULL) {
|
|
|
|
// This link is broken
|
|
|
|
printf("Warning: \"%s\" contains a broken link", currentPage->title);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int linkLength = nextLinkEnd - nextLinkStart;
|
|
|
|
|
2023-01-20 23:43:53 +00:00
|
|
|
char *nextVerticalBar = strchr(nextLinkStart, '|');
|
2023-01-17 01:13:12 +00:00
|
|
|
|
2023-01-20 23:43:53 +00:00
|
|
|
char *linkTitle = calloc(linkLength - 3, sizeof(char));
|
|
|
|
char *linkPageTitle = NULL;
|
2023-01-17 01:13:12 +00:00
|
|
|
|
2023-01-20 23:43:53 +00:00
|
|
|
if (nextVerticalBar != NULL && nextVerticalBar < nextLinkEnd) {
|
|
|
|
// The link is in the from [[link title|page title]]
|
|
|
|
strncpy(linkTitle, nextLinkStart + 2, (nextVerticalBar - 1) - (nextLinkStart + 1));
|
|
|
|
linkPageTitle = calloc((nextLinkEnd - 2) - nextVerticalBar, sizeof(char));
|
|
|
|
strncpy(linkPageTitle, nextVerticalBar + 1, (nextLinkEnd - 1) - nextVerticalBar);
|
|
|
|
puts(linkPageTitle);
|
|
|
|
} else {
|
|
|
|
// The link is of the form [[page title]]
|
|
|
|
linkPageTitle = linkTitle;
|
|
|
|
strncpy(linkTitle, nextLinkStart + 2, (linkLength - 2)*sizeof(char));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Page *linkedPage = map_get(pageMap, mapSize, linkPageTitle);
|
2023-01-20 17:23:19 +00:00
|
|
|
|
2023-01-20 18:37:11 +00:00
|
|
|
char *compiledLink = NULL;
|
2023-01-17 01:13:12 +00:00
|
|
|
|
2023-01-19 23:33:16 +00:00
|
|
|
if (linkedPage == NULL) {
|
2023-01-17 01:13:12 +00:00
|
|
|
// i.e. the page does not exist
|
2023-01-20 18:37:11 +00:00
|
|
|
compiledLink = concat_strings(3,
|
|
|
|
"<a class=\"calathea-404\" href=\"#\">",
|
2023-01-20 23:43:53 +00:00
|
|
|
linkTitle,
|
2023-01-20 18:37:11 +00:00
|
|
|
"</a>"
|
|
|
|
);
|
2023-01-17 01:13:12 +00:00
|
|
|
} else {
|
2023-01-20 17:23:19 +00:00
|
|
|
page_list_insert(linkedPage->incoming, currentPage);
|
2023-01-20 23:43:53 +00:00
|
|
|
compiledLink = concat_strings(5, "[", linkTitle, "](", linkedPage->permalink, ")");
|
2023-01-17 01:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *newContent = substitute_string(
|
|
|
|
currentPage->content, compiledLink, nextLinkStart, nextLinkEnd + 2);
|
|
|
|
free(currentPage->content);
|
|
|
|
currentPage->content = newContent;
|
|
|
|
|
|
|
|
free(compiledLink);
|
2023-01-20 23:43:53 +00:00
|
|
|
if (linkTitle != linkPageTitle) {
|
|
|
|
free(linkPageTitle);
|
|
|
|
}
|
|
|
|
free(linkTitle);
|
2023-01-17 01:13:12 +00:00
|
|
|
|
|
|
|
// Move to the next chunk of the file
|
|
|
|
// NOTE: This is suboptimal, because we search for "[[" from the
|
|
|
|
// beginning of the content at each iteration. We could start from
|
|
|
|
// the nextLinkEnd pointer, but since the memory is reallocated, we'd
|
|
|
|
// need to take into account that this pointer now points to a chunk
|
|
|
|
// of deallocated memory
|
|
|
|
nextLinkStart = strstr(currentPage->content, "[[");
|
|
|
|
}
|
|
|
|
|
2023-01-20 17:23:19 +00:00
|
|
|
currentPage = currentPage->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** Rendering ***/
|
|
|
|
currentPage = firstPage;
|
|
|
|
while (currentPage != NULL) {
|
2023-01-17 01:13:12 +00:00
|
|
|
// Compile the markdown
|
|
|
|
currentPage->content = cmark_markdown_to_html(
|
|
|
|
currentPage->content,
|
|
|
|
strlen(currentPage->content),
|
|
|
|
(1 << 17) // Disables safe mode, allowing raw HTML
|
|
|
|
);
|
|
|
|
|
2023-01-20 17:23:19 +00:00
|
|
|
// Insert the content into the template
|
|
|
|
char *renderedPage = NULL;
|
2023-01-17 01:13:12 +00:00
|
|
|
char *templateTagStart = strstr(templateContent, "{{content}}");
|
2023-01-20 17:23:19 +00:00
|
|
|
char *renderedWithContent = substitute_string(
|
|
|
|
templateContent,
|
|
|
|
currentPage->content,
|
|
|
|
templateTagStart,
|
|
|
|
templateTagStart + 11
|
2023-01-17 01:13:12 +00:00
|
|
|
);
|
|
|
|
|
2023-01-20 17:23:19 +00:00
|
|
|
renderedPage = renderedWithContent;
|
|
|
|
|
|
|
|
char *incomingTagStart = strstr(renderedPage, "{{incoming}}");
|
|
|
|
if (incomingTagStart != NULL) {
|
|
|
|
// Build the incoming links list
|
|
|
|
int incomingListSize = 37; // <ul class="calathea-incoming">\n</ul>\n
|
|
|
|
for (int i = 0; i < currentPage->incoming->length; i++) {
|
|
|
|
// ` <li><a href=\"[permalink]\">[title]</a></li>\n`
|
|
|
|
struct Page *page = currentPage->incoming->pages[i];
|
|
|
|
incomingListSize += 27 + strlen(page->title) + strlen(page->permalink);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *incomingLinksList = malloc((incomingListSize + 1) * sizeof(char));
|
|
|
|
memset(incomingLinksList, 0, (incomingListSize + 1) * sizeof(char));
|
|
|
|
|
|
|
|
strcpy(incomingLinksList, "<ul class=\"calathea-incoming\">\n");
|
|
|
|
for (int i = 0; i < currentPage->incoming->length; i++) {
|
|
|
|
struct Page *page = currentPage->incoming->pages[i];
|
2023-01-20 18:37:11 +00:00
|
|
|
char *link = concat_strings(5,
|
|
|
|
" <li><a href=\"",
|
|
|
|
page->permalink,
|
|
|
|
"\">",
|
|
|
|
page->title,
|
|
|
|
"</a></li>\n"
|
|
|
|
);
|
|
|
|
strcat(incomingLinksList, link);
|
|
|
|
free(link);
|
2023-01-20 17:23:19 +00:00
|
|
|
}
|
|
|
|
strcat(incomingLinksList, "</ul>\n");
|
|
|
|
|
|
|
|
char *renderedWithInbound = substitute_string(
|
|
|
|
renderedPage,
|
|
|
|
incomingLinksList,
|
|
|
|
incomingTagStart,
|
|
|
|
incomingTagStart + 12
|
|
|
|
);
|
|
|
|
|
|
|
|
renderedPage = renderedWithInbound;
|
|
|
|
free(renderedWithContent);
|
|
|
|
}
|
|
|
|
|
2023-01-17 01:13:12 +00:00
|
|
|
// Output the page to the file
|
2023-01-20 18:37:11 +00:00
|
|
|
char *outputFileName = concat_strings(3,
|
|
|
|
outputDirectoryName, "/", currentPage->permalink);
|
2023-01-17 01:13:12 +00:00
|
|
|
|
|
|
|
FILE *outputFile = fopen(outputFileName, "w");
|
|
|
|
|
|
|
|
if (outputFile != NULL) {
|
2023-01-20 17:23:19 +00:00
|
|
|
fputs(renderedPage, outputFile);
|
2023-01-17 01:13:12 +00:00
|
|
|
fclose(outputFile);
|
|
|
|
} else {
|
|
|
|
printf("Warning: failed to create %s\n", outputFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(outputFileName);
|
2023-01-20 17:23:19 +00:00
|
|
|
free(renderedPage);
|
2023-01-17 01:13:12 +00:00
|
|
|
|
2023-01-19 23:33:16 +00:00
|
|
|
currentPage = currentPage->next;
|
|
|
|
}
|
|
|
|
|
2023-01-20 17:23:19 +00:00
|
|
|
|
2023-01-19 23:33:16 +00:00
|
|
|
/*** Deallocation and whatnot ***/
|
|
|
|
currentPage = firstPage;
|
|
|
|
while (currentPage != NULL) {
|
2023-01-17 01:13:12 +00:00
|
|
|
free(currentPage->content);
|
|
|
|
free(currentPage->permalink);
|
2023-01-20 17:23:19 +00:00
|
|
|
page_list_free(currentPage->incoming);
|
2023-01-19 23:33:16 +00:00
|
|
|
struct Page *next = currentPage->next;
|
2023-01-17 01:13:12 +00:00
|
|
|
free(currentPage);
|
2023-01-19 23:33:16 +00:00
|
|
|
currentPage = next;
|
2023-01-17 01:13:12 +00:00
|
|
|
}
|
|
|
|
closedir(pagesDir);
|
|
|
|
free(templateContent);
|
|
|
|
|
|
|
|
// Deallocate all the pages
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|