summary refs log tree commit diff
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
commit284c1b390e03c108e1de90101ec0884203b2227f (patch)
tree86b3649e64710522dc5a0ac0316c8262b74a2562
parentPrevent matching the same tag twice (diff)
downloadsrc-284c1b390e03c108e1de90101ec0884203b2227f.tar.gz
src-284c1b390e03c108e1de90101ec0884203b2227f.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 b72b5e39..09807cba 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