programlisting.php 1.3 KB

1234567891011121314151617181920212223242526272829
  1. <?php
  2. $dir = new DirectoryIterator('../module_specs/');
  3. function InsertSpace($matches)
  4. {
  5. return $matches[1].$matches[2].$matches[3].str_pad(' ',strlen($matches[1])).$matches[4];
  6. }
  7. foreach ($dir as $file)
  8. {
  9. if ($file->isFile()) {
  10. $text = file_get_contents($file->getPathName());
  11. // Put \n after CDATA if none
  12. $text = preg_replace('/(\<\!\[CDATA\[)([^\n])/', "$1\n$2", $text);
  13. // Omit space (not \n) before ]]>
  14. $text = preg_replace('/([^\n])(\s*)(\]\]\>\<\/programlisting\>)/', "$1\n$3", $text);
  15. // Reset all space between ]]> and </programlisting>
  16. $text = preg_replace('/(\]\]\>)(\n|\s)*(\<\/programlisting\>)/', "$1$3", $text);
  17. // Omit last ? > of the programlisting
  18. $text = preg_replace('/(\?\>)(\n|\s)*(\]\]\>)/', "$2$3", $text);
  19. // Put \n before ]]> if none
  20. $text = preg_replace('/([^\n])(\]\]\>)(\<\/programlisting\>)/', "$1\n$2$3", $text);
  21. // Put \n between ]]> and </programlisting>
  22. $text = preg_replace('/(\]\]\>)(\<\/programlisting\>)/', "$1\n$2", $text);
  23. // Put same indent before </programlisting> as before <programlisting>
  24. $text = preg_replace_callback('/([^\n]*)(<programlisting role="php"><)(.*?)(<\/programlisting>)/s', "InsertSpace", $text);
  25. file_put_contents($file->getPathName(),$text);
  26. }
  27. }