From 1ce84e67d0da2f0cf29a08e084a4c5884e22915a Mon Sep 17 00:00:00 2001 From: maste9 Date: Tue, 17 Mar 2015 22:34:14 +0100 Subject: [PATCH] Working on archive with per month list of articles and month shortcuts in sidebar. Trying to clean the code a bit up and to reuse more code. --- picoblogger/main.py | 58 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/picoblogger/main.py b/picoblogger/main.py index 4cef481..c8ba0cd 100755 --- a/picoblogger/main.py +++ b/picoblogger/main.py @@ -267,7 +267,7 @@ def recent_articles(amount): def archive_articles(): articles = [parse_article(os.path.join(articleDir,item)) for item in os.listdir(articleDir) if not "~" in item] -# print(articles) + cMonth = timestamp.tm_mon cYear = timestamp.tm_year # sort articles by date of creation into archive which is structured by subfolders of year and month @@ -314,22 +314,60 @@ def list_of_months(): monthDir = os.path.join(yearDir,month) tMonth = time.strptime(month+"/"+year,"%m/%Y") sMonth = time.strftime("%B",tMonth) - dArchives[year].append(sMonth) - print(dArchives) + dArchives[year].append(month)#sMonth) return dArchives + +def list_articles(folder): + artDir = folder + joined_html = "" + articles_dict = {} + for name in [os.path.splitext(item)[0] for item in os.listdir(artDir) if not "~" in item]: + article = (parse_article(os.path.join(artDir, name + ".txt"))) + # Bring articles into chronological order + t = time.mktime(time.strptime(article['DATE'] + " " + article['TIME'], "%m/%d/%y %H:%M:%S")) + articles_dict[str(t)] = article + dates_of_creation = [float(value) for value in articles_dict.keys()] + dates_of_creation.sort(key=None,reverse=True) + for i in range(0, len(dates_of_creation)): + + joined_html += recent_to_html(articles_dict[str(time.strftime(str(dates_of_creation[i])))]) + + return joined_html + + + +def months_to_html(archived_months): + html = "" + return html + -def list_articles(): - pass +def list_archive(archived_months): + html = "" + for year in archived_months: + html += "

"+str(year)+"

" + for month in archived_months[year]: + html += "

"+str(month)+"

" + html += list_articles(os.path.join(archiveDir,str(year),str(month))) + html += "" + return html + def update_blog(subsection): """ Joins config defined variables into templates""" - if subsection == "recent": + if subsection in ("recent","all"): tplIndex = os.path.join(tplDir,"index.htm") blogIndex = os.path.join(blog_dir,"index.html") - elif subsection == "archive": + elif subsection in ("archive"): tplIndex = os.path.join(tplDir,"index.htm") blogIndex = os.path.join(blog_dir,"archive.html") @@ -340,7 +378,7 @@ def update_blog(subsection): if subsection == "recent": sMAIN = join_articles() elif subsection == "archive": - sMAIN = list_articles() + sMAIN = list_archive(list_of_months()) sASIDE = open(os.path.join(tplDir,"aside.htm")).read() sRECENT = open(os.path.join(tplDir,"recent.htm")).read() @@ -366,10 +404,10 @@ def update_blog(subsection): sRECENT = Template(sRECENT).safe_substitute(pbconf) sFOOTER = Template(sFOOTER).safe_substitute(pbconf) - if subsection == "recent": + if subsection in ("recent","all"): sRECENT = Template(sRECENT).safe_substitute({"RECENT_ARTICLES":recent_articles(amountRecent)}) elif subsection == "archive": - sRECENT = Template(sRECENT).safe_substitute({"RECENT_ARTICLES":list_of_months()}) + sRECENT = Template(sRECENT).safe_substitute({"RECENT_ARTICLES":months_to_html(list_of_months())}) sASIDE = Template(sASIDE).safe_substitute({"ASIDE_RECENT":sRECENT, "ASIDE_CONTACT":sCONTACT}) tplSub = {