Upload New File

parent 653203c3
# coding: utf8
"""Extrait une strophe du fichier info.md et l'affiche dans firefox
Usage: python3 md2html.py nom_fichier.md [numéro_strophe]
La strophe à afficher est encadrée par un commentaire spécial dans info.md :
<!--:numéro_strophe:-->
strophe
<!--::numéro_strophe::-->
L'appel sans numéro de strophe donne la table des matières .
Auteur: B. Cordeau sur une idée de L. Pointal
"""
# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import sys
import re
from pathlib import Path
from markdown import markdown
import webbrowser
# Programme principal =========================================================
fichier_md = sys.argv[1]
motif = 'toc' if len(sys.argv) == 2 else sys.argv[2]
motif_deb = r"<!--:" + motif + ":-->"
motif_fin = r"<!--::" + motif + "::-->"
deb = re.compile(motif_deb)
fin = re.compile(motif_fin)
infos = "" # chaîne résultat
dedans = False
with open(fichier_md, encoding='utf-8') as f:
for line in f:
if fin.match(line):
break
if dedans:
infos += line
if deb.match(line):
dedans = True
path = Path(sys.argv[1])
fichier = path.name.split(".")[0]
titre = "TOC" if len(sys.argv) == 2 else fichier + " n° " + sys.argv[2]
entete = """\
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>""" + titre + """</title>
</head>
<body>
"""
body = markdown(infos)
enbas = """\
</body>
</html>
"""
page = entete + body + enbas
with open("page.html", "w", encoding="utf-8") as f:
f.write(page)
webbrowser.open("page.html")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment