]> git.armaanb.net Git - stagit.git/blobdiff - src/stagit.c
Render .md files with cmark-gfm
[stagit.git] / src / stagit.c
index 6e20754ed9fe82f23f4ba30789bc13d31aef08d1..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 */
@@ -364,7 +368,7 @@ writeheader(FILE *fp, const char *title)
                relpath, relpath);
        fputs("</td><td><h1>", fp);
        xmlencode(fp, strippedname, strlen(strippedname));
-       fputs("</h1><span class=\"desc\">", fp);
+       fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
        xmlencode(fp, description, strlen(description));
        fputs("</span></td></tr>", fp);
        if (cloneurl[0]) {
@@ -387,6 +391,8 @@ writeheader(FILE *fp, const char *title)
        if (license)
                fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
                        relpath, license);
+       fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
+                                       relpath, strippedname);
        fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
 }
 
@@ -396,42 +402,61 @@ 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 filename through STDIN:
-       fprintf(child, "%s\n", filename);
-       // 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;
 }
 
-       int
+int
 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
 {
        int lc = 0;
@@ -445,7 +470,7 @@ writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
        return lc;
 }
 
-       void
+void
 printcommit(FILE *fp, struct commitinfo *ci)
 {
        fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
@@ -473,7 +498,7 @@ printcommit(FILE *fp, struct commitinfo *ci)
        }
 }
 
-       void
+void
 printshowfile(FILE *fp, struct commitinfo *ci)
 {
        const git_diff_delta *delta;
@@ -803,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)) {
@@ -812,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);
 
@@ -1209,6 +1236,12 @@ main(int argc, char *argv[])
                submodules = ".gitmodules";
        git_object_free(obj);
 
+       /* Generate tarball */
+       char tarball[255];
+       sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
+                           strippedname, repodir);
+       system(tarball);
+
        /* log for HEAD */
        fp = efopen("log.html", "w");
        relpath = "";
@@ -1270,6 +1303,8 @@ main(int argc, char *argv[])
        writefooter(fp);
        fclose(fp);
 
+       cp("files.html", "index.html");
+
        /* summary page with branches and tags */
        fp = efopen("refs.html", "w");
        writeheader(fp, "Refs");