stagit

My personal build of stagit 😸️

commit 61460e506560a79800ea75ea8d2dddbbcb3e50e6
parent 093c5ab67e04936e91e44d0bf94f2bf4d148198f
Author: Pablo <pablo-escobar@riseup.net>
Date:   Sat,  1 May 2021 15:54:55 +0000

Made it so that Markdown READMEs are processed with Pandoc

Diffstat:
Mstagit.c | 45+++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 43 insertions(+), 2 deletions(-)
diff --git a/stagit.c b/stagit.c
@@ -71,6 +71,7 @@ static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:COPYING" };
 static char *license;
 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md", "HEAD:README.adoc" };
 static char *readme;
+static int fancyreadme = 0;
 static long long nlogcommits = -1; /* < 0 indicates not used */
 
 /* cache */
@@ -491,9 +492,21 @@ writeheader(FILE *fp, const char *title)
 	if (submodules)
 		fprintf(fp, "<li><a href=\"%sfile/%s.html\">Submodules</a></li>\n",
 		        relpath, submodules);
+
 	if (readme)
-		fprintf(fp, "<li><a href=\"%sfile/%s.html\">README</a></li>\n",
-		        relpath, readme);
+  {
+    if (fancyreadme)
+    {
+      fprintf(fp, "<li><a href=\"%sREADME.html\">README</a></li>\n",
+              relpath);
+    }
+    else
+    {
+      fprintf(fp, "<li><a href=\"%sfile/%s.html\">README</a></li>\n",
+              relpath, readme);
+    }
+  }
+
 	if (license)
 		fprintf(fp, "<li><a href=\"%sfile/%s.html\">LICENSE</a></li>\n",
 		        relpath, license);
@@ -1264,7 +1277,35 @@ main(int argc, char *argv[])
 	for (i = 0; i < LEN(readmefiles) && !readme; i++) {
 		if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
 		    git_object_type(obj) == GIT_OBJ_BLOB)
+    {
 			readme = readmefiles[i] + strlen("HEAD:");
+      char *ext = readme + strlen("README.");
+      
+      // If the README is a Markdown file, use pandoc to render it to HTML
+      if (strcmp(ext, "md") || strcmp(ext, "MD"))
+      {
+        readme = "README.html";
+        fancyreadme = 1;
+
+        const char *tmp = "/tmp/stagit-tmp";
+        fp = efopen(readme, "w");
+
+        // Copy the contents of the README to a temporary file
+        FILE *tmpfile = efopen(tmp, "w");
+        fprintf(tmpfile, "%s", git_blob_rawcontent((git_blob *)obj));
+        fclose(tmpfile);
+
+        // Write the header of the file
+        writeheader(fp, "README");
+        fclose(fp);
+
+        // Write the HTML using Pandoc
+        system(
+          "pandoc -f markdown -t html -o - /tmp/stagit-tmp >> README.html"
+        );
+        remove(tmp);
+      }
+    }
 		git_object_free(obj);
 	}