aboutsummaryrefslogtreecommitdiffstats
path: root/cgit-about-filter.c
blob: 02b6043cb339ebe67bd119046b98eb3dc77270a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#define _POSIX_C_SOURCE 200809L

#include <ctype.h>
#include <string.h>
#include <unistd.h>

#define EXEC1(args) execvp(args[0], args)
#define EXEC(...) EXEC1(((char *const []){ __VA_ARGS__, NULL }))

static const char *extension(const char *filename);

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", "-Thtml", "-Ofragment");
	} else if (strcmp(ext, "md") == 0) {
		EXEC("lowdown", "-Thtml");
	}

	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;
}