forked from shopware/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.php
More file actions
50 lines (44 loc) · 2.4 KB
/
migrate.php
File metadata and controls
50 lines (44 loc) · 2.4 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
48
49
50
<?php
// sed does not support lookahead/lookbehind
// fallback to good old PHP :)
// $ find . -name '*.md' -exec php ./.github/scripts/migrate.php {} +
$files = $_SERVER['argv'];
array_shift($files);
$examples = [
'[here](https://github.com/shopware/shopware/blob/trunk/adr/workflow/2020-08-19-handling-feature-flags.md)' => '[here](https://github.com/shopware/shopware/blob/trunk/adr/workflow/2020-08-19-handling-feature-flags.md)',
'[here](http://github.com/shopware/shopware/blob/trunk/adr/workflow/2020-08-19-handling-feature-flags.md)' => '[here](http://github.com/shopware/shopware/blob/trunk/adr/workflow/2020-08-19-handling-feature-flags.md)',
'[here](http/2020-08-19-handling-feature-flags)' => '[here](http/2020-08-19-handling-feature-flags)',
'[here](https/2020-08-19-handling-feature-flags)' => '[here](https/2020-08-19-handling-feature-flags)',
'[here](workflow/2020-08-19-handling-feature-flags)' => '[here](workflow/2020-08-19-handling-feature-flags)',
'[here](./workflow/2020-08-19-handling-feature-flags)' => '[here](./workflow/2020-08-19-handling-feature-flags)',
'[here](../workflow/2020-08-19-handling-feature-flags)' => '[here](../workflow/2020-08-19-handling-feature-flags)',
'[here](../workflow/2020-08-19-handling-feature-flags#some)' => '[here](../workflow/2020-08-19-handling-feature-flags#some)',
'[App base Guide](../../guides/plugins/apps/app-base-guide)' => '[App base Guide](../../guides/plugins/apps/app-base-guide)',
'[Commerce](concepts/commerce/)' => '[Commerce](concepts/commerce/)',
'[App base Guide](../../guides/plugins/apps/app-base-guide.html#test)' => '[App base Guide](../../guides/plugins/apps/app-base-guide.html#test)',
];
$transformer = function ($content) {
// replace .md with empty string
$content = preg_replace(
'_\[([^\[]+)\]\((?!http:|https:)([^\)]*).md(#?[^\)]*)\)_',
'[\\1](\\2\\3)',
$content
);
// replace /README with /
return preg_replace(
'_\[([^\[]+)\]\((?!http:|https:)([^\)]*)/README(#?[^\)]*)\)_',
'[\\1](\\2/\\3)',
$content
);
};
// test
foreach ($examples as $input => $output) {
$transformed = $transformer($input);
if ($output !== $transformed) {
exit('Not okay: ' . $input . ' SHOULD BE ' . $output . ' IS ' . $transformed);
}
}
// transform
foreach ($files as $file) {
file_put_contents($file, $transformer(file_get_contents($file)));
}