summaryrefslogtreecommitdiff
path: root/sfeed_frames.c
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-08-02 19:47:57 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-08-02 19:52:47 +0200
commitc32005c3c1f31b2518b888d2f33d0c04e3c42f85 (patch)
treea8596a39ef04856582cd2a8a9e281f89bf3b3de5 /sfeed_frames.c
parent13a00fb97bdf5ce92124db8f6f46b16f1ca95f8c (diff)
sfeed_frames: wrap plain-text, encode as XML/HTML 2.0
Diffstat (limited to 'sfeed_frames.c')
-rw-r--r--sfeed_frames.c46
1 files changed, 41 insertions, 5 deletions
diff --git a/sfeed_frames.c b/sfeed_frames.c
index ab18408..e5252a5 100644
--- a/sfeed_frames.c
+++ b/sfeed_frames.c
@@ -26,24 +26,53 @@ static struct feed **feeds = NULL;
/* Unescape / decode fields printed by string_print_encoded()
* "\\" to "\", "\t", to TAB, "\n" to newline. Unrecognised escape sequences
- * are ignored: "\z" etc. Call `fn` on each escaped character. */
-void
+ * are ignored: "\z" etc. */
+static void
printcontent(const char *s, FILE *fp)
{
for (; *s; s++) {
- if (*s == '\\') {
+ switch (*s) {
+ case '\\':
switch (*(++s)) {
case '\0': return; /* ignore */
case '\\': fputc('\\', fp); break;
case 't': fputc('\t', fp); break;
case 'n': fputc('\n', fp); break;
}
- } else {
+ break;
+ default:
fputc((int)*s, fp);
}
}
}
+/* Unescape / decode fields printed by string_print_encoded()
+ * "\\" to "\", "\t", to TAB, "\n" to newline. Unrecognised escape sequences
+ * are ignored: "\z" etc. Encode HTML 2.0 / XML 1.0 entities. */
+static void
+printcontentxml(const char *s, FILE *fp)
+{
+ for (; *s; s++) {
+ switch (*s) {
+ case '\\':
+ switch (*(++s)) {
+ case '\0': return; /* ignore */
+ case '\\': fputc('\\', fp); break;
+ case 't': fputc('\t', fp); break;
+ case 'n': fputc('\n', fp); break;
+ }
+ break;
+ /* XML entities */
+ case '<': fputs("&lt;", fp); break;
+ case '>': fputs("&gt;", fp); break;
+ case '\'': fputs("&apos;", fp); break;
+ case '&': fputs("&amp;", fp); break;
+ case '"': fputs("&quot;", fp); break;
+ default: fputc((int)*s, fp);
+ }
+ }
+}
+
/* normalize path names, transform to lower-case and replace non-alpha and
* non-digit with '-' */
static size_t
@@ -135,7 +164,14 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f)
/* NOTE: this prints the raw HTML of the feed, this is
* potentially dangerous, it is up to the user / browser
* to trust a feed it's HTML content. */
- printcontent(fields[FieldContent], fpcontent);
+ if (!strcmp(fields[FieldContentType], "html")) {
+ printcontent(fields[FieldContent], fpcontent);
+ } else {
+ /* plain-text, wrap with <pre> */
+ fputs("<pre>", fpcontent);
+ printcontentxml(fields[FieldContent], fpcontent);
+ fputs("</pre>", fpcontent);
+ }
fputs("</div></body></html>", fpcontent);
fclose(fpcontent);
}