repostead

static page generator for VCS repositories

git clone https://www.shemshak.org/~amin/repostead


diff --git a/Makefile b/Makefile
index d0a3b46..e425227 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,9 @@
 CFLAGS = -g -Wall -Wextra -Werror -Wformat=2 -Wno-unused-parameter\
 	-fsanitize=address,undefined -pedantic
 
-LIBS = $(shell pkg-config --cflags --libs libgit2)
+LIBS = $$(pkg-config --cflags --libs libgit2)
+LOWDOWN = $$(pkg-config --exists lowdown && \
+	echo -DHAVE_LOWDOWN $$(pkg-config --cflags --libs lowdown))
 
 repostead: repostead.c
-	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ repostead.c $(LIBS) $(LOWDOWN)
diff --git a/repostead.c b/repostead.c
index 6a22d88..9da4e0c 100644
--- a/repostead.c
+++ b/repostead.c
@@ -34,6 +34,11 @@
 
 #include <git2.h>
 
+#ifdef HAVE_LOWDOWN
+#include <sys/queue.h>
+#include <lowdown.h>
+#endif
+
 #define RS_NAME "repostead"
 #define RS_VERSION "0.1.0"
 
@@ -644,7 +649,7 @@ page_begin(const struct repostead_config *conf,
 	FILE *f = fopen(path, "w");
 	if (!f) {
 		die("failed to open %s for writing: %s", path, strerror(errno));
-        }
+	}
 	write_header(f, conf, title, is_root);
 	return f;
 }
@@ -794,6 +799,17 @@ tree_readme(FILE *f, const struct repostead_config *conf, const git_tree *tree)
 			git_blob *blob = NULL;
 			const void *data = NULL;
 			git_object_size_t size = 0;
+#ifdef HAVE_LOWDOWN
+			size_t fnlen = strlen(readme_files[i]);
+			bool is_md = (fnlen >= 3
+				&& strcmp(readme_files[i] + fnlen - 3, ".md") == 0);
+			struct lowdown_buf *out;
+			struct lowdown_doc *doc;
+			struct lowdown_node *n;
+			struct lowdown_opts opts;
+			char *ovr[] = { "shiftheadinglevelby: 1\n" };
+			void *rndr;
+#endif
 
 			if (git_blob_lookup(&blob, conf->repo, oid) != 0) {
 				gitdie("git_blob_lookup");
@@ -806,7 +822,40 @@ tree_readme(FILE *f, const struct repostead_config *conf, const git_tree *tree)
 			fputs("<h2 id=\"readme\">", f);
 			html_esc(f, readme_files[i]);
 			fputs("</h2>", f);
+#ifdef HAVE_LOWDOWN
+			if (is_md) {
+				memset(&opts, 0, sizeof(opts));
+				opts.type = LOWDOWN_HTML;
+				opts.maxdepth = 128;
+				opts.metaovr = ovr;
+				opts.metaovrsz = 1;
+				if ((doc = lowdown_doc_new(&opts)) == NULL) {
+					die("lowdown_doc_new");
+				}
+				if ((n = lowdown_doc_parse(doc, NULL, data,
+						(size_t)size, NULL)) == NULL) {
+					die("lowdown_doc_parse");
+				}
+				if ((out = lowdown_buf_new(256)) == NULL) {
+					die("lowdown_buf_new");
+				}
+				if ((rndr = lowdown_html_new(NULL)) == NULL) {
+					die("lowdown_html_new");
+				}
+				if (!lowdown_html_rndr(out, rndr, n)) {
+					die("lowdown_html_rndr");
+				}
+				fwrite(out->data, 1, out->size, f);
+				lowdown_html_free(rndr);
+				lowdown_buf_free(out);
+				lowdown_node_free(n);
+				lowdown_doc_free(doc);
+			} else {
+#endif
 			print_listing(f, data, (size_t)size);
+#ifdef HAVE_LOWDOWN
+			}
+#endif
 			git_blob_free(blob);
 		}
 	}