diff options
author | Erik K <erikk@previousplan.org> | 2022-05-13 17:24:26 +0000 |
---|---|---|
committer | Erik K <erikk@previousplan.org> | 2022-05-13 17:24:26 +0000 |
commit | b2fada1dd19211d71e04557653d08e697134a6ce (patch) | |
tree | 063b6d1280193046321db1e53506be5b72d2220f /rss.c |
initial commit
Diffstat (limited to 'rss.c')
-rw-r--r-- | rss.c | 84 |
1 files changed, 84 insertions, 0 deletions
@@ -0,0 +1,84 @@ +#include <stdlib.h> + +#include "config.h" +#include "sm.h" +#include "util.h" + +extern char kcontentdir[]; +extern char krssexport[]; +extern char krsstitle[]; +extern char krssdescription[]; +extern char krsslink[]; + +static void rssexport(Document **, int); + +Plugin rss = {"rss", rssexport, NULL, 0}; + +static int +creatcompar(const Document **d1, const Document **d2) +{ + return (*d2)->creat - (*d1)->creat; +} + +static void +rssexport(Document **doc, int ndoc) +{ + char inpath[512]; + FILE *outf; + int i, j; + + char *header[] = { "\ +<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\ +<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n\ +\n\ +<channel>\n\ +<title>", krsstitle, "</title>\n\ +<description>", krssdescription, "</description>\n\ +<language>en-us</language>\n\ +<link>", krsslink, "</link>\n\ +<generator>sm-"VERSION"</generator>\n\ +<atom:link href=\"", krsslink, "\" rel=\"self\" type=\"application/rss+xml\" />\n\ +" }; + + char *footer[] = { "\ +</channel>\n\ +</rss>\n\ +" }; + + char filename[64], title[128], creat[64], link[CLEN]; + char *itemheader[] = { "\ +<item>\n\ + <title>", title, "</title>\n\ + <guid isPermaLink=\"false\">", filename, "</guid>\n\ + <pubDate>", creat, "</pubDate>\n\ + <link>", link, "</link>\n\ + <description><![CDATA[" }; + + char *itemfooter[] = { "\ +]]></description>\n\ +</item>\n\ +" }; + + qsort(doc, ndoc, sizeof(*doc), (int (*)(const void *, const void *))creatcompar); + + outf = xfopen(krssexport, "w"); + for (j = 0; j < LEN(header); j++) + xfputs(header[j], outf); + for (i = 0; i < ndoc; i++) { + snprintf(inpath, sizeof(inpath), "%s/%s", kcontentdir, + doc[i]->filename); + my_strlcpy(filename, doc[i]->filename, sizeof(filename)); + my_strlcpy(title, doc[i]->title, sizeof(title)); + strftime(creat, sizeof(creat), "%a, %d %b %Y %H:%M:%S %z", + gmtime(&doc[i]->creat)); + my_strlcpy(link, doc[i]->link, sizeof(link)); + for (j = 0; j < LEN(itemheader); j++) + xfputs(itemheader[j], outf); + cat(inpath, outf); + for (j = 0; j < LEN(itemfooter); j++) + xfputs(itemfooter[j], outf); + } + for (j = 0; j < LEN(footer); j++) + xfputs(footer[j], outf); + fclose(outf); +} |