Menu

[r4761]: / branches / parsnip / Src / USnippetDoc.pas  Maximize  Restore  History

Download this file

235 lines (208 with data), 7.4 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2008-2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements an abstract base class that renders a text document that describes
* a snippet. Should be overridden by classes that generate actual documents in
* required output format.
}
unit USnippetDoc;
interface
uses
// Delphi
Classes,
// Project
CS.ActiveText,
CS.Database.Types,
Compilers.UGlobals,
UEncodings,
UIStringList;
type
/// <summary>Records information about a compilation result as text.
/// </summary>
TCompileDocInfo = record
/// <summary>Name of compiler.</summary>
Compiler: string;
/// <summary>Description of compilation result.</summary>
Result: string;
/// <summary>Initialises record files from given compiler name and
/// compilation result.</summary>
constructor Create(const ACompiler: string; const ACompRes: TCompileResult);
end;
type
/// <summary>Array of textual compiler result information.</summary>
TCompileDocInfoArray = TArray<TCompileDocInfo>;
type
/// <summary>Abstract base class for classes that render documents that
/// describe snippets.</summary>
TSnippetDoc = class(TObject)
strict private
/// <summary>Creates and returns a string list containing snippet names
/// from given snippet list.</summary>
function SnippetsToStrings(SnippetList: ISnippetIDList): IStringList;
/// <summary>Creates and returns a string list containing the names of
/// tags from the given tag set.</summary>
function TagsToStrings(TagSet: ITagSet): IStringList;
/// <summary>Creates and returns an array of compiler compatibility
/// information for given snippet.</summary>
function CompilerInfo(Snippet: ISnippet): TCompileDocInfoArray;
strict protected
/// <summary>Initialise document.</summary>
/// <remarks>Does nothing. Descendant classes should perform any required
/// initialisation here.</remarks>
procedure InitialiseDoc; virtual;
/// <summary>Output given heading.</summary>
procedure RenderHeading(const Heading: string); virtual; abstract;
/// <summary>Output given snippet description.</summary>
procedure RenderDescription(const Desc: IActiveText); virtual; abstract;
/// <summary>Output given source code.</summary>
procedure RenderSourceCode(const SourceCode: string); virtual; abstract;
/// <summary>Output given title followed by given text.</summary>
procedure RenderTitledText(const Title, Text: string); virtual; abstract;
/// <summary>Output given list of text items, preceded by given title.
/// </summary>
procedure RenderTitledList(const Title: string; List: IStringList);
virtual; abstract;
/// <summary>Output given compiler info, preceeded by given heading.
/// </summary>
procedure RenderCompilerInfo(const Heading: string;
const Info: TCompileDocInfoArray); virtual; abstract;
/// <summary>Outputs given snippet notes.</summary>
/// <remarks>Active text must be interpreted in a manner that makes sense
/// for document format.</remarks>
procedure RenderNotes(const NotesText: IActiveText); virtual; abstract;
/// <summary>Finalise document and return content as encoded data.
/// </summary>
/// <remarks>Descendant classes should perform any required finalisation
/// here.</remarks>
function FinaliseDoc: TEncodedData; virtual; abstract;
/// <summary>Builds and returns a comma separated list from strings in
/// given string list.</summary>
function CommaList(const List: IStringList): string;
public
/// <summary>Generates a document that describes given snippet and returns
/// as encoded data using an encoding that suits type of document.
/// </summary>
function Generate(Snippet: ISnippet): TEncodedData;
end;
implementation
uses
// Delphi
SysUtils,
// Project
CS.Config,
Compilers.UCompilers,
DB.UMain,
UStrUtils,
Web.UInfo;
{ TSnippetDoc }
function TSnippetDoc.CommaList(const List: IStringList): string;
resourcestring
sNone = 'None.'; // string output for empty lists
begin
Assert(Assigned(List), ClassName + '.CommaList: List is nil');
if not List.IsEmpty then
Result := StrMakeSentence(List.GetText(', ', False))
else
Result := sNone;
end;
function TSnippetDoc.CompilerInfo(Snippet: ISnippet):
TCompileDocInfoArray;
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, Snippet.CompileResults[Compiler.GetID]
);
Inc(InfoIdx);
end;
end;
function TSnippetDoc.Generate(Snippet: ISnippet): TEncodedData;
resourcestring
// Literal string required in output
sKindTitle = 'Snippet Type:';
sTagsTitle = 'Tags:';
sLanguageTitle = 'Language:';
sUnitListTitle = 'Required units:';
sDependListTitle = 'Required snippets:';
sXRefListTitle = 'See also:';
sCompilers = 'Supported compilers:';
begin
Assert(Assigned(Snippet), ClassName + '.Create: Snippet is nil');
// generate document
InitialiseDoc;
RenderHeading(Snippet.Title);
RenderDescription(Snippet.Description);
RenderSourceCode(Snippet.SourceCode);
RenderTitledText(
sLanguageTitle,
TConfig.Instance.SourceCodeLanguages[Snippet.LanguageID].FriendlyName
);
RenderTitledText(
sKindTitle, Database.GetAllSnippetKinds[Snippet.KindID].DisplayName
);
RenderTitledList(sTagsTitle, TagsToStrings(Snippet.Tags));
RenderTitledList(sUnitListTitle, Snippet.RequiredModules);
RenderTitledList(
sDependListTitle, SnippetsToStrings(Snippet.RequiredSnippets)
);
RenderTitledList(sXRefListTitle, SnippetsToStrings(Snippet.XRefs));
if Snippet.KindID <> skFreeform then
RenderCompilerInfo(sCompilers, CompilerInfo(Snippet));
if not Snippet.Notes.IsEmpty then
RenderNotes(Snippet.Notes);
Result := FinaliseDoc;
end;
procedure TSnippetDoc.InitialiseDoc;
begin
// Do nothing
end;
function TSnippetDoc.SnippetsToStrings(SnippetList: ISnippetIDList):
IStringList;
var
SnippetID: TSnippetID; // each snippet in list
begin
Result := TIStringList.Create;
for SnippetID in SnippetList do
Result.Add(Database.LookupSnippet(SnippetID).Title);
end;
function TSnippetDoc.TagsToStrings(TagSet: ITagSet): IStringList;
var
Tag: TTag;
begin
Result := TIStringList.Create;
for Tag in TagSet do
Result.Add(Tag.ToString);
end;
{ TCompileDocInfo }
constructor TCompileDocInfo.Create(const ACompiler: string;
const ACompRes: TCompileResult);
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.