diff --git a/picoblogger/main.py b/picoblogger/main.py index 94847b9..f50c1fe 100755 --- a/picoblogger/main.py +++ b/picoblogger/main.py @@ -159,7 +159,7 @@ except: raise locale.setlocale(locale.LC_ALL,os.environ['LANG']) -# print(time.strftime("%x",time.localtime())) +# print(time.strftime(dateFormat,time.localtime())) ######################## # Templating functions # @@ -181,12 +181,16 @@ def parse_article(path): return article_dict def tags_to_html(tags): + tag_list = tags.split(",") html_tags = "" sTAG = '#${TAG} ' if len(tag_list) > 0 and len(tag_list[0]) > 1: for tag in tag_list: + tag = tag.strip() + if len(tag) < 2: + continue html_tags += Template(sTAG).safe_substitute({'TAG':tag}) return html_tags @@ -198,8 +202,8 @@ def article_to_html(article_dict): # Read out article txt file - article_dict['DATE'] = time.strftime(dateFormat, time.strptime(article_dict['DATE'],"%x")) - article_dict['TIME'] = time.strftime(timeFormat, time.strptime(article_dict['TIME'],"%X")) + article_dict['DATE'] = time.strftime(dateFormat, time.strptime(article_dict['DATE'],dateFormat)) + article_dict['TIME'] = time.strftime(timeFormat, time.strptime(article_dict['TIME'],timeFormat)) article_dict['TAGS'] = tags_to_html(article_dict['TAGS']) tplSub = { @@ -217,14 +221,29 @@ def article_to_html(article_dict): sARTICLE = Template(sARTICLE).safe_substitute(tplSub) return sARTICLE -def join_articles(artDir = articleDir): + +def join_articles(list_of_articles): + joined_html = "" + articles_dict = {} + for article in list_of_articles: + # Bring articles into chronological order + t = time.mktime(time.strptime(article['DATE'] + " " + article['TIME'], dateFormat + " " + timeFormat)) + 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 date in dates_of_creation: + joined_html += article_to_html(articles_dict[str(time.strftime(str(date)))]) + return joined_html + + +def join_articles_of_dir(artDir = articleDir): 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'], "%x %H:%M:%S")) + t = time.mktime(time.strptime(article['DATE'] + " " + article['TIME'], dateFormat + " " + timeFormat)) articles_dict[str(t)] = article dates_of_creation = [float(value) for value in articles_dict.keys()] dates_of_creation.sort(key=None,reverse=True) @@ -238,8 +257,8 @@ def recent_to_html(article_dict): # Read out article txt file - article_dict['DATE'] = time.strftime(dateFormat, time.strptime(article_dict['DATE'],"%x")) - article_dict['TIME'] = time.strftime(timeFormat, time.strptime(article_dict['TIME'],"%X")) + article_dict['DATE'] = time.strftime(dateFormat, time.strptime(article_dict['DATE'], dateFormat)) + article_dict['TIME'] = time.strftime(timeFormat, time.strptime(article_dict['TIME'], timeFormat)) article_dict['TAGS'] = tags_to_html(article_dict['TAGS']) tplSub = { @@ -265,7 +284,7 @@ def recent_articles(amount): 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'], "%x %H:%M:%S")) + t = time.mktime(time.strptime(article['DATE'] + " " + article['TIME'], dateFormat + " " + timeFormat)) articles_dict[str(t)] = article dates_of_creation = [float(value) for value in articles_dict.keys()] dates_of_creation.sort(key=None,reverse=True) @@ -283,7 +302,7 @@ def archive_articles(): for article in articles: aName = article["FILENAME"] aPath = os.path.join(article["PATH"], aName) - aDate = time.strptime(article["DATE"],"%x") + aDate = time.strptime(article["DATE"], dateFormat) aMonth = aDate.tm_mon aYear = aDate.tm_year @@ -333,19 +352,24 @@ def list_articles(folder): for name in [os.path.splitext(item)[0] for item in os.listdir(folder) if not "~" in item]: article = (parse_article(os.path.join(folder, name + ".txt"))) # Bring articles into chronological order - t = time.mktime(time.strptime(article['DATE'] + " " + article['TIME'], "%x %H:%M:%S")) + t = time.mktime(time.strptime(article['DATE'] + " " + article['TIME'], dateFormat + " " + timeFormat)) articles_dict[str(t)] = article return articles_dict + def list_articles_by_tag(): - tags_dict = {} + folder = articleDir - for name in [os.path.splitext(item)[0] for item in os.listdir(folder) if not "~" in item]: - article = (parse_article(os.path.join(folder, name + ".txt"))) + tags_dict = {} + articles = [item for item in os.listdir(folder) if os.path.splitext(item)[1] == ".txt"] + for name in articles: + article = (parse_article(os.path.join(folder, name))) for tag in article['TAGS'].split(","): tag = tag.strip() + if len(tag) < 2: + continue if tag not in tags_dict.keys(): tags_dict[tag] = list() tags_dict[tag].append(article) @@ -354,7 +378,11 @@ def list_articles_by_tag(): for year in months: for month in months[year]: monthFolder = os.path.join(archiveDir,str(year),str(month)) - for article in list_articles(monthFolder).values(): + + articles = [item for item in os.listdir(monthFolder) if os.path.splitext(item)[1] == ".txt"] + for name in articles: + article = (parse_article(os.path.join(monthFolder, name))) + for tag in article['TAGS'].split(","): tag = tag.strip() if len(tag) < 2: @@ -362,7 +390,7 @@ def list_articles_by_tag(): if tag not in tags_dict.keys(): tags_dict[tag] = list() tags_dict[tag].append(article) - + return tags_dict @@ -467,7 +495,7 @@ def templating(dic): def build_index(): param = {} param['sRECENT'] = recent_articles(amountRecent) - param['sMAIN'] = join_articles() + param['sMAIN'] = join_articles_of_dir() param['sMAIN_TITLE'] = l10nconf['CURRENT'] htmlString = templating(param) with open(htmlIndex,"w", encoding=l10nconf['BLOG_CHARSET']) as f: @@ -516,18 +544,25 @@ def build_archive_articles(): def build_tag(tag,articles): param={} param['sRECENT'] = recent_articles(amountRecent) - param['sMAIN'] = list_articles_by_tag() + param['sMAIN'] = join_articles(articles) param['sMAIN_TITLE'] = l10nconf['TAG'] + " #" + tag htmlTag = os.path.join(htmlDir,tag + ".html") htmlString = templating(param) -# with open(htmlTag,"w", encoding=l10nconf['BLOG_CHARSET']) as f: -# print(htmlString, file=f) + with open(htmlTag,"w", encoding=l10nconf['BLOG_CHARSET']) as f: + print(htmlString, file=f) -def build_tags(): +def build_tag_cloud(): dic = list_articles_by_tag() for tag in dic.keys(): print(tag + " " + str(len(dic[tag]))) + +def build_tags(): + dic = list_articles_by_tag() + for tag in dic.keys(): + print(dic[tag]) + build_tag(tag,dic[tag]) + def build_feed(): pass @@ -599,7 +634,7 @@ if vars(args)['target'] == 'draft': draft = os.path.join(blog_dir,"drafts",vars(args)['name'] + ".txt") tpl = os.path.join(tplDir,"article.tpl") sTpl = open(tpl).read() - subs = {"ID":int(time.mktime(time.localtime())), "DATE" : time.strftime("%x",time.localtime()), "TIME" : time.strftime("%X",time.localtime()), + subs = {"ID":int(time.mktime(time.localtime())), "DATE" : time.strftime(dateFormat, time.localtime()), "TIME" : time.strftime(timeFormat,time.localtime()), "AUTHOR" : user } @@ -649,7 +684,7 @@ if vars(args)['target'] == 'article': article = os.path.join(blog_dir,"articles",vars(args)['name'] + ".txt") tpl = os.path.join(tplDir,"article.tpl") sTpl = open(tpl).read() - subs = {"ID":int(time.mktime(time.localtime())), "DATE" : time.strftime("%x",time.localtime()), "TIME" : time.strftime("%X",time.localtime()), + subs = {"ID":int(time.mktime(time.localtime())), "DATE" : time.strftime(dateFormat,time.localtime()), "TIME" : time.strftime(timeFormat,time.localtime()), "AUTHOR" : user }