|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ublaboo\Anabelle\Markdown; |
|
6
|
|
|
|
|
7
|
|
|
use Ublaboo\Anabelle\Console\Utils\Logger; |
|
8
|
|
|
use Ublaboo\Anabelle\Generator\Assets; |
|
9
|
|
|
use Ublaboo\Anabelle\Generator\Exception\DocuFileGeneratorException; |
|
10
|
|
|
use Ublaboo\Anabelle\Generator\Exception\DocuGeneratorException; |
|
11
|
|
|
use Ublaboo\Anabelle\Http\AuthCredentials; |
|
12
|
|
|
use Ublaboo\Anabelle\Markdown\DocuScope; |
|
13
|
|
|
use Ublaboo\Anabelle\Markdown\Macros\MacroBlockVariable; |
|
14
|
|
|
use Ublaboo\Anabelle\Markdown\Macros\MacroBlockVariableOutput; |
|
15
|
|
|
use Ublaboo\Anabelle\Markdown\Macros\MacroCleanIndex; |
|
16
|
|
|
use Ublaboo\Anabelle\Markdown\Macros\MacroInclude; |
|
17
|
|
|
use Ublaboo\Anabelle\Markdown\Macros\MacroInlineFileLink; |
|
18
|
|
|
use Ublaboo\Anabelle\Markdown\Macros\MacroInlineVariable; |
|
19
|
|
|
use Ublaboo\Anabelle\Markdown\Macros\MacroInlineVariableOutput; |
|
20
|
|
|
use Ublaboo\Anabelle\Markdown\Macros\MacroSection; |
|
21
|
|
|
use Ublaboo\Anabelle\Parsedown\CustomParsedown; |
|
22
|
|
|
|
|
23
|
|
|
final class Parser |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
private $isLayout; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AuthCredentials |
|
33
|
|
|
*/ |
|
34
|
|
|
private $authCredentials; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Logger |
|
38
|
|
|
*/ |
|
39
|
|
|
private $logger; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var DocuScope |
|
43
|
|
|
*/ |
|
44
|
|
|
private $docuScope; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var CustomParsedown |
|
48
|
|
|
*/ |
|
49
|
|
|
private $parsedown; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var IMacro[] |
|
53
|
|
|
*/ |
|
54
|
|
|
private $macros = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var Assets |
|
58
|
|
|
*/ |
|
59
|
|
|
private $assets; |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function __construct( |
|
63
|
|
|
bool $isLayout, |
|
64
|
|
|
AuthCredentials $authCredentials, |
|
65
|
|
|
Logger $logger, |
|
66
|
|
|
DocuScope $docuScope |
|
67
|
|
|
) { |
|
68
|
|
|
$this->isLayout = $isLayout; |
|
69
|
|
|
$this->authCredentials = $authCredentials; |
|
70
|
|
|
$this->logger = $logger; |
|
71
|
|
|
$this->docuScope = $docuScope; |
|
72
|
|
|
|
|
73
|
|
|
$this->parsedown = new CustomParsedown; |
|
74
|
|
|
$this->assets = new Assets($authCredentials); |
|
75
|
|
|
|
|
76
|
|
|
$this->setupMacros(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @throws DocuGeneratorException |
|
82
|
|
|
* @throws DocuFileGeneratorException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function parseFile(string $inputFile, string $outputFile): void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->logger->logProcessingFile($inputFile); |
|
87
|
|
|
|
|
88
|
|
|
if (!file_exists($inputFile)) { |
|
89
|
|
|
throw new DocuGeneratorException("Missing file [$inputFile]"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$content = file_get_contents($inputFile); |
|
93
|
|
|
|
|
94
|
|
|
foreach ($this->macros as $macro) { |
|
95
|
|
|
try { |
|
96
|
|
|
$macro->runMacro(dirname($inputFile), dirname($outputFile), $content); |
|
97
|
|
|
} catch (DocuGeneratorException $e) { |
|
98
|
|
|
throw new DocuFileGeneratorException( |
|
99
|
|
|
$inputFile, |
|
100
|
|
|
$e->getMessage(), |
|
101
|
|
|
$e->getCode(), |
|
102
|
|
|
$e |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (!file_exists(dirname($outputFile))) { |
|
108
|
|
|
mkdir(dirname($outputFile), 0777, true); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->assets->saveFile( |
|
112
|
|
|
$this->parsedown->text($content), |
|
113
|
|
|
$outputFile, |
|
114
|
|
|
$this->isLayout |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
private function setupMacros(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$this->macros[] = new MacroInclude; |
|
122
|
|
|
$this->macros[] = new MacroInlineVariable($this->docuScope); |
|
123
|
|
|
$this->macros[] = new MacroInlineVariableOutput($this->docuScope); |
|
124
|
|
|
$this->macros[] = new MacroInlineFileLink($this->docuScope); |
|
125
|
|
|
$this->macros[] = new MacroBlockVariable($this->docuScope); |
|
126
|
|
|
$this->macros[] = new MacroBlockVariableOutput($this->docuScope); |
|
127
|
|
|
|
|
128
|
|
|
if ($this->isLayout) { |
|
129
|
|
|
$this->macros[] = new MacroCleanIndex; |
|
130
|
|
|
$this->macros[] = new MacroSection( |
|
131
|
|
|
$this->logger, |
|
132
|
|
|
$this->authCredentials, |
|
133
|
|
|
$this->docuScope |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|