From 81633c97a89de0f044c1cfdf97f945e21ca17ca7 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Sun, 19 Jan 2020 02:08:47 +1030 Subject: rss feed: first image in each post shown in CDATA tag --- config.h | 2 ++ pages.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ pages.h | 1 + rss.txt | 1 - 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/config.h b/config.h index 1b13b12..4e7cc5c 100644 --- a/config.h +++ b/config.h @@ -100,6 +100,8 @@ static const int ignore[] = {}; /* ignore these posts */ static const char *rss_output = "posts/posts.rss"; static const char *rss_template = "rss.txt"; static const int post_count = 10; +static const char *base_url = "https://danieljon.es/posts/"; +static const char *author_string = "daniel@danieljon.es"; /* each page to be generated go into this array */ static const struct page pages[] = { diff --git a/pages.c b/pages.c index 1669778..9cc5868 100644 --- a/pages.c +++ b/pages.c @@ -762,6 +762,44 @@ char return date; } +char +*getimage(const char *line, char *buff, size_t size) +{ + long pos = 0; + size_t linesize = strlen(line); + while (((line[pos] != '>') && (line[pos] != '\0')) + && pos < linesize + && pos < size) + { + buff[pos] = line[pos]; + pos++; + } + buff[pos++] = '>'; + buff[pos++] = '\0'; + /* check src attribute for relative link and make it absolute */ + char b1[8] = {0}; + char newbuff[size-pos]; // ensure we dont exceed size, pos bytes already written + char finalbuff[size]; + memset(finalbuff, 0, size); + char *src = strstr(buff, "src=\""); + pos = 5; // size of src=" + if (src) + { + src += pos; + strncpy(b1, src, 4); + if (strcmp(b1, "http") != 0) // naively assume an http means a url + { + fprintf(stderr, "%s: relative path in rss feed, fixing..\n", src); + snprintf(newbuff, size-pos, "%s/%s", base_url, src); + strncpy(finalbuff, buff, src-buff); + strncat(finalbuff, newbuff, size-pos); + strncpy(buff, finalbuff, size); + } + } + + return buff; +} + int writerss(FILE *out, int post) { @@ -784,8 +822,10 @@ writerss(FILE *out, int post) char line[4096]; char title[512] = {0}; char date[512] = {0}; - char description[512] = {0}; - char item[4096] = {0}; + char description[4096] = {0}; + char item[8195] = {0}; + char image[512] = {0}; + int hasimg = 0; int pos = 0; while ((fgets(line, 4096, in)) != NULL) { @@ -794,22 +834,34 @@ writerss(FILE *out, int post) /* should be the title */ strncpy(title, line, 512); } - if (pos == 1) + else if (pos == 1) { /* should be the date */ strncpy(date, line, 512); } - if (pos >= 2 && (title[0] == '\n' || date[0] == '\n')) + else if (pos >= 2 && (title[0] == '\n' || date[0] == '\n')) { fprintf(stderr, "post %s has a broken format, please fix it\n", buff); fclose(in); return 0; } - if (pos == 3) + else if (pos == 3) { strncpy(description, line, 100); } + + /* try to find images */ + else if (pos > 3) + { + char *img; + if ((img = strstr(line, "\n\t%s\n\t%s\n\thttps://danieljon.es/posts/direct/%d\n\thttps://danieljon.es/posts/direct/%d\n\t%s\n\n", title, date, post, post, description); + if (hasimg) + { + snprintf(item, 8195, "\n\t%s\n\t%s\n\t%s\n\thttps://danieljon.es/posts/direct/%d\n\thttps://danieljon.es/posts/direct/%d\n\t%s\n \n\n", title, date, author_string, post, post, description, image); + } + else + { + snprintf(item, 8195, "\n\t%s\n\t%s\n\t%s\n\thttps://danieljon.es/posts/direct/%d\n\thttps://danieljon.es/posts/direct/%d\n\t%s\n\n", title, date, author_string, post, post, description); + } fprintf(out, "%s", item); fclose(in); diff --git a/pages.h b/pages.h index d20ea83..f99a6aa 100644 --- a/pages.h +++ b/pages.h @@ -51,6 +51,7 @@ int generaterss(const int *posts, size_t totalposts); int writerss(FILE *out, int post); char *striphtml(char *str, size_t size); char *rfc822date(char *date, size_t size); +char *getimage(const char *line, char *buff, size_t size); /* generators (to be put into the pages array) */ int frontpage(int flags); diff --git a/rss.txt b/rss.txt index 8c9c5f2..a23192c 100644 --- a/rss.txt +++ b/rss.txt @@ -5,7 +5,6 @@ https://danieljon.es/posts Posts made by daniel_j on https://danieljon.es/posts https://github.com/daniel-Jones/websitegenerator - Daniel Jones (daniel@danieljon.es) https://danieljon.es/media/tux.png danieljon.es posts -- cgit v1.2.3