summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2020-01-02 16:13:48 +1030
committerDaniel Jones <admin@danieljon.es>2020-01-02 16:13:48 +1030
commitbf2ffd03740894bd7592b7a1bbd2c9101e47e888 (patch)
tree6557351c7dd9cc5d2c1cd204c6f38492db96faf8
parentcceda0abba23c83811be34362c38c15427b1356a (diff)
downloadwebsitegenerator-bf2ffd03740894bd7592b7a1bbd2c9101e47e888.tar.gz
websitegenerator-bf2ffd03740894bd7592b7a1bbd2c9101e47e888.zip
start generating page (blog navigation) bar
really needs to be cleaned up though
-rw-r--r--pages.c38
-rw-r--r--pages.h1
2 files changed, 38 insertions, 1 deletions
diff --git a/pages.c b/pages.c
index ae845e3..da437a2 100644
--- a/pages.c
+++ b/pages.c
@@ -463,7 +463,6 @@ createdirectpages(int *posts, size_t totalposts)
char file[512];
char source[512];
- char buff[128];
for (int x = 1; x < totalposts; x++)
{
memset(source, 0, 1);
@@ -489,6 +488,40 @@ createdirectpages(int *posts, size_t totalposts)
}
return 1;
}
+#include <math.h>
+char
+*generatepagebar(char *bar, size_t size, int *posts, size_t totalposts, int currentpage)
+{
+ /*
+ * create a navigation bar for the posts pages highlighting the 'currentpage' link
+ * store it in bar and return it, NULL on error
+ */
+ char buff[size];
+ int i;
+ size_t freespace = size;
+ /* determine how many pages we need */
+ int pagecount = (totalposts%posts_per_page == 0) ? totalposts/posts_per_page : totalposts/posts_per_page+1;
+ /* gross hack, sxprintf returns the number of chars written, keep track of our freespace */
+ freespace -= snprintf(buff, freespace, "<div class='middle'> <a href='%d.html'>prev</a>", (currentpage == 1) ? pagecount : currentpage-1);
+ strncat(bar, buff, freespace);
+ for (int i = 1; i < pagecount; i++)
+ {
+ // FIXME: this doesn't need to be an if/else..
+ if (currentpage == i)
+ {
+ freespace -= snprintf(buff, freespace, "<strong><i><a href='%d.html'>%d</a></i></strong> ", i, i);
+ }
+ else
+ {
+ freespace -= snprintf(buff, freespace, "<a href='%d.html'>%d</a> ", i, i);
+ }
+ strncat(bar, buff, freespace);
+ }
+ freespace -= snprintf(buff, freespace, "<div class='middle'> <a href='%d.html'>next</a>", (currentpage == pagecount) ? 1 : currentpage+1);
+ strncat(bar, buff, freespace);
+ printf("%s: %ld pages = %d\n", bar, freespace, pagecount);
+ return bar;
+}
int
postspage(int flags)
@@ -537,11 +570,14 @@ postspage(int flags)
/* sort posts */
qsort(posts, totalposts, sizeof(int), postscompare);
+ /* create direct link pages */
if (!createdirectpages(posts, totalposts))
{
fprintf(stderr, "unable to create direct post pages, unrecoverable failure\n");
return 0;
}
+ char pagebar[1024] = {0};
+ generatepagebar(pagebar, 1024, posts, totalposts, 1);
return 1;
}
diff --git a/pages.h b/pages.h
index e61eeb4..106938d 100644
--- a/pages.h
+++ b/pages.h
@@ -43,6 +43,7 @@ int makedirectories(const char *basedir, const char *file);
int postscompare(const void *a, const void *b);
int genericpage(int flags, const char *ind, const char *out, const char *tit, const char *inf);
int createdirectpages(int *posts, size_t totalposts);
+char *generatepagebar(char *bar, size_t size, int *posts, size_t totalposts, int currentpage);
/* generators (to be put into the pages array) */
int frontpage(int flags);