"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
templateify gotweb_render_navs
To:
gameoftrees@openbsd.org
Date:
Thu, 15 Dec 2022 12:29:20 +0100

Download raw body.

Thread
This turns gotweb_render_navs into gotweb_get_navs that returns only
the prev/next url.  In doing so I've also merged the two switches.

diff ed619ca07e51b9c984c8404ca2b1153efdb14d1e cd42567e3c1e8890bce34df42b603f1a6675a110
commit - ed619ca07e51b9c984c8404ca2b1153efdb14d1e
commit + cd42567e3c1e8890bce34df42b603f1a6675a110
blob - 0ce90275bab1028c73efcf66d2d59a727f7ca7c7
blob + 85a4ee7974b69efd0c56c02dc288ecf6e719fc21
--- gotwebd/gotweb.c
+++ gotwebd/gotweb.c
@@ -99,8 +99,6 @@ const struct got_error *gotweb_render_navs(struct requ
 static const struct got_error *gotweb_render_tree(struct request *);
 static const struct got_error *gotweb_render_branches(struct request *);
 
-const struct got_error *gotweb_render_navs(struct request *);
-
 static void gotweb_free_querystring(struct querystring *);
 static void gotweb_free_repo_dir(struct repo_dir *);
 
@@ -679,105 +677,53 @@ const struct got_error *
 	return NULL;
 }
 
