Menu

[r3136]: / branches / 3.x / Src / URoutineHTML.pas  Maximize  Restore  History

Download this file

301 lines (268 with data), 9.0 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
{
* URoutineHTML.pas
*
* Set of classes that generate HTML used to display snippets in information and
* compiler check panes.
*
* $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 URoutineHTML.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2006-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit URoutineHTML;
interface
uses
// Project
USnippets;
type
{
TRoutineHTML:
Base for classes that provide HTML fragments used in pages that describe
snippets. Provides helper and common methods.
}
TRoutineHTML = class(TObject)
strict private
fSnippet: TRoutine; // Value of Snippet property
strict protected
function HiliteSource(const SourceCode: string): string;
{Highlights source code in a style suitable for display in UI.
@param SourceCode [in] Source code to be highlighted.
@return Highlighted source code.
}
property Snippet: TRoutine read fSnippet;
{Reference to snippet for which we're generating HTML}
public
constructor Create(const Snippet: TRoutine);
{Object constructor. Sets up object to provide HTML for a snippet.
@param Snippet [in] Snippet for which to generate HTML.
}
function SnippetName: string;
{Provides snippet name as valid HTML text.
@return Required HTML.
}
end;
{
TInfoHTML:
Class that provides HTML used to display snippet details in information
pane.
}
TInfoHTML = class(TRoutineHTML)
strict private
function SnippetList(const Snippets: TRoutineList): string;
{Generates HTML of a comma separated list of snippets, where each snippet
name is a link to the snippet.
@param Snippets [in] List of snippets in list.
@return HTML of snippet list or 'None' if list empty.
}
function EmptyListSentence: string;
{Generates an HTML safe sentence that indicates a list is empty.
@return Required sentenct.
}
public
function Description: string;
{Provides description of snippet as valid HTML text.
@return Required HTML.
}
function SnippetKind: string;
{Provides HTML containing a description of snippet's kind.
@return Required HTML.
}
function Category: string;
{Provides HTML containing the category that the snippet belongs to.
@return Required HTML.
}
function SourceCode: string;
{Provides HTML containing snippet's source code, syntax highlighted in a
style suitable for display in the UI.
@return Required HTML.
}
function Depends: string;
{Provides list of links to snippets on which the snippet depends.
@return Required HTML containing either a list of links to snippets or
text informing there are no dependencies.
}
function XRefs: string;
{Provides list of links to the snippets with which this snippet is cross-
referenced.
@return Required HTML containing either a list of links to snippets or
text informing there are no cross references.
}
function Units: string;
{Provides comma separated list of units required by the snippet as valid
HTML text.
@return Required HTML of list or text informing if no units are
required.
}
function Extra: string;
{Builds valid HTML containing information from snippet's Extra property.
May contain links and some formatting.
@return Required HTML.
}
end;
implementation
uses
// Delphi
SysUtils, StrUtils,
// Project
Hiliter.UAttrs, Hiliter.UGlobals, Hiliter.UHiliters, UActiveTextHTML,
UHTMLDetailUtils, UHTMLUtils, USnippetKindInfo, UUtils;
{ TRoutineHTML }
constructor TRoutineHTML.Create(const Snippet: TRoutine);
{Object constructor. Sets up object to provide HTML for a snippet.
@param Snippet [in] Snippet for which to generate HTML.
}
begin
inherited Create;
fSnippet := Snippet;
end;
function TRoutineHTML.HiliteSource(const SourceCode: string): string;
{Highlights source code in a style suitable for display in UI.
@param SourceCode [in] Source code to be highlighted.
@return Highlighted source code.
}
var
Hiliter: ISyntaxHiliter; // highlighter object
begin
Hiliter := TSyntaxHiliterFactory.CreateHiliter(hkDetailHTML);
Result := Hiliter.Hilite(SourceCode, THiliteAttrsFactory.CreateDisplayAttrs);
end;
function TRoutineHTML.SnippetName: string;
{Provides snippet name as valid HTML text.
@return Required HTML.
}
begin
Result := MakeSafeHTMLText(Snippet.Name);
end;
{ TInfoHTML }
function TInfoHTML.Category: string;
{Provides HTML containing the category that the snippet belongs to.
@return Required HTML.
}
var
Cat: TCategory; // category that snippet belongs to
begin
Cat := Snippets.Categories.Find(Snippet.Category);
Assert(Assigned(Cat), ClassName + '.Category: Category not found');
Result := MakeSentence(
CategoryALink(Cat.Category, Cat.Description)
);
end;
function TInfoHTML.Depends: string;
{Provides list of links to snippets on which the snippet depends.
@return Required HTML containing either a list of links to snippets or text
informing there are no dependencies.
}
begin
Result := SnippetList(Snippet.Depends);
end;
function TInfoHTML.Description: string;
{Provides description of snippet as valid HTML text.
@return Required HTML.
}
begin
Result := MakeSafeHTMLText(MakeSentence(Snippet.Description));
end;
function TInfoHTML.EmptyListSentence: string;
{Generates an HTML safe sentence that indicates a list is empty.
@return Required sentenct.
}
resourcestring
sEmpty = 'None'; // word that indicates list is empty
begin
Result := MakeSafeHTMLText(MakeSentence(sEmpty));
end;
function TInfoHTML.Extra: string;
{Builds valid HTML containing information from snippet's Extra property. May
contain links and some formatting.
@return Required HTML.
}
begin
Result := TActiveTextHTML.Render(Snippet.Extra);
end;
function TInfoHTML.SnippetList(const Snippets: TRoutineList): string;
{Generates HTML of a comma separated list of snippets, where each snippet name
is a link to the snippet.
@param Snippets [in] List of snippets in list.
@return HTML of snippet list or 'None' if list empty.
}
var
Snippet: TRoutine; // refers to each snippet in list
begin
if Snippets.Count = 0 then
// There are no snippets: say so
Result := EmptyListSentence
else
begin
// Build comma separated list of snippet links
Result := '';
for Snippet in Snippets do
begin
if Result <> '' then
Result := Result + ', ';
Result := Result + RoutineALink(Snippet.Name, Snippet.UserDefined);
end;
Result := MakeSentence(Result);
end;
end;
function TInfoHTML.SnippetKind: string;
{Provides HTML containing a description of snippet's kind.
@return Required HTML.
}
begin
Result := MakeSafeHTMLText(
MakeSentence(TSnippetKindInfoList.Instance[Snippet.Kind].Description)
);
end;
function TInfoHTML.SourceCode: string;
{Provides HTML containing snippet's source code, syntax highlighted in a style
suitable for display in the UI.
@return Required HTML.
}
begin
Result := HiliteSource(Snippet.SourceCode);
end;
function TInfoHTML.Units: string;
{Provides comma separated list of units required by the snippet as valid HTML
text.
@return Required HTML of list or text informing if no units are
required.
}
begin
if Snippet.Units.Count = 0 then
Result := EmptyListSentence
else
Result := MakeSafeHTMLText(JoinStr(Snippet.Units, ', ', False) + '.');
end;
function TInfoHTML.XRefs: string;
{Provides list of links to the snippets with which this snippet is cross
referenced.
@return Required HTML containing either a list of links to snippets or text
informing there are no cross references.
}
begin
Result := SnippetList(Snippet.XRef);
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.