Bug correction + RSS
Corrected some bugs with the index for years / months. Added a RSS syndication
This commit is contained in:
parent
2512273590
commit
079d3a8cdf
2
TODO
2
TODO
@ -1,4 +1,3 @@
|
||||
Finish gen.py -> RSS
|
||||
Search ?
|
||||
What happen when a file is moved with git ?
|
||||
Flake8 the whole thing ! :)
|
||||
@ -6,3 +5,4 @@ Flake8 the whole thing ! :)
|
||||
Known bugs:
|
||||
==========
|
||||
* Articles in page by month and page by year are not sorted by date
|
||||
* Test RSS in Firefox (different view than traditionnal RSS view ?)
|
||||
|
141
pre-commit.py
141
pre-commit.py
@ -8,16 +8,20 @@ import os
|
||||
import datetime
|
||||
import subprocess
|
||||
|
||||
from time import gmtime, strftime, mktime
|
||||
|
||||
def isset(variable):
|
||||
return variable in locals() or variable in globals()
|
||||
|
||||
|
||||
def list_directory(path):
|
||||
fichier=[]
|
||||
fichier = []
|
||||
for root, dirs, files in os.walk(path):
|
||||
for i in files:
|
||||
fichier.append(os.path.join(root, i))
|
||||
return fichier
|
||||
|
||||
|
||||
def get_tags(fh):
|
||||
line = fh.readline()
|
||||
while "@tags=" not in line:
|
||||
@ -25,45 +29,49 @@ def get_tags(fh):
|
||||
if "@tags" not in line:
|
||||
return []
|
||||
|
||||
line = line.strip() #Delete \n at the end of the line
|
||||
line = line.strip() # Delete \n at the end of the line
|
||||
tag_pos = line.find("@tags=")
|
||||
tags = line[tag_pos+6:].split(",")
|
||||
return tags
|
||||
|
||||
|
||||
def latest_articles(directory, number):
|
||||
now = datetime.datetime.now()
|
||||
counter = 0
|
||||
latest_articles = []
|
||||
|
||||
for i in range(int(now.strftime('%Y')), 0, -1):
|
||||
if counter>=number:
|
||||
if counter >= number:
|
||||
break
|
||||
|
||||
if os.path.isdir(directory+"/"+str(i)):
|
||||
for j in range(12, 0, -1):
|
||||
if j<10:
|
||||
if j < 10:
|
||||
j = "0"+str(j)
|
||||
|
||||
if os.path.isdir(directory+"/"+str(i)+"/"+str(j)):
|
||||
articles_list = list_directory(directory+str(i)+"/"+str(j))
|
||||
articles_list.sort(key=lambda x: os.stat(x).st_mtime) #Sort by date the articles
|
||||
# Sort by date the articles
|
||||
articles_list.sort(key=lambda x: os.stat(x).st_mtime)
|
||||
|
||||
latest_articles += articles_list[:number-counter]
|
||||
if len(latest_articles) < number-counter:
|
||||
counter+=len(articles_list)
|
||||
counter += len(articles_list)
|
||||
else:
|
||||
counter=number
|
||||
counter = number
|
||||
|
||||
#Delete directory in file names
|
||||
return latest_articles
|
||||
|
||||
|
||||
def auto_dir(path):
|
||||
directory = os.path.dirname(path)
|
||||
try:
|
||||
if not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
except IOError:
|
||||
sys.exit("[ERROR] An error occured while creating "+path+" file and parent dirs.")
|
||||
sys.exit("[ERROR] An error occured while creating "+path+" file \
|
||||
and parent dirs.")
|
||||
|
||||
#Find the changes to be committed
|
||||
try:
|
||||
@ -76,6 +84,8 @@ except:
|
||||
with open("raw/params", "r") as params_fh:
|
||||
params = {}
|
||||
for line in params_fh.readlines():
|
||||
if line.strip() == "" or line.strip()[0] == "#":
|
||||
continue
|
||||
option, value = line.split("=", 1)
|
||||
params[option.strip()] = value.strip()
|
||||
|
||||
@ -112,6 +122,7 @@ for filename in list(added_files):
|
||||
|
||||
if filename[-4:] != "html" and filename[-6:] != "ignore":
|
||||
print("[INFO] (Not HTML file) Copying directly not html file "+filename[4:]+" to blog dir.")
|
||||
|
||||
auto_dir("blog/"+filename[4:])
|
||||
shutil.copy(filename, "blog/"+filename[4:])
|
||||
added_files.remove(filename)
|
||||
@ -174,6 +185,7 @@ print("[INFO] Modified files : "+", ".join(modified_files))
|
||||
print("[INFO] Deleted filed : "+", ".join(deleted_files))
|
||||
|
||||
print("[INFO] Updating tags for added and modified files")
|
||||
#TODO : refactor since there
|
||||
for filename in added_files:
|
||||
try:
|
||||
with open(filename, 'r') as fh:
|
||||
@ -203,11 +215,11 @@ for filename in modified_files:
|
||||
try:
|
||||
with open(tag, 'r+') as tag_file:
|
||||
if tag[tag.index("tags/") + 5:tag.index(".tmp")] in tags and filename[4:] not in tag_file.read():
|
||||
tag_file.seek(0, 2) #Append to end of file
|
||||
tag_file.seek(0, 2) # Append to end of file
|
||||
tag_file.write(filename[4:]+"\n")
|
||||
print("[INFO] (TAGS) Found new tag "+tag[:tag.index(".tmp")]+" for modified article "+filename[4:])
|
||||
tags.remove(tag_file[9:])
|
||||
if tag[tag.index("tags/") + 5:tag_index(".tmp")] not in tags and filename[4:] in tag_file.read():
|
||||
if tag[tag.index("tags/") + 5:tag.index(".tmp")] not in tags and filename[4:] in tag_file.read():
|
||||
old_tag_file_content = tag_file.read()
|
||||
tag_file.truncate()
|
||||
tag_file.write(old_tag_file_content.replace(filename[4:]+"\n", ""))
|
||||
@ -216,7 +228,7 @@ for filename in modified_files:
|
||||
except IOError:
|
||||
sys.exit("[ERROR] (TAGS) An error occured when parsing tags of article "+filename[4:]+".")
|
||||
|
||||
for tag in tags: #New tags added
|
||||
for tag in tags: # New tags added
|
||||
try:
|
||||
auto_dir("gen/tags/"+tag+".tmp")
|
||||
with open("gen/tags/"+tag+".tmp", "a+") as tag_file:
|
||||
@ -282,13 +294,13 @@ for filename in added_files+modified_files:
|
||||
if "@title=" in line:
|
||||
line = line.strip()
|
||||
title_pos = line.find("@title=")
|
||||
title = line[title_pos+7:]
|
||||
title = line[title_pos+7:].strip()
|
||||
continue
|
||||
|
||||
if "@date=" in line:
|
||||
line = line.strip()
|
||||
date_pos = line.find("@date=")
|
||||
date = line[date_pos+6:]
|
||||
date = line[date_pos+6:].strip()
|
||||
continue
|
||||
|
||||
if isset("date") and isset("title"):
|
||||
@ -373,14 +385,14 @@ else:
|
||||
|
||||
|
||||
#Regenerate index file
|
||||
last_25_articles = latest_articles("gen/", int(params["NB_ARTICLES_INDEX"]))
|
||||
last_articles_index = latest_articles("gen/", int(params["NB_ARTICLES_INDEX"]))
|
||||
try:
|
||||
auto_dir("blog/index.html")
|
||||
with open("blog/index.html", "w") as index_fh:
|
||||
try:
|
||||
with open("gen/header.gen", "r") as header_gen_fh:
|
||||
index = header_gen_fh.read()
|
||||
for article in last_25_articles:
|
||||
for article in last_articles_index:
|
||||
with open(article, "r") as article_fh:
|
||||
index += article_fh.read()
|
||||
with open("gen/footer.gen") as footer_gen_fh:
|
||||
@ -440,5 +452,102 @@ for filename in added_files+modified_files:
|
||||
except IOError:
|
||||
sys.exit("[ERROR] An error occured while generating article "+filename[4:]+" page.")
|
||||
|
||||
#Generate pages for each year and month
|
||||
with open("gen/header.gen", "r") as header_gen_fh:
|
||||
header_gen = header_gen_fh.read()
|
||||
|
||||
with open("gen/footer.gen", "r") as footer_gen_fh:
|
||||
footer_gen = footer_gen_fh.read()
|
||||
|
||||
for i in os.listdir("blog/"):
|
||||
try:
|
||||
int(i)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
#Generate page per year
|
||||
page_year = header_gen.replace("@titre", params["BLOG_TITLE"]+" - "+i, 1)
|
||||
|
||||
for j in os.listdir("blog/"+i):
|
||||
if not os.path.isdir(j):
|
||||
continue
|
||||
|
||||
#Generate pages per month
|
||||
page_month = header_gen.replace("@titre", params["BLOG_TITLE"]+" - "+i+"/"+j, 1)
|
||||
|
||||
for article in list_directory("gen/"+i+"/"+j): # TODO : Sort by date
|
||||
try:
|
||||
with open(article, "r") as article_fh:
|
||||
page_month += article_fh.read()
|
||||
page_year += article_fh.read()
|
||||
except IOError:
|
||||
sys.exit("[ERROR] Error while generating years and months pages. Check your gen folder, you may need to regenerate some articles. The error was due to "+article+".")
|
||||
|
||||
|
||||
page_month += footer_gen
|
||||
try:
|
||||
with open("blog/"+i+"/"+j+"/index.html", "w") as page_month_fh:
|
||||
page_month_fh.write(page_month)
|
||||
except IOError:
|
||||
sys.exit("[ERROR] Unable to write index file for "+i+"/"+j+".")
|
||||
|
||||
page_year += footer_gen
|
||||
try:
|
||||
with open("blog/"+i+"/index.html", "w") as page_year_fh:
|
||||
page_year_fh.write(page_year)
|
||||
except IOError:
|
||||
sys.exit("[ERROR] Unable to write index file for "+i+".")
|
||||
|
||||
|
||||
#Generate RSS
|
||||
#TODO
|
||||
rss = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">"
|
||||
rss += "<channel><atom:link href=\""+params["BLOG_URL"]+"rss.xml\" rel=\"self\" type=\"application/rss+xml\"/><title>"+params["BLOG_TITLE"]+"</title><link>"+params["BLOG_URL"]+"</link>"
|
||||
rss += "<description>"+params["DESCRIPTION"]+"</description><language>"+params["LANGUAGE"]+"</language><copyright>"+params["COPYRIGHT"]+"</copyright>"
|
||||
rss += "<webMaster>"+params["WEBMASTER"]+"</webMaster><lastBuildDate>"+strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())+"</lastBuildDate>"
|
||||
|
||||
del date, title
|
||||
|
||||
for article in last_articles_index:
|
||||
try:
|
||||
with open(article, "r") as article_fh:
|
||||
tags = get_tags(article_fh)
|
||||
article_fh.seek(0)
|
||||
|
||||
for line in article_fh.readlines():
|
||||
if "@title=" in line:
|
||||
line = line.strip()
|
||||
title_pos = line.find("@title=")
|
||||
title = line[title_pos+7:].strip()
|
||||
continue
|
||||
|
||||
if "@date=" in line:
|
||||
line = line.strip()
|
||||
date_pos = line.find("@date=")
|
||||
date = line[date_pos+6:].strip()
|
||||
continue
|
||||
|
||||
if isset("date") and isset("title"):
|
||||
break
|
||||
|
||||
date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime(mktime(datetime.datetime.strptime(date, "%d%m%Y-%H%M").timetuple())))
|
||||
article_fh.seek(0)
|
||||
|
||||
rss += "<item> \
|
||||
<title>"+title+"</title> \
|
||||
<link>"+params["BLOG_URL"]+article[5:]+"</link> \
|
||||
<guid isPermaLink=\"false\">"+params["BLOG_URL"]+article[5:]+"</guid> \
|
||||
<description><![CDATA["+article_fh.read()+"]]></description> \
|
||||
<pubDate>"+date+"</pubDate> \
|
||||
<category>"+', '.join(tags)+"</category> \
|
||||
<author>"+params["WEBMASTER"]+"</author> \
|
||||
</item>"
|
||||
except IOError:
|
||||
sys.exit("[ERROR] Unable to read article "+article+" to generate RSS file.")
|
||||
|
||||
rss += "</channel></rss>"
|
||||
|
||||
try:
|
||||
with open("blog/rss.xml", "w") as rss_fh:
|
||||
rss_fh.write(rss)
|
||||
except IOError:
|
||||
sys.exit("[ERROR] An error occured while writing RSS file.")
|
||||
|
Loading…
Reference in New Issue
Block a user