import re

slide = r'''
<s>
    <cha>VFonctions trinômes</cha>
    <os>
        <o>Généralités, définition</o>
        <o>Forme canonique</o>
        <o>Racines, factorisationn</o>
        <o>Représentation graphique</o>
        <o>Sens de variation</o>
        <o>Signe</o>
    </os>
</s>

<s>
    <par>A. Généralités</par>
</s>

<s>
    <ti>
        A.1 Définition et exemples
    </ti>
    <d>
        <t>
            <n>Les fonctions <e>trinômes</e> </n><n> sont les fonctions définies sur $\mathbb{R}$ </n><n>et pouvant s'écrire :</n>
            $$<n>P(x)=ax^2+bx+c \quad  \text{avec} \quad a\not= 0</n>$$
        </t>
    </d>
    <ex/>
    <ul>
        <li>
            <t>
                <n>La fonction $P:x\longmapsto -8+11x^2-5x$ est un trinôme.</n><br>
                <n>En effet, on a : $a=11$, </n><n> $b=-5$ </n><n> et $c=-8$.</n>
            </t>
        </li>
        <li>
            <t>
                <n>La fonction $Q:x\longmapsto 2(x-1)(x+3)$ </n><n>est également un trinôme </n><n> puisque :</n>
                 $$<n>Q(x)=  2(x^2+3x-x-3) </n><n> =2(x^2+2x-3) </n><n> =2x^2+4x-6</n>$$
                <n>On a ainsi : </n><n> $a=2$, </n><n> $b=4$ </n><n> et $c=-6$.</n>
            </t>
        </li>
        <li>
            <t>
                <n>La fonction $R:x\longmapsto (x+1)^2-(x-1)^2$ </n><n> n'est pas un trinôme </n><n> puisque :</n>
                $$<n>R(x)= x^2+2x+1 </n><n> -(x^2-2x+1) </n><n> =4x</n>$$
            </t>
        </li>
    </ul>
</s>

'''
def dispatch(s):
    def to_frag(txt):
        return re.sub(r"(<n>(?:.|\s)+?<\/n>)", lambda x: f'<n>$\\, \\displaystyle {x.group(1)[3:-4]}$</n>', txt)
    return re.sub(r"(\$\$(?:.|\s)*?\$\$)", lambda x:f'<div class="text-center mt-4 mb-4">{to_frag(x.group(1)[2:-2])}</div>', s)


def make_code(code_str):
    lines = code_str.split('\n')
    n = len(lines)
    sep = '|'.join(f"1-{i}" if i> 1 else "1" for i in range(1, n + 1))
    return f'<div class="text-2xl text-left font-bold mb-8 mt-8">Implémentation Python</div>\n<pre><code data-line-numbers="{sep}" class="language-python">{code_str}</code></pre>'

def make_text(txt):
    txt = re.sub(r"(<e>(?:.|\s)+?<\/e>)", lambda x: f'<span class="emph">{x.group(1)[3:-4]}</span>', txt)
    txt = re.sub(r"(<n>(?:.|\s)+?<\/n>)", lambda x: f'<span class="fragment fade-in">{x.group(1)[3:-4]}</span>', txt)
    return txt
        
def to_fragments(txt):
    return re.sub(r"(<t>(?:.|\s)+?<\/t>)", lambda x: f'<div class="text-left">{make_text(x.group(1)[3:-4])}</div>', txt)
    
def make_objs(t):
    return re.sub(r"(<o>(?:.|\s)+?<\/o>)", lambda x: f'<li class="flex items-center fragment fade-right"><span class="w-6 h-6 rounded bg-blue-800 text-white mr-3 flex items-center justify-center">✓</span><div>{x.group(0)[3:-4]}</div></li>', t)

