ccalathea/calathea.c

564 lines
15 KiB
C
Raw Normal View History

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>
2023-01-21 01:23:48 +00:00
#include <argp.h>
2023-01-17 01:13:12 +00:00
#include <cmark.h>
2023-01-21 02:43:52 +00:00
static const char *VERSION = "1.0.0-beta.1";
2023-01-17 01:13:12 +00:00
// 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 + 1);
2023-01-17 01:13:12 +00:00
if (buffer == NULL) {
printf("Warning: Failed to allocate enough memory for %s\n", filename);
fclose(file);
return NULL;
}
fread(buffer, 1, fileLength, file);
buffer[fileLength] = 0;
2023-01-17 01:13:12 +00:00
fclose(file);
return buffer;
}
/*** Hash map implementation ***/
struct PageMap {
struct Page **pages;
int capacity;
int size;
};
2023-01-17 01:13:12 +00:00
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 capacity, char key[]) {
return helper_hash_polynomial(key, 0, strlen(key), capacity, 0);
2023-01-17 01:13:12 +00:00
}
char * to_lower_case(char str[]) {
char * lower = malloc((strlen(str) + 1) * sizeof(char));
2023-01-17 01:13:12 +00:00
int i = 0;
for (; str[i] != '\0'; i++) {
lower[i] = tolower(str[i]);
2023-01-17 01:13:12 +00:00
}
lower[i] = 0;
return lower;
}
struct PageMap * map(int capacity) {
struct PageMap *map = malloc(sizeof(struct PageMap));
map->pages = calloc(capacity, sizeof(struct Page *));
map->capacity = capacity;
map->size = 0;
return map;
}
void map_free(struct PageMap *map) {
free(map->pages);
free(map);
2023-01-17 01:13:12 +00:00
}
void map_put(struct PageMap *map, char title[], struct Page *page) {
char *lowercased = to_lower_case(title);
2023-01-17 01:13:12 +00:00
int index = hash_polynomial(map->capacity, lowercased);
while (map->pages[index] != NULL) index++;
map->pages[index] = page;
map->size++;
// Resize the map if we're running low on space
if (map->size/map->capacity > 0.75) {
map->pages = realloc(map->pages, map->capacity * 2);
for (int i = map->capacity; i < map->capacity * 2; i++) {
map->pages[i] = 0;
}
map->capacity *= 2;
}
free(lowercased);
2023-01-17 01:13:12 +00:00
}
struct Page * map_get(struct PageMap *map, char title[]) {
char *lowercased = to_lower_case(title);
2023-01-17 01:13:12 +00:00
int index = hash_polynomial(map->capacity, lowercased);
struct Page *page = map->pages[index];
char *lowercasedFoundTitle = to_lower_case(page->title);
while (strcmp(lowercasedFoundTitle, lowercased) != 0) {
page = map->pages[++index];
free(lowercasedFoundTitle);
lowercasedFoundTitle = to_lower_case(page->title);
}
free(lowercased);
free(lowercasedFoundTitle);
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;
}
2023-01-21 01:23:48 +00:00
static struct argp_option options[] = {
{ "src", 's', "dir", 0, "Source directory of pages", 0 },
2023-01-21 02:43:52 +00:00
{ "version", 'v', 0, 0, "Print the version", 0 },
2023-01-21 01:23:48 +00:00
{ "template", 't', "file", 0, "Template file path", 0 },
{ "output", 'o', "dir", 0, "Output directory", 0 },
{ 0 }
};
static char *pagesLocation = "./pages";
static char *templateFileName = "./template.html";
static char *outputDirectoryName = "./build";
static int parse_opt(int key, char *arg, struct argp_state *state) {
2023-01-21 02:43:52 +00:00
// Suppress unused parameter warnings
(void) state;
2023-01-21 01:23:48 +00:00
switch (key) {
case 's': {
pagesLocation = arg;
break;
}
case 't': {
templateFileName = arg;
break;
}
case 'o': {
outputDirectoryName = arg;
break;
2023-01-17 01:13:12 +00:00
}
2023-01-21 02:43:52 +00:00
case 'v': {
printf("v%s\n", VERSION);
exit(0);
}
2023-01-17 01:13:12 +00:00
}
2023-01-21 01:23:48 +00:00
return 0;
}
int main(int argc, char *argv[]) {
int initialInboundCapacity = 2;
2023-01-21 02:43:52 +00:00
struct argp argp = {options, parse_opt, 0, 0, 0, 0, 0 };
2023-01-21 01:23:48 +00:00
argp_parse(&argp, argc, argv, 0, 0, 0);
2023-01-17 01:13:12 +00:00
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;
}
struct PageMap *pageMap = map(100);
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 = calloc(strlen(buffer), sizeof(char));
2023-01-17 01:13:12 +00:00
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
map_put(pageMap, 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-21 00:52:39 +00:00
char *createOutputDir = concat_strings(3, "mkdir ", outputDirectoryName, " 2> /dev/null");
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);
} 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, 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
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
// <ul class="calathea-incoming">\n</ul>\n
int incomingListSize = 37;
if (currentPage->incoming->length == 0) {
// ` <li>none</li>\n`
incomingListSize += 16;
} else {
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);
}
2023-01-20 17:23:19 +00:00
}
char *incomingLinksList = malloc((incomingListSize + 1) * sizeof(char));
memset(incomingLinksList, 0, (incomingListSize + 1) * sizeof(char));
strcpy(incomingLinksList, "<ul class=\"calathea-incoming\">\n");
if (currentPage->incoming->length == 0) {
strcat(incomingLinksList, " <li>none</li>\n");
} else {
for (int i = 0; i < currentPage->incoming->length; i++) {
struct Page *page = currentPage->incoming->pages[i];
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
currentPage = currentPage->next;
}
2023-01-21 00:52:39 +00:00
printf("Pages built successfully in %s\n", outputDirectoryName);
2023-01-20 17:23:19 +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);
struct Page *next = currentPage->next;
2023-01-17 01:13:12 +00:00
free(currentPage);
currentPage = next;
2023-01-17 01:13:12 +00:00
}
closedir(pagesDir);
free(templateContent);
map_free(pageMap);
2023-01-17 01:13:12 +00:00
return 0;
}