ctucx.git: stagit

ctucx' stagit fork

commit 7d85ec5283210275b854e4c8d3abbcda2d63ccd6
parent e6c4833f313ceb3f286af5d343703afceebc6549
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 24 May 2022 13:09:00 +0200

commit: refactor html output
1 file changed, 54 insertions(+), 49 deletions(-)
M
stagit.c
|
103
+++++++++++++++++++++++++++++++++++++++++--------------------------------------
diff --git a/stagit.c b/stagit.c
@@ -663,29 +663,31 @@ writeblobhtml(FILE *fp, const git_blob *blob)
 void
 printcommit(FILE *fp, struct commitinfo *ci)
 {
-	fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
-		relpath, ci->oid, ci->oid);
+	fprintf(fp, "<div class=\"event\"><b>commit</b> <a href=\"%scommit/%s.html\">%s</a><br>", relpath, ci->oid, ci->oid);
 
 	if (ci->parentoid[0])
-		fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
-			relpath, ci->parentoid, ci->parentoid);
+		fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a><br>", relpath, ci->parentoid, ci->parentoid);
 
 	if (ci->author) {
 		fputs("<b>Author:</b> ", fp);
 		xmlencode(fp, ci->author->name, strlen(ci->author->name));
 		fputs(" &lt;<a href=\"mailto:", fp);
-		xmlencode(fp, ci->author->email, strlen(ci->author->email)); /* not percent-encoded */
+		xmlencodeline(fp, ci->author->email, strlen(ci->author->email)); /* not percent-encoded */
 		fputs("\">", fp);
-		xmlencode(fp, ci->author->email, strlen(ci->author->email));
-		fputs("</a>&gt;\n<b>Date:</b>   ", fp);
+		xmlencodeline(fp, ci->author->email, strlen(ci->author->email));
+		fputs("</a>&gt;<br>"
+			"<b>Date:</b> ", fp);
 		printtime(fp, &(ci->author->when));
-		putc('\n', fp);
+		fputs("<br>", fp);
 	}
+
 	if (ci->msg) {
-		putc('\n', fp);
+		fputs("<br><pre>", fp);
 		xmlencode(fp, ci->msg, strlen(ci->msg));
-		putc('\n', fp);
+		fputs("</pre>", fp);
 	}
+
+	fputs("</div>\n", fp);
 }
 
 void

@@ -708,12 +710,18 @@ printshowfile(FILE *fp, struct commitinfo *ci)
 	    ci->ndeltas   > 1000   ||
 	    ci->addcount  > 100000 ||
 	    ci->delcount  > 100000) {
-		fputs("Diff is too large, output suppressed.\n", fp);
+		fputs("<div class=\"event\">Diff is too large, output suppressed.</div>", fp);
 		return;
 	}
 
 	/* diff stat */
-	fputs("<b>Diffstat:</b>\n<table>", fp);
+	fputs("<div class=\"event responsive\">", fp);
+	fprintf(fp, "<pre>%zu file%s changed, <span class=\"insertion\">%zu</span> insertion%s(+), <span class=\"deletion\">%zu</span> deletion%s(-)</pre>",
+		ci->filecount, ci->filecount == 1 ? "" : "s",
+		ci->addcount,  ci->addcount  == 1 ? "" : "s",
+		ci->delcount,  ci->delcount  == 1 ? "" : "s");
+
+	fputs("<div class=\"diffstat\">", fp);
 	for (i = 0; i < ci->ndeltas; i++) {
 		delta = git_patch_get_delta(ci->deltas[i]->patch);
 

@@ -726,12 +734,13 @@ printshowfile(FILE *fp, struct commitinfo *ci)
 		case GIT_DELTA_TYPECHANGE: c = 'T'; break;
 		default:                   c = ' '; break;
 		}
+
 		if (c == ' ')
-			fprintf(fp, "<tr><td>%c", c);
+			fprintf(fp, "<div><div>%c", c);
 		else
-			fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
+			fprintf(fp, "<div><div class=\"%c\">%c", c, c);
 
-		fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
+		fprintf(fp, "</div><div><a href=\"#h%zu\">", i);
 		xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
 		if (strcmp(delta->old_file.path, delta->new_file.path)) {
 			fputs(" -&gt; ", fp);

@@ -751,36 +760,28 @@ printshowfile(FILE *fp, struct commitinfo *ci)
 		memset(&linestr, '+', add);
 		memset(&linestr[add], '-', del);
 
-		fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
+		fprintf(fp, "</a></div><div>|</div><div>%zu</div><div><span class=\"insertion\">",
 		        ci->deltas[i]->addcount + ci->deltas[i]->delcount);
 		fwrite(&linestr, 1, add, fp);
-		fputs("</span><span class=\"d\">", fp);
+		fputs("</span><span class=\"deletion\">", fp);
 		fwrite(&linestr[add], 1, del, fp);
-		fputs("</span></td></tr>\n", fp);
+		fputs("</span></div></div>", fp);
 	}
