forked from Villavu/Simba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docgen.simba
156 lines (132 loc) · 5.07 KB
/
docgen.simba
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
// To run to locally build docs you need:
//
// 1) Python installed which is on the systems PATH too so Simba can execute "Python"
// 2) "sphinx" and "furo" installed: pip3 install sphinx furo myst-parser
// Notes:
// - The theme used is furo > https://pradyunsg.me/furo/quickstart/
// - This script extracts documentation comments from `APIFiles` into .rst files
// where sphinx generates static html files.
// Example documentation comment:
(*
SomeMethod
----------
> procedure SomeMethod(SomeInt: Integer);
This method does something.
Use it like:
```
SomeMethod(123);
```
*)
var
APIFiles: array of record
FileName: String;
Name: String;
end;
begin
APIFiles += ['Source\script\imports\simba.import_base.pas', 'Base' ];
APIFiles += ['Source\script\imports\simba.import_colormath.pas', 'Color Math' ];
APIFiles += ['Source\script\imports\simba.import_point.pas', 'TPoint' ];
APIFiles += ['Source\script\imports\simba.import_tpa.pas', 'TPointArray' ];
APIFiles += ['Source\script\imports\simba.import_atpa.pas', 'T2DPointArray' ];
APIFiles += ['Source\script\imports\simba.import_box.pas', 'TBox' ];
APIFiles += ['Source\script\imports\simba.import_boxarray.pas', 'TBoxArray' ];
APIFiles += ['Source\script\imports\simba.import_quad.pas', 'TQuad' ];
APIFiles += ['Source\script\imports\simba.import_circle.pas', 'TCircle' ];
APIFiles += ['Source\script\imports\simba.import_windowhandle.pas', 'TWindowHandle' ];
APIFiles += ['Source\script\imports\simba.import_debugimage.pas', 'Debug Image' ];
APIFiles += ['Source\script\imports\simba.import_random.pas', 'Random' ];
APIFiles += ['Source\script\imports\simba.import_target.pas', 'Target' ];
APIFiles += ['Source\script\imports\simba.import_web.pas', 'Web' ];
APIFiles += ['Source\script\imports\simba.import_file.pas', 'File' ];
APIFiles += ['Source\script\imports\simba.import_encoding.pas', 'Encoding' ];
APIFiles += ['Source\script\imports\simba.import_timing.pas', 'Timing' ];
APIFiles += ['Source\script\imports\simba.import_string.pas', 'String' ];
APIFiles += ['Source\script\imports\simba.import_process.pas', 'Process' ];
APIFiles += ['Source\script\imports\simba.import_math.pas', 'Math' ];
APIFiles += ['Source\script\imports\simba.import_matchtemplate.pas', 'Match Template' ];
APIFiles += ['Source\script\imports\simba.import_misc.pas', 'Misc' ];
APIFiles += ['Source\script\imports\simba.import_matrix.pas', 'Matrix' ];
APIFiles += ['Source\script\imports\simba.import_json.pas', 'JSON' ];
APIFiles += ['Source\script\imports\simba.import_image.pas', 'Image' ];
APIFiles += ['Source\script\imports\simba.import_dtm.pas', 'DTM' ];
APIFiles += ['Source\script\imports\simba.import_async.pas', 'ASync' ];
end;
procedure H2ToH3(Dir: String);
var
FileName: String;
begin
for FileName in DirList(Dir) do
FileWrite(FileName, FileRead(FileName).Replace('h2', 'h3', [rfReplaceAll]));
end;
// The one non sphinx/markdown we change is this:
// - Change lines that begin with '> ' to codeblocks.
procedure RightArrowToCodeBlock(var Str: String);
var
I: Integer;
Lines: TStringArray;
begin
Lines := Str.SplitLines;
while (I < Length(Lines)) do
begin
if Lines[I].StartsWith('> ') then
begin
Lines[I].Delete(1, 2);
Lines.Insert('```', I);
Lines.Insert('```', I+2);
Inc(I, 2);
end;
Inc(I);
end;
Str := LINE_SEP.Join(Lines);
end;
// Delete old api generation
procedure CleanAPI;
var
FileName: String;
begin
for FileName in DirList('DocGen/source/api/') do
if (not (PathExtractName(FileName) in ['index.rst'])) then
FileDelete(FileName);
end;
procedure BuildAPI;
function BuildFile(FileName, Name: String): Boolean;
var
Comment, Comments: String;
begin
if not FileExists(FileName) then
begin
WriteLn('API file "', FileName, '" does not exist');
Exit;
end;
for Comment in FileRead(FileName).BetweenAll('(*', '*)') do
begin
if (Comments <> '') then
Comments += LINE_SEP + '----' + LINE_SEP;
Comments += Comment;
end;
Result := Length(Comments) > 0;
if Result then
begin
RightArrowToCodeBlock(Comments);
FileWrite('DocGen/source/api/' + Name + '.md', Comments);
end;
end;
var
I: Integer;
begin
for I := 0 to High(APIFiles) do
BuildFile(APIFiles[I].FileName, APIFiles[I].Name);
end;
var
SphinxOutput: String;
begin
ClearSimbaOutput();
CleanAPI();
BuildAPI();
WriteLn('Sphinx exit code: ', RunCommand('sphinx-build', ['-q', '-E', 'DocGen/source', 'DocGen/build'], SphinxOutput));
if (SphinxOutput <> '') then
WriteLn(SphinxOutput);
WriteLn('Link: "' + PathNormalize('DocGen/build/index.html') + '"');
// looks better like this, imo
H2ToH3('DocGen/build/api');
end.