Working navigable page index

This commit is contained in:
likho 2022-03-20 06:47:58 -07:00
parent 55bdc78582
commit fc0b9c47cb
2 changed files with 21 additions and 14 deletions

View File

@ -22,6 +22,8 @@
<h4>%s total posts</h4> <h4>%s total posts</h4>
<h3>Tags</h3> <h3>Tags</h3>
<p>%s</p> <p>%s</p>
<h3>Pages</h3>
<p>%s</p>
</div> </div>
</div> </div>
</div> </div>

View File

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