|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +const SEARCH_PATH = __DIR__ . '/../Documentation/'; |
| 4 | + |
| 5 | +$tagsToFiles = []; |
| 6 | + |
| 7 | +foreach (json_decode(file_get_contents(SEARCH_PATH . 'files.json'), true) as $file) { |
| 8 | + if (basename($file['path']) === 'Index.md' || !$file['exists']) { |
| 9 | + continue; |
| 10 | + } |
| 11 | + |
| 12 | + $content = file_get_contents(SEARCH_PATH . $file['path']); |
| 13 | + |
| 14 | + if (preg_match('/<!--(.*)-->/', $content, $matches)) { |
| 15 | + $comment = $matches[0]; |
| 16 | + |
| 17 | + $expandedTagLine = ''; |
| 18 | + |
| 19 | + $typo3Versions = []; |
| 20 | + $tags = []; |
| 21 | + $authors = []; |
| 22 | + |
| 23 | + foreach (preg_split('/\s+/', $matches[1], -1, PREG_SPLIT_NO_EMPTY) as $tag) { |
| 24 | + $tagValue = substr($tag, 1); |
| 25 | + |
| 26 | + // Skip if the value contains non-alphanumerical characters |
| 27 | + if (preg_match('/[^A-Za-z0-9]/', $tagValue)) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + if (preg_match('/#TYPO3v\d\d/', $tag)) { |
| 32 | + $typo3Versions[] = $tagValue; |
| 33 | + } elseif ($tag[0] === '#') { |
| 34 | + $tags[] = $tagValue; |
| 35 | + } elseif ($tag[0] === '@') { |
| 36 | + $authors[] = $tagValue; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if ($typo3Versions !== []) { |
| 41 | + $expandedTagLine .= ' **Tested in:**'; |
| 42 | + |
| 43 | + foreach ($typo3Versions as $typo3Version) { |
| 44 | + $tagsToFiles[$typo3Version][] = [$file['path'], $file['title']]; |
| 45 | + |
| 46 | + $expandedTagLine .= ' [' . $typo3Version . '](/Tags/' . $typo3Version . '.md)'; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if ($tags !== []) { |
| 51 | + $expandedTagLine .= ' **Categories:**'; |
| 52 | + |
| 53 | + foreach ($tags as $tag) { |
| 54 | + $tagsToFiles[$tag][] = [$file['path'], $file['title']]; |
| 55 | + |
| 56 | + $expandedTagLine .= ' [' . $tag . '](/Tags/' . $tag . '.md)'; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if ($authors !== []) { |
| 61 | + $expandedTagLine .= ' **Author:**'; |
| 62 | + |
| 63 | + foreach ($authors as $author) { |
| 64 | + $expandedTagLine .= ' [@' . $author . '](https://my.typo3.org/u/' . $author . ')'; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + file_put_contents(SEARCH_PATH . $file['path'], str_replace($comment, $expandedTagLine, $content)); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +if (!file_exists(SEARCH_PATH . '/Tags')) { |
| 73 | + mkdir(SEARCH_PATH . '/Tags'); |
| 74 | +} |
| 75 | + |
| 76 | +foreach ($tagsToFiles as $tag => $files) { |
| 77 | + $content = '# ' . $tag . PHP_EOL . PHP_EOL; |
| 78 | + |
| 79 | + foreach ($files as [$filePath, $fileTitle]) { |
| 80 | + $content .= '* [' . $fileTitle . '](/' . $filePath . ')' . PHP_EOL; |
| 81 | + } |
| 82 | + |
| 83 | + file_put_contents(SEARCH_PATH . '/Tags/' . $tag . '.md', $content); |
| 84 | +} |
0 commit comments