137 lines
4.1 KiB
Python
137 lines
4.1 KiB
Python
|
#!/usr/bin/python
|
||
|
|
||
|
import sys
|
||
|
import shutil
|
||
|
import os
|
||
|
import datetime
|
||
|
|
||
|
def listdirectory(path):
|
||
|
fichier=[]
|
||
|
for root, dirs, files in os.walk(path):
|
||
|
for i in files:
|
||
|
fichier.append(os.path.join(root, i))
|
||
|
return fichier
|
||
|
|
||
|
if len(sys.argv) == 1:
|
||
|
#If no argument specified, nothing to do
|
||
|
sys.exit("Script expects at least one parameter")
|
||
|
|
||
|
for filename in sys.argv[1:]:
|
||
|
#Create new empty files in tags dir if new categories appeared
|
||
|
with open(filename, 'r') as fh:
|
||
|
line = fh.readline()
|
||
|
while "@tags=" not in line:
|
||
|
line = fh.readline()
|
||
|
line = line.strip() #Delete \n at the end of the line
|
||
|
tag_pos = line.find("@tags=")
|
||
|
tags = line[tag_pos+6:].split(",")
|
||
|
for tag in tags:
|
||
|
try:
|
||
|
with open("gen/tags/"+tag+".tmp", 'a+') as tag_file:
|
||
|
tag_file.seek(0)
|
||
|
if filename[4:] not in tag_file.read():
|
||
|
tag_file.write(filename[4:]+"\n") #Assuming filename is relative to the repertory with .git
|
||
|
print("[TAGS] Found tag "+tag+" for article "+filename[4:])
|
||
|
pass
|
||
|
except IOError:
|
||
|
print("[TAG ERROR] New tag found but error occured : "+tag)
|
||
|
|
||
|
|
||
|
#Generate HTML for the updated articles
|
||
|
fh.seek(0)
|
||
|
line = fh.readline()
|
||
|
while "@title=" not in line:
|
||
|
line = fh.readline()
|
||
|
line = line.strip()
|
||
|
title_pos = line.find("@title=")
|
||
|
title = line[title_pos+7:]
|
||
|
|
||
|
fh.seek(0)
|
||
|
line = fh.readline()
|
||
|
while "@date=" not in line:
|
||
|
line = fh.readline()
|
||
|
line = line.strip()
|
||
|
date_pos = line.find("@date=")
|
||
|
date = line[date_pos+6:]
|
||
|
|
||
|
fh.seek(0)
|
||
|
article = fh.read()
|
||
|
|
||
|
try:
|
||
|
with open("gen/"+filename[4:-5]+".gen", 'w') as article_file:
|
||
|
article_file.write("<article><nav class=\"aside_article\"></nav><div class=\"article\"><h1>"+title+"</h1>"+article+"<p class=\"date\">"+date+"</p></div>\n")
|
||
|
print("[GEN ARTICLES] Article "+filename[4:]+" generated")
|
||
|
except:
|
||
|
print("[GEN ARTICLES ERROR] An error occurred while generating article "+filename[4:])
|
||
|
|
||
|
#Generate headers file (except title)
|
||
|
with open("raw/header.html") as header_fh:
|
||
|
#Tags
|
||
|
tags = os.listdir("gen/tags")
|
||
|
header = header_fh.read()
|
||
|
tags_header = "<ul>"
|
||
|
for tag in tags:
|
||
|
tags_header += "<li><a href=\"tags/"+tag[:-4]+".html\">"+tag[:-4]+"</a></li>"
|
||
|
tags_header += "</ul>"
|
||
|
header = header.replace("@categories", tags_header, 1)
|
||
|
|
||
|
#Articles
|
||
|
latest_articles = listdirectory("gen")
|
||
|
latest_articles = [i for i in latest_articles if i[4:8].isdigit()]
|
||
|
latest_articles.sort()
|
||
|
articles_header = "<ul>"
|
||
|
for article in latest_articles[0:5]: # ??? TODO
|
||
|
with open(article, 'r') as fh:
|
||
|
line = fh.readline()
|
||
|
while "@title" not in line:
|
||
|
line = fh.readline()
|
||
|
line = line.strip()
|
||
|
title_pos = line.find("@title=")
|
||
|
title = line[title_pos+7:]
|
||
|
|
||
|
articles_header += "<li><a href=\""+article[4:-4]+".html\">"+title+"</a></li>"
|
||
|
articles_header += "</ul>"
|
||
|
header = header.replace("@articles", articles_header, 1)
|
||
|
|
||
|
with open("gen/header.gen", "w") as header_gen_fh:
|
||
|
header_gen_fh.write(header)
|
||
|
print("[GEN HEADER] Header has been generated successfully")
|
||
|
|
||
|
#Generate footer file
|
||
|
shutil.copy("raw/footer.html", "gen/footer.gen")
|
||
|
print("[GEN FOOTER] Footer has been generated successfully")
|
||
|
|
||
|
#Regenerate index file
|
||
|
#* First, get last 25 articles
|
||
|
count = 0
|
||
|
this_year = datetime.now().year
|
||
|
this_month = datetime.now().month
|
||
|
#* Try this year, first
|
||
|
for article in listdir("gen/"+this_year+"/"+this_month):
|
||
|
if not is_dir("gen/"+this_year+"/"+this_month+"/"+article):
|
||
|
count += 1
|
||
|
date = article
|
||
|
last_25[date] = article
|
||
|
#* Then, get back progressively
|
||
|
#TODO
|
||
|
|
||
|
#* Then generate effectively the index file
|
||
|
|
||
|
#Regenerate tags pages
|
||
|
for tag in listdir("gen/tags"):
|
||
|
if not isdir(tag):
|
||
|
with open(tag) as tag_fh:
|
||
|
with open("gen/header.gen") as header_handler:
|
||
|
tag_content = header_handler.read()
|
||
|
tag_content.replace("<title>@titre</title", "<title>"+tag+"</title>")
|
||
|
while line = tag_fh.readline():
|
||
|
line = line.replace(".html", ".gen")
|
||
|
with open("gen/"+line) as article_handler:
|
||
|
tag_content += "<section>"+article_handler.read()+"</section>\n"
|
||
|
with open("gen/footer.gen") as footer_handler:
|
||
|
tag_content += footer_handler.read()
|
||
|
|
||
|
#Finish articles pages generation
|
||
|
|
||
|
#Generate RSS
|