Menu

[r127]: / trunk / Src / URoutineHTML.pas  Maximize  Restore  History

Download this file

418 lines (383 with data), 14.5 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
{
* URoutineHTML.pas
*
* Set of classes that generate HTML used to display snippets in information and
* compiler check panes.
*
* v1.0 of 03 Dec 2006 - Original version.
* v1.1 of 04 Dec 2006 - Changed to use TTestUnit to generate test unit. This
* causes name of test unit to change from 'TestUnit' to
* a name based on routine name.
* v1.2 of 12 Feb 2007 - Removed MakeSentence routine. Now use version in
* UUtils.
* - Rationalised use of MakeSentence.
* - Improved generated HTML to reduce likelihood of
* illegal characters being included.
* v1.3 of 31 Oct 2007 - Added 'external-link' class to credits web-links.
* - Modified to use IStringList instead of TStringList.
* v1.4 of 26 Aug 2008 - Modified to work with revised signature of
* RoutineALink routine.
* v1.5 of 30 Dec 2008 - Made public and private sections strict.
* - Changed TInfoHTML to generate a routines' extra info
* from new active text TRoutine.Extra property rather
* than from Credits, CreditsURL and Comments properties.
* - Now access all routines in routine list using for..do.
* - Added method to TInfoHTML to generate a sentence
* indicating an empty list.
* v1.6 of 20 Jun 2009 - Removed TCompHTML class.
* - Added support for new REML tags in TRoutine.Extra
* property.
* - Added new TInfoHTML.SnippetKind method.
* v1.7 of 12 Jul 2009 - Added new TInfoHTML.Category method.
* - Added full stop to end of snippet kind text.
*
*
* ***** 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-2009 Peter
* Johnson. All Rights Reserved.
*
* ***** 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
fRoutine: TRoutine; // Value of Routine 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 Routine: TRoutine
read fRoutine;
{Reference to snippet for which we're generating HTML}
public
constructor Create(const Routine: TRoutine);
{Class constructor. Sets up object to provide HTML for a snippet.
@param Routine [in] Snippet for which to generate HTML.
}
function RoutineName: 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 RoutineList(const Routines: TRoutineList): string;
{Generates HTML of a comma separated list of snippets, where each snippet
name is a link to the snippet.
@param Routines [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 RoutineDesc: string;
{Provides description of snippet as valid HTML text.
@return Required HTML.
}
function SnippetKind: string;
{Provides HTML containing a description of snippet kind.
@return Required HTML.
}
function Category: string;
{Provides HTML containing the category that a 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 UI.
@return Required HTML.
}
function Depends: string;
{Provides list of links to snippets on which 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 snippets with which 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
IntfHiliter, UActiveText, UHiliteAttrs, UHTMLDetailUtils, UHTMLUtils,
USnippetKindInfo, UIStringList, USyntaxHiliters, UTestUnit, UUtils;
{ TRoutineHTML }
constructor TRoutineHTML.Create(const Routine: TRoutine);
{Class constructor. Sets up object to provide HTML for a snippet.
@param Routine [in] Snippet for which to generate HTML.
}
begin
inherited Create;
fRoutine := Routine;
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.CreateDefaultAttrs);
end;
function TRoutineHTML.RoutineName: string;
{Provides snippet name as valid HTML text.
@return Required HTML.
}
begin
Result := MakeSafeHTMLText(Routine.Name);
end;
{ TInfoHTML }
function TInfoHTML.Category: string;
{Provides HTML containing the category that a snippet belongs to.
@return Required HTML.
}
var
Cat: TCategory; // category that snippet belongs to
begin
Cat := Snippets.Categories.Find(Routine.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 snippet depends.
@return Required HTML containing either a list of links to snippets or text
informing there are no dependencies.
}
begin
Result := RoutineList(Routine.Depends);
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.
}
const
// maps state of an active text element to equivalent HTML tag type
cTagTypeMap: array[TActiveTextElemState] of THTMLTagType = (
ttClose, // fsClose: closing tag e.g. </tagname>
ttOpen // fsOpen: opening tag e.g. <tagname [params]>
);
resourcestring
sCreditsURLHint = 'Visit %s'; // hint used in <a> tag title attribute
var
Elem: IActiveTextElem; // each active text element
TextElem: IActiveTextTextElem; // a text active text element
ActionElem: IActiveTextActionElem; // an action active text element
EncloseInDiv: Boolean;
// ---------------------------------------------------------------------------
function ClassAttr(const ClassName: string): IHTMLAttributes;
{Creates an HTML attributes object containing a class attribute.
@param ClassName [in] Name of class.
@return Required HTML attributes object.
}
begin
Result := THTMLAttributes.Create;
Result.Add('class', ClassName);
end;
// ---------------------------------------------------------------------------
begin
Result := '';
// Process each active text element
for Elem in Routine.Extra do
begin
if Supports(Elem, IActiveTextTextElem, TextElem) then
// A text element: write it literally as safe HTML text
Result := Result + MakeSafeHTMLText(TextElem.Text)
else if Supports(Elem, IActiveTextActionElem, ActionElem) then
begin
// An action element: if supported output it in HTML
case ActionElem.Kind of
ekLink:
begin
// REML <a> => HTML <a class="external-link">
// REML href => HTML href
if ActionElem.State = fsOpen then
// opening tag: element's Param property is HTML href attribute
Result := Result + AOpenTag(
ActionElem.Param,
'',
'|' + Format(sCreditsURLHint, [ActionElem.Param]),
TIStringList.Create('external-link')
)
else
// an </a> tag
Result := Result + MakeTag('a', ttClose);
end;
ekStrong:
// REML <strong> => HTML <strong>
Result := Result + MakeTag('strong', cTagTypeMap[ActionElem.State]);
ekEm:
// REML <em> => HTML <em>
Result := Result + MakeTag('em', cTagTypeMap[ActionElem.State]);
ekVar:
// REML <var> => HTML <var class="extra">
Result := Result + MakeTag(
'var', cTagTypeMap[ActionElem.State], ClassAttr('extra')
);
ekPara:
// REML <p> => HTML <p>
Result := Result + MakeTag('p', cTagTypeMap[ActionElem.State]);
ekWarning:
// REML <warning> => HTML <span class="extra-warning">
Result := Result + MakeTag(
'span', cTagTypeMap[ActionElem.State], ClassAttr('extra-warning')
);
ekMono:
// REML <mono> => HTML <span class="extra-mono">
Result := Result + MakeTag(
'span', cTagTypeMap[ActionElem.State], ClassAttr('extra-mono')
);
ekHeading:
// REML <heading> => HTML <h2 class="extra">
Result := Result + MakeTag(
'h2', cTagTypeMap[ActionElem.State], ClassAttr('extra')
);
else
{Unsupported action element type: do nothing};
end;
end;
end;
// Extra property may have "p" or "heading" tags, but may not have. So we
// check and add enclosing "div" tags if necessary with required properties.
// paragraph tags if
EncloseInDiv := not Routine.Extra.IsEmpty and
not ((Routine.Extra[0].Kind in [ekPara, ekHeading]));
if EncloseInDiv then
Result := MakeTag('div', ttOpen, ClassAttr('extra-wrapper')) +
Result +
MakeTag('div', ttClose);
end;
function TInfoHTML.RoutineDesc: string;
{Provides description of snippet as valid HTML text.
@return Required HTML.
}
begin
Result := MakeSafeHTMLText(MakeSentence(Routine.Description));
end;
function TInfoHTML.RoutineList(const Routines: TRoutineList): string;
{Generates HTML of a comma separated list of snippets, where each snippet name
is a link to the snippet.
@param Routines [in] List of snippets in list.
@return HTML of snippet list or 'None' if list empty.
}
var
Routine: TRoutine; // refers to each snippet in list
begin
if Routines.Count = 0 then
// There are no snippets: say so
Result := EmptyListSentence
else
begin
// Build comma separated list of snippet links
Result := '';
for Routine in Routines do
begin
if Result <> '' then
Result := Result + ', ';
Result := Result + RoutineALink(Routine.Name, Routine.UserDefined);
end;
Result := MakeSentence(Result);
end;
end;
function TInfoHTML.SnippetKind: string;
{Provides HTML containing a description of snippet kind.
@return Required HTML.
}
begin
Result := MakeSafeHTMLText(
MakeSentence(TSnippetKindInfoList.Instance[Routine.Kind].Description)
);
end;
function TInfoHTML.SourceCode: string;
{Provides HTML containing snippet's source code, syntax highlighted in a style
suitable for display in UI.
@return Required HTML.
}
begin
Result := HiliteSource(Routine.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 Routine.Units.Count = 0 then
Result := EmptyListSentence
else
Result := MakeSafeHTMLText(JoinStr(Routine.Units, ', ', False) + '.');
end;
function TInfoHTML.XRefs: string;
{Provides list of links to snippets with which 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 := RoutineList(Routine.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.