]> git.armaanb.net Git - stagit.git/commitdiff
Render .md files with cmark-gfm
authorArmaan Bhojwani <me@armaanb.net>
Tue, 6 Apr 2021 00:13:12 +0000 (20:13 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Tue, 6 Apr 2021 00:13:12 +0000 (20:13 -0400)
Makefile
README
src/stagit.c

index 50461fd7ca12e07be5fbc6ed6f2e2df944f9a7ca..15dcd8697dc9b2d411e36b08e0422979b64019ca 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ DOCPREFIX = ${PREFIX}/share/doc/${NAME}
 SHAREPREFIX = ${PREFIX}/share/${NAME}
 
 LIBGIT_INC = -I/usr/local/include
-LIBGIT_LIB = -L/usr/local/lib -lgit2
+LIBGIT_LIB = -L/usr/local/lib -lgit2 -lcmark-gfm
 
 # use system flags.
 STAGIT_CFLAGS = ${LIBGIT_INC} ${CFLAGS}
diff --git a/README b/README
index e8369940f43d9ee808d25a1fdd8236fb2d05fc83..a411c497624436f2735cebab869d9e17d0d37fd0 100644 (file)
--- a/README
+++ b/README
@@ -32,8 +32,9 @@ Dependencies
 - C compiler (C99).
 - libc (tested with OpenBSD, FreeBSD, NetBSD, Linux: glibc and musl).
 - libgit2 (v0.22+).
+- Chroma
+- cmark-gfm
 - POSIX make (optional).
-- Python3, pip.
 
 
 Documentation
index 75fca676c335c2404f940806ba457539065ebd52..217021ee896c6a2124693b829175f65c69315ea1 100644 (file)
@@ -8,11 +8,13 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
 
 #include <git2.h>
+#include <cmark-gfm.h>
 
 #include "cp.h"
 #include "compat.h"
@@ -65,6 +67,8 @@ static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
 static char *readme;
 static long long nlogcommits = -1; /* < 0 indicates not used */
 
+bool htmlized; /* true if markdoown converted to HTML */
+
 /* cache */
 static git_oid lastoid;
 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
@@ -398,37 +402,57 @@ writefooter(FILE *fp)
        fputs("</div>\n</body>\n</html>\n", fp);
 }
 
+const char *
+get_ext(const char *filename)
+{
+       const char *dot = strrchr(filename, '.');
+       if(!dot || dot == filename) return "";
+       return dot + 1;
+}
+
 int
 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
 {
+       htmlized = false;
        // Flush HTML-file
        fflush(fp);
-       // Copy STDOUT
-       int stdout_copy = dup(1);
-       // Redirect STDOUT
-       dup2(fileno(fp), 1);
-
-       char cmd[] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
-       strcat(cmd, filename);
-
-       FILE *child = popen(cmd, "w");
-       if (child == NULL) {
-               printf("child is null: %s", strerror(errno));
-               exit(1);
-       }
 
-       // Give code to highlight through STDIN:
+       char *html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
+       if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
+
        int lc;
-       size_t i;
-       for (i = 0; *s && i < len; s++, i++) {
-               if (*s == '\n') lc++;
-               fprintf(child, "%c", *s);
+       if (!htmlized) {
+               // Copy STDOUT
+               int stdout_copy = dup(1);
+
+               // Redirect STDOUT
+               dup2(fileno(fp), 1);
+
+               char cmd[255] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
+               strncat(cmd, filename, strlen(filename) + 1);
+               FILE *child = popen(cmd, "w");
+               if (child == NULL) {
+                       printf("child is null: %s", strerror(errno));
+                       exit(1);
+               }
+
+               // Give code to highlight through STDIN:
+               size_t i;
+               for (i = 0; *s && i < len; s++, i++) {
+                       if (*s == '\n') lc++;
+                       fprintf(child, "%c", *s);
+               }
+
+               pclose(child);
+               fflush(stdout);
+
+               // Give back STDOUT.
+               dup2(stdout_copy, 1);
+
+       } else {
+               fprintf(fp, "%s", html);
        }
 
-       pclose(child);
-       fflush(stdout);
-       // Give back STDOUT.
-       dup2(stdout_copy, 1);
        return lc;
 }
 
@@ -804,6 +828,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
        fputs("<p> ", fp);
        xmlencode(fp, filename, strlen(filename));
        fprintf(fp, " (%juB)", (uintmax_t)filesize);
+
        fputs("</p><hr/>", fp);
 
        if (git_blob_is_binary((git_blob *)obj)) {
@@ -813,6 +838,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
                if (ferror(fp))
                        err(1, "fwrite");
        }
+
        writefooter(fp);
        fclose(fp);