cld-ssg

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

commit e21de81a0fe02447e26a3e641b6c4252cbefdb6e
parent bd28b464022f5f8c639615de775afa6502dc4565
Author: Christopher Ray Langford <chris@hellocld.com>
Date:   Thu, 26 Sep 2019 16:46:09 -0400

Doing a little refactoring and broke it.

Diffstat:
Mmain.c | 97+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 50 insertions(+), 47 deletions(-)

diff --git a/main.c b/main.c @@ -16,7 +16,9 @@ int md_filter(const struct dirent *); struct post { char *title; - char *url; + char *dir; + char *fsource; + char *fhtml; char *content; struct tm *time; }; @@ -25,7 +27,7 @@ struct post { struct post *create_post(const char *file); char *get_post_title(struct cmark_node *root); struct tm *get_post_time(const char *file); -char *create_post_url(struct tm *time, char *title); +void free_post(struct post *p); int create_all_posts(struct post *posts); int write_index(struct post *posts, int totalPosts); @@ -57,13 +59,17 @@ int main() /* Copy images/videos/audio/static pages */ printf("*** read_file testing ***\n\n"); - char file[MAX_URL_CHARS] = "2019-09-24-20-24-real-test.md"; - struct post *tp = create_post(file); - printf("File Path:\t%s\nPost Title:\t%s\nPost URL:\t%s\n%s\n", - file, tp->title, tp->url, tp->content); + struct post *tp = create_post("2019-09-24-20-24-real-test.md"); - write_post(tp); + printf("Title:\t%s\n", tp->title); + printf("Dir:\t%s\n", tp->dir); + printf("Source:\t%s\n", tp->fsource); + printf("HTML:\t%s\n", tp->fhtml); + printf("Content:\n%s\n", tp->content); +// write_post(tp); + + free_post(tp); free(tp->content); free(tp->title); free(tp); @@ -83,12 +89,13 @@ int md_filter(const struct dirent *d) struct post *create_post(const char *file) { struct post *p = malloc(sizeof(struct post)); + p->fsource = malloc(MAX_URL_CHARS); + memcpy(p->fsource, buf, MAX_URL_CHARS); + + sprintf(buf, "%s%s", POSTDIR, p->fsource); - char *path = malloc(MAX_URL_CHARS); - strcpy(path, POSTDIR); - strcat(path, file); /* read in the post text file */ - char *tmp = read_text(path, MAX_POST_CHARS); + char *tmp = read_text(buf, MAX_POST_CHARS); struct cmark_node *t_root = cmark_parse_document( tmp, strlen(tmp), CMARK_OPT_DEFAULT); free(tmp); @@ -96,11 +103,26 @@ struct post *create_post(const char *file) /* convert the markdown to html */ p->content = cmark_render_html(t_root, CMARK_OPT_DEFAULT); + /* extract the post title from the first header in the post */ p->title = get_post_title(t_root); + /* generate the html file name */ + p->fhtml = malloc(MAX_URL_CHARS); + while((*(p->fhtml++) = *(p->title++))) + if(*(p->fhtml) == ' ') + *(p->fhtml) = '-'; + strncat(p->fhtml, ".html", 6); + + /* generate the struct tm representing the post date */ p->time = get_post_time(file); - p->url = create_post_url(p->time, p->title); + /* generate the base directory of the post */ + p->dir = malloc(MAX_URL_CHARS); + sprintf(p->dir, "%d/%02d/%02d/", + p->time->tm_year + 1900, + p->time->tm_mon, + p->time->tm_mday); + free(t_root); return p; } @@ -129,37 +151,33 @@ char *get_post_title(struct cmark_node *root) struct tm *get_post_time(const char *file) { struct tm *time = malloc(sizeof(struct tm)); - char t_time[16] = ""; - strncpy(t_time, file, 16); - if(strptime(t_time, "%Y-%m-%d-%H-%M", time) == NULL) + strncpy(buf, file, 16); + if(strptime(buf, "%Y-%m-%d-%H-%M", time) == NULL) printf("ERROR: Failed to convert time\n"); return time; } -char *create_post_url(struct tm *time, char *title) +void free_post(struct post *p) { - char *t_url = malloc(MAX_URL_CHARS); - sprintf(t_url, "%d/%02d/%02d/%s.html", - time->tm_year + 1900, - time->tm_mon, - time->tm_mday, - title); - char *c; - while((c = strchr(t_url, ' '))) - *c = '-'; - return t_url; + free(p->title); + free(p->dir); + free(p->fsource); + free(p->fhtml); + free(p->content); + free(p->time); + free(p); } int write_post(struct post *post) { - char *dir = malloc(MAX_URL_CHARS); - sprintf(dir, "%s%s", HTMLDIR, post->url); + sprintf(buf, "%s%s", HTMLDIR, post->dir); - printf("DEBUG: %s\n", dir); + printf("DEBUG: %s\n", buf); - printf("%d\n", create_directory(dir)); + printf("%d\n", create_directory(buf)); - FILE *f = fopen(post->url, "w"); + sprintf(buf, "%s%s%s", HTMLDIR, post->dir, post->fhtml); + FILE *f = fopen(buf, "w"); fprintf(f, post->content); fclose(f); } @@ -182,22 +200,7 @@ char *read_text(const char *path, int maxLength) /* Creates a directory structure based on the path provided */ int create_directory(const char *path) { - int status; mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - status = errno; - /* - char t_buf[MAX_URL_CHARS] = ""; - char *dir = strtok(path, "/"); - do { - strcat(t_buf, dir); - strcat(t_buf, "/"); - mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - status = errno; - printf("%s\t%d\n", t_buf, status); - dir = strtok(NULL, "/"); - } while (dir != NULL); - */ - - return status; + }