changeset 22:71493f7ab51d

story: insert par-breaks anytime multiple newlines are encountered
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 04 Feb 2009 16:26:14 -0500
parents 1f5cf4308249
children f0dbfa85ce14
files post.c
diffstat 1 files changed, 43 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/post.c	Wed Feb 04 15:39:15 2009 -0500
+++ b/post.c	Wed Feb 04 16:26:14 2009 -0500
@@ -50,6 +50,47 @@
 	close(fd);
 }
 
+#define CATP_SKIP	0
+#define CATP_ECHO	1
+#define CATP_PAR	2
+
+static void __do_cat_post(struct post *post, char *ibuf, int len)
+{
+	int sidx, eidx;
+	int state = CATP_SKIP;
+	char tmp;
+
+	for(eidx = sidx = 0; eidx < len; eidx++) {
+		tmp = ibuf[eidx];
+
+		switch(state) {
+			case CATP_SKIP:
+				if (tmp != '\n') {
+					fwrite(ibuf+sidx, 1, eidx-sidx, post->out);
+					fwrite("<p>", 1, 3, post->out);
+					sidx = eidx;
+					state = CATP_ECHO;
+				}
+				break;
+
+			case CATP_ECHO:
+				if (tmp == '\n')
+					state = CATP_PAR;
+				break;
+
+			case CATP_PAR:
+				if (tmp == '\n') {
+					fwrite(ibuf+sidx, 1, eidx-sidx, post->out);
+					fwrite("</p>", 1, 4, post->out);
+					sidx = eidx+1;
+					state = CATP_SKIP;
+				}
+				break;
+
+		}
+	}
+}
+
 void cat_post(struct post *post)
 {
 	char path[FILENAME_MAX];
@@ -78,10 +119,7 @@
 		goto out_close;
 	}
 
-	/* FIXME: do <p> insertion, etc. */
-	fwrite("<p>", 1, 3, post->out);
-	fwrite(ibuf, 1, statbuf.st_size, post->out);
-	fwrite("</p>", 1, 4, post->out);
+	__do_cat_post(post, ibuf, statbuf.st_size);
 
 	munmap(ibuf, statbuf.st_size);
 
@@ -118,10 +156,7 @@
 		goto out_close;
 	}
 
-	/* FIXME: do <p> insertion, etc. */
-	fwrite("<p>", 1, 3, post->out);
-	fwrite(ibuf, 1, statbuf.st_size, post->out);
-	fwrite("</p>", 1, 4, post->out);
+	__do_cat_post(post, ibuf, statbuf.st_size);
 
 	munmap(ibuf, statbuf.st_size);