Share Pastebin
Guest
Public paste!

jeff__

By: a guest | Jun 19th, 2009 | Syntax: PHP | Size: 3.21 KB | Hits: 145 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?
  2. // la page source qu'on souhaite traiter
  3. $page = file_get_contents('./source/fichier.html');
  4.  
  5. // le header qu'on va ajouter à toutes les pages
  6. $header = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
  7. <html dir=\"ltr\" xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"fr-FR\">
  8. <head>
  9.         <title>\${title} | \${chapterTitle}</title>
  10.         <meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=UTF-8\" />
  11.         <style type=\"text/css\">
  12.                 .nav {
  13.                         width: 100%;
  14.                         height: 2.5em;
  15.                         border-bottom: solid 2px;
  16.                         border-top: solid 2px;
  17.                         }
  18.                 .previous , .index {
  19.                         float: left;
  20.                         width: 33%;
  21.                         }
  22.                 .index {
  23.                         text-align : center;
  24.                         }
  25.                 .next {
  26.                         float: right;
  27.                         width: 33%;
  28.                         text-align: right;
  29.                         }
  30.         </style>
  31. </head>
  32. <body><a name=\"top\"></a>
  33. ";
  34.  
  35. // le footer
  36. $footer = '<p><a href="#top">Haut de la page</a></p></body></html>';
  37.  
  38. // ne retiens que ce qui est entre les balises body, mais on peut imaginer choisir d'autres balises (pour n'avoir que tel ou tel conteneur par exemple)
  39. preg_match('#<body[^>]{0,}>(.*)</body>#mis',$page,$match);
  40. $page = $match[1];
  41.  
  42. // récupération du titre du document
  43. preg_match('#<h1[^>]{0,}>(.*)</h1>#i',$page,$match);
  44. $title = $match[1];
  45.  
  46. // on repère chaque titre de 2° niveau
  47. preg_match_all('#<h([2])[^>]{0,}>(.*)</h\1>#i',$page,$match);
  48. $match = $match[0];
  49. $summary = array();
  50. // pour chaque titre…
  51. foreach ($match as $m) {
  52.         $page = str_replace($m,'<!-- cut here -->'.$m,$page); // …on place un marqueur de découpe
  53.         $summary[] = trim(strip_tags($m)); // …on stocke les titres pour créer une page index
  54. }
  55.  
  56. // découpage du fichier en chapitres
  57. $page = explode('<!-- cut here -->',$page);
  58. foreach ($page as $num=>$chunk) {
  59.         $nav = ''; // barre de navigation
  60.         if ($num > 0) {
  61.                 $chapterTitle = $summary[$num-1];
  62.                 if ($num > 1) $nav = '<div class="previous"><a href="'. ($num-2) . '.html">« ' . $summary[$num-2] . '</a></div>'; //  lien précédent
  63.                 if ($num == 1) $nav = '<div class="previous"><a href="index.html">« Introduction</a></div>'; // lien précédent lorsque premier chapitre
  64.                 $nav .= '<div class="index"><a href="index.html#summary">Sommaire</a></div>'; // lien vers le sommaire
  65.                 if ($num < count($summary)-1) $nav .= '<div class="next"><a href="'. ($num+1) . '.html">' . $summary[$num+1] . ' »</a></div>'; // lien suivant, s'il y a lieu
  66.         } else {
  67.                 $chapterTitle = 'Introduction';
  68.                 $nav = ''; // navigation
  69.                 $num = 'index';
  70.         }
  71.         // formattage de la barre de navigation
  72.         if ($nav != '') $nav = '<div class="nav">' . $nav .'</div>';
  73.         // formattage du chapitre
  74.         $chapter = str_replace('${title}', $title, str_replace('${chapterTitle}', $chapterTitle, $header)) . $nav . trim($chunk) . $nav . $footer;
  75.         // création du fichier
  76.         file_put_contents('./result/'.$num.'.html',$chapter);
  77. }
  78.  
  79. // création d'un index/sommaire
  80. $index = '';
  81. foreach ($summary as $num=>$entry) {
  82.         $index .= '<li><a href="'.$num.'.html">'.$entry.'</a></li>';
  83. }
  84. // ajout du sommaire/index à la premiere page
  85. $index = file_get_contents('./result/index.html') . '<div class="summary"><a name="summary"></a><h2>Sommaire</h2><ul>'.$index.'</ul></div>';
  86. file_put_contents('./result/index.html',$index);
  87. ?>