commit f1e24dd2761bd786a8bfae19812435b50c289cea
parent 516f65ffea797d22346a1acf596e69673e1c9a1d
Author: Armaan Bhojwani <me@armaanb.net>
Date: Tue, 6 Apr 2021 13:24:03 -0400
Make Chroma and libcmark-gfm optional
Diffstat:
3 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/Makefile b/Makefile
@@ -9,13 +9,24 @@ MANPREFIX = ${PREFIX}/man
DOCPREFIX = ${PREFIX}/share/doc/${NAME}
SHAREPREFIX = ${PREFIX}/share/${NAME}
+LIBCMARK = $(shell pkg-config --silence-errors --libs libcmark-gfm > /dev/null; echo $$?)
+CHROMA = $(shell which chroma; echo $$?)
LIBGIT_INC = -I/usr/local/include
-LIBGIT_LIB = -L/usr/local/lib -lgit2 -lcmark-gfm
+LIBS = -L/usr/local/lib `pkg-config --libs libgit2`
+
+ifeq (${CHROMA}, 0)
+ STAGIT_CPPFLAGS := ${STAGIT_CPPFLAGS} -DHAS_CHROMA
+endif
+
+ifeq (${LIBCMARK}, 0)
+ LIBS := ${LIBS} -lcmark-gfm
+ STAGIT_CPPFLAGS := ${STAGIT_CPPFLAGS} -DHAS_CMARK
+endif
# use system flags.
STAGIT_CFLAGS = ${LIBGIT_INC} ${CFLAGS}
-STAGIT_LDFLAGS = ${LIBGIT_LIB} ${LDFLAGS}
-STAGIT_CPPFLAGS = -D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE -D_BSD_SOURCE
+STAGIT_LDFLAGS = ${LIBS} ${LDFLAGS}
+STAGIT_CPPFLAGS := ${STAGIT_CPPFLAGS} -D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE -D_BSD_SOURCE
SRC = \
src/stagit.c\
diff --git a/README.md b/README.md
@@ -30,9 +30,10 @@ $ make
- C compiler (C99).
- libc (tested with OpenBSD, FreeBSD, NetBSD, Linux: glibc and musl).
- libgit2 (v0.22+).
- - Chroma
- - cmark-gfm
+ - Chroma (optional - for syntax highlighting).
+ - libcmark-gfm (optional - for rendering Markdown).
- POSIX make (optional).
+ - pkg-config (if using the makefile).
## Documentation
See man pages: stagit(1) and stagit-index(1).
diff --git a/src/stagit.c b/src/stagit.c
@@ -14,7 +14,10 @@
#include <unistd.h>
#include <git2.h>
+
+#ifdef HAS_CMARK
#include <cmark-gfm.h>
+#endif
#include "cp.h"
#include "compat.h"
@@ -414,12 +417,17 @@ void
call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
{
htmlized = false;
+ char *html = "";
// Flush HTML-file
fflush(fp);
- char *html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
+#ifdef HAS_CMARK
+ html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
+#endif
+
+#ifdef HAS_CHROMA
if (!htmlized) {
// Copy STDOUT
int stdout_copy = dup(1);
@@ -450,6 +458,9 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
} else {
fprintf(fp, "%s", html);
}
+#else
+ fprintf(fp, "<pre>%s</pre>", s);
+#endif
}
void