summaryrefslogtreecommitdiffstats
path: root/posts.c
blob: 04535b9d2daac5bbfcea9c5bde86dac9766a2ac8 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "sm.h"
#include "util.h"

extern char kcontentdir[];
extern char kpostsexport[];

static void postsexport(Document **, int);

Plugin posts = {"posts", postsexport, NULL, 0};

static int
creatcompar(const Document **d1, const Document **d2)
{
	return (*d2)->creat - (*d1)->creat;
}

static void
postsexport(Document **doc, int ndoc)
{
	struct tm *tm, lasttm;
	char dateheader[64];
	FILE *outf;
	int i, j;

	static char header[] = "\
<!DOCTYPE html>\n\
<html>\n\
  <title>Posts</title>\n\
  <meta charset=\"utf-8\"/>\n\
  <style>\n\
    h1, nav { text-align: center }\n\
    p { margin-left: 6%; margin-right: 6% }\n\
  </style>\n\
</head>\n\
<body bgcolor=\"#000000\" background=\"/pix/crackblk.jpg\" text=\"#eeeeee\" \
link=\"orange\" alink=\"red\" vlink=\"red\">\n\
  <a href=\"https://previousplan.org\"><img src=\"/pix/previousplan-button.gif\" alt=\"PreviousPlan! web button\" title=\"PreviousPlan! web button\"></a>\n\
  <nav>\n\
    <img src=\"/pix/s-spin.gif\"/>\n\
    <i><font size=\"6\" color=\"gold\">Previous Plan!</font></i>\n\
    -\n\
    <a href=\"/about.html\">Home</a>\n\
    -\n\
    <a href=\"/posts.html\">Posts</a>\n\
    -\n\
    <a href=\"/rss.xml\">RSS</a>\n\
    -\n\
    <a href=\"/donate.html\">Donate</a>\n\
    <img src=\"/pix/s-spin.gif\"/>\n\
  </nav>\n\
  <h2>List of posts</h2>\n\
";

	static char footer[] = "\
  <hr/>\n\
  <img src=\"pix/s-mail.gif\" alt=\"EMAIL\"/>\n\
  <p>Webmaster email: <a \
href=\"mailto:erikk@previousplan.org\">erikk@previousplan.org</a> \
<a href=\"/erikk.asc\">(PGP key)</a></p>\n\
</body>\n\
</html>\n\
";

	char title[128], link[CLEN];
	char *item[] = { "  <li><a href=\"", link, "\">", title, "</a></li>\n" };

	qsort(doc, ndoc, sizeof(*doc), (int (*)(const void *, const void *))creatcompar);

	if (!mkparentdirs(kpostsexport))
		return;
	if ((outf = fopen(kpostsexport, "w")) == NULL) {
		fprintf(stderr, "%s: %s\n", kpostsexport, strerror(errno));
		return;
	}
	xfputs(header, outf);
	for (i = 0; i < ndoc; i++) {
		my_strlcpy(title, doc[i]->title, sizeof(title));
		my_strlcpy(link, doc[i]->link, sizeof(link));

		tm = gmtime(&doc[i]->creat);
		if (i == 0 || tm->tm_mon != lasttm.tm_mon || tm->tm_year != lasttm.tm_year) {
			strftime(dateheader, sizeof(dateheader), i != 0 ? "</ul>\n<h3>%B %Y</h3>\n<ul>\n" : "<h3>%B %Y</h3>\n<ul>\n", tm);
			xfputs(dateheader, outf);
		}
		memcpy(&lasttm, tm, sizeof(lasttm));
		for (j = 0; j < LEN(item); j++)
			xfputs(item[j], outf);
	}
	if (ndoc)
		xfputs("</ul>\n", outf);
	xfputs(footer, outf);
	fclose(outf);
}