From ede72d38ccb920ddf92104cf899bf1e461978a4e Mon Sep 17 00:00:00 2001 From: Oxbian Date: Sun, 2 Apr 2023 22:42:10 +0200 Subject: =?UTF-8?q?Init=20commit\nAjout=20du=20g=C3=A9n=C3=A9rateur=20de?= =?UTF-8?q?=20page=20html=20=C3=A0=20partir=20des=20fichiers=20markdown,?= =?UTF-8?q?=20d'un=20style=20basique=20inspir=C3=A9=20du=20blog=20de=20plo?= =?UTF-8?q?um.net.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/page-generator.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++ tools/page_template.html | 40 +++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 tools/page-generator.py create mode 100644 tools/page_template.html (limited to 'tools') diff --git a/tools/page-generator.py b/tools/page-generator.py new file mode 100644 index 0000000..e5747db --- /dev/null +++ b/tools/page-generator.py @@ -0,0 +1,110 @@ +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'] += "\n" + inquote = False + + # Ferme la liste si on ne liste plus + if inul and not line.startswith("-"): + content['content'] += "\n" + inul = False + + # Vérifie si on est dans un bloc de code + if line.startswith("```"): + if inpre: + content['content'] += "\n" + + content['content'] += "
" + line.lstrip("```")
+            inpre = True
+
+        # Vérifie si on est dans une citation
+        elif line.startswith(">"):
+            if inquote:
+                content['content'] += "\n"
+            
+            content['content'] += "
" + line.lstrip("> ") + inquote = True + + # Vérifie si on est dans un listing + elif line.startswith("-"): + if inul: + content['content'] += "\n" + content['content'] += "
  • " + line.lstrip("- ") + else: + content['content'] += "
    • " + line.lstrip("- ") + inul = True + + # Vérifie si on est dans un titre + elif line.startswith("###"): + content['content'] += "

      " + line.lstrip("# ") + "

      \n" + elif line.startswith("##"): + content['content'] += "

      " + line.lstrip("# ") + "

      \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('*', '', 1) + line = line.replace('*', '', 1) + while '**' in line: + line = line.replace('**', '', 1) + line = line.replace('**', '', 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('![' + title + '](' + link + ')', '' + 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 + ')', '' + title + '') + content['content'] += "

      " + line + "

      \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) \ No newline at end of file diff --git a/tools/page_template.html b/tools/page_template.html new file mode 100644 index 0000000..3dda284 --- /dev/null +++ b/tools/page_template.html @@ -0,0 +1,40 @@ + + + + + + + + + + + + $TITLE + + + + + +
      + +
      +
      +
      +

      $TITLE

      + $CONTENT +
      +
      + + + \ No newline at end of file -- cgit v1.2.3