78 lines
2.1 KiB
C
78 lines
2.1 KiB
C
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <md4c.h>
|
|
#include <md4c-html.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MD4C_USE_UTF8
|
|
|
|
char* combine_strs(char* str1, char* str2) {
|
|
int str1_len = strlen(str1);
|
|
int str2_len = strlen(str2);
|
|
char* output_buffer = malloc((str1_len + str2_len + 1) * sizeof(char));
|
|
if (output_buffer == NULL) {
|
|
printf("Error: Allocation Error\n");
|
|
return NULL;
|
|
}
|
|
memset(output_buffer, '\0', str1_len + str2_len + 1);
|
|
strncat(output_buffer, str1, str1_len);
|
|
strncat(output_buffer, str2, str2_len);
|
|
return output_buffer;
|
|
}
|
|
|
|
void* process(const MD_CHAR* text, MD_SIZE text_size, void* output) {
|
|
char* output_filename = combine_strs("output/", output);
|
|
if (output_filename == NULL) {
|
|
printf("Error: Unable to create filename buffer... Exiting\n");
|
|
exit(3);
|
|
}
|
|
FILE* output_file = fopen(output_filename, "");
|
|
if (output_file == NULL) {
|
|
printf("Error: Cannot open output file %s... Exiting\n", output_filename);
|
|
free(output_filename);
|
|
exit(4);
|
|
}
|
|
free(output_filename);
|
|
int bytes = fwrite(text, sizeof(char), text_size, output_file);
|
|
if (bytes != text_size) {
|
|
printf("Warning: %d bytes written when %d bytes were expected\n", bytes, text_size);
|
|
}
|
|
fclose(output_file);
|
|
return NULL;
|
|
}
|
|
|
|
void render_html(char* markdown_dir) {
|
|
int pf = MD_FLAG_COLLAPSEWHITESPACE | MD_FLAG_TABLES | MD_FLAG_TASKLISTS | MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE;
|
|
int rf = 0;
|
|
|
|
struct dirent* de;
|
|
DIR* dr = opendir(markdown_dir);
|
|
errno = 0;
|
|
if (dr == NULL) {
|
|
printf("Error: Couldn't open directory %s\n", markdown_dir);
|
|
exit(2);
|
|
}
|
|
while ((de = readdir(dr)) != NULL) {
|
|
switch (de->d_type) {
|
|
case DT_DIR:
|
|
render_html(de->d_name);
|
|
break;
|
|
case DT_REG:
|
|
md_html(input, strlen(input), process, NULL, pf, rf);
|
|
break;
|
|
}
|
|
}
|
|
if (errno != 0) {
|
|
printf("Warning: Error occured while scanning directory %s\n", markdown_dir);
|
|
}
|
|
}
|
|
|
|
int main(int argv, char** argc) {
|
|
if (argv != 2) {
|
|
printf("Usage: ssg MARKDOWN_DIR\n");
|
|
exit(1);
|
|
}
|
|
}
|