La page de la formation gagne une section a part pour qui ecrit une application qu'un site de l'Alliance heberge : les modules D1 a D5, les trois atlas interactifs (le chemin d'une requete, la vie d'une version, ce qui revient apres une perte) et le guide du developpeur rendu en page. - presentations/dev-01 a dev-05, atlas-developpeur/ et guide/ : copies de Set-OPS-public (promo/formation/, docs/exploitation/). - outils/guide.py : un Markdown restreint vers developpeur/guide.html, les liens relatifs menes a la forge publique de l'Alliance. - construire.py et presentations.py : une collection « developpeur » a cote de la formation et des conferences ; les atlas recoivent leur empreinte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
178 lines
7.2 KiB
Python
178 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Le guide du développeur, rendu en page du site web.
|
|
|
|
Appelé par outils/construire.py ; ne s'exécute pas seul.
|
|
|
|
LA SOURCE. guide/ecrire-une-application.md, copie du guide de Set-OPS-public
|
|
(docs/exploitation/ecrire-une-application.md), où il est versionné : c'est la source de vérité
|
|
du parcours développeur. Une correction se fait là, puis se recopie ici.
|
|
|
|
LA CONVERSION. Un Markdown restreint, celui que le guide emploie : titres, paragraphes,
|
|
citations, listes (et cases à cocher), tableaux, blocs de code, et en ligne le code, le gras,
|
|
l'italique et les liens. Un lien vers un autre document du dépôt mène à la forge publique de
|
|
l'Alliance. Rien d'autre n'est admis : un élément inconnu sort tel quel, en texte."""
|
|
import html
|
|
import re
|
|
from pathlib import Path
|
|
|
|
RACINE = Path(__file__).resolve().parent.parent
|
|
SOURCE = RACINE / "guide" / "ecrire-une-application.md"
|
|
# Le dossier du guide dans le dépôt public, pour résoudre ses liens relatifs.
|
|
FORGE = "https://forge.alliance-boreale.ca/Alliance-Boreale/Set-OPS-Public/src/branch/main/"
|
|
DOSSIER = "docs/exploitation/"
|
|
|
|
|
|
def _lien(cible: str) -> str:
|
|
if re.match(r"https?://", cible):
|
|
return cible
|
|
parties = (DOSSIER + cible).split("/")
|
|
pile = []
|
|
for p in parties:
|
|
if p == "..":
|
|
pile.pop()
|
|
elif p not in ("", "."):
|
|
pile.append(p)
|
|
return FORGE + "/".join(pile)
|
|
|
|
|
|
def _ligne(texte: str) -> str:
|
|
"""Le Markdown en ligne : le code d'abord (rien ne s'y interprète), puis le reste."""
|
|
morceaux = re.split(r"(`[^`]+`)", texte)
|
|
out = []
|
|
for m in morceaux:
|
|
if m.startswith("`") and m.endswith("`") and len(m) > 1:
|
|
out.append(f"<code>{html.escape(m[1:-1])}</code>")
|
|
continue
|
|
t = html.escape(m, quote=False)
|
|
t = re.sub(r"\[([^\]]+)\]\(([^)]+)\)",
|
|
lambda x: f'<a href="{html.escape(_lien(x.group(2)))}">{x.group(1)}</a>', t)
|
|
t = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", t)
|
|
t = re.sub(r"(?<![\w*])\*(?!\s)(.+?)(?<!\s)\*(?![\w*])", r"<em>\1</em>", t)
|
|
out.append(t)
|
|
return "".join(out)
|
|
|
|
|
|
def _ident(titre: str) -> str:
|
|
t = re.sub(r"`", "", titre.lower())
|
|
t = re.sub(r"[^\w\s-]", "", t)
|
|
return re.sub(r"\s+", "-", t.strip())
|
|
|
|
|
|
def _tableau(lignes: list[str]) -> str:
|
|
def cellules(l):
|
|
return [c.strip() for c in l.strip().strip("|").split("|")]
|
|
tete = cellules(lignes[0])
|
|
corps = [cellules(l) for l in lignes[2:]]
|
|
h = "".join(f"<th>{_ligne(c)}</th>" for c in tete)
|
|
b = "".join("<tr>" + "".join(f"<td>{_ligne(c)}</td>" for c in r) + "</tr>" for r in corps)
|
|
return f'<div class="guide-tableau"><table><thead><tr>{h}</tr></thead><tbody>{b}</tbody></table></div>'
|
|
|
|
|
|
def convertir(md: str) -> tuple[str, str, list[tuple[str, str]]]:
|
|
"""→ (titre, corps HTML, sommaire [(ident, titre)])."""
|
|
lignes = md.splitlines()
|
|
out, sommaire, titre = [], [], ""
|
|
i = 0
|
|
while i < len(lignes):
|
|
l = lignes[i]
|
|
if not l.strip():
|
|
i += 1
|
|
continue
|
|
if l.startswith("```"):
|
|
j = i + 1
|
|
while j < len(lignes) and not lignes[j].startswith("```"):
|
|
j += 1
|
|
out.append("<pre><code>" + html.escape("\n".join(lignes[i + 1:j])) + "</code></pre>")
|
|
i = j + 1
|
|
continue
|
|
m = re.match(r"(#{1,3}) (.*)", l)
|
|
if m:
|
|
niveau, texte = len(m.group(1)), m.group(2).strip()
|
|
if niveau == 1:
|
|
titre = texte
|
|
else:
|
|
ident = _ident(texte)
|
|
if niveau == 2:
|
|
sommaire.append((ident, texte))
|
|
out.append(f'<h{niveau} id="{ident}">{_ligne(texte)}</h{niveau}>')
|
|
i += 1
|
|
continue
|
|
if l.startswith(">"):
|
|
bloc = []
|
|
while i < len(lignes) and lignes[i].startswith(">"):
|
|
bloc.append(lignes[i].lstrip(">").strip())
|
|
i += 1
|
|
out.append(f'<blockquote><p>{_ligne(" ".join(bloc))}</p></blockquote>')
|
|
continue
|
|
if l.startswith("|"):
|
|
bloc = []
|
|
while i < len(lignes) and lignes[i].startswith("|"):
|
|
bloc.append(lignes[i])
|
|
i += 1
|
|
out.append(_tableau(bloc))
|
|
continue
|
|
if re.match(r"- ", l):
|
|
# Un élément continue sur ses lignes indentées, même après une ligne vide : la base
|
|
# PostgreSQL du guide porte ainsi un tableau, puis un paragraphe, dans une puce.
|
|
items = []
|
|
while i < len(lignes):
|
|
ligne = lignes[i]
|
|
if ligne.startswith("- "):
|
|
items.append([ligne[2:].strip()])
|
|
elif ligne.startswith(" ") and items:
|
|
items[-1].append(ligne[2:])
|
|
elif not ligne.strip() and items:
|
|
suivante = next((x for x in lignes[i + 1:] if x.strip()), "")
|
|
if not suivante.startswith(" "):
|
|
break
|
|
items[-1].append("")
|
|
else:
|
|
break
|
|
i += 1
|
|
cases = all(it[0].startswith("[ ]") for it in items)
|
|
lis = []
|
|
for it in items:
|
|
if cases:
|
|
it[0] = it[0][3:].strip()
|
|
blocs, courant = [], []
|
|
for x in it:
|
|
if x.strip():
|
|
courant.append(x.strip())
|
|
elif courant:
|
|
blocs.append(courant)
|
|
courant = []
|
|
if courant:
|
|
blocs.append(courant)
|
|
corps = _ligne(" ".join(blocs[0]))
|
|
for b in blocs[1:]:
|
|
corps += _tableau(b) if b[0].startswith("|") else f"<p>{_ligne(' '.join(b))}</p>"
|
|
lis.append(f"<li>{corps}</li>")
|
|
classe = ' class="guide-cases"' if cases else ""
|
|
out.append(f"<ul{classe}>" + "".join(lis) + "</ul>")
|
|
continue
|
|
bloc = []
|
|
while i < len(lignes) and lignes[i].strip() and not re.match(r"(#|```|>|\||- )", lignes[i]):
|
|
bloc.append(lignes[i].strip())
|
|
i += 1
|
|
out.append(f"<p>{_ligne(' '.join(bloc))}</p>")
|
|
return titre, "\n".join(out), sommaire
|
|
|
|
|
|
def corps() -> str:
|
|
titre, contenu, sommaire = convertir(SOURCE.read_text(encoding="utf-8"))
|
|
som = "".join(f'<li><a href="#{i}">{_ligne(t)}</a></li>' for i, t in sommaire)
|
|
return f""" <section class="hero hero-gauche wrap">
|
|
<p class="eyebrow">Parcours développeur · Le guide</p>
|
|
<h1>{_ligne(titre)}</h1>
|
|
<p class="lead">La référence du parcours : chaque module en tire ses faits. Il ne demande aucun accès aux serveurs.</p>
|
|
<div class="diapo-outils">
|
|
<a class="btn btn-ghost" href="../formation.html#developpeur">Tout le parcours</a>
|
|
<a class="btn btn-ghost" href="{FORGE}{DOSSIER}ecrire-une-application.md">La source, à la forge ↗</a>
|
|
</div>
|
|
</section>
|
|
<section class="wrap guide">
|
|
<nav class="guide-sommaire" aria-label="Sommaire"><p class="eyebrow">Sommaire</p><ul>{som}</ul></nav>
|
|
<article class="guide-texte">
|
|
{contenu}
|
|
</article>
|
|
</section>"""
|