Commit Diff


commit - a4ec04c5ab4f4ffaffd39d3e2e770c145402c304
commit + 48e3f26d25252f33d0e18c5d1990f25f2b2f5ca8
blob - 8aa25d05c20943b9104193262145453470f005b4
blob + d4aec734906480b830fa6a47629c715a4f34f6fd
--- ChangeLog
+++ ChangeLog
@@ -1,3 +1,7 @@
+2021-05-13  Omar Polo  <op@omarpolo.com>
+
+	* gemtext.c (gemtext_free): heuristic to obtain the page title: if no h1 found, try with h2s and h3s
+
 2021-05-12  Omar Polo  <op@omarpolo.com>
 
 	* ui.c (handle_resize): debounce resize event
blob - ee8108e204c88885edebdd96654e85b59dab0ee3
blob + 15d892dd929bf4d51291f953413596d105fbf31f
--- gemtext.c
+++ gemtext.c
@@ -40,6 +40,7 @@ static int	parse_quote(struct parser*, enum line_type,
 static int	parse_pre_start(struct parser*, enum line_type, const char*, size_t);
 static int	parse_pre_cnt(struct parser*, enum line_type, const char*, size_t);
 static int	parse_pre_end(struct parser*, enum line_type, const char*, size_t);
+static void	search_title(struct parser*, enum line_type);
 
 typedef int (parselinefn)(struct parser*, enum line_type, const char*, size_t);
 
@@ -369,5 +370,29 @@ gemtext_free(struct parser *p)
 	}
 
 	free(p->buf);
+
+	/*
+	 * use the first level 2 or 3 header as page title if none
+	 * found yet.
+	 */
+	if (*p->title == '\0')
+		search_title(p, LINE_TITLE_2);
+	if (*p->title == '\0')
+		search_title(p, LINE_TITLE_3);
+
 	return 1;
 }
+
+static void
+search_title(struct parser *p, enum line_type level)
+{
+	struct line *l;
+
+	TAILQ_FOREACH(l, &p->head, lines) {
+		if (l->type == level) {
+			if (l->line == NULL)
+				continue;
+			strlcpy(p->title, l->line, sizeof(p->title));
+		}
+	}
+}