add-vimline.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/php
  2. <?php
  3. chdir(dirname(__FILE__));
  4. require_once 'common.php';
  5. assertCli();
  6. /**
  7. * @file
  8. * Adds vimline to files
  9. */
  10. chdir(dirname(__FILE__) . '/..');
  11. $FS = new FSTools();
  12. $vimline = 'vim: et sw=4 sts=4';
  13. $files = $FS->globr('.', '*');
  14. foreach ($files as $file) {
  15. if (
  16. !is_file($file) ||
  17. prefix_is('./docs/doxygen', $file) ||
  18. prefix_is('./library/standalone', $file) ||
  19. prefix_is('./docs/specimens', $file) ||
  20. postfix_is('.ser', $file) ||
  21. postfix_is('.tgz', $file) ||
  22. postfix_is('.patch', $file) ||
  23. postfix_is('.dtd', $file) ||
  24. postfix_is('.ent', $file) ||
  25. postfix_is('.png', $file) ||
  26. postfix_is('.ico', $file) ||
  27. // wontfix
  28. postfix_is('.vtest', $file) ||
  29. postfix_is('.svg', $file) ||
  30. postfix_is('.phpt', $file) ||
  31. postfix_is('VERSION', $file) ||
  32. postfix_is('WHATSNEW', $file) ||
  33. postfix_is('FOCUS', $file) ||
  34. postfix_is('configdoc/usage.xml', $file) ||
  35. postfix_is('library/HTMLPurifier.includes.php', $file) ||
  36. postfix_is('library/HTMLPurifier.safe-includes.php', $file) ||
  37. postfix_is('smoketests/xssAttacks.xml', $file) ||
  38. // phpt files
  39. postfix_is('.diff', $file) ||
  40. postfix_is('.exp', $file) ||
  41. postfix_is('.log', $file) ||
  42. postfix_is('.out', $file) ||
  43. $file == './library/HTMLPurifier/Lexer/PH5P.php' ||
  44. $file == './maintenance/PH5P.php'
  45. ) continue;
  46. $ext = strrchr($file, '.');
  47. if (
  48. postfix_is('README', $file) ||
  49. postfix_is('LICENSE', $file) ||
  50. postfix_is('CREDITS', $file) ||
  51. postfix_is('INSTALL', $file) ||
  52. postfix_is('NEWS', $file) ||
  53. postfix_is('TODO', $file) ||
  54. postfix_is('WYSIWYG', $file) ||
  55. postfix_is('Changelog', $file)
  56. ) $ext = '.txt';
  57. if (postfix_is('Doxyfile', $file)) $ext = 'Doxyfile';
  58. if (postfix_is('.php.in', $file)) $ext = '.php';
  59. $no_nl = false;
  60. switch ($ext) {
  61. case '.php':
  62. case '.inc':
  63. case '.js':
  64. $line = '// %s';
  65. break;
  66. case '.html':
  67. case '.xsl':
  68. case '.xml':
  69. case '.htc':
  70. $line = "<!-- %s\n-->";
  71. break;
  72. case '.htmlt':
  73. $no_nl = true;
  74. $line = '--# %s';
  75. break;
  76. case '.ini':
  77. $line = '; %s';
  78. break;
  79. case '.css':
  80. $line = '/* %s */';
  81. break;
  82. case '.bat':
  83. $line = 'rem %s';
  84. break;
  85. case '.txt':
  86. case '.utf8':
  87. if (
  88. prefix_is('./library/HTMLPurifier/ConfigSchema', $file) ||
  89. prefix_is('./smoketests/test-schema', $file) ||
  90. prefix_is('./tests/HTMLPurifier/StringHashParser', $file)
  91. ) {
  92. $no_nl = true;
  93. $line = '--# %s';
  94. } else {
  95. $line = ' %s';
  96. }
  97. break;
  98. case 'Doxyfile':
  99. $line = '# %s';
  100. break;
  101. default:
  102. throw new Exception('Unknown file: ' . $file);
  103. }
  104. echo "$file\n";
  105. $contents = file_get_contents($file);
  106. $regex = '~' . str_replace('%s', 'vim: .+', preg_quote($line, '~')) . '~m';
  107. $contents = preg_replace($regex, '', $contents);
  108. $contents = rtrim($contents);
  109. if (strpos($contents, "\r\n") !== false) $nl = "\r\n";
  110. elseif (strpos($contents, "\n") !== false) $nl = "\n";
  111. elseif (strpos($contents, "\r") !== false) $nl = "\r";
  112. else $nl = PHP_EOL;
  113. if (!$no_nl) $contents .= $nl;
  114. $contents .= $nl . str_replace('%s', $vimline, $line) . $nl;
  115. file_put_contents($file, $contents);
  116. }
  117. // vim: et sw=4 sts=4