aboutsummaryrefslogtreecommitdiffstats
path: root/cgit-about-filter.c
diff options
context:
space:
mode:
authorWynn Wolf Arbor2020-05-27 15:30:14 +0200
committerWynn Wolf Arbor2020-05-27 15:43:20 +0200
commitebcc62e2a84b3b2b819f55d8910bc0b3ce84a133 (patch)
treebe52a049056da85ab9947d17ffceb860cb1a477f /cgit-about-filter.c
downloadskein-ebcc62e2a84b3b2b819f55d8910bc0b3ce84a133.tar.gz
Initial import0.0.1
Diffstat (limited to 'cgit-about-filter.c')
-rw-r--r--cgit-about-filter.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/cgit-about-filter.c b/cgit-about-filter.c
new file mode 100644
index 0000000..68dfd9b
--- /dev/null
+++ b/cgit-about-filter.c
@@ -0,0 +1,59 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
+
+#define EXEC(args) execvp(args[0], args)
+
+static const char *extension(const char *filename);
+
+char *mandoc[] = { "mandoc", "-Thtml", "-Ofragment", NULL };
+char *lowdown[] = { "lowdown", "-Thtml", NULL };
+
+int
+main(int argc, char *argv[])
+{
+ int opt;
+ while ((opt = getopt(argc, argv, "")) != -1)
+ return 1;
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1)
+ return 1;
+
+ const char *ext = extension(argv[0]);
+ if (ext == NULL)
+ return 1;
+
+ if (strlen(ext) == 1 && isdigit(*ext)) {
+ EXEC(mandoc);
+ } else if (strcmp(ext, "md") == 0) {
+ EXEC(lowdown);
+ }
+
+ return 1;
+}
+
+static const char *
+extension(const char *filename)
+{
+ const char *end = strrchr(filename, '.');
+
+ // no dot
+ if (end == NULL)
+ return NULL;
+
+ // no extension
+ if (end == filename)
+ return NULL;
+
+ const char *ext = end + 1;
+
+ // extension is empty/name ends with dot
+ if (*ext == '\0')
+ return NULL;
+
+ return ext;
+}