summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2020-01-19 02:08:47 +1030
committerDaniel Jones <admin@danieljon.es>2020-01-19 02:08:47 +1030
commit81633c97a89de0f044c1cfdf97f945e21ca17ca7 (patch)
treeb76df5c2aac7392b7c4f6ed8876ac4b94ebcb2be
parent4457695aeb0c03b7c3f18fa1527b8e83b2823ea0 (diff)
downloadwebsitegenerator-81633c97a89de0f044c1cfdf97f945e21ca17ca7.tar.gz
websitegenerator-81633c97a89de0f044c1cfdf97f945e21ca17ca7.zip
rss feed: first image in each post shown in CDATA tag
-rw-r--r--config.h2
-rw-r--r--pages.c71
-rw-r--r--pages.h1
-rw-r--r--rss.txt1
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, "<img")))
+ {
+ hasimg = 1;
+ getimage(img, image, 1024);
+ break;
+ }
+ }
pos++;
}
striphtml(title, strlen(title));
@@ -818,7 +870,14 @@ writerss(FILE *out, int post)
strcat(description, " ...");
rfc822date(date, 512);
//printf("title is: %s date is: %s: desc is: %s\n", title, date, description);
- snprintf(item, 4096, "<item>\n\t<title>%s</title>\n\t<pubDate>%s</pubDate>\n\t<link>https://danieljon.es/posts/direct/%d</link>\n\t<guid>https://danieljon.es/posts/direct/%d</guid>\n\t<description>%s</description>\n</item>\n", title, date, post, post, description);
+ if (hasimg)
+ {
+ snprintf(item, 8195, "<item>\n\t<title>%s</title>\n\t<pubDate>%s</pubDate>\n\t<author>%s</author>\n\t<link>https://danieljon.es/posts/direct/%d</link>\n\t<guid>https://danieljon.es/posts/direct/%d</guid>\n\t<description>%s\n<![CDATA[%s]]> </description>\n</item>\n", title, date, author_string, post, post, description, image);
+ }
+ else
+ {
+ snprintf(item, 8195, "<item>\n\t<title>%s</title>\n\t<pubDate>%s</pubDate>\n\t<author>%s</author>\n\t<link>https://danieljon.es/posts/direct/%d</link>\n\t<guid>https://danieljon.es/posts/direct/%d</guid>\n\t<description>%s</description>\n</item>\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 @@
<link>https://danieljon.es/posts</link>
<description>Posts made by daniel_j on https://danieljon.es/posts</description>
<generator>https://github.com/daniel-Jones/websitegenerator</generator>
- <author>Daniel Jones (daniel@danieljon.es)</author>
<image>
<url>https://danieljon.es/media/tux.png</url>
<title>danieljon.es posts</title>