cld-ssg

The static site generator (to be) used for hellocld.com
git clone git://git.hellocld.com/cld-ssg
Log | Files | Refs

commit b2a09391fcb0460327fcf0cac79b2a69c1504516
parent ebddfcf28d8272c64543b262a7adb879675b4396
Author: Christopher Ray Langford <chris@hellocld.com>
Date:   Wed, 11 Sep 2019 22:31:59 -0400

Wrote a basic (untested) file reader for loading .md posts

Diffstat:
Mmain.c | 26++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/main.c b/main.c @@ -1,6 +1,7 @@ #include "cmark.h" #include <dirent.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> #include "config.h" @@ -9,7 +10,7 @@ int md_filter(const struct dirent *); struct post { char *title; char *url; - char * content; + char *content; struct tm *time; }; @@ -19,6 +20,10 @@ int create_index(struct post *posts, int totalPosts); int create_archive(struct post *posts, int totalPosts); int create_rss(struct post *posts, int totalPosts); +void read_text(char *out, const char *path, int maxLength); + +char buf[MAX_CONTENT_CHARS]; + int main() { @@ -57,10 +62,18 @@ int md_filter(const struct dirent *d) return 0; } -/* Returns a pointer to a post struct from a .md file */ +/* Returns a pointer to a malloc'd post struct from a .md file */ struct post *create_post(const char *path) { + struct post *p = malloc(sizeof(struct post)); + + /* read in the post text file */ + read_text(buf, path, MAX_CONTENT_CHARS); + + /* convert the markdown to html */ + p->content = cmark_markdown_to_html(buf, strlen(buf), CMARK_OPT_DEFAULT); + return p; } /* Generates all posts found in ./_posts; returns total number of posts */ @@ -86,3 +99,12 @@ int create_rss(struct post *posts, int totalPosts) { } + +/* Reads a text file into a char* */ +void read_text(char *out, const char *path, int maxLength) +{ + FILE *f = fopen(path, "r"); + int c; + while((c = getc(f)) != EOF && --maxLength > 0) + *(out++) = (char)c; +}