refactor code + propre

This commit is contained in:
Valentin
2024-02-06 17:28:48 +01:00
parent 903c9a993e
commit abc1b70eb5
42 changed files with 1752 additions and 1338 deletions
@@ -0,0 +1,40 @@
// créer header icons pour encarts
if (debug) console.log('start addInsertIcons');
(function() {
let encarts = document.querySelectorAll('.latour, .latour_nohead, .lampe, .lampe_nohead');
for(let encart of encarts) {
let headContent = encart.firstChild
let headerEl = document.createElement('div');
headerEl.setAttribute('class', 'encart-header')
let iconImg = document.createElement('img');
let imgFile = encart.classList.contains('latour') || encart.classList.contains('latour_nohead') ? 'map' : 'lampe';
iconImg.setAttribute('src', `/user/themes/carnet-atterrissage/assets/${ imgFile }.png`);
if (encart.classList.contains('latour_nohead') || encart.classList.contains('lampe_nohead')) {
headerEl.style.flexDirection = 'column';
headerEl.style.alignItems = 'flex-start';
headerEl.style.fontWeight = 'normal';
encart.style.paddingTop = '5mm';
encart.style.paddingBottom = '5mm';
iconImg.style.marginBottom = '2mm';
iconImg.style.margin = '0';
iconImg.style.marginBottom = '2.5mm';
}
headerEl.append(iconImg);
headerEl.append(headContent);
encart.firstChild?.remove();
encart.prepend(headerEl);
if (encart.querySelector('.encart-header').nextElementSibling == null) {
encart.querySelector('.encart-header').style.marginBottom = '0';
}
}
})();
if (debug) console.log('end addInsertIcons');
@@ -0,0 +1,15 @@
// nettoyer le bibliographie
if (debug) console.log('start cleanBibliography');
(function() {
let bibliographies = document.querySelectorAll('.bibliographie');
for (let bibliographie of bibliographies) {
if (bibliographie.nextElementSibling.innerText === "") {
bibliographie.nextElementSibling.remove();
}
}
})();
if (debug) console.log('end cleanBibliography');
@@ -0,0 +1,36 @@
// nettoyer le parsing des pragraphes
if (debug) console.log('start cleanParagraphsParsing');
(function() {
let labeurs = document.querySelectorAll('.labeur, .free');
for(let labeur of labeurs) {
if (labeur.childNodes.length == 1) {
labeur.innerHTML = '<p>' + labeur.innerHTML + '</p>';
} else {
labeur.innerHTML = "<p>" + labeur.innerHTML.replace("<p>", "");
}
// data align last split
if (labeur.innerHTML.includes('<br>')) {
if (labeur.childElementCount > 0) {
for (let j = 0; j < labeur.childElementCount; j++) {
let parts = labeur.children[j].innerHTML.split('<br>');
let newParts = parts.map(function(part) {
return `<p>${ part }</p>`;
});
labeur.children[j].innerHTML = newParts.join('');
}
} else {
let parts = labeur.innerHTML.split('<br>');
let newParts = parts.map(function(part) {
return `<p>${ part }</p>`;
});
labeur.innerHTML = newParts.join('');
}
}
}
})();
if (debug) console.log('end cleanParagraphsParsing');
@@ -0,0 +1,124 @@
// passage des notes du texte à la fin de la partie
if (debug) console.log('start footnotes');
(function() {
// détecter la partie des notes
function findH3InAncestorsAndSiblings(element, findNext) {
let currentElement = element;
let siblingSelector = findNext ? 'nextElementSibling' : 'previousElementSibling';
while (currentElement !== null) {
if (currentElement.tagName === 'H3') {
return currentElement;
}
currentElement = currentElement.parentElement;
}
currentElement = element;
while (currentElement !== null) {
let sibling = currentElement[siblingSelector];
while (sibling !== null) {
if (sibling.tagName === 'H3') {
return sibling;
}
let descendantH3 = sibling.querySelector('h3');
if (descendantH3 !== null) {
return descendantH3;
}
sibling = sibling[siblingSelector];
}
currentElement = currentElement.parentElement;
}
return null;
}
// note dans le texte ou note d'image
function isImgNote(note) {
if (
note.parentElement.classList.contains('imgsmall') ||
note.parentElement.classList.contains('imgsmallsmall') ||
note.parentElement.classList.contains('imgsmall_bottom') ||
note.parentElement.classList.contains('fullpage2imgs') ||
note.parentElement.classList.contains('fullpageimage') ||
note.parentElement.classList.contains('tripleimgs') ||
note.parentElement.classList.contains('tripleimgs_bottom') ||
note.parentElement.classList.contains('tripleimgs2') ||
note.parentElement.classList.contains('tripleimgs2_bottom') ||
note.parentElement.classList.contains('bottomimg') ||
note.parentElement.classList.contains('imgfullspreadleft') ||
note.parentElement.classList.contains('imgfullspreadright') ||
note.parentElement.classList.contains('imgfullspreadright_bleedtop') ||
note.parentElement.classList.contains('imgfullspreadright_bleed') ||
note.parentElement.classList.contains('doublepage_bigright') ||
note.parentElement.classList.contains('doublepage_bigleft')
) {
return true;
} else {
return false;
}
}
// notes
let noteNumber = 1;
let prevPartie;
let partie = 0;
let notes = document.querySelectorAll('.note');
let noteTitleNextEl;
let prevNoteContent;
for (let note of notes) {
let currentPartie = findH3InAncestorsAndSiblings(note, false);
let noteContent = note.innerText;
if (currentPartie === prevPartie) {
noteNumber++;
} else {
// créer la page des notes
let notePageTitle = document.createElement('h4');
notePageTitle.innerText = "Notes";
// insert before the next partie page
findH3InAncestorsAndSiblings(note, true).parentNode.insertBefore(notePageTitle, findH3InAncestorsAndSiblings(note, true));
noteNumber = 1;
partie++;
notePageTitle.setAttribute('id', 'note_page_title_' + partie);
noteTitleNextEl = document.getElementById(`note_page_title_${partie}`).nextElementSibling;
let notesContentEl = document.createElement('div');
notesContentEl.setAttribute('id', `notes_content_${partie}`);
noteTitleNextEl.parentNode.insertBefore(notesContentEl, noteTitleNextEl);
}
if (noteContent != prevNoteContent) {
let noteTextEl = document.createElement('p');
noteTextEl.setAttribute('class', 'note_text');
noteTextEl.innerHTML = '<span class="note_texte_number">' + noteNumber + '</span><span> ' + noteContent + '</span>';
let notesContentEl = document.getElementById(`notes_content_${partie}`);
notesContentEl.append(noteTextEl);
} else {
// si les notes qui se suivent ont le même contenu
let notesContentEl = document.getElementById(`notes_content_${partie}`);
let lastSpan = notesContentEl.querySelector('p:last-of-type span:last-of-type');
let numberToAdd = document.createElement('span');
numberToAdd.setAttribute('class', 'note_texte_number');
numberToAdd.innerText = `, ${noteNumber}`;
notesContentEl.querySelector('p:last-of-type').insertBefore(numberToAdd, lastSpan);
}
prevNoteContent = noteContent;
prevPartie = currentPartie;
if (isImgNote(note)) {
note.innerText = '';
note.classList.add('note_img');
note.setAttribute('id', `note_img_${noteNumber}`);
} else {
note.innerText = noteNumber;
}
}
})();
if (debug) console.log('end footnotes');
@@ -0,0 +1,16 @@
// isoler les chiffres des titres des moments
if (debug) console.log('start increaseMomentNumber');
(function() {
const moments = document.querySelectorAll('.moment');
for(let moment of moments) {
const number = moment.innerHTML.substring(0, moment.innerHTML.indexOf('—')),
titre = moment.innerHTML.slice(moment.innerHTML.indexOf(' —'));
moment.innerHTML = `<span class='first-letter'>${ number }</span>${ titre }`;
}
})();
if (debug) console.log('end increaseMomentNumber');
@@ -0,0 +1,40 @@
// insérer des espaces insécables avant les signes de ponctuations et remplacer les guillemets
if (debug) console.log('start nonBreakingSpaces');
(function() {
let contentsToClean = document.querySelectorAll('.labeur, .free, .latour, .latour_nohead, .lampe, .lampe_nohead, .bibliographie');
for (let content of contentsToClean) {
content.innerHTML = content.innerHTML.replaceAll(' :', '&nbsp;:');
content.innerHTML = content.innerHTML.replaceAll(' ?', '&nbsp;?');
content.innerHTML = content.innerHTML.replaceAll(' !', '&nbsp;!');
content.innerHTML = content.innerHTML.replaceAll('« ', '«&nbsp;');
content.innerHTML = content.innerHTML.replaceAll(' »', '&nbsp;»');
content.innerHTML = content.innerHTML.replaceAll('(« ', '(«&nbsp;');
content.innerHTML = content.innerHTML.replaceAll(' »)', '&nbsp;»)');
content.innerHTML = content.innerHTML.replaceAll('“', '«&nbsp;');
content.innerHTML = content.innerHTML.replaceAll('”', '&nbsp;»');
content.innerHTML = content.innerHTML.replaceAll(' min', '&nbsp;min');
}
})();
if (debug) console.log('end nonBreakingSpaces');
/*
// micro-typo
for(let 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('“', '«&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');
}
} */
@@ -0,0 +1,61 @@
// transformer les balises [...] en <div class="..."> si ça correspond aux classes prédéfinies
if (debug) console.log('start tagToClass');
(function() {
let baliseWords = [
'labeur',
'free',
'temps',
'moment',
'pilote',
'free_left',
'citation',
'latour',
'latour_nohead',
'lampe',
'lampe_nohead',
'note',
'bibliographie',
'imgsmall',
'imgsmallsmall',
'imgsmall_bottom',
'fullpage2imgs',
'fullpageimage',
'tripleimgs',
'tripleimgs_bottom',
'tripleimgs2',
'tripleimgs2_bottom',
'bottomimg',
'imgfullspreadleft',
'imgfullspreadright',
'imgfullspreadright_bleedtop',
'imgfullspreadright_bleed',
'breakbefore',
'breakafter',
`doublepage_bigright`,
`doublepage_bigleft`
];
let bodyContent = $('#body').html();
bodyContent = bodyContent.replace(/\[([^\/\]]+)\]/g, function(match, word) {
if (baliseWords.includes(word)) {
return word === 'note' ? `<span class="${ word }">` : `<div class="${ word }">`;
} else {
return match;
}
}).replace(/\[\/([^\]]+)\]/g, function(match, word) {
if (baliseWords.includes(word)) {
return word === 'note' ? '</span>' : '</div>';
} else {
return match;
}
});
$('#body').empty().append(bodyContent);
})();
if (debug) console.log('end tagToClass');
@@ -0,0 +1,34 @@
// composition des pages titres
if (debug) console.log('start titlePages');
(function() {
let h3s = document.querySelectorAll('h3');
for (let h3 of h3s) {
if (/^\d/.test(h3.innerText)) {
h3.innerHTML = h3.innerHTML.replace(/[^\s]*/, function(match) {
return `<div class="gros-chiffre">${match}</div>`;
});
}
h3.innerHTML = h3.innerHTML.replace(/ \«/g, '<br>«');
}
let dessinsPreH3 = document.querySelectorAll('h3 + p img');
for (let dessin of dessinsPreH3) {
dessin.style.width = '0px';
dessin.style.height = '0px';
}
// pour les serpentins
let h4s = document.querySelectorAll('h4');
for (let h4 of h4s) {
if (h4.innerText === "Serpentin") {
let serpentin = h4.nextElementSibling.nextElementSibling;
serpentin.nextElementSibling.remove();
h4.remove();
}
}
})();
if (debug) console.log('end titlePages');