commit:7d8ef1902ef3030991b50a9414116a0615ac49d9
author:Chip Black
committer:Chip Black
date:Wed Feb 2 17:17:39 2011 -0600
parents:d07aee6282c303bba6ca9dbd0fa0ad3dfca58c5a
Add brief mode of JSON generation for blerglatest
diff --git a/cgi/cgi_blerg.c b/cgi/cgi_blerg.c
line changes: +2/-2
index 737910c..9974513
--- a/cgi/cgi_blerg.c
+++ b/cgi/cgi_blerg.c
@@ -33,7 +33,7 @@ void respond_for_range(struct blerg *b, uint64_t from, uint64_t to) {
 	yajl_gen_array_open(g);
 
 	for (i = to; i != from - 1; i--) {
-		json_generate_one_record(g, NULL, b, i);
+		json_generate_one_record(g, NULL, b, i, 0);
 		yajl_gen_get_buf(g, &ybuf, &len);
 		fwrite(ybuf, len, 1, stdout);
 		yajl_gen_clear(g);
@@ -61,7 +61,7 @@ void respond_taglist(struct tag *results, int i) {
 	while (i >= 0) {
 		b = blerg_open(results[i].author);
 		if (b != NULL) {
-			json_generate_one_record(g, results[i].author, b, results[i].record);
+			json_generate_one_record(g, results[i].author, b, results[i].record, 0);
 			blerg_close(b);
 		}
 		yajl_gen_get_buf(g, &ybuf, &len);

diff --git a/common/json.c b/common/json.c
line changes: +11/-1
index 5494e4a..eebea2c
--- a/common/json.c
+++ b/common/json.c
@@ -8,7 +8,7 @@
 #include "database.h"
 #include "json.h"
 
-void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) {
+void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record, int brief) {
 	char *data;
 	char number[21];
 	int len;
@@ -18,6 +18,16 @@ void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, u
 		return;
 	}
 
+	if (brief && len > 150) {
+		len = 150;
+		/* This is a bit of a hackish way to add an ellipsis, and will
+		 * probably cause weird results if we overwrite part of a UTF-8
+		 * char */
+		data[149] = '.';
+		data[148] = '.';
+		data[147] = '.';
+	}
+
 	yajl_gen_map_open(g);
 	if (author != NULL) {
 		yajl_gen_string(g, "author", 6);

diff --git a/common/json.h b/common/json.h
line changes: +1/-1
index 78cb4d7..8a73e4c
--- a/common/json.h
+++ b/common/json.h
@@ -4,6 +4,6 @@
 #ifndef _JSON_H
 #define _JSON_H
 
-void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record);
+void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record, int brief);
 
 #endif /* _JSON_H */

diff --git a/http/http_blerg.c b/http/http_blerg.c
line changes: +2/-2
index 9fc72a7..6fd8f11
--- a/http/http_blerg.c
+++ b/http/http_blerg.c
@@ -75,7 +75,7 @@ ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) {
 	}
 
 	/* Snarf one record */
-	json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]);
+	json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i], 0);
 
 	if (gs->i == 0) {
 		yajl_gen_array_close(gs->g);
@@ -136,7 +136,7 @@ ssize_t GET_generate_taglist(void *cls, uint64_t pos, char *buf, size_t max) {
 	/* Snarf one record */
 	b = blerg_open(ts->results[ts->i].author);
 	if (b != NULL) {
-		json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record);
+		json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record, 0);
 		blerg_close(b);
 	}
 

diff --git a/tools/blerglatest.c b/tools/blerglatest.c
line changes: +1/-1
index a2d1c67..727eb18
--- a/tools/blerglatest.c
+++ b/tools/blerglatest.c
@@ -124,7 +124,7 @@ void latest_records(yajl_gen g) {
 		}
 		uint64_t record_count = blerg_get_record_count(b);
 		if (record_count > 0)
-			json_generate_one_record(g, things->arr[i].name, b, record_count - 1);
+			json_generate_one_record(g, things->arr[i].name, b, record_count - 1, 1);
 		blerg_close(b);
 	}
 	yajl_gen_array_close(g);