summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-01-12 20:53:29 -0500
committerJune McEnroe <june@causal.agency>2021-01-12 20:57:39 -0500
commitd6b449f4ac88ec30a3882272e6261651d3d1348b (patch)
tree721e73c06fc7af67885d1b0c27e3c352400d5ff0 /bin
parentPrevent matching the same tag twice (diff)
downloadsrc-d6b449f4ac88ec30a3882272e6261651d3d1348b.tar.gz
src-d6b449f4ac88ec30a3882272e6261651d3d1348b.zip
Add htagml -x
Diffstat (limited to '')
-rw-r--r--bin/htagml.c35
-rw-r--r--bin/man1/htagml.118
2 files changed, 41 insertions, 12 deletions
diff --git a/bin/htagml.c b/bin/htagml.c
index 1c8c683f..9c48ed48 100644
--- a/bin/htagml.c
+++ b/bin/htagml.c
@@ -20,10 +20,19 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/stat.h>
 #include <sysexits.h>
 #include <unistd.h>
 
+struct Tag {
+	char *tag;
+	int num;
+	regex_t regex;
+};
+
+static int compar(const void *a, const void *b) {
+	return ((const struct Tag *)a)->num - ((const struct Tag *)b)->num;
+}
+
 static char *nomagic(const char *pattern) {
 	char *buf = malloc(2 * strlen(pattern) + 1);
 	if (!buf) err(EX_OSERR, "malloc");
@@ -50,11 +59,13 @@ static size_t escape(const char *ptr, size_t len) {
 
 int main(int argc, char *argv[]) {
 	bool pre = false;
+	bool index = false;
 	const char *tagsFile = "tags";
-	for (int opt; 0 < (opt = getopt(argc, argv, "f:p"));) {
+	for (int opt; 0 < (opt = getopt(argc, argv, "f:px"));) {
 		switch (opt) {
 			break; case 'f': tagsFile = optarg;
 			break; case 'p': pre = true;
+			break; case 'x': index = true;
 			break; default:  return EX_USAGE;
 		}
 	}
@@ -66,11 +77,7 @@ int main(int argc, char *argv[]) {
 
 	size_t len = 0;
 	size_t cap = 256;
-	struct Tag {
-		char *tag;
-		int num;
-		regex_t regex;
-	} *tags = malloc(cap * sizeof(*tags));
+	struct Tag *tags = malloc(cap * sizeof(*tags));
 	if (!tags) err(EX_OSERR, "malloc");
 
 	char *buf = NULL;
@@ -131,6 +138,7 @@ int main(int argc, char *argv[]) {
 			tag->num = num;
 			break;
 		}
+		if (index) continue;
 		if (!tag) {
 			escape(buf, strlen(buf));
 			continue;
@@ -157,4 +165,17 @@ int main(int argc, char *argv[]) {
 		if (match) escape(match, strlen(match));
 	}
 	if (pre) printf("</pre>");
+
+	if (!index) return EX_OK;
+	qsort(tags, len, sizeof(*tags), compar);
+	printf("<ul class=\"index\">\n");
+	for (size_t i = 0; i < len; ++i) {
+		if (!tags[i].num) continue;
+		printf("<li><a class=\"tag\" href=\"#");
+		escape(tags[i].tag, strlen(tags[i].tag));
+		printf("\">");
+		escape(tags[i].tag, strlen(tags[i].tag));
+		printf("</a></li>\n");
+	}
+	printf("</ul>\n");
 }
diff --git a/bin/man1/htagml.1 b/bin/man1/htagml.1
index 70619647..f313c09d 100644
--- a/bin/man1/htagml.1
+++ b/bin/man1/htagml.1
@@ -8,9 +8,9 @@
 .
 .Sh SYNOPSIS
 .Nm
-.Op Fl p
-.Op Fl f Ar tags_file
-.Ar name
+.Op Fl px
+.Op Fl f Ar tagsfile
+.Ar file
 .
 .Sh DESCRIPTION
 The
@@ -25,9 +25,9 @@ with the class
 .Pp
 The arguments are as follows:
 .Bl -tag -width Ds
-.It Fl f Ar tags_file
+.It Fl f Ar tagsfile
 Read the tag descriptions from a file called
-.Ar tags_file .
+.Ar tagsfile .
 The default behavior is
 to read them from a file called
 .Pa tags .
@@ -35,6 +35,14 @@ to read them from a file called
 Wrap the output in a
 .Sy pre
 element.
+.It Fl x
+Instead produce an index of tags
+ordered by their occurrence in
+.Ar file .
+The index is formatted as a
+.Sy ul
+element with the class
+.Qq index .
 .El
 .
 .Sh FILES