-	fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
-		ci->filecount, ci->filecount == 1 ? "" : "s",
-	        ci->addcount,  ci->addcount  == 1 ? "" : "s",
-	        ci->delcount,  ci->delcount  == 1 ? "" : "s");
-
-	fputs("<hr/>", fp);
+	fputs("</div></div>\n", fp);
 
 	for (i = 0; i < ci->ndeltas; i++) {
 		patch = ci->deltas[i]->patch;
 		delta = git_patch_get_delta(patch);
-		fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
-		percentencode(fp, delta->old_file.path, strlen(delta->old_file.path));
-		fputs(".html\">", fp);
+
+		fprintf(fp, "<div class=\"event\" id=\"h%zu\"><pre><b><a href=\"#h%zu\">diff --git a/", i, i);
 		xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
-		fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
-		percentencode(fp, delta->new_file.path, strlen(delta->new_file.path));
-		fprintf(fp, ".html\">");
+		fputs(" b/", fp);
 		xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
-		fprintf(fp, "</a></b>\n");
+		fputs("</b></a>", fp);
 
 		/* check binary data */
 		if (delta->flags & GIT_DIFF_FLAG_BINARY) {
-			fputs("Binary files differ.\n", fp);
+			fputs("  Binary files differ.</pre></div>", fp);
 			continue;
 		}
 

@@ -789,7 +790,7 @@ printshowfile(FILE *fp, struct commitinfo *ci)
 			if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
 				break;
 
-			fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
+			fprintf(fp, "\n<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
 			xmlencode(fp, hunk->header, hunk->header_len);
 			fputs("</a>", fp);
 

@@ -797,31 +798,29 @@ printshowfile(FILE *fp, struct commitinfo *ci)
 				if (git_patch_get_line_in_hunk(&line, patch, j, k))
 					break;
 				if (line->old_lineno == -1)
-					fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
-						i, j, k, i, j, k);
+					fputs("<span class=\"insertion\">+", fp);
 				else if (line->new_lineno == -1)
-					fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
-						i, j, k, i, j, k);
+					fputs("<span class=\"deletion\">-", fp);
 				else
 					putc(' ', fp);
-				xmlencodeline(fp, line->content, line->content_len);
-				putc('\n', fp);
-				if (line->old_lineno == -1 || line->new_lineno == -1)
-					fputs("</a>", fp);
+					xmlencode(fp, line->content, line->content_len);
+
+				if (line->old_lineno == -1 || line->new_lineno == -1) fputs("</span>", fp);
 			}
 		}
+
+		fputs("</pre></div>", fp);
 	}
 }
 
 void
 writelogline(FILE *fp, struct commitinfo *ci)
 {
-	fputs("<div class=\"event\">", fp);
+	fprintf(fp, "<a href=\"%scommit/%s.html\"><div class=\"event\">", relpath, ci->oid);
 
 	if (ci->author) {
-		fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
 		fwrite(ci->short_oid.ptr, sizeof(char), ci->short_oid.size, fp);
-		fputs("</a> - ", fp);
+		fputs(" - ", fp);
 		xmlencode(fp, ci->author->name, strlen(ci->author->name));
 
 		fputs("<small class=\"right\">", fp);

@@ -829,9 +828,13 @@ writelogline(FILE *fp, struct commitinfo *ci)
 		fputs("</small>", fp);
 	}
 
-	if (ci->summary) fprintf(fp, "<pre>%s</pre>", ci->summary);
+	if (ci->summary) {
+		fputs("<pre>", fp);
+		xmlencodeline(fp, ci->summary, strlen(ci->summary));
+		fputs("</pre>", fp);
+	}
 
-	fputs("</div>", fp);
+	fputs("</div></a>", fp);
 }
 
 int

@@ -848,6 +851,8 @@ writelog(FILE *fp, const git_oid *oid)
 	git_revwalk_new(&w, repo);
 	git_revwalk_push(w, oid);
 
+	fputs("<div class=\"log\">", fp);
+
 	while (!git_revwalk_next(&id, w)) {
 		relpath = "";
 

@@ -887,10 +892,8 @@ writelog(FILE *fp, const git_oid *oid)
 		if (r) {
 			relpath = "../";
 			fpfile = efopen(path, "w");
-			fputs("<pre>", fpfile);
 			writeheader(fpfile, ci->summary, "log");
 			printshowfile(fpfile, ci);
-			fputs("</pre>\n", fpfile);
 			writefooter(fpfile);
 			checkfileerror(fpfile, path, 'w');
 			fclose(fpfile);

@@ -901,13 +904,15 @@ err:
 	git_revwalk_free(w);
 
 	if (nlogcommits == 0 && remcommits != 0) {
-		fprintf(fp, "<div class=\"event\"><p>"
+		fprintf(fp, "<div class=\"event center-text\"><p>"
 		        "%zu more commits remaining, fetch the repository"
 		        "</p></div>", remcommits);
 	}
 
 	relpath = "";
 
+	fputs("</div>", fp);
+
 	return 0;
 }