ctucx.git: stagit

ctucx' stagit fork

commit 1fdbc7e8ef4025e50678261ca670daca85ac298c
parent 0a81a2fac1b84fb5ee9870fafb3d62975eaf0f96
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date: Fri, 31 Jul 2020 16:56:52 +0200

Add about page for repos with REAMDE

This commits adds a new dependency: md4c (https://github.com/mity/md4c).
Now stagit will generate an about page for each repo with a README,
converting it to HTML if it is a Markdown file.
7 files changed, 65 insertions(+), 12 deletions(-)
M
Makefile
|
8
++++----
M
README
|
1
+
M
example_create.sh
|
4
+++-
M
example_post-receive.sh
|
4
+++-
M
stagit-index.c
|
2
+-
M
stagit.c
|
37
+++++++++++++++++++++++++++++++++----
M
style.css
|
21
++++++++++++++++++++-
diff --git a/Makefile b/Makefile
@@ -8,12 +8,12 @@ PREFIX = /usr/local
 MANPREFIX = ${PREFIX}/man
 DOCPREFIX = ${PREFIX}/share/doc/${NAME}
 
-LIBGIT_INC = -I/usr/local/include
-LIBGIT_LIB = -L/usr/local/lib -lgit2
+LIB_INC = -I/usr/local/include
+LIB_LIB = -L/usr/local/lib -lgit2 -lmd4c-html
 
 # use system flags.
-STAGIT_CFLAGS = ${LIBGIT_INC} ${CFLAGS}
-STAGIT_LDFLAGS = ${LIBGIT_LIB} ${LDFLAGS}
+STAGIT_CFLAGS = ${LIB_INC} ${CFLAGS}
+STAGIT_LDFLAGS = ${LIB_LIB} ${LDFLAGS}
 STAGIT_CPPFLAGS = -D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE -D_BSD_SOURCE
 
 SRC = \
diff --git a/README b/README
@@ -33,6 +33,7 @@ Dependencies
 - libc (tested with OpenBSD, FreeBSD, NetBSD, Linux: glibc and musl).
 - libgit2 (v0.22+).
 - POSIX make (optional).
+- md4c (v0.4.4+) (https://github.com/mity/md4c).
 
 
 Documentation
diff --git a/example_create.sh b/example_create.sh
@@ -49,7 +49,9 @@ for dir in "$reposdir/"*.git/; do
     stagit -c ".stagit-build-cache" "$reposdir/$r"
 
     # symlinks
-    ln -sf log.html index.html
+    [ -f "about.html" ] \
+        && ln -sf about.html index.html \
+        || ln -sf log.html index.html
     ln -sf "$reposdir/$r" ".git"
 
     echo "done"
diff --git a/example_post-receive.sh b/example_post-receive.sh
@@ -57,7 +57,9 @@ cd "${destdir}/${d}" || exit 1
 
 # make pages
 stagit -c "${cachefile}" "${reposdir}/${r}"
-ln -sf log.html index.html
+[ -f "about.html" ] \
+    && ln -sf about.html index.html \
+    || ln -sf log.html index.html
 ln -sf "${dir}" .git
 
 # make index
diff --git a/stagit-index.c b/stagit-index.c
@@ -121,7 +121,7 @@ writelog(FILE *fp)
 
 	fputs("<tr><td><a href=\"", fp);
 	xmlencode(fp, stripped_name, strlen(stripped_name));
-	fputs("/log.html\">", fp);
+	fputs("/\">", fp);
 	xmlencode(fp, stripped_name, strlen(stripped_name));
 	fputs("</a></td><td>", fp);
 	xmlencode(fp, description, strlen(description));
diff --git a/stagit.c b/stagit.c
@@ -13,6 +13,7 @@
 #include <unistd.h>
 
 #include <git2.h>
+#include <md4c-html.h>
 
 #include "compat.h"
 

@@ -375,15 +376,14 @@ writeheader(FILE *fp, const char *title)
 		fputs("</a></td></tr>", fp);
 	}
 	fputs("<tr><td></td><td>\n", fp);
+	if (readme)
+		fprintf(fp, "<a href=\"%sabout.html\">About</a> | ", relpath);
 	fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
 	fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
 	fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
 	if (submodules)
 		fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
 		        relpath, submodules);
-	if (readme)
-		fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
-		        relpath, readme);
 	if (license)
 		fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
 		        relpath, license);

@@ -1064,6 +1064,12 @@ usage(char *argv0)
 	exit(1);
 }
 
+void
+process_output_md(const char* text, unsigned int size, void* fp)
+{
+	fprintf((FILE *)fp, "%.*s", size, text);
+}
+
 int
 main(int argc, char *argv[])
 {

@@ -1074,7 +1080,7 @@ main(int argc, char *argv[])
 	char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
 	char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
 	size_t n;
-	int i, fd;
+	int i, fd, r;
 
 	for (i = 1; i < argc; i++) {
 		if (argv[i][0] != '-') {

@@ -1190,6 +1196,7 @@ main(int argc, char *argv[])
 		if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
 		    git_object_type(obj) == GIT_OBJ_BLOB)
 			readme = readmefiles[i] + strlen("HEAD:");
+			r = i;
 		git_object_free(obj);
 	}
 

@@ -1198,6 +1205,28 @@ main(int argc, char *argv[])
 		submodules = ".gitmodules";
 	git_object_free(obj);
 
+	/* about page */
+	if (readme) {
+		fp = efopen("about.html", "w");
+		writeheader(fp, "About");
+		git_revparse_single(&obj, repo, readmefiles[r]);
+		const char *s = git_blob_rawcontent((git_blob *)obj);
+		if (r == 1) {
+			git_off_t len = git_blob_rawsize((git_blob *)obj);
+			fputs("<div class=\"md\">", fp);
+			if (md_html(s, len, process_output_md, fp, MD_FLAG_TABLES | MD_FLAG_TASKLISTS |
+			    MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEURLAUTOLINKS, 0))
+				fprintf(stderr, "Error parsing markdown\n");
+			fputs("</div>\n", fp);
+		} else {
+			fputs("<pre id=\"about\">", fp);
+			xmlencode(fp, s, strlen(s));
+			fputs("</pre>\n", fp);
+		}
+		writefooter(fp);
+		fclose(fp);
+	}
+
 	/* log for HEAD */
 	fp = efopen("log.html", "w");
 	relpath = "";
diff --git a/style.css b/style.css
@@ -28,7 +28,7 @@ body {
 	overflow-x: auto;
 }
 
-pre {
+pre:not(#about) {
 	overflow-x: auto;
 	border: 1px solid var(--code-border);
 	border-radius: 4px;

@@ -56,6 +56,14 @@ h1, h2, h3, h4, h5, h6 {
 	margin: 0;
 }
 
+.md h1 {
+	font-size: 1.5em;
+}
+
+.md h2 {
+	font-size: 1.25em;
+}
+
 img, h1, h2 {
 	vertical-align: middle;
 }

@@ -147,3 +155,14 @@ pre a.i:hover,
 pre a.d:hover {
 	text-decoration: none;
 }
+
+.md table {
+	border-collapse: collapse;
+	margin: 1em 1em;
+	border: 1px solid var(--border);
+}
+.md table td,
+.md table th {
+	padding: 0.25em 1em;
+	border: 1px solid var(--border);
+}