commit 0e1080b6e4c4089c9af18d31002c1b562f5fa5cb
parent 02c00248d8134c69758557a78e3c632ac77ae687
Author: Pollux <pollux@pollux.codes>
Date: Fri, 17 Jan 2025 21:32:30 -0600
feat: add markdown support using lowdown library
src: http://git.vgx.fr/stagit/
Signed-off-by: Pollux <pollux@pollux.codes>
Diffstat:
2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -9,7 +9,7 @@ MANPREFIX = ${PREFIX}/man
DOCPREFIX = ${PREFIX}/share/doc/${NAME}
LIBGIT_INC = -I/usr/local/include
-LIBGIT_LIB = -L/usr/local/lib -lgit2
+LIBGIT_LIB = -L/usr/local/lib -lgit2 -llowdown -lm
# use system flags.
STAGIT_CFLAGS = ${LIBGIT_INC} ${CFLAGS}
diff --git a/stagit.c b/stagit.c
@@ -1,3 +1,4 @@
+#include <git2/blob.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -14,6 +15,9 @@
#include <git2.h>
+#include <sys/queue.h>
+#include <lowdown.h>
+
#include "compat.h"
#define LEN(s) (sizeof(s)/sizeof(*s))
@@ -946,6 +950,26 @@ writeatom(FILE *fp, int all)
return 0;
}
+int
+writemarkdownblob(FILE *fp, git_blob *blob)
+{
+ char *buf;
+ size_t bufsz;
+ struct lowdown_opts opt = {
+ .type = LOWDOWN_HTML,
+ .feat = LOWDOWN_DEFINITION|LOWDOWN_FENCED|LOWDOWN_FOOTNOTES|
+ LOWDOWN_METADATA|LOWDOWN_STRIKE|LOWDOWN_SUPER|LOWDOWN_TABLES,
+ };
+
+ lowdown_buf(&opt, git_blob_rawcontent(blob), git_blob_rawsize(blob),
+ &buf, &bufsz, NULL);
+
+ fwrite(buf, 1, bufsz, fp);
+ free(buf);
+
+ return 0;
+}
+
size_t
writeblob(git_object *obj, const char *fpath, const char *filename, size_t filesize)
{
@@ -976,7 +1000,9 @@ writeblob(git_object *obj, const char *fpath, const char *filename, size_t files
if (git_blob_is_binary((git_blob *)obj))
fputs("<p>Binary file.</p>\n", fp);
- else
+ else if (strlen(filename) >= 3 && !strcmp(filename + strlen(filename) - 3, ".md")) {
+ lc = writemarkdownblob(fp, (git_blob *)obj);
+ } else
lc = writeblobhtml(fp, (git_blob *)obj);
writefooter(fp);