summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2020-01-12 00:30:04 +1030
committerDaniel Jones <admin@danieljon.es>2020-01-12 00:30:04 +1030
commitb57adf7a8c5ba78fdfdbf9f33dbd4837f10dd5a3 (patch)
treee58644a5a52cc2aca400a38f0eeb00d512086ca0
parentfcab620cc039fd96ea9976ef549448e4aca851de (diff)
downloadwebsitegenerator-b57adf7a8c5ba78fdfdbf9f33dbd4837f10dd5a3.tar.gz
websitegenerator-b57adf7a8c5ba78fdfdbf9f33dbd4837f10dd5a3.zip
further work implementing rss feed
currently broken
-rw-r--r--config.h7
-rw-r--r--pages.c139
-rw-r--r--pages.h4
-rw-r--r--rss.txt10
4 files changed, 155 insertions, 5 deletions
diff --git a/config.h b/config.h
index cca7d3b..1b13b12 100644
--- a/config.h
+++ b/config.h
@@ -21,6 +21,7 @@
enum flag
{
NONE = 0,
+ RSS = 1,
};
struct page
@@ -35,6 +36,7 @@ static const char *title_string = "{TITLE}";
static const char *content_string = "{CONTENT}";
static const char *info_string = "{INFO}";
static const char *time_string = "{TIME}";
+static const char *rss_string = "{ITEMS}";
static const char *template_file = "template.txt";
/* frontpage */
@@ -96,6 +98,8 @@ static const int ignore[] = {}; /* ignore these posts */
/* rss */
static const char *rss_output = "posts/posts.rss";
+static const char *rss_template = "rss.txt";
+static const int post_count = 10;
/* each page to be generated go into this array */
static const struct page pages[] = {
@@ -108,8 +112,7 @@ static const struct page pages[] = {
{opinions_animepage, NONE},
{opinions_everythingpage, NONE},
{portfoliopage, NONE},
- {postspage, NONE},
- {rsspage, NONE},
+ {postspage, RSS}, /* flag RSS to generate RSS feed */
};
#endif
diff --git a/pages.c b/pages.c
index f0b2eb4..5afd8c3 100644
--- a/pages.c
+++ b/pages.c
@@ -278,7 +278,7 @@ writeatbyte(const char *dest, struct fileorstring *source, long offset)
* rename file 'tmp' 'filename'
*/
- char c, t, a;
+ int c, t, a;
long curpos = 1;
int i = 0;
while ((c = fgetc(in)) != EOF)
@@ -716,15 +716,150 @@ postspage(int flags)
fprintf(stderr, "unable to create post pages, unrecoverable failure\n");
return 0;
}
+
+ /* generate rss feed if required */
+ if (flags & RSS)
+ {
+ generaterss(posts, totalposts);
+ }
return 1;
}
/* rss */
+
+char
+*striphtml(char *str, size_t size)
+{
+ /*
+ * strip html tags from 'str'
+ * dont copy newline
+ * return pointer to 'str'
+ */
+ int idx = 0;
+ int opened = 0; // false
+ for(size_t i = 0; i < size; i++)
+ {
+ if(str[i] == '<')
+ {
+ opened = 1; // true
+ }
+ else if (str[i] == '>')
+ {
+ opened = 0; // false
+ }
+ else if (!opened)
+ {
+ str[idx++] = str[i];
+ }
+ }
+ str[idx] = '\0';
+ if (str[idx-1] == '\n')
+ str[idx-1] = '\0';
+ return str;
+}
+int
+writerss(FILE *out, int post)
+{
+ /*
+ * create rss item using 'post_content'/'posts'.txt into FILE 'out'
+ * return 1 on success, 0 on fail
+ */
+
+ if (!out)
+ return 0;
+ char buff[512];
+ snprintf(buff, 512, "%s/%d.txt", posts_content, post);
+ FILE *in = fopen(buff, "r");
+ if (!in)
+ {
+ fprintf(stderr, "unable to open file %s, unrecoverable failure\n", buff);
+ return 0;
+ }
+
+ char line[4096];
+ char title[512] = {0};
+ char date[512] = {0};
+ char description[512] = {0};
+ char item[4096] = {0};
+ int pos = 0;
+ while ((fgets(line, 4096, in)) != NULL)
+ {
+ if (pos == 0)
+ {
+ /* should be the title */
+ strncpy(title, line, 512);
+ }
+ if (pos == 1)
+ {
+ /* should be the title */
+ strncpy(date, line, 512);
+ }
+
+ if (pos >= 2 && (title == NULL || date == NULL))
+ {
+ fprintf(stderr, "post %s has a broken format, please fix it\n", buff);
+ fclose(in);
+ return 0;
+ }
+ if (pos == 3)
+ {
+ strncpy(description, line, 100);
+ }
+ pos++;
+ }
+ striphtml(title, strlen(title));
+ striphtml(date, strlen(date));
+ striphtml(description, strlen(description));
+ strcat(description, " ...");
+ //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);
+
+ fprintf(out, "%s", item);
+ fclose(in);
+ return 1;
+}
+
int
-rsspage(int flags)
+generaterss(const int *posts, size_t totalposts)
{
/*
* generate an rss feed for our blog/posts
*/
+
+ if (!createfile(rss_output, rss_template))
+ {
+ fprintf(stderr, "unable to create %s, unrecoverable failure\n", rss_output);
+ return 0;
+ }
+
+ /* open a temporary file for writing items to */
+ FILE *tmp = fopen("rss.tmp", "w+");
+ if (!tmp)
+ {
+ fprintf(stderr, "unable to create %s, unrecoverable failure\n", "rss.tmp");
+ return 0;
+ }
+
+ /* loop through each post and write an rss file to our open file */
+ int i;
+ int towrite = post_count;
+ while (towrite > totalposts)
+ towrite--;
+ for (i = 1; i < towrite; i++)
+ {
+ if (!writerss(tmp, (totalposts - posts[i])))
+ return 0;
+ }
+
+ struct fileorstring src = {"rss.tmp", NULL};
+
+ if (!replaceinpage(rss_output, rss_string, &src))
+ {
+ fprintf(stderr, "unable to replace %s in %s, unrecoverable failure\n", rss_string, rss_output);
+ return 0;
+ }
+
+ fclose(tmp);
+ //remove("rss.tmp");
return 1;
}
diff --git a/pages.h b/pages.h
index 7639e4f..09c1abe 100644
--- a/pages.h
+++ b/pages.h
@@ -46,6 +46,9 @@ int createdirectpages(const int *posts, size_t totalposts);
char *generatepagebar(char *bar, size_t size, const int *posts, size_t totalposts, int currentpage, int pagecount);
int generatepostpages(const int *posts, size_t totalposts, int pagecount);
int writeposts(const int *posts, size_t totalposts, const char *outfile, int currentpage, int pagecount);
+int generaterss(const int *posts, size_t totalposts);
+int writerss(FILE *out, int post);
+char *striphtml(char *str, size_t size);
/* generators (to be put into the pages array) */
int frontpage(int flags);
@@ -57,6 +60,5 @@ int opinions_animepage(int flags);
int opinions_everythingpage(int flags);
int portfoliopage(int flags);
int postspage(int flags);
-int rsspage(int flags);
#endif
diff --git a/rss.txt b/rss.txt
new file mode 100644
index 0000000..f7db4cf
--- /dev/null
+++ b/rss.txt
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0">
+<channel>
+ <title>danieljon.es posts</title>
+ <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>
+ {ITEMS}
+</channel>
+</rss>