110 lines
4.2 KiB
Python
110 lines
4.2 KiB
Python
import os
|
|
|
|
articles_path = 'articles/'
|
|
generate_path = 'archives/'
|
|
|
|
"""Parse le fichier markdown et retourne le contenu à mettre dans notre page html"""
|
|
def parsemd(filename):
|
|
content = {'content': '', 'title': '', 'date': '', 'description': ''}
|
|
inquote, inpre, inul = False, False, False
|
|
for line in open('../' + articles_path + filename, "r"):
|
|
line = line.strip()
|
|
|
|
# Récupère la date
|
|
if line.startswith('date:'):
|
|
content['date'] = line.split(':')[1].strip()
|
|
|
|
# Récupère la description
|
|
if line.startswith('description:'):
|
|
content['description'] = line.split(':')[1].strip()
|
|
|
|
# Ferme la citation si on ne cite plus
|
|
if inquote and not line.startswith(">"):
|
|
content['content'] += "</blockquote>\n"
|
|
inquote = False
|
|
|
|
# Ferme la liste si on ne liste plus
|
|
if inul and not line.startswith("-"):
|
|
content['content'] += "</li></ul>\n"
|
|
inul = False
|
|
|
|
# Vérifie si on est dans un bloc de code
|
|
if line.startswith("```"):
|
|
if inpre:
|
|
content['content'] += "</code></pre>\n"
|
|
|
|
content['content'] += "<pre><code>" + line.lstrip("```")
|
|
inpre = True
|
|
|
|
# Vérifie si on est dans une citation
|
|
elif line.startswith(">"):
|
|
if inquote:
|
|
content['content'] += "</blockquote>\n"
|
|
|
|
content['content'] += "<blockquote>" + line.lstrip("> ")
|
|
inquote = True
|
|
|
|
# Vérifie si on est dans un listing
|
|
elif line.startswith("-"):
|
|
if inul:
|
|
content['content'] += "</li>\n"
|
|
content['content'] += "<li>" + line.lstrip("- ")
|
|
else:
|
|
content['content'] += "<ul><li>" + line.lstrip("- ")
|
|
inul = True
|
|
|
|
# Vérifie si on est dans un titre
|
|
elif line.startswith("###"):
|
|
content['content'] += "<h3>" + line.lstrip("# ") + "</h3>\n"
|
|
elif line.startswith("##"):
|
|
content['content'] += "<h2>" + line.lstrip("# ") + "</h2>\n"
|
|
elif line.startswith("#"):
|
|
content['title'] += line.lstrip("# ")
|
|
|
|
# Sinon, on est dans un paragraphe
|
|
elif line != " " and line != "":
|
|
# Vérifie si on a des italiques ou gras
|
|
while '*' in line:
|
|
line = line.replace('*', '<i>', 1)
|
|
line = line.replace('*', '</i>', 1)
|
|
while '**' in line:
|
|
line = line.replace('**', '<b>', 1)
|
|
line = line.replace('**', '</b>', 1)
|
|
|
|
# Vérifie si on a des images
|
|
while '![' in line and ']' in line:
|
|
title = line.split(']')[0].split('[')[1]
|
|
link = line.split(']')[1].split('(')[1].split(')')[0]
|
|
line = line.replace('', '<img src="' + link + '" alt="' + title + '"/>')
|
|
|
|
# Vérifie si on a des liens
|
|
while '[' in line and ']' in line:
|
|
title = line.split(']')[0].split('[')[1]
|
|
link = line.split(']')[1].split('(')[1].split(')')[0]
|
|
line = line.replace('[' + title + '](' + link + ')', '<a href="' + link + '">' + title + '</a>')
|
|
content['content'] += "<p>" + line + "</p>\n"
|
|
return content
|
|
|
|
|
|
def md2html(filename):
|
|
content = parsemd(filename) # Contenu parsé de notre fichier markdown
|
|
template = open('page_template.html', 'r').read()
|
|
output = open('../' + generate_path + filename.split('.')[0] + '.html', 'w')
|
|
output.write(template.replace("$CONTENT", content['content']).replace("$TITLE", content['title']).
|
|
replace("$DATE", content['date']).replace("$DESC", content['description']))
|
|
|
|
|
|
"""
|
|
def generateRSS(data):
|
|
|
|
def updateIndex(data):
|
|
"""
|
|
if __name__=="__main__":
|
|
if not os.path.exists('../' + generate_path):
|
|
os.mkdir('../' + generate_path)
|
|
else:
|
|
for file in os.listdir('../' + generate_path):
|
|
os.remove('../' + generate_path + file)
|
|
for file in os.listdir('../' + articles_path):
|
|
print("Génération en cours du fichier: " + file)
|
|
md2html(file) |