Menu

[r660]: / trunk / Src / URoutineDoc.pas  Maximize  Restore  History

Download this file

304 lines (276 with data), 10.1 kB

  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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
{
* URoutineDoc.pas
*
* Implements an abstract base class that renders a text document that describes
* a snippet. Should be overridden by class that generate actual documents in
* required output format.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is URoutineDoc.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2008-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit URoutineDoc;
interface
uses
// Delphi
Classes,
// Project
IntfCompilers, UActiveText, UIStringList, USnippets;
type
{
TCompileDocInfo:
Provides information about a compilation result as text.
}
TCompileDocInfo = record
Compiler: string; // name of compiler
Result: string; // description of compilation result
constructor Create(const ACompiler: string; const ACompRes: TCompileResult);
{Record constructor. Initialises fields from compiler name and a compiler
result.
@param ACompiler [in] Compiler name.
@param CompRes [in] Compiler result.
}
end;
{
TCompileDocInfoArray:
Array of compiler result information.
}
TCompileDocInfoArray = array of TCompileDocInfo;
{
TRoutineDoc:
Abstract base class that renders a text document that describes a snippet.
}
TRoutineDoc = class(TObject)
strict private
fDocStream: TStream; // Stream that receives rendered document
function RoutinesToStrings(const RoutineList: TRoutineList): IStringList;
{Creates a string list containing a list of snippet names.
@param RoutineList [in] List of snippets.
@return String list containing names of snippets from list.
}
function CompilerInfo(const Routine: TRoutine): TCompileDocInfoArray;
{Gets compiler compatibility information for a snippet.
@param Routine [in] Snippet for which compiler information is required.
@return Array of compiler compatibility information.
}
strict protected
procedure InitialiseDoc; virtual;
{Initialises document. Does nothing. Descendant classes should add any
required initialisation here.
}
procedure RenderHeading(const Heading: string); virtual; abstract;
{Outputs heading (snippet name).
@param Heading [in] Heading to be written.
}
procedure RenderDescription(const Desc: string); virtual; abstract;
{Outputs snippet description.
@param Desc [in] Description to be written.
}
procedure RenderSourceCode(const SourceCode: string); virtual; abstract;
{Outputs snippet's source code.
@param SourceCode [in] Source code to be written.
}
procedure RenderTitledList(const Title: string; const List: IStringList);
virtual; abstract;
{Outputs a list preceded by a title.
@param Title [in] List title.
@param List [in] List of text to be written.
}
procedure RenderTitledText(const Title, Text: string); virtual; abstract;
{Outputs text preceded by a title.
@param Title [in] Text title.
@param Text [in] Text to be written.
}
procedure RenderCompilerInfo(const Heading: string;
const Info: TCompileDocInfoArray); virtual; abstract;
{Outputs details of compiler information.
@param Heading [in] Heading for compiler information.
@param Info [in] Array of compiler results (name and result as text).
}
procedure RenderExtra(const ExtraText: IActiveText); virtual; abstract;
{Outputs snippet's extra information.
@param ExtraText [in] Text to be written.
}
procedure RenderDBInfo(const Text: string); virtual; abstract;
{Outputs information about code snippets database.
@param Text [in] Text to be written.
}
procedure FinaliseDoc; virtual;
{Finalises document. Does nothing. Descendant classes should add any
required finalisation here.
}
function CommaList(const List: IStringList): string;
{Builds a comma delimited list of names from a string list.
@param List [in] List of names.
@return Required comma separated list or "none" if list is empty.
}
property DocStream: TStream read fDocStream;
{Reference to stream that receives rendered document. For use by
sub-classes}
public
procedure Generate(const Routine: TRoutine; const DocStream: TStream);
{Generates document that describes a snippet.
@param Routine [in] Snippet for which document is required.
@param Stream [in] Stream to which document is written.
}
end;
implementation
uses
// Delphi
SysUtils,
// Project
UCompilers, UUtils, USnippetKindInfo, UWebInfo;
{ TRoutineDoc }
function TRoutineDoc.CommaList(const List: IStringList): string;
{Builds a comma delimited list of names from a string list.
@param List [in] List of names.
@return Required comma separated list or "none" if list is empty.
}
resourcestring
sNone = 'None.'; // string output for empty lists
begin
Assert(Assigned(List), ClassName + '.CommaList: List is nil');
if List.Count > 0 then
Result := MakeSentence(List.GetText(', ', False))
else
Result := sNone;
end;
function TRoutineDoc.CompilerInfo(
const Routine: TRoutine): TCompileDocInfoArray;
{Gets compiler compatibility information for a snippet.
@param Routine [in] Snippet for which compiler information is required.
@return Array of compiler compatibility information.
}
var
Compilers: ICompilers; // provided info about compilers
Compiler: ICompiler; // each supported compiler
InfoIdx: Integer; // index into output array
begin
Compilers := TCompilersFactory.CreateAndLoadCompilers;
SetLength(Result, Compilers.Count);
InfoIdx := 0;
for Compiler in Compilers do
begin
Result[InfoIdx] := TCompileDocInfo.Create(
Compiler.GetName, Routine.Compatibility[Compiler.GetID]
);
Inc(InfoIdx);
end;
end;
procedure TRoutineDoc.FinaliseDoc;
{Finalises document. Does nothing. Descendant classes should add any required
finalisation here.
}
begin
// Do nothing
end;
procedure TRoutineDoc.Generate(const Routine: TRoutine;
const DocStream: TStream);
{Generates document that describes a snippet.
@param Routine [in] Snippet for which document is required.
@param Stream [in] Stream to which document is written.
}
resourcestring
// Literal string required in output
sKindTitle = 'Snippet Type:';
sCategoryTitle = 'Category:';
sUnitListTitle = 'Required units:';
sDependListTitle = 'Required snippets:';
sXRefListTitle = 'See also:';
sCompilers = 'Supported compilers:';
sMainDatabaseInfo = 'A snippet from the DelphiDabbler CodeSnip Database (%s)';
begin
Assert(Assigned(Routine), ClassName + '.Create: Routine is nil');
Assert(Assigned(DocStream), ClassName + '.Create: DocStream is nil');
// record output stream
fDocStream := DocStream;
// generate document
InitialiseDoc;
RenderHeading(Routine.Name);
RenderDescription(Routine.Description);
RenderSourceCode(Routine.SourceCode);
RenderTitledText(
sKindTitle, TSnippetKindInfoList.Instance[Routine.Kind].Description
);
RenderTitledText(
sCategoryTitle, Snippets.Categories.Find(Routine.Category).Description
);
RenderTitledList(sUnitListTitle, TIStringList.Create(Routine.Units));
RenderTitledList(sDependListTitle, RoutinesToStrings(Routine.Depends));
RenderTitledList(sXRefListTitle, RoutinesToStrings(Routine.XRef));
RenderCompilerInfo(sCompilers, CompilerInfo(Routine));
if not Routine.Extra.IsEmpty then
RenderExtra(Routine.Extra);
if not Routine.UserDefined then
// database info written only if snippet is from main database
RenderDBInfo(Format(sMainDatabaseInfo, [TWebInfo.DatabaseURL]));
FinaliseDoc;
end;
procedure TRoutineDoc.InitialiseDoc;
{Initialises document. Does nothing. Descendant classes should add any
required initialisation here.
}
begin
// Do nothing
end;
function TRoutineDoc.RoutinesToStrings(
const RoutineList: TRoutineList): IStringList;
{Creates a string list containing a list of snippet names.
@param RoutineList [in] List of snippets.
@return String list containing names of snippets from list.
}
var
Routine: TRoutine; // each snippet in list
begin
Result := TIStringList.Create;
for Routine in RoutineList do
Result.Add(Routine.Name);
end;
{ TCompileDocInfo }
constructor TCompileDocInfo.Create(const ACompiler: string;
const ACompRes: TCompileResult);
{Record constructor. Initialises fields from compiler name and a compiler
result.
@param ACompiler [in] Compiler name.
@param CompRes [in] Compiler result.
}
resourcestring
// Compiler results descriptions
sSuccess = 'Compiles OK';
sWarning = 'Compiles with warnings';
sError = 'Does not compile';
sQuery = 'Not tested';
const
// Map of compiler results to descriptions
cResults: array[TCompileResult] of string = (
sSuccess, sWarning, sError, sQuery
);
begin
Compiler := ACompiler;
Result := cResults[ACompRes];
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.