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