1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 00:34:40 +01:00
MadelineProto/tools/build_docs.php

213 lines
7.0 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
2019-10-28 22:39:23 +01:00
/**
2020-02-17 14:13:46 +01:00
* Copyright 2016-2020 Daniil Gentili
2019-10-28 22:39:23 +01:00
* (https://daniil.it)
* 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/>.
*/
2019-12-14 16:47:04 +01:00
use danog\MadelineProto\API;
2020-06-16 17:52:55 +02:00
use danog\MadelineProto\Logger;
use danog\MadelineProto\Magic;
2019-12-14 16:47:04 +01:00
use danog\MadelineProto\MTProto;
2020-09-24 20:49:34 +02:00
use danog\MadelineProto\Settings\Logger as SettingsLogger;
2021-12-21 14:44:39 +01:00
use danog\MadelineProto\Tools;
2019-12-14 16:47:04 +01:00
2022-12-08 20:16:40 +01:00
chdir($d=__DIR__.'/..');
2019-10-29 22:00:21 +01:00
require 'vendor/autoload.php';
2019-10-28 22:39:23 +01:00
2023-01-25 16:32:48 +01:00
Magic::start(light: false);
2020-09-24 20:49:34 +02:00
Logger::constructorFromSettings(new SettingsLogger);
2020-06-16 17:52:55 +02:00
$logger = Logger::$default;
2022-12-08 20:16:40 +01:00
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
2020-06-16 17:52:55 +02:00
$logger->logger('Merging constructor localization...', Logger::NOTICE);
mergeExtracted();
2020-06-16 17:52:55 +02:00
$logger->logger('Loading schemas...', Logger::NOTICE);
$schemas = loadSchemas();
$logger->logger('Upgrading layer...', Logger::NOTICE);
$layer = maxLayer($schemas);
layerUpgrade($layer);
2020-03-06 13:09:52 +01:00
2020-10-01 18:02:54 +02:00
$logger->logger("Initing docs (layer $layer)...", Logger::NOTICE);
$docs = [
[
2020-10-01 18:02:54 +02:00
'tl_schema' => ['mtproto' => '', 'telegram' => "$d/schemas/TL_telegram_v$layer.tl", 'secret' => "$d/schemas/TL_secret.tl", 'td' => "$d/schemas/TL_td.tl"],
2020-03-06 13:09:52 +01:00
'title' => "MadelineProto API documentation (layer $layer)",
'description' => "MadelineProto API documentation (layer $layer)",
2019-10-29 22:00:21 +01:00
'output_dir' => "$d/docs/docs/API_docs",
2020-06-16 17:52:55 +02:00
'template' => "$d/docs/template",
2017-01-07 12:40:51 +01:00
'readme' => false,
],
];
2022-12-08 20:16:40 +01:00
$docs = array_merge($docs, initDocs($schemas));
2020-06-16 17:52:55 +02:00
$logger->logger('Creating annotations...', Logger::NOTICE);
2019-12-14 16:47:04 +01:00
$doc = new \danog\MadelineProto\AnnotationsBuilder(
$logger,
2022-05-01 20:17:16 +02:00
$docs[0],
2019-12-14 16:47:04 +01:00
[
'API' => API::class,
'MTProto' => MTProto::class
],
'danog\\MadelineProto'
);
2019-10-29 22:00:21 +01:00
$doc->mkAnnotations();
2020-06-16 17:52:55 +02:00
$logger->logger('Creating docs...', Logger::NOTICE);
foreach ($docs as $settings) {
2018-04-19 19:56:52 +02:00
$doc = new \danog\MadelineProto\DocsBuilder($logger, $settings);
2019-10-29 22:00:21 +01:00
$doc->mkDocs();
}
2019-03-08 15:16:58 +01:00
2022-12-08 20:16:40 +01:00
chdir(__DIR__.'/..');
2019-03-08 15:16:58 +01:00
2020-06-16 17:52:55 +02:00
$logger->logger('Fixing readme...', Logger::NOTICE);
2019-03-08 15:16:58 +01:00
$orderedfiles = [];
$order = [
'CREATING_A_CLIENT',
'LOGIN',
'FEATURES',
'REQUIREMENTS',
'INSTALLATION',
2023-05-26 17:54:42 +02:00
'BROADCAST',
2019-03-08 15:16:58 +01:00
'UPDATES',
2021-09-05 19:33:22 +02:00
'DATABASE',
2019-03-08 15:16:58 +01:00
'SETTINGS',
'SELF',
'EXCEPTIONS',
'FLOOD_WAIT',
'LOGGING',
'CALLS',
'FILES',
'CHAT_INFO',
'DIALOGS',
'INLINE_BUTTONS',
'SECRET_CHATS',
'PROXY',
2019-12-30 20:41:06 +01:00
'ASYNC',
2019-03-08 15:16:58 +01:00
'USING_METHODS',
'CONTRIB',
2019-06-04 14:55:58 +02:00
'TEMPLATES',
2019-03-08 15:16:58 +01:00
];
$index = '';
2022-12-08 20:16:40 +01:00
$files = glob('docs/docs/docs/*md');
2019-03-08 15:16:58 +01:00
foreach ($files as $file) {
2022-12-08 20:16:40 +01:00
$base = basename($file, '.md');
2019-10-28 19:48:59 +01:00
if ($base === 'UPDATES_INTERNAL') {
continue;
}
2022-12-08 20:16:40 +01:00
$key = array_search($base, $order);
2019-03-08 15:16:58 +01:00
if ($key !== false) {
$orderedfiles[$key] = $file;
}
}
2022-12-08 20:16:40 +01:00
ksort($orderedfiles);
2019-03-08 15:16:58 +01:00
foreach ($orderedfiles as $key => $filename) {
2022-12-08 20:16:40 +01:00
$lines = explode("\n", file_get_contents($filename));
while (end($lines) === '' || strpos(end($lines), 'Next')) {
unset($lines[count($lines) - 1]);
2019-03-08 15:16:58 +01:00
}
if ($lines[0] === '---') {
2022-12-08 20:16:40 +01:00
array_shift($lines);
2019-03-08 15:16:58 +01:00
while ($lines[0] !== '---') {
2022-12-08 20:16:40 +01:00
array_shift($lines);
2019-03-08 15:16:58 +01:00
}
2022-12-08 20:16:40 +01:00
array_shift($lines);
2019-03-08 15:16:58 +01:00
}
if (basename($filename) === 'UPDATES.md') {
$idx = array_search('<!-- cut_here -->', $lines, true);
$before = array_slice($lines, 0, $idx);
$idx = array_search('<!-- cut_here_end -->', $lines, true);
$after = array_slice($lines, $idx+1);
$lines = array_merge(
$before,
['<!-- cut_here -->', '```php'],
explode("\n", file_get_contents(__DIR__.'/../examples/bot.php')),
['```', '<!-- cut_here_end -->'],
$after
);
}
2022-12-08 20:16:40 +01:00
preg_match('|^# (.*)|', $lines[0], $matches);
2019-03-08 15:16:58 +01:00
$title = $matches[1];
2022-12-08 20:16:40 +01:00
$description = str_replace('"', "'", Tools::toString($lines[2]));
2019-03-08 15:16:58 +01:00
2022-12-08 20:16:40 +01:00
array_unshift(
2022-05-01 20:17:16 +02:00
$lines,
'---',
'title: "'.$title.'"',
'description: "'.$description.'"',
'nav_order: '.($key+4),
'image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png',
'---'
);
2019-03-08 15:16:58 +01:00
if (isset($orderedfiles[$key + 1])) {
2022-12-08 20:16:40 +01:00
$nextfile = 'https://docs.madelineproto.xyz/docs/'.basename($orderedfiles[$key + 1], '.md').'.html';
$lines[count($lines)] = "\n<a href=\"$nextfile\">Next section</a>";
2019-03-08 15:16:58 +01:00
} else {
2022-12-08 20:16:40 +01:00
$lines[count($lines)] = "\n<a href=\"https://docs.madelineproto.xyz/#very-complex-and-complete-examples\">Next section</a>";
2019-03-08 15:16:58 +01:00
}
2022-12-08 20:16:40 +01:00
file_put_contents($filename, implode("\n", $lines));
2019-03-08 15:16:58 +01:00
2022-12-08 20:16:40 +01:00
$file = file_get_contents($filename);
2019-03-08 15:16:58 +01:00
2022-12-08 20:16:40 +01:00
preg_match_all('|( *)\* \[(.*)\]\((.*)\)|', $file, $matches);
$file = 'https://docs.madelineproto.xyz/docs/'.basename($filename, '.md').'.html';
2019-03-08 15:16:58 +01:00
$index .= "* [$title]($file)\n";
2022-12-08 20:16:40 +01:00
if (basename($filename) !== 'FEATURES.md') {
2019-03-08 15:16:58 +01:00
foreach ($matches[1] as $key => $match) {
$spaces = " $match";
$name = $matches[2][$key];
2021-09-05 20:51:29 +02:00
if ($matches[3][$key][0] === '#') {
$url = $file.$matches[3][$key];
2022-12-08 20:16:40 +01:00
} elseif (substr($matches[3][$key], 0, 3) === '../') {
$url = 'https://docs.madelineproto.xyz/'.str_replace('.md', '.html', substr($matches[3][$key], 3));
2021-09-05 20:51:29 +02:00
} else {
$url = $matches[3][$key];
}
2019-03-08 15:16:58 +01:00
$index .= "$spaces* [$name]($url)\n";
if ($name === 'FULL API Documentation with descriptions') {
2019-06-04 14:55:58 +02:00
$spaces .= ' ';
2022-12-08 20:16:40 +01:00
preg_match_all('|\* (.*)|', file_get_contents('docs/docs/API_docs/methods/index.md'), $smatches);
2019-03-08 15:16:58 +01:00
foreach ($smatches[1] as $key => $match) {
2022-12-08 20:16:40 +01:00
if (str_contains($match, 'You cannot use this method directly')) {
2022-05-01 20:17:16 +02:00
continue;
}
2022-12-08 20:16:40 +01:00
$match = str_replace('href="', 'href="https://docs.madelineproto.xyz/API_docs/methods/', $match);
2019-03-08 15:16:58 +01:00
$index .= "$spaces* ".$match."\n";
}
}
}
}
}
2020-06-16 17:52:55 +02:00
$logger->logger('Fixing readme...', Logger::NOTICE);
2022-12-08 20:16:40 +01:00
$readme = explode('## ', file_get_contents('README.md'));
2019-03-08 15:16:58 +01:00
foreach ($readme as &$section) {
2022-12-08 20:16:40 +01:00
if (explode("\n", $section)[0] === 'Documentation') {
2019-03-08 15:16:58 +01:00
$section = "Documentation\n\n".$index."\n";
}
}
2022-12-08 20:16:40 +01:00
$readme = implode('## ', $readme);
2019-03-08 15:16:58 +01:00
2022-12-08 20:16:40 +01:00
file_put_contents('README.md', $readme);
file_put_contents('docs/docs/index.md', '---
2022-05-01 19:59:07 +02:00
title: MadelineProto
2019-03-08 15:16:58 +01:00
description: PHP client/server for the telegram MTProto protocol (a better tg-cli)
2022-05-01 20:17:16 +02:00
nav_order: 1
2019-03-08 15:16:58 +01:00
image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png
---
'.$readme);
2022-05-01 20:17:16 +02:00
2023-05-26 17:54:42 +02:00
include 'phpdoc.php';