sfeed_html.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include "util.h"
  7. static struct feed *feeds;
  8. static int showsidebar;
  9. static char *line;
  10. static size_t linesize;
  11. static unsigned long totalnew, total;
  12. static time_t comparetime;
  13. static void
  14. printfeed(FILE *fp, struct feed *f)
  15. {
  16. char *fields[FieldLast];
  17. struct tm rtm, *tm;
  18. time_t parsedtime;
  19. unsigned int isnew;
  20. ssize_t linelen;
  21. if (f->name[0]) {
  22. fputs("<h2 id=\"", stdout);
  23. xmlencode(f->name, stdout);
  24. fputs("\"><a href=\"#", stdout);
  25. xmlencode(f->name, stdout);
  26. fputs("\">", stdout);
  27. xmlencode(f->name, stdout);
  28. fputs("</a></h2>\n", stdout);
  29. }
  30. fputs("<pre>\n", stdout);
  31. while ((linelen = getline(&line, &linesize, fp)) > 0 &&
  32. !ferror(stdout)) {
  33. if (line[linelen - 1] == '\n')
  34. line[--linelen] = '\0';
  35. parseline(line, fields);
  36. parsedtime = 0;
  37. if (!strtotime(fields[FieldUnixTimestamp], &parsedtime) &&
  38. (tm = localtime_r(&parsedtime, &rtm))) {
  39. isnew = (parsedtime >= comparetime) ? 1 : 0;
  40. totalnew += isnew;
  41. f->totalnew += isnew;
  42. fprintf(stdout, "%04d-%02d-%02d&nbsp;%02d:%02d ",
  43. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  44. tm->tm_hour, tm->tm_min);
  45. } else {
  46. isnew = 0;
  47. fputs(" ", stdout);
  48. }
  49. f->total++;
  50. total++;
  51. if (fields[FieldLink][0]) {
  52. fputs("<a href=\"", stdout);
  53. xmlencode(fields[FieldLink], stdout);
  54. fputs("\">", stdout);
  55. }
  56. if (isnew)
  57. fputs("<b><u>", stdout);
  58. xmlencode(fields[FieldTitle], stdout);
  59. if (isnew)
  60. fputs("</u></b>", stdout);
  61. if (fields[FieldLink][0])
  62. fputs("</a>", stdout);
  63. fputs("\n", stdout);
  64. }
  65. fputs("</pre>\n", stdout);
  66. }
  67. int
  68. main(int argc, char *argv[])
  69. {
  70. struct feed *f;
  71. char *name;
  72. FILE *fp;
  73. int i;
  74. if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
  75. err(1, "pledge");
  76. if (!(feeds = calloc(argc, sizeof(struct feed))))
  77. err(1, "calloc");
  78. if ((comparetime = time(NULL)) == (time_t)-1)
  79. errx(1, "time");
  80. /* 1 day is old news */
  81. comparetime -= 86400;
  82. fputs("<!DOCTYPE HTML>\n"
  83. "<html>\n"
  84. "\t<head>\n"
  85. "\t<meta name=\"referrer\" content=\"no-referrer\" />\n"
  86. "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
  87. "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
  88. "\t</head>\n"
  89. "\t<body class=\"noframe\">\n", stdout);
  90. showsidebar = (argc > 1);
  91. if (showsidebar)
  92. fputs("\t\t<div id=\"items\">\n", stdout);
  93. else
  94. fputs("\t\t<div id=\"items\" class=\"nosidebar\">\n", stdout);
  95. if (argc == 1) {
  96. feeds[0].name = "";
  97. printfeed(stdin, &feeds[0]);
  98. checkfileerror(stdin, "<stdin>", 'r');
  99. } else {
  100. for (i = 1; i < argc; i++) {
  101. name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
  102. feeds[i - 1].name = name;
  103. if (!(fp = fopen(argv[i], "r")))
  104. err(1, "fopen: %s", argv[i]);
  105. printfeed(fp, &feeds[i - 1]);
  106. checkfileerror(fp, argv[i], 'r');
  107. checkfileerror(stdout, "<stdout>", 'w');
  108. fclose(fp);
  109. }
  110. }
  111. fputs("</div>\n", stdout); /* div items */
  112. if (showsidebar) {
  113. fputs("\t<div id=\"sidebar\">\n\t\t<ul>\n", stdout);
  114. for (i = 1; i < argc; i++) {
  115. f = &feeds[i - 1];
  116. if (f->totalnew > 0)
  117. fputs("<li class=\"n\"><a href=\"#", stdout);
  118. else
  119. fputs("<li><a href=\"#", stdout);
  120. xmlencode(f->name, stdout);
  121. fputs("\">", stdout);
  122. if (f->totalnew > 0)
  123. fputs("<b><u>", stdout);
  124. xmlencode(f->name, stdout);
  125. fprintf(stdout, " (%lu)", f->totalnew);
  126. if (f->totalnew > 0)
  127. fputs("</u></b>", stdout);
  128. fputs("</a></li>\n", stdout);
  129. }
  130. fputs("\t\t</ul>\n\t</div>\n", stdout);
  131. }
  132. fprintf(stdout, "\t</body>\n\t<title>(%lu/%lu) - Newsfeed</title>\n</html>\n",
  133. totalnew, total);
  134. checkfileerror(stdout, "<stdout>", 'w');
  135. return 0;
  136. }