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>
<h3>Tags</h3>
<p>%s</p>
<h3>Pages</h3>
<p>%s</p>
</div>
</div>
</div>

View File

@ -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 = "<a href=\"%s/%s\">[%i]</a>" #(self.subdir, page, page number)
fmt = "<a href=\"%s\">[%i]</a>" #(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("<b>[%i]</b>" % 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))