From fc0b9c47cb4be2b13f534042e3fb479ae0bbdf74 Mon Sep 17 00:00:00 2001 From: likho Date: Sun, 20 Mar 2022 06:47:58 -0700 Subject: [PATCH] Working navigable page index --- example/template-generic.html | 2 ++ microblog.py | 33 +++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/example/template-generic.html b/example/template-generic.html index 914bde5..5fad48c 100644 --- a/example/template-generic.html +++ b/example/template-generic.html @@ -22,6 +22,8 @@

%s total posts

Tags

%s

+

Pages

+

%s

diff --git a/microblog.py b/microblog.py index f89a0b9..cf4ed88 100644 --- a/microblog.py +++ b/microblog.py @@ -136,28 +136,33 @@ class Paginator: self.SUBDIR = loc if not os.path.exists(loc): os.makedirs(loc) + self.FILENAME = "%i.html" pass - def toc(self, n=None, page=None): #style 1 - if self.TOTAL_PAGES < 1 or (n == None or page == None): + def toc(self, current_page=None, path=None): #style 1 + if self.TOTAL_PAGES < 1: return "[no pages]" + if path == None: + path = self.SUBDIR # For page 'n' do not create an anchor tag - fmt = "[%i]" #(self.subdir, page, page number) + fmt = "[%i]" #(filename, page number) anchors = [] - for i in range(self.TOTAL_PAGES): - if i != n: - anchors.append(fmt % (self.SUBDIR, page, i)) + for i in reversed(range(self.TOTAL_PAGES)): + if i != current_page: + x = path + "/" + (self.FILENAME % i) + anchors.append(fmt % (x, i)) else: anchors.append("[%i]" % i) return "\n".join(anchors) - def singlepage(self, template, tagcloud, timeline): - tc = "\n".join(tagcloud) - tl = "\n\n".join(timeline) - return (template % (self.TOTAL_POSTS, tc, tl)) + def singlepage(self, template, tagcloud, timeline, i=None, p=None): + tc = "\n".join(tagcloud) + tl = "\n\n".join(timeline) + tbl = self.toc(i, p) + return (template % (self.TOTAL_POSTS, tc, tbl, tl)) def paginate(self, template, tagcloud, timeline): - outfile = self.SUBDIR + "/%i.html" + outfile = "%s/%s" % (self.SUBDIR, self.FILENAME) timeline.reverse() # reorder from oldest to newest for i in range(self.TOTAL_PAGES): with open(outfile % i, 'w') as fout: @@ -166,7 +171,7 @@ class Paginator: sliced = timeline[prev:curr] sliced.reverse() fout.write( - self.singlepage(template, tagcloud, sliced)) + self.singlepage(template, tagcloud, sliced, i, ".")) pass if __name__ == "__main__": @@ -205,10 +210,10 @@ if __name__ == "__main__": html = f.read() #print(html % (count, "\n".join(tcl), "\n\n".join(tl))) count = len(tl) - posts_per_page = 20 + posts_per_page = 5 pagectrl = Paginator(count, posts_per_page) if count <= posts_per_page: - print(pagectrl.paginate(html, tcl, timeline)) + print(pagectrl.singlepage(html, tcl, tl)) else: latest = tl[:posts_per_page] print(pagectrl.singlepage(html, tcl, latest))