From f1e24dd2761bd786a8bfae19812435b50c289cea Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Tue, 6 Apr 2021 13:24:03 -0400 Subject: [PATCH] Make Chroma and libcmark-gfm optional --- Makefile | 17 ++++++++++++++--- README.md | 5 +++-- src/stagit.c | 13 ++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index eb9203e..94ffe30 100644 --- 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 index a06eb0a..c7cdf40 100644 --- 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 index 247fe70..be6d859 100644 --- a/src/stagit.c +++ b/src/stagit.c @@ -14,7 +14,10 @@ #include #include + +#ifdef HAS_CMARK #include +#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, "
%s
", s); +#endif } void -- 2.39.2