-
Notifications
You must be signed in to change notification settings - Fork 17
/
generate_api_from_docs.php
118 lines (93 loc) · 2.45 KB
/
generate_api_from_docs.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
<?php
// Path to https://github.com/SteamDatabase/SteamworksDocumentation
$Folder = __DIR__ . '/../SteamworksDocumentation';
/** @var SplFileInfo[] $AllDocs */
$AllDocs = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$Folder,
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS
)
);
$Methods = [];
foreach( $AllDocs as $fileInfo )
{
if( $fileInfo->getExtension() !== 'html' )
{
continue;
}
$Doc = file_get_contents( $fileInfo );
if( $Doc === false )
{
throw new Exception( "Failed to read $fileInfo" );
}
$Doc = explode( '<h2 class="bb_section">', $Doc );
foreach( $Doc as $Section )
{
if( preg_match( '/<div class="bb_code(?: http)?">(.+?)<\/div>/s', $Section, $UrlMatches ) !== 1 )
{
continue;
}
$Url = htmlspecialchars_decode( $UrlMatches[ 1 ] );
$Url = explode( ' ', trim( preg_replace( '/[\n\r ]+/', ' ', $Url ) ) );
if( count( $Url ) < 2 )
{
continue;
}
$UrlPath = parse_url( $Url[ 1 ], PHP_URL_PATH );
if( empty( $UrlPath ) )
{
continue;
}
$Parameters = [];
$HttpMethod = $Url[ 0 ];
$HttpPath = explode( '/', $UrlPath );
if( count( $HttpPath ) < 4 )
{
continue;
}
$ApiService = $HttpPath[ 1 ];
$ApiMethod = $HttpPath[ 2 ];
$ApiVersion = (int)str_replace( 'v', '', $HttpPath[ 3 ] );
$Method =
[
'name' => $ApiMethod,
'version' => $ApiVersion,
'httpmethod' => $HttpMethod,
'parameters' => [],
];
$Section = explode( '</table>', $Section, 2 ); // Can be multiple tables (like response)
preg_match_all( '/<tr>(.*?)<\/tr>/s', $Section[ 0 ], $Matches );
foreach( $Matches[ 1 ] as $ParamsHtml )
{
if( preg_match_all( '/<td>(.*?)<\/td>/s', $ParamsHtml, $ParamsMatches ) === 0 )
{
continue;
}
$Method[ 'parameters' ][] =
[
'name' => trim( strip_tags( $ParamsMatches[ 1 ][ 0 ] ) ),
'type' => trim( strip_tags( $ParamsMatches[ 1 ][ 1 ] ) ),
'optional' => $ParamsMatches[ 1 ][ 2 ] !== '✔',
'description' => trim( strip_tags( $ParamsMatches[ 1 ][ 3 ] ) ),
];
}
if( !isset( $Methods[ $ApiService ] ) )
{
$Methods[ $ApiService ] = [];
}
$Methods[ $ApiService ][] = $Method;
}
}
$FoundServices = [];
foreach( $Methods as $ServiceName => $FoundMethods )
{
$FoundServices[] =
[
'name' => $ServiceName,
'methods' => $FoundMethods,
];
}
file_put_contents(
__DIR__ . DIRECTORY_SEPARATOR . 'api_from_docs.json',
json_encode( $FoundServices, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) . PHP_EOL
);