commit 4f69a8f35d71d480295fcf6c3cd0c977b3e2544d
parent 209ccd1d9db5b343544a994feab31c0957a920a9
Author: David Gričar <suckless@coppie.xyz>
Date:   Thu, 19 Mar 2020 03:07:37 +0100
Fix: Don't print unnecessary ul tags
This will make website code even cleaner as build-page should no
longer print empty ul tags if selected menu item has no subdirectories.
Diffstat:
1 file changed, 27 insertions(+), 2 deletions(-)
diff --git a/build-page.c b/build-page.c
@@ -206,6 +206,32 @@ qsort_strcmp(const void *a, const void *b)
 	return strcmp(*(const char **)a, *(const char **)b);
 }
 
+int
+last_dir(char *this)
+{
+	DIR *dp;
+	struct dirent *de;
+	char newdir[PATH_MAX];
+	int dir;
+
+	if ((dp = opendir(this ? this : ".")) == NULL)
+		die_perror("opendir: %s", this ? this : ".");
+
+	dir = 0;
+	while (dir == 0 && (de = readdir(dp))) {
+		if (*de->d_name == '.')
+			continue;
+		snprintf(newdir, sizeof(newdir), this ? "%2$s/%1$s" : "%s", de->d_name, this);
+		if (!stat_isdir(newdir))
+			continue;
+
+		dir = 1;
+	}
+	closedir(dp);
+
+	return !dir;
+}
+
 void
 menu_panel(char *domain, char *page, char *this, int depth)
 {
@@ -251,11 +277,10 @@ menu_panel(char *domain, char *page, char *this, int depth)
 			fputs("/</a>", stdout);
 		}
 
-		if (highlight) {
+		if (highlight && !last_dir(newdir)) {
 			putchar('\n');
 			for (i = 0; i < depth + 2; ++i)
 				putchar('\t');
-			/* TODO: empty <ul></ul> is printed for subitems */
 			puts("<ul>");
 			menu_panel(domain, page, newdir, depth + 1);
 			for (i = 0; i < depth + 2; ++i)