parsing.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // transformer les balises [...] en <div class="..."> si ça correspond aux classes prédéfinies
  2. let baliseWords = [
  3. 'labeur',
  4. 'free',
  5. 'temps',
  6. 'moment',
  7. 'pilote',
  8. 'free_left',
  9. 'citation',
  10. 'latour',
  11. 'latour_nohead',
  12. 'lampe',
  13. 'lampe_nohead',
  14. 'bibliographie',
  15. 'imgsmall',
  16. 'imgsmallsmall',
  17. 'imgsmall_bottom',
  18. 'fullpage2imgs',
  19. 'fullpageimage',
  20. 'tripleimgs',
  21. 'tripleimgs_bottom',
  22. 'tripleimgs2',
  23. 'tripleimgs2_bottom',
  24. 'bottomimg',
  25. 'imgfullspreadleft',
  26. 'imgfullspreadright',
  27. 'imgfullspreadright_bleedtop',
  28. 'imgfullspreadright_bleed',
  29. 'breakbefore',
  30. 'breakafter',
  31. `doublepage_bigright`,
  32. `doublepage_bigleft`
  33. ];
  34. var bodyContent = $('#body').html();
  35. bodyContent = bodyContent.replace(/\[([^\/\]]+)\]/g, function(match, word) {
  36. if (baliseWords.includes(word)) {
  37. return '<div class="' + word + '">';
  38. } else {
  39. return match;
  40. }
  41. }).replace(/\[\/([^\]]+)\]/g, function(match, word) {
  42. if (baliseWords.includes(word)) {
  43. return '</div>';
  44. } else {
  45. return match;
  46. }
  47. });
  48. $('#body').empty().append(bodyContent);
  49. // isoler les chiffres des titres
  50. let moments = document.getElementsByClassName("moment");
  51. for(let i = 0; i < moments.length; i++) {
  52. 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(' —'));
  53. }
  54. // cleaner le balisage des éléments contenant plusieurs paragraphes
  55. let contentToParse = [];
  56. let labeurs = document.querySelectorAll('.labeur, .free');
  57. for(let labeur of labeurs) {
  58. contentToParse.push(labeur);
  59. }
  60. // créer header icons pour encarts
  61. let encarts = document.querySelectorAll('.latour, .latour_nohead, .lampe, .lampe_nohead');
  62. for(let encart of encarts) {
  63. encart.innerHTML = encart.innerHTML.replaceAll(' :', '&nbsp;:');
  64. encart.innerHTML = encart.innerHTML.replaceAll(' ?', '&nbsp;?');
  65. encart.innerHTML = encart.innerHTML.replaceAll(' !', '&nbsp;!');
  66. encart.innerHTML = encart.innerHTML.replaceAll('« ', '«&nbsp;');
  67. encart.innerHTML = encart.innerHTML.replaceAll(' »', '&nbsp;»');
  68. encart.innerHTML = encart.innerHTML.replaceAll('(« ', '(«&nbsp;');
  69. encart.innerHTML = encart.innerHTML.replaceAll(' »)', '&nbsp;»)');
  70. encart.innerHTML = encart.innerHTML.replaceAll('“', '«&nbsp;');
  71. encart.innerHTML = encart.innerHTML.replaceAll('”', '&nbsp;»');
  72. encart.innerHTML = encart.innerHTML.replaceAll(' min', '&nbsp;min');
  73. let headContent = encart.firstChild
  74. let headerEl = document.createElement('div');
  75. headerEl.setAttribute('class', 'encart-header')
  76. let iconImg = document.createElement('img');
  77. if (encart.classList.contains('latour') || encart.classList.contains('latour_nohead')) {
  78. iconImg.setAttribute('src', '/user/themes/carnet-atterrissage/assets/map.png');
  79. } else if (encart.classList.contains('lampe') || encart.classList.contains('lampe_nohead')) {
  80. iconImg.setAttribute('src', '/user/themes/carnet-atterrissage/assets/lampe.png');
  81. }
  82. if (encart.classList.contains('latour_nohead') || encart.classList.contains('lampe_nohead')) {
  83. headerEl.style.flexDirection = 'column';
  84. headerEl.style.alignItems = 'flex-start';
  85. headerEl.style.fontWeight = 'normal';
  86. iconImg.style.marginBottom = '2mm';
  87. encart.style.paddingTop = '5mm';
  88. encart.style.paddingBottom = '5mm';
  89. iconImg.style.margin = '0';
  90. iconImg.style.marginBottom = '2.5mm';
  91. }
  92. headerEl.append(iconImg);
  93. headerEl.append(headContent);
  94. encart.firstChild?.remove();
  95. encart.prepend(headerEl);
  96. if (encart.querySelector('.encart-header').nextElementSibling == null) {
  97. encart.querySelector('.encart-header').style.marginBottom = '0';
  98. }
  99. /* console.log(encart.nextElementSibling);
  100. if (encart.nextElementSibling?.classList.contains('moment')) {
  101. console.log('BAAAAAAAAH');
  102. encart.style.color = 'red';
  103. }
  104. */}
  105. // clean bibliographie
  106. let bibliographies = document.querySelectorAll('.bibliographie');
  107. for (let bibliographie of bibliographies) {
  108. contentToParse.push(bibliographie);
  109. if (bibliographie.nextElementSibling.innerText === "") {
  110. bibliographie.nextElementSibling.remove();
  111. }
  112. }
  113. for(let i = 0; i < contentToParse.length; i++) {
  114. if (contentToParse[i].childNodes.length == 1) {
  115. contentToParse[i].innerHTML = '<p>' + contentToParse[i].innerHTML + '</p>';
  116. } else {
  117. contentToParse[i].innerHTML = "<p>" + contentToParse[i].innerHTML.replace("<p>", "");
  118. }
  119. // micro-typo
  120. for(j = 0; j < contentToParse[i].childNodes.length; j++) {
  121. let contentHTML = contentToParse[i].childNodes[j].innerHTML;
  122. if (contentHTML != undefined) {
  123. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' :', '&nbsp;:');
  124. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' ?', '&nbsp;?');
  125. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' !', '&nbsp;!');
  126. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('« ', '«&nbsp;');
  127. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' »', '&nbsp;»');
  128. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('(« ', '(«&nbsp;');
  129. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' »)', '&nbsp;»)');
  130. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('“', '«&nbsp;');
  131. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('”', '&nbsp;»');
  132. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' min', '&nbsp;min');
  133. }
  134. }
  135. // data align last split
  136. if (contentToParse[i].innerHTML.includes('<br>')) {
  137. if (contentToParse[i].childElementCount > 0) {
  138. for (let j = 0; j < contentToParse[i].childElementCount; j++) {
  139. let parts = contentToParse[i].children[j].innerHTML.split('<br>');
  140. let newParts = parts.map(function(part) {
  141. return '<p>' + part + '</p>';
  142. });
  143. contentToParse[i].children[j].innerHTML = newParts.join('');
  144. }
  145. } else {
  146. let parts = contentToParse[i].innerHTML.split('<br>');
  147. let newParts = parts.map(function(part) {
  148. return '<p>' + part + '</p>';
  149. });
  150. contentToParse[i].innerHTML = newParts.join('');
  151. }
  152. }
  153. }
  154. // PAGES TITRES
  155. let h3s = document.querySelectorAll('h3');
  156. for (let h3 of h3s) {
  157. if (/^\d/.test(h3.innerText)) {
  158. h3.innerHTML = h3.innerHTML.replace(/[^\s]*/, function(match) {
  159. return `<div class="gros-chiffre">${match}</div>`;
  160. });
  161. }
  162. h3.innerHTML = h3.innerHTML.replace(/ \«/g, '<br>«');
  163. }
  164. let dessinsPreH3 = document.querySelectorAll('h3 + p img');
  165. for (let dessin of dessinsPreH3) {
  166. dessin.style.width = '0px';
  167. dessin.style.height = '0px';
  168. }
  169. // pour les serpentins
  170. let h4s = document.querySelectorAll('h4');
  171. for (let h4 of h4s) {
  172. if (h4.innerText === "Serpentin") {
  173. let serpentin = h4.nextElementSibling.nextElementSibling;
  174. serpentin.nextElementSibling.remove();
  175. h4.remove();
  176. }
  177. }