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.

This commit is contained in:
maste9 2015-03-17 22:34:14 +01:00 committed by Homer S
parent f3f343a2a8
commit 1ce84e67d0
1 changed files with 48 additions and 10 deletions

View File

@ -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 = "<ul>"
for year in archived_months:
html += "<li>"+str(year)+"</li>"
html += "<ul>"
for month in archived_months[year]:
html += "<li>"+str(month)+"</li>"
html += "</ul>"
html += "</ul>"
return html
def list_articles():
pass
def list_archive(archived_months):
html = ""
for year in archived_months:
html += "<h2>"+str(year)+"</h2>"
for month in archived_months[year]:
html += "<h3>"+str(month)+"</h3>"
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 = {