def make_title(t):
    t = re.sub(r"(<cha>(?:.|\s)+?<\/cha>)", lambda x: f'<div class ="border-[#A5D7FB] border-t-2 border-b-0 border-l-0 border-r-0 mb-24"></div><div class="font-extrabold text-black text-6xl mb-32 fragment fade-up">{x.group(0)[5:-6]}</div>\n', t)  
    fmt = '''<div class="display block bg-blue-50 border p-8 mt-12 border-blue-200 border-t-2 border-b-2 border-r-0 border-l-0 h-40 mx-auto fragment fade-up">
<div class="text-xl flex justify-center text-center items-center -mt-14 px-20 mx-64 py-2 rounded-xl bg-blue-800 text-white font-bold">Objectifs</div>
<ul class="ml-24 text-lg font-semibold mt-12 pt-8 list-inside list-none space-y-1 columns-2">'''
    t = re.sub(r"(<os>(?:.|\s)+?<\/os>)", lambda x: f'''{fmt}{make_objs(x.group(0)[4:-5])}</ul>\n</div>''', t)
    return f'<section>{t}</section>'

    
def make_section(section):
    # proofs, remarks and examples
    section = section.replace("<pr/>", '<div class="text-center align-middle text-white bg-orange-500 p-2 rounded-lg font-bold mb-2 fragment fade-in w-24 shadow-md">Preuve</div>')
    section = section.replace("<rq/>", '<div class="text-left text-orange-500 font-bold mb-2 fragment fade-in">Remarque</div>')
    section = section.replace("<ex/>", '<div class="text-center align-middle text-white bg-green-500 p-2 rounded-lg font-bold mb-2 fragment fade-in w-24 shadow-md">Exemple</div>')
    section = section.replace("<li>", '<li class="fragment fade-in">')
    # paragraphs
    section = re.sub(r"(<par>(?:.|\s)+?<\/par>)", lambda x: f'<div class="text-5xl font-black text-sky-600">{x.group(1)[5:-6]}</div>', section)
    # title
    section = re.sub(r"(<ti>(?:.|\s)+?<\/ti>)", lambda x: f'<div class="text-3xl text-left font-bold text-sky-600 mb-8 fragment fade-in">{x.group(1)[4:-5]}</div>', section)
    # defs
    section = re.sub(r"(<d>(?:.|\s)+?<\/d>)", lambda x: f'<div class="relative border rounded-xl p-8 mt-12 mb-6 w-full bg-green-50 border-green-300 shadow-lg text-left fragment"><div class="absolute -top-5 left-4 px-3 py-3 text-white bg-green-600 font-bold">Définition</div>{to_fragments(x.group(1)[3:-4])}</div>', section)
    # props
    section = re.sub(r"(<p>(?:.|\s)+?<\/p>)", lambda x: f'<div class="relative border rounded-xl p-8 mt-12 mb-6 w-full bg-blue-50 border-blue-300 shadow-lg text-left fragment"><div class="absolute -top-5 left-4 px-3 py-3 text-white bg-blue-600 font-bold">Proposition</div>{to_fragments(x.group(1)[3:-4])}</div>', section)
    # theos
    section = re.sub(r"(<thm>(?:.|\s)+?<\/thm>)", lambda x: f'<div class="relative border rounded-xl p-8 mt-12 mb-6 w-full bg-red-50 border-red-300 shadow-lg text-left fragment"><div class="absolute -top-5 left-4 px-3 py-3 text-white bg-red-600 font-bold">Théorème</div>{to_fragments(x.group(1)[5:-6])}</div>', section)
    # simple txts
    section = to_fragments(section)
    # python snippet
    section = re.sub(r"(<py>(?:.|\s)+?<\/py>)", lambda x: make_code(x.group(0)[4:-5].strip()), section)
    return section

def make_slide(slide):
    # slide = re.sub(r"(\$\$\s*.+\s*\$\$)", lambda x: f"$$<n>{x.group(0)[2:-2]}</n>$$", slide)
    title, *sections = re.findall(r"(?<=<s>)(?:.|\s)+?(?=<\/s>)", slide)
    slides = '\n\n'.join(f"<section>{make_section(dispatch(section))}</section>" for section in sections)
    return f"{make_title(title)}\n\n{slides}"

html = make_slide(slide)
print(html)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: