diff --git a/microblog.py b/microblog.py
index 7844e69..11528af 100644
--- a/microblog.py
+++ b/microblog.py
@@ -1,7 +1,7 @@
import sys
-
import subprocess
+import os
def make_post(num, timestamp, email, msg):
# used for name and email but it's not actually needed.
@@ -127,6 +127,48 @@ def make_tagcloud(d):
output.append(fmt % (key, d[key]))
return output
+class Paginator:
+ def __init__(self, x, y, loc="./pages"):
+ self.TOTAL_POSTS = x
+ self.PPP = y # posts per page
+ self.TOTAL_PAGES = int(x/y)
+ if self.TOTAL_PAGES > 0:
+ self.SUBDIR = loc
+ if not os.path.exists(loc):
+ os.makedirs(loc)
+ pass
+
+ def toc(self, n=None, page=None): #style 1
+ if self.TOTAL_PAGES < 1 or (n == None and page == None):
+ return "[no pages]"
+ # For page 'n' do not create an anchor tag
+ fmt = "[%i]" #(self.subdir, page, page number)
+ anchors = []
+ for i in range(self.TOTAL_PAGES):
+ if i != n:
+ anchors.append(fmt % (self.SUBDIR, page, 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 paginate(self, template, tagcloud, timeline):
+ outfile = self.SUBDIR + "/%i.html"
+ timeline.reverse() # reorder from oldest to newest
+ for i in range(self.TOTAL_PAGES):
+ with open(outfile % i, 'w') as fout:
+ prev = self.PPP * i
+ curr = self.PPP * (i+1)
+ sliced = timeline[prev:curr]
+ sliced.reverse()
+ fout.write(
+ self.singlepage(template, tagcloud, sliced))
+ pass
+
if __name__ == "__main__":
def get_args():
argc = len(sys.argv)
@@ -147,7 +189,18 @@ if __name__ == "__main__":
tl, tc = make_timeline(content, email)
tcl = make_tagcloud(tc)
count = len(tl)
+ html = ""
with open(template,'r') as f:
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)
+ posts_per_page = 5
+ pagectrl = Paginator(count, posts_per_page)
+ if count <= posts_per_page:
+ print(pagectrl.paginate(html, tcl, timeline))
+ else:
+ latest = tl[:posts_per_page]
+ print(pagectrl.singlepage(html, tcl, latest))
+ pagectrl.paginate(html, tcl, tl)
+
main()