|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ublaboo\Anabelle\Markdown\Macros; |
|
6
|
|
|
|
|
7
|
|
|
use Ublaboo\Anabelle\Console\Utils\Logger; |
|
8
|
|
|
use Ublaboo\Anabelle\Generator\Exception\DocuGeneratorException; |
|
9
|
|
|
use Ublaboo\Anabelle\Markdown\Parser; |
|
10
|
|
|
|
|
11
|
|
|
final class MacroCleanIndex implements IMacro |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @throws DocuGeneratorException |
|
16
|
|
|
*/ |
|
17
|
|
|
public function runMacro( |
|
18
|
|
|
string $inputDirectory, |
|
19
|
|
|
string $outputDirectory, |
|
20
|
|
|
string & $content // Intentionally & |
|
21
|
|
|
): void |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Everything except for title and sections |
|
25
|
|
|
*/ |
|
26
|
|
|
$heading = $this->findHeading($content); |
|
27
|
|
|
|
|
28
|
|
|
$lines = explode("\n", $content); |
|
29
|
|
|
|
|
30
|
|
|
[1 => $siteSections, 2 => $methodSections] = $this->findSections($lines); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Now put allowed lines back together |
|
34
|
|
|
*/ |
|
35
|
|
|
$content = "$heading\n\n"; |
|
36
|
|
|
|
|
37
|
|
|
foreach ($siteSections as $siteSection) { |
|
38
|
|
|
$content .= "$siteSection\n"; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
foreach ($methodSections as $methodSection) { |
|
42
|
|
|
$content .= "$methodSection\n"; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
private function findHeading(string $content): string |
|
48
|
|
|
{ |
|
49
|
|
|
if (preg_match('/^# ?[^#].+/m', $content, $matches)) { |
|
50
|
|
|
return $matches[0]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return '# API Docu'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
private function findSections(array $lines): array |
|
58
|
|
|
{ |
|
59
|
|
|
$return = [1 => [], 2 => []]; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($lines as $line) { |
|
62
|
|
|
if (preg_match('/^(@@?) ?.+[^:]:.+\.md/', $line, $matches)) { // Section |
|
63
|
|
|
$return[sizeof($matches[1])][] = $line; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $return; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.