1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
import os
articles_path = 'articles/'
generate_path = 'pages/'
atom_content = ""
index_content = "<h2>Articles</h2><ul>"
"""Parse le fichier markdown et retourne le contenu à mettre dans notre page html"""
def parsemd(filename):
content = {'content': '', 'title': '', 'date': '', 'description': '', 'filename': generate_path + filename.split('.')[0] + '.html'}
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'] += parseline(line.lstrip("> "))
else:
content['content'] += "<blockquote>" + parseline(line.lstrip("> "))
inquote = True
# Vérifie si on est dans un listing
elif line.startswith("-"):
if inul:
content['content'] += "</li>\n"
content['content'] += "<li>" + parseline(line.lstrip("- "))
else:
content['content'] += "<ul><li>" + parseline(line.lstrip("- "))
inul = True
# Vérifie si on est dans un titre
elif line.startswith("###"):
content['content'] += "<h3>" + parseline(line.lstrip("# ")) + "</h3>\n"
elif line.startswith("##"):
content['content'] += "<h2>" + parseline(line.lstrip("# ")) + "</h2>\n"
elif line.startswith("#"):
content['title'] += parseline(line.lstrip("# "))
# Sinon, on est dans un paragraphe
elif line != " " and line != "":
content['content'] += "<p>" + parseline(line) + "</p>\n"
# Vérification que chaque balise est bien fermée
if inquote:
content['content'] += "</blockquote>\n"
inquote = False
if inul:
content['content'] += "</li></ul>\n"
inul = False
if inpre:
content['content'] += "</code></pre>\n"
inpre = False
return content
def parseline(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>')
return line
def md2html(filename):
content = parsemd(filename) # Contenu parsé de notre fichier markdown
template = open('page_template.html', 'r').read()
# Création du fichier html & ajout du contenu
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']))
output.close()
# Génération du post Atom et de l'index
generatePageXML(content)
global index_content
index_content += '<li><a href="' + content['filename'] + '">' + content['title'] + '</a></li>\n'
""" Génère le post Atom"""
def generatePageXML(data):
global atom_content
template = open('atom_post_template.xml', 'r').read()
atom_content += template.replace("$TITLE", data['title']).replace("$DATE", data['date']).replace("$CONTENT",
data['content']).replace("$URL", "https://blog.arka.rocks/" + data['filename'])
""" Génère le fichier Atom"""
def generateAtom():
template = open('atom_template.xml', 'r').read()
output = open('../atom.xml', 'w')
output.write(template.replace("$CONTENT", atom_content))
output.close()
""" Génère l'index"""
def updateIndex():
template = open('index_template.html', 'r').read()
output = open('../index.html', 'w')
output.write(template.replace("$CONTENT", index_content + '</ul>'))
output.close()
if __name__=="__main__":
if not os.path.exists('../' + generate_path):
os.mkdir('../' + generate_path)
else: # Régénation du blog
for file in os.listdir('../' + generate_path):
os.remove('../' + generate_path + file)
os.remove('../atom.xml')
os.remove('../index.html')
for file in os.listdir('../' + articles_path):
print("Génération en cours du fichier: " + file)
md2html(file)
print("Génération du fichier RSS / Atom")
generateAtom()
print("Génération de l'index")
updateIndex()
|