cld-ssg

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

commit 6adf8bdc1b674bc9961b3c2a049518897cada8fc
parent a9dd7c13f9bd29095c631d21f3f3b3394871ea30
Author: Christopher Ray Langford <chris@hellocld.com>
Date:   Tue, 24 Sep 2019 12:56:57 -0400

Able to extract the title from the first header in the post!

This uses the node structure cmark generates rather than the super simple cmark_markdown_to_html call. I should definitely rework the content generation to use this as well.

Diffstat:
Mmain.c | 23++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/main.c b/main.c @@ -15,6 +15,7 @@ struct post { }; struct post *create_post(const char *path); +char *get_title(struct cmark_node *root); int create_all_posts(struct post *posts); int create_index(struct post *posts, int totalPosts); int create_archive(struct post *posts, int totalPosts); @@ -54,10 +55,10 @@ int main() char file[MAX_URL_CHARS] = ""; strcat(file, "./testdir/"); strcat(file, (*files)->d_name); - printf("%s\n", file); struct post *tp = create_post(file); - printf("%s\n%s", file, tp->content); + printf("File Path:\t%s\nPost Title:\t%s\n%s\n", file, tp->title, tp->content); free(tp->content); + free(tp->title); free(tp); free(testdir); while(filecount-- > 0) @@ -82,14 +83,30 @@ struct post *create_post(const char *path) /* read in the post text file */ char *tmp = read_text(path, MAX_POST_CHARS); - + /* convert the markdown to html */ p->content = cmark_markdown_to_html(tmp, strlen(tmp), CMARK_OPT_DEFAULT); + + p->title = get_title(cmark_parse_document(tmp, strlen(tmp), CMARK_OPT_DEFAULT)); free(tmp); return p; } +/* Gets the post title from the first HEADER in the cmark tree */ +char *get_title(struct cmark_node *root) +{ + cmark_iter *t_iter = cmark_iter_new(root); + while(cmark_iter_next(t_iter) != CMARK_EVENT_DONE) + if(cmark_node_get_type(cmark_iter_get_node(t_iter)) == CMARK_NODE_HEADING) { + cmark_iter_next(t_iter); + break; + } + cmark_node *t_title = cmark_iter_get_node(t_iter); + cmark_iter_free(t_iter); + return cmark_node_get_literal(t_title); +} + /* Generates all posts found in ./_posts; returns total number of posts */ int create_all_posts(struct post *posts) {