parsing.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 'latour',
  8. 'latour_nohead',
  9. 'lampe',
  10. 'lampe_nohead',
  11. 'bibliographie',
  12. 'imgsmall',
  13. 'imgsmall_bottom',
  14. 'fullpage2imgs',
  15. 'fullpageimage',
  16. 'tripleimgs',
  17. 'tripleimgs_bottom',
  18. 'tripleimgs2',
  19. 'tripleimgs2_bottom',
  20. 'bottomimg',
  21. 'imgfullspreadleft',
  22. 'imgfullspreadright',
  23. 'imgfullspreadright_bleedtop',
  24. 'imgfullspreadright_bleed',
  25. 'breakbefore',
  26. 'breakafter',
  27. `doublepage_bigright`,
  28. `doublepage_bigleft`
  29. ];
  30. var bodyContent = $('#body').html();
  31. bodyContent = bodyContent.replace(/\[([^\/\]]+)\]/g, function(match, word) {
  32. if (baliseWords.includes(word)) {
  33. return '<div class="' + word + '">';
  34. } else {
  35. return match;
  36. }
  37. }).replace(/\[\/([^\]]+)\]/g, function(match, word) {
  38. if (baliseWords.includes(word)) {
  39. return '</div>';
  40. } else {
  41. return match;
  42. }
  43. });
  44. $('#body').empty().append(bodyContent);
  45. // isoler les chiffres des titres
  46. let moments = document.getElementsByClassName("moment");
  47. for(let i = 0; i < moments.length; i++) {
  48. 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(' —'));
  49. }
  50. // cleaner le balisage des éléments contenant plusieurs paragraphes
  51. let contentToParse = [];
  52. let labeurs = document.querySelectorAll('.labeur');
  53. for(let labeur of labeurs) {
  54. contentToParse.push(labeur);
  55. }
  56. // créer header icons pour encarts
  57. let encarts = document.querySelectorAll('.latour, .latour_nohead, .lampe, .lampe_nohead');
  58. for(let encart of encarts) {
  59. contentToParse.push(encart);
  60. let headContent = encart.firstChild
  61. let headerEl = document.createElement('div');
  62. headerEl.setAttribute('class', 'encart-header')
  63. let iconImg = document.createElement('img');
  64. if (encart.classList.contains('latour') || encart.classList.contains('latour')) {
  65. iconImg.setAttribute('src', '/user/themes/carnet-atterrissage/assets/map.png');
  66. } else if (encart.classList.contains('lampe') || encart.classList.contains('lampe_nohead')) {
  67. iconImg.setAttribute('src', '/user/themes/carnet-atterrissage/assets/lampe.png');
  68. }
  69. if (encart.classList.contains('latour_nohead') || encart.classList.contains('lampe_nohead')) {
  70. headerEl.style.flexDirection = 'column';
  71. headerEl.style.alignItems = 'flex-start';
  72. headerEl.style.fontWeight = 'normal';
  73. iconImg.style.marginBottom = '2mm';
  74. encart.style.paddingTop = '5mm';
  75. encart.style.paddingBottom = '5mm';
  76. iconImg.style.margin = '0';
  77. iconImg.style.marginBottom = '2.5mm';
  78. }
  79. headerEl.append(iconImg);
  80. headerEl.append(headContent);
  81. encart.firstChild?.remove();
  82. encart.prepend(headerEl);
  83. if (encart.querySelector('.encart-header').nextElementSibling == null) {
  84. encart.querySelector('.encart-header').style.marginBottom = '0';
  85. }
  86. }
  87. for(let i = 0; i < contentToParse.length; i++) {
  88. if (contentToParse[i].childNodes.length == 1) {
  89. contentToParse[i].innerHTML = '<p>' + contentToParse[i].innerHTML + '</p>';
  90. } else {
  91. contentToParse[i].innerHTML = "<p>" + contentToParse[i].innerHTML.replace("<p>", "");
  92. }
  93. // micro-typo
  94. for(j = 0; j < contentToParse[i].childNodes.length; j++) {
  95. let contentHTML = contentToParse[i].childNodes[j].innerHTML;
  96. if (contentHTML != undefined) {
  97. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' :', '&nbsp;:');
  98. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' ?', '&nbsp;?');
  99. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' !', '&nbsp;!');
  100. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('« ', '«&nbsp;');
  101. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' »', '&nbsp;»');
  102. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('“', '«&nbsp;');
  103. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('”', '&nbsp;»');
  104. contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' min', '&nbsp;min');
  105. }
  106. }
  107. }
  108. // PAGES TITRES
  109. let h3s = document.querySelectorAll('h3');
  110. for (let h3 of h3s) {
  111. if (/^\d/.test(h3.innerText)) {
  112. h3.innerHTML = h3.innerHTML.replace(/[^\s]*/, function(match) {
  113. return `<div class="gros-chiffre">${match}</div>`;
  114. });
  115. }
  116. }
  117. let dessinsPreH3 = document.querySelectorAll('h3 + p img');
  118. for (let dessin of dessinsPreH3) {
  119. dessin.style.width = '0px';
  120. dessin.style.height = '0px';
  121. }