summaryrefslogtreecommitdiff
path: root/sfeed_html.c
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-06-21 00:24:30 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-06-21 00:24:30 +0200
commit7e575e13c0cdcc4341fa2f0b6dcb90cb4bd3cd71 (patch)
tree144969df46ca9a344ba1eef0fc095ca750904e63 /sfeed_html.c
parentb2fa0c97f2b5d97631d39fa0cb3f7fe51a10a3c7 (diff)
improvements
Diffstat (limited to 'sfeed_html.c')
-rw-r--r--sfeed_html.c154
1 files changed, 81 insertions, 73 deletions
diff --git a/sfeed_html.c b/sfeed_html.c
index 4bd1b4b..c123411 100644
--- a/sfeed_html.c
+++ b/sfeed_html.c
@@ -5,80 +5,42 @@
#include <string.h>
#include <time.h>
-#include "queue.h"
#include "util.h"
-static int showsidebar = 1; /* show sidebar ? */
-static SLIST_HEAD(feedshead, feed) fhead = SLIST_HEAD_INITIALIZER(fhead);
+static struct feed **feeds = NULL;
+static int showsidebar = 0; /* show sidebar ? */
static char *line = NULL;
+static size_t linesize = 0;
+static unsigned long totalnew = 0;
+static time_t comparetime;
-int
-main(void)
+static void
+printfeed(FILE *fp, struct feed *f)
{
char *fields[FieldLast];
- unsigned long totalfeeds = 0, totalnew = 0;
+ time_t parsedtime;
unsigned int islink, isnew;
- struct feed *f, *fcur = NULL;
- time_t parsedtime, comparetime;
- size_t size = 0;
int r;
- /* 1 day old is old news */
- comparetime = time(NULL) - 86400;
-
- fputs("<!DOCTYPE HTML>\n"
- "<html>\n"
- "\t<head>\n"
- "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
- "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
- "\t</head>\n"
- "\t<body class=\"noframe\">\n", stdout);
-
- if(!(fcur = calloc(1, sizeof(struct feed))))
- err(1, "calloc");
- SLIST_INSERT_HEAD(&fhead, fcur, entry);
+ if(f->name[0] != '\0') {
+ fputs("<h2 id=\"", stdout);
+ printxmlencoded(f->name, stdout);
+ fputs("\"><a href=\"#", stdout);
+ printxmlencoded(f->name, stdout);
+ fputs("\">", stdout);
+ printxmlencoded(f->name, stdout);
+ fputs("</a></h2>\n", stdout);
+ }
+ fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n", stdout);
- while(parseline(&line, &size, fields, FieldLast, '\t', stdin) > 0) {
+ while(parseline(&line, &linesize, fields, FieldLast, '\t', fp) > 0) {
r = strtotime(fields[FieldUnixTimestamp], &parsedtime);
isnew = (r != -1 && parsedtime >= comparetime) ? 1 : 0;
islink = (fields[FieldLink][0] != '\0') ? 1 : 0;
- /* first of feed section or new feed section (differs from
- * previous one). */
- if(!totalfeeds || strcmp(fcur->name, fields[FieldFeedName])) {
- if(!(f = calloc(1, sizeof(struct feed))))
- err(1, "calloc");
- if(!(f->name = strdup(fields[FieldFeedName])))
- err(1, "strdup");
-
- SLIST_INSERT_AFTER(fcur, f, entry);
- fcur = f;
-
- if(totalfeeds) { /* end previous one. */
- fputs("</table>\n", stdout);
- } else {
- if(fields[FieldFeedName][0] == '\0' || !showsidebar) {
- /* set nosidebar class on div for styling */
- fputs("\t\t<div id=\"items\" class=\"nosidebar\">\n", stdout);
- showsidebar = 0;
- } else {
- fputs("\t\t<div id=\"items\">\n", stdout);
- }
- }
- if(fields[FieldFeedName][0] != '\0') {
- fputs("<h2 id=\"", stdout);
- printfeednameid(fcur->name, stdout);
- fputs("\"><a href=\"#", stdout);
- printfeednameid(fcur->name, stdout);
- fputs("\">", stdout);
- fputs(fcur->name, stdout);
- fputs("</a></h2>\n", stdout);
- }
- fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n", stdout);
- totalfeeds++;
- }
+
totalnew += isnew;
- fcur->totalnew += isnew;
- fcur->total++;
+ f->totalnew += isnew;
+ f->total++;
if(isnew)
fputs("<tr class=\"n\">", stdout);
@@ -91,34 +53,79 @@ main(void)
fputs("<b><u>", stdout);
if(islink) {
fputs("<a href=\"", stdout);
- if(fields[FieldBaseSiteUrl][0] != '\0')
- printlink(fields[FieldLink],
- fields[FieldBaseSiteUrl], stdout);
- else
- printlink(fields[FieldLink],
- fields[FieldFeedUrl], stdout);
+ printxmlencoded(fields[FieldLink], stdout);
fputs("\">", stdout);
}
- printhtmlencoded(fields[FieldTitle], stdout);
+ printxmlencoded(fields[FieldTitle], stdout);
if(islink)
fputs("</a>", stdout);
if(isnew)
fputs("</u></b>", stdout);
fputs("</td></tr>\n", stdout);
}
- if(totalfeeds)
- fputs("</table>\n\t\t</div>\n", stdout); /* div items */
+ fputs("</table>\n", stdout);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct feed *f;
+ FILE *fp;
+ int i;
+
+ if(!(feeds = calloc(argc, sizeof(struct feed *))))
+ err(1, "calloc");
+
+ /* 1 day old is old news */
+ comparetime = time(NULL) - 86400;
+
+ fputs("<!DOCTYPE HTML>\n"
+ "<html>\n"
+ "\t<head>\n"
+ "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
+ "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
+ "\t</head>\n"
+ "\t<body class=\"noframe\">\n", stdout);
+
+ showsidebar = (argc > 1);
+ if(showsidebar)
+ fputs("\t\t<div id=\"items\">\n", stdout);
+ else
+ fputs("\t\t<div id=\"items\" class=\"nosidebar\">\n", stdout);
+
+ if(argc == 1) {
+ if(!(feeds[0] = calloc(1, sizeof(struct feed))))
+ err(1, "calloc");
+ feeds[0]->name = "";
+ printfeed(stdin, feeds[0]);
+ if(ferror(stdin))
+ err(1, "ferror: <stdin>:");
+ } else {
+ for(i = 1; i < argc; i++) {
+ if(!(feeds[i - 1] = calloc(1, sizeof(struct feed))))
+ err(1, "calloc");
+ feeds[i - 1]->name = xbasename(argv[i]);
+
+ if(!(fp = fopen(argv[i], "r")))
+ err(1, "fopen: %s", argv[i]);
+ printfeed(fp, feeds[i - 1]);
+ if(ferror(fp))
+ err(1, "ferror: %s", argv[i]);
+ fclose(fp);
+ }
+ }
+ fputs("</div>\n", stdout); /* div items */
+
if(showsidebar) {
fputs("\t<div id=\"sidebar\">\n\t\t<ul>\n", stdout);
- SLIST_FOREACH(f, &fhead, entry) {
- if(!f->name || f->name[0] == '\0')
- continue;
+ for (i = 1; i < argc; i++) {
+ f = feeds[i - 1];
if(f->totalnew > 0)
fputs("<li class=\"n\"><a href=\"#", stdout);
else
fputs("<li><a href=\"#", stdout);
- printfeednameid(f->name, stdout);
+ printxmlencoded(f->name, stdout);
fputs("\">", stdout);
if(f->totalnew > 0)
fputs("<b><u>", stdout);
@@ -129,6 +136,7 @@ main(void)
}
fputs("\t\t</ul>\n\t</div>\n", stdout);
}
+
fprintf(stdout, "\t</body>\n\t<title>Newsfeed (%lu)</title>\n</html>",
totalnew);