-
-
Notifications
You must be signed in to change notification settings - Fork 714
/
Copy pathDocsBuilder.php
168 lines (158 loc) · 6.68 KB
/
DocsBuilder.php
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
/**
* DocsBuilder module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2025 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
use danog\MadelineProto\DocsBuilder\Constructors;
use danog\MadelineProto\DocsBuilder\Methods;
use danog\MadelineProto\TL\TL;
// This code was written a few years ago: it is garbage, and has to be rewritten
final class DocsBuilder
{
public const DEFAULT_TEMPLATES = [
'User' => ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'],
'ReplyMarkup' => ['ReplyMarkup'],
'InputFile' => ['InputFile', 'InputEncryptedFile'],
'InputEncryptedChat' => ['InputEncryptedChat'],
'PhoneCall' => ['PhoneCall'],
'InputPhoto' => ['InputPhoto'],
'InputDocument' => ['InputDocument'],
'InputMedia' => ['InputMedia'],
'InputMessage' => ['InputMessage'],
'KeyboardButton' => ['KeyboardButton'],
'InputCheckPasswordSRP' => ['InputCheckPasswordSRP'],
'Updates' => ['Updates'],
];
use Methods;
use Constructors;
public $td = false;
protected array $settings;
protected string $index;
protected Logger $logger;
protected TL $TL;
protected array $tdDescriptions;
public function __construct(Logger $logger, array $settings)
{
$this->logger = $logger;
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
/** @psalm-suppress InvalidArgument */
$this->TL = new TL(null);
$this->TL->init($settings['TL']);
$this->settings = $settings;
if (!file_exists($this->settings['output_dir'])) {
mkdir($this->settings['output_dir']);
}
chdir($this->settings['output_dir']);
$this->index = $settings['readme'] ? 'README.md' : 'index.md';
foreach (glob($this->settings['template'].'/*') as $template) {
$this->templates[basename($template, '.md')] = file_get_contents($template);
}
}
/**
* Documentation templates.
*
*/
protected array $templates = [];
protected static function markdownEscape(string $s): string
{
return str_replace('_', '\\_', $s);
}
public $types = [];
public $any = '*';
public function mkDocs(): void
{
Logger::log('Generating documentation index...', Logger::NOTICE);
file_put_contents($this->index, $this->template('index', $this->settings['description']));
$this->mkMethods();
$this->mkConstructors();
foreach (glob('types/*') as $unlink) {
unlink($unlink);
}
if (file_exists('types')) {
rmdir('types');
}
mkdir('types');
ksort($this->types);
$index = '';
Logger::log('Generating types documentation...', Logger::NOTICE);
foreach ($this->types as $otype => $keys) {
$type = StrTools::typeEscape($otype);
$index .= '['.self::markdownEscape($type).'](/API_docs/types/'.$type.'.md)<a name="'.$type.'"></a>
';
$constructors = '';
foreach ($keys['constructors'] as $data) {
$predicate = $data['predicate'].(isset($data['layer']) && $data['layer'] !== '' ? '_'.$data['layer'] : '');
$md_predicate = self::markdownEscape($predicate);
$constructors .= "[$md_predicate](/API_docs/constructors/$predicate.md) \n\n";
}
$methods = '';
foreach ($keys['methods'] as $data) {
$name = $data['method'];
$md_name = str_replace(['.', '_'], ['->', '\\_'], $name);
$methods .= "[\$MadelineProto->$md_name](/API_docs/methods/$name.md) \n\n";
}
$symFile = str_replace('.', '_', $type);
$redir = $symFile !== $type ? "\nredirect_from: /API_docs/types/{$symFile}.html" : '';
$header = '';
if (!isset($this->settings['td'])) {
foreach (self::DEFAULT_TEMPLATES as $template => $types) {
if (\in_array($type, $types, true)) {
$header .= $this->template($template, $type);
}
}
}
if (isset($this->tdDescriptions['types'][$otype])) {
$header = "{$this->tdDescriptions['types'][$otype]}\n\n$header";
}
$header = sprintf(
$this->templates['Type'],
$type,
$redir,
self::markdownEscape($type),
$header,
$constructors,
$methods,
);
file_put_contents('types/'.$type.'.md', $header);
}
Logger::log('Generating types index...', Logger::NOTICE);
file_put_contents('types/'.$this->index, $this->templates['types-index'].$index);
Logger::log('Generating additional types...', Logger::NOTICE);
foreach (['waveform', 'string', 'bytes', 'int', 'int53', 'long', 'long|string', 'int128', 'int256', 'int512', 'double', 'Bool', 'DataJSON', '!X'] as $type) {
file_put_contents("types/$type.md", $this->templates[$type]);
}
foreach (['boolFalse', 'boolTrue', 'null', 'photoStrippedSize'] as $constructor) {
file_put_contents("constructors/$constructor.md", $this->templates[$constructor]);
}
Logger::log('Done!', Logger::NOTICE);
}
public static function addToLang(string $key, string $value = '', bool $force = false): void
{
if (!isset(Lang::$lang['en'][$key]) || $force) {
Lang::$lang['en'][$key] = $value;
}
}
/**
* Get formatted template string.
*
* @param string $name Template name
* @param string ...$params Params
*/
protected function template(string $name, string ...$params): string
{
return sprintf($this->templates[$name], ...$params);
}
}