// transformer les balises [...] en <div class="...">
let bodyContent = $('#body').html()
    .replace(/\[([^\/\]]+)\]/g, '<div class="$1">')
    .replace(/\[\/([^\]]+)\]/g, '</div>');
$('#body').empty().append(bodyContent);

// isoler les chiffres des titres
let moments = document.getElementsByClassName("moment");

for(let i = 0; i < moments.length; i++) {
    moments[i].innerHTML = "<span class='first-letter'>" + moments[i].innerHTML.substring(0, moments[i].innerHTML.indexOf('—')) + "</span>" + moments[i].innerHTML.slice(moments[i].innerHTML.indexOf(' —'));
}

// cleaner le balisage des éléments contenant plusieurs paragraphes
let contentToParse = [];
let labeurs = document.getElementsByClassName("labeur");
for(let i = 0; i < labeurs.length; i++) {
    contentToParse.push(labeurs[i]);
}
let latours = document.getElementsByClassName("latour");
for(let i = 0; i < latours.length; i++) {
    contentToParse.push(latours[i]);
}
let lampes = document.getElementsByClassName("lampe");
for(let i = 0; i < lampes.length; i++) {
    contentToParse.push(lampes[i]);
}

for(let i = 0; i < contentToParse.length; i++) {
    if (contentToParse[i].childNodes.length == 1) {
        contentToParse[i].innerHTML = '<p>' + contentToParse[i].innerHTML + '</p>';
    } else {
        contentToParse[i].innerHTML = "<p>" + contentToParse[i].innerHTML.replace("<p>", "");
    }
    // headers latour / lampe
    if (contentToParse[i].classList.contains("latour")) {
        contentToParse[i].childNodes[0].innerHTML = "<div class='latour-header'><div><img src='{{ url('theme://assets/map.png') }}' alt='carte'></div><div>" + contentToParse[i].childNodes[0].innerHTML + "</div></div>";
    } else if (contentToParse[i].classList.contains("lampe")) {
        contentToParse[i].childNodes[0].innerHTML = "<div class='lampe-header'><div><img src='{{ url('theme://assets/lampe.png') }}' alt='lampe'></div><div>" + contentToParse[i].childNodes[0].innerHTML + "</div></div>";
    }
    // micro-typo
    for(j = 0; j < contentToParse[i].childNodes.length; j++) {
        let contentHTML = contentToParse[i].childNodes[j].innerHTML;
        if (contentHTML != undefined) {
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' :', '&nbsp;:');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' ?', '&nbsp;?');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' !', '&nbsp;!');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('« ', '«&nbsp;');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' »', '&nbsp;»');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('“', '«&nbsp;');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('”', '&nbsp;»');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' min', '&nbsp;min');
        }
    }
}