-const struct got_error *
-gotweb_render_navs(struct request *c)
+void
+gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
+    struct gotweb_url *next, int *have_next)
 {
-	const struct got_error *error = NULL;
 	struct transport *t = c->t;
 	struct querystring *qs = t->qs;
 	struct server *srv = c->srv;
-	int r;
 
-	r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
-	if (r == -1)
-		goto done;
+	*have_prev = *have_next = 0;
 
 	switch(qs->action) {
 	case INDEX:
 		if (qs->index_page > 0) {
-			struct gotweb_url url = {
+			*have_prev = 1;
+			*prev = (struct gotweb_url){
 				.action = -1,
 				.index_page = qs->index_page - 1,
 				.page = -1,
 			};
-
-			r = gotweb_link(c, &url, "Previous");
 		}
-		break;
-	case BRIEFS:
-		if (t->prev_id && qs->commit != NULL &&
-		    strcmp(qs->commit, t->prev_id) != 0) {
-			struct gotweb_url url = {
-				.action = BRIEFS,
-				.index_page = -1,
-				.page = qs->page - 1,
-				.path = qs->path,
-				.commit = t->prev_id,
-				.headref = qs->headref,
-			};
-
-			r = gotweb_link(c, &url, "Previous");
-		}
-		break;
-	case COMMITS:
-		if (t->prev_id && qs->commit != NULL &&
-		    strcmp(qs->commit, t->prev_id) != 0) {
-			struct gotweb_url url = {
-				.action = COMMIT,
-				.index_page = -1,
-				.page = qs->page - 1,
-				.path = qs->path,
-				.commit = t->prev_id,
-				.headref = qs->headref,
-				.folder = qs->folder,
-				.file = qs->file,
-			};
-
-			r = gotweb_link(c, &url, "Previous");
-		}
-		break;
-	case TAGS:
-		if (t->prev_id && qs->commit != NULL &&
-		    strcmp(qs->commit, t->prev_id) != 0) {
-			struct gotweb_url url = {
-				.action = TAGS,
-				.index_page = -1,
-				.page = qs->page - 1,
-				.path = qs->path,
-				.commit = t->prev_id,
-				.headref = qs->headref,
-			};
-
-			r = gotweb_link(c, &url, "Previous");
-		}
-		break;
-	}
-
-	if (r == -1)
-		goto done;
-
-	r = fcgi_printf(c, "</div>\n"	/* #nav_prev */
-	    "<div id='nav_next'>");
-	if (r == -1)
-		goto done;
-
-	switch(qs->action) {
-	case INDEX:
 		if (t->next_disp == srv->max_repos_display &&
 		    t->repos_total != (qs->index_page + 1) *
 		    srv->max_repos_display) {
-			struct gotweb_url url = {
+			*have_next = 1;
+			*next = (struct gotweb_url){
 				.action = -1,
 				.index_page = qs->index_page + 1,
 				.page = -1,
 			};
-
-			r = gotweb_link(c, &url, "Next");
 		}
 		break;
 	case BRIEFS:
+		if (t->prev_id && qs->commit != NULL &&
+		    strcmp(qs->commit, t->prev_id) != 0) {
+			*have_prev = 1;
+			*prev = (struct gotweb_url){
+				.action = BRIEFS,
+				.index_page = -1,
+				.page = qs->page - 1,
+				.path = qs->path,
+				.commit = t->prev_id,
+				.headref = qs->headref,
+			};
+		}
 		if (t->next_id) {
-			struct gotweb_url url = {
+			*have_next = 1;
+			*next = (struct gotweb_url){
 				.action = BRIEFS,
 				.index_page = -1,
 				.page = qs->page + 1,
@@ -785,13 +731,26 @@ gotweb_render_navs(struct request *c)
 				.commit = t->next_id,
 				.headref = qs->headref,
 			};
-
-			r = gotweb_link(c, &url, "Next");
 		}
 		break;
 	case COMMITS:
+		if (t->prev_id && qs->commit != NULL &&
+		    strcmp(qs->commit, t->prev_id) != 0) {
+			*have_prev = 1;
+			*prev = (struct gotweb_url){
+				.action = COMMIT,
+				.index_page = -1,
+				.page = qs->page - 1,
+				.path = qs->path,
+				.commit = t->prev_id,
+				.headref = qs->headref,
+				.folder = qs->folder,
+				.file = qs->file,
+			};
+		}
 		if (t->next_id) {
-			struct gotweb_url url = {
+			*have_next = 1;
+			*next = (struct gotweb_url){
 				.action = COMMIT,
 				.index_page = -1,
 				.page = qs->page + 1,
@@ -801,13 +760,24 @@ gotweb_render_navs(struct request *c)
 				.folder = qs->folder,
 				.file = qs->file,
 			};
-
-			r = gotweb_link(c, &url, "Next");
 		}
 		break;
 	case TAGS:
+		if (t->prev_id && qs->commit != NULL &&
+		    strcmp(qs->commit, t->prev_id) != 0) {
+			*have_prev = 1;
+			*prev = (struct gotweb_url){
+				.action = TAGS,
+				.index_page = -1,
+				.page = qs->page - 1,
+				.path = qs->path,
+				.commit = t->prev_id,
+				.headref = qs->headref,
+			};
+		}
 		if (t->next_id) {
-			struct gotweb_url url = {
+			*have_next = 1;
+			*next = (struct gotweb_url){
 				.action = TAGS,
 				.index_page = -1,
 				.page = qs->page + 1,
@@ -815,22 +785,9 @@ gotweb_render_navs(struct request *c)
 				.commit = t->next_id,
 				.headref = qs->headref,
 			};
-
-			r = gotweb_link(c, &url, "Next");
 		}
 		break;
 	}
-	if (r == -1)
-		goto done;
-
-	fcgi_printf(c, "</div>\n"); /* #nav_next */
-	fcgi_printf(c, "</div>\n"); /* #np_wrapper */
-done:
-	free(t->next_id);
-	t->next_id = NULL;
-	free(t->prev_id);
-	t->prev_id = NULL;
-	return error;
 }
 
 static const struct got_error *
@@ -925,8 +882,7 @@ gotweb_render_index(struct request *c)
 	    t->repos_total <= srv->max_repos_display)
 		goto done;
 
-	error = gotweb_render_navs(c);
-	if (error)
+	if (gotweb_render_navs(c->tp) == -1)
 		goto done;
 done:
 	if (sd_dent) {
@@ -1085,8 +1041,7 @@ gotweb_render_commits(struct request *c)
 	}
 
 	if (t->next_id || t->prev_id) {
-		error = gotweb_render_navs(c);
-		if (error)
+		if (gotweb_render_navs(c->tp) == -1)
 			goto done;
 	}
 	fcgi_printf(c, "</div>\n"); /* .commits_content */
@@ -1622,8 +1577,7 @@ gotweb_render_tags(struct request *c)
 		msg = NULL;
 	}
 	if (t->next_id || t->prev_id) {
-		error = gotweb_render_navs(c);
-		if (error)
+		if (gotweb_render_navs(c->tp) == -1)
 			goto done;
 	}
 	fcgi_printf(c, "</div>\n"); /* #tags_content */
blob - 64fce7c6d8ee3f57305034f0c0434cff991a82bd
blob + 700307a1481fa9b5b991ba6ed6090d29566a7cd0
--- gotwebd/gotwebd.h
+++ gotwebd/gotwebd.h
@@ -436,6 +436,8 @@ const struct got_error *gotweb_get_time_str(char **, t
     const uint8_t *);
 const struct got_error
     *gotweb_render_content_type_file(struct request *, const uint8_t *, char *);
+void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
+    struct gotweb_url *, int *);
 const struct got_error *gotweb_get_time_str(char **, time_t, int);
 const struct got_error *gotweb_init_transport(struct transport **);
 const struct got_error *gotweb_escape_html(char **, const char *);
@@ -455,6 +457,7 @@ int	gotweb_render_briefs(struct template *);
 int	gotweb_render_repo_table_hdr(struct template *);
 int	gotweb_render_repo_fragment(struct template *, struct repo_dir *);
 int	gotweb_render_briefs(struct template *);
+int	gotweb_render_navs(struct template *);
 
 /* parse.y */
 int parse_config(const char *, struct gotwebd *);
blob - 0d49898e174e9bfad77b158729eb04ab949bd736
blob + 88158c157c6d585b1e9692319e29e9bbe08f9390
--- gotwebd/pages.tmpl
+++ gotwebd/pages.tmpl
@@ -30,8 +30,6 @@ const struct got_error	*gotweb_render_navs(struct requ
 #include "gotwebd.h"
 #include "tmpl.h"
 
-const struct got_error	*gotweb_render_navs(struct request *);
-
 static int
 gotweb_render_age(struct template *tp, time_t time, int ref_tm)
 {
@@ -296,7 +294,41 @@ gotweb_render_age(struct template *tp, time_t time, in
     <div class="dotted_line"></div>
   {{ end }}
   {{ if t->next_id || t->prev_id }}
-    {! gotweb_render_navs(c); !}
+    {{ render gotweb_render_navs(tp) }}
   {{ end }}
 </div>
 {{ end }}
+
+{{ define gotweb_render_navs(struct template *tp) }}
+{!
+	struct request		*c = tp->tp_arg;
+	struct transport	*t = c->t;
+	struct gotweb_url	 prev, next;
+	int			 have_prev, have_next;
+
+	gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
+!}
+<div id="np_wrapper">
+  <div id="nav_prev">
+    {{ if have_prev }}
+      <a href="{{ render gotweb_render_url(c, &prev) }}">
+        Previous
+      </a>
+    {{ end }}
+  </div>
+  <div id="nav_next">
+    {{ if have_next }}
+      <a href="{{ render gotweb_render_url(c, &next) }}">
+        Next
+      </a>
+    {{ end }}
+  </div>
+</div>
+{{ finally }}
+{!
+	free(t->next_id);
+	t->next_id = NULL;
+	free(t->prev_id);
+	t->prev_id = NULL;
+!}
+{{ end }}