summaryrefslogtreecommitdiffstats
path: root/rss.c
blob: e0f22fe909a2b9ba6beb952e63615bba58aceff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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);
}