-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_fix_pipes.php
More file actions
47 lines (40 loc) · 1.38 KB
/
table_fix_pipes.php
File metadata and controls
47 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
* Replace \| to | in table cells.
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
*/
$docPath = isset($argv[1]) ? $argv[1] : 'doc';
$docPath = sprintf('%s/%s', getcwd(), $docPath);
$docPath = realpath($docPath);
$rdi = new RecursiveDirectoryIterator($docPath . '/html');
$rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::SELF_FIRST);
$files = new RegexIterator($rii, '/\.html$/', RecursiveRegexIterator::GET_MATCH);
$process = function () use ($files) {
$fileInfo = $files->getInnerIterator()->current();
if (! $fileInfo->isFile()) {
return true;
}
if ($fileInfo->getBasename('.html') === $fileInfo->getBasename()) {
return true;
}
$file = $fileInfo->getRealPath();
$html = file_get_contents($file);
if (! preg_match('#<table[^>]*>.*?(<\/table>)#s', $html)) {
return true;
}
$matches = [];
if (preg_match_all('/<table[^>]*>.*?<\/table>/s', $html, $matches)) {
foreach ($matches[0] as $origin) {
$count = 0;
$content = str_replace('\|', '|', $origin, $count);
if ($count) {
$html = str_replace($origin, $content, $html);
}
}
}
file_put_contents($file, $html);
return true;
};
iterator_apply($files, $process);