add-vimline.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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('configdoc/usage.xml', $file) ||
  34. postfix_is('library/HTMLPurifier.includes.php', $file) ||
  35. postfix_is('library/HTMLPurifier.safe-includes.php', $file) ||
  36. postfix_is('smoketests/xssAttacks.xml', $file) ||
  37. // phpt files
  38. postfix_is('.diff', $file) ||
  39. postfix_is('.exp', $file) ||
  40. postfix_is('.log', $file) ||
  41. postfix_is('.out', $file) ||
  42. $file == './library/HTMLPurifier/Lexer/PH5P.php' ||
  43. $file == './maintenance/PH5P.php'
  44. ) continue;
  45. $ext = strrchr($file, '.');
  46. if (
  47. postfix_is('README', $file) ||
  48. postfix_is('LICENSE', $file) ||
  49. postfix_is('CREDITS', $file) ||
  50. postfix_is('INSTALL', $file) ||
  51. postfix_is('NEWS', $file) ||
  52. postfix_is('TODO', $file) ||
  53. postfix_is('WYSIWYG', $file) ||
  54. postfix_is('Changelog', $file)
  55. ) $ext = '.txt';
  56. if (postfix_is('Doxyfile', $file)) $ext = 'Doxyfile';
  57. if (postfix_is('.php.in', $file)) $ext = '.php';
  58. $no_nl = false;
  59. switch ($ext) {
  60. case '.php':
  61. case '.inc':
  62. case '.js':
  63. $line = '// %s';
  64. break;
  65. case '.html':
  66. case '.xsl':
  67. case '.xml':
  68. case '.htc':
  69. $line = "<!-- %s\n-->";
  70. break;
  71. case '.htmlt':
  72. $no_nl = true;
  73. $line = '--# %s';
  74. break;
  75. case '.ini':
  76. $line = '; %s';
  77. break;
  78. case '.css':
  79. $line = '/* %s */';
  80. break;
  81. case '.bat':
  82. $line = 'rem %s';
  83. break;
  84. case '.txt':
  85. case '.utf8':
  86. if (
  87. prefix_is('./library/HTMLPurifier/ConfigSchema', $file) ||
  88. prefix_is('./smoketests/test-schema', $file) ||
  89. prefix_is('./tests/HTMLPurifier/StringHashParser', $file)
  90. ) {
  91. $no_nl = true;
  92. $line = '--# %s';
  93. } else {
  94. $line = ' %s';
  95. }
  96. break;
  97. case 'Doxyfile':
  98. $line = '# %s';
  99. break;
  100. default:
  101. throw new Exception('Unknown file: ' . $file);
  102. }
  103. echo "$file\n";
  104. $contents = file_get_contents($file);
  105. $regex = '~' . str_replace('%s', 'vim: .+', preg_quote($line, '~')) . '~m';
  106. $contents = preg_replace($regex, '', $contents);
  107. $contents = rtrim($contents);
  108. if (strpos($contents, "\r\n") !== false) $nl = "\r\n";
  109. elseif (strpos($contents, "\n") !== false) $nl = "\n";
  110. elseif (strpos($contents, "\r") !== false) $nl = "\r";
  111. else $nl = PHP_EOL;
  112. if (!$no_nl) $contents .= $nl;
  113. $contents .= $nl . str_replace('%s', $vimline, $line) . $nl;
  114. file_put_contents($file, $contents);
  115. }
  116. // vim: et sw=4 sts=4