Menu

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

Download this file

406 lines (365 with data), 12.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
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
{
* 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) 2006-2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Classes that generates HTML used to display snippets in detail pane.
}
unit USnippetHTML;
interface
uses
// Project
CS.ActiveText,
CS.Database.Types;
type
/// <summary>
/// Provides HTML used to display snippet details in information pane.
/// </summary>
TSnippetHTML = class sealed(TObject)
strict private
var
/// <summary>Reference to snippet for which HTML is being generated.
/// </summary>
fSnippet: ISnippet;
/// <summary>Generates HTML of a comma separated list of snippets, where
/// each snippet name is a link to the snippet.</summary>
/// <param name="Snippets">TSnippetList [in] List of snippets.</param>
/// <returns>string. HTML of snippet list or 'None' if list empty.
/// </returns>
function SnippetList(SnippetIDs: ISnippetIDList): string;
/// <summary>Returns HTML text indicating a list is empty.</summary>
function EmptyListSentence: string;
/// <summary>Renders given active text as HTML and returns it.</summary>
function RenderActiveText(ActiveText: IActiveText): string;
/// <summary>Returns HTML of link to given snippet.</summary>
class function SnippetALink(Snippet: ISnippet): string; overload;
/// <summary>Returns HTML of a link that performs its action using
/// JavaScript.</summary>
/// <param name="JSFn">string [in] Javascript function to be called when
/// link is clicked.</param>
/// <param name="CSSClass">string [in] CSS class of link.</param>
/// <param name="Text">string [in] HTML to be displayed in link. Must be
/// valid HTML.</param>
/// <returns>string. Required HTML.</returns>
class function JSALink(const JSFn, CSSClass, HTML: string): string;
public
/// <summary>Object constructor. Sets up object to provide HTML for given
/// snippet.</summary>
constructor Create(Snippet: ISnippet);
/// <summary>Returns snippet title as HTML.</summary>
function SnippetTitle: string;
/// <summary>Returns snippet description as HTML.</summary>
function Description: string;
/// <summary>Returns description of snippet's kind as HTML.</summary>
function SnippetKind: string;
/// <summary>Returns HTML of a list of links to the snippet's associated
/// tags, or a message if list is empty.</summary>
function Tags: string;
/// <summary>Highlights snippet's source code and returns it as HTML.
/// </summary>
function SourceCode: string;
/// <summary>Returns snippet's programming language as HTML.</summary>
function Language: string;
/// <summary>Returns HTML of a comma separated list of links to snippets on
/// which the snippet depends, or a message if list is empty.</summary>
function Depends: string;
/// <summary>Returns HTML of a comma separated list of links to snippet's
/// cross-referenced snippets, or a message if list is empty.</summary>
function XRefs: string;
/// <summary>Returns HTML of a comma separated list of units required by
/// snippet, or a messafe if list is empty.</summary>
function Units: string;
/// <summary>Returns HTML representation of active text from snippet's
/// Notes property.</summary>
function Notes: string;
/// <summary>Returns HTML containing rows of a table representing snippet's
/// compilation results for each supported compiler.</summary>
function CompileResults: string;
/// <summary>Returns HTML of link to snippet.</summary>
function SnippetALink: string; overload;
/// <summary>Returns an image tag referencing the image used to display the
/// snippet's test information.</summary>
function TestingImage: string;
/// <summary>Returns an image referencing the image used to show whether
/// the snippet is starred or not wrapped in a JavaScript link that toggles
/// the starred state.</summary>
function Star: string;
end;
implementation
uses
// Delphi
SysUtils,
// Project
CS.ActiveText.Renderers.HTML,
CS.Config,
CS.SourceCode.Languages,
CS.SourceCode.Hiliter.Brushes,
CS.SourceCode.Hiliter.Renderers,
DB.UMain,
UCompResHTML,
UHTMLBuilder,
UHTMLUtils,
UIStringList,
UJavaScriptUtils,
UPreferences,
UResourceUtils,
UStrUtils;
{ TSnippetHTML }
function TSnippetHTML.CompileResults: string;
begin
Result := TCompResHTML.TableRows(fSnippet.CompileResults);
end;
constructor TSnippetHTML.Create(Snippet: ISnippet);
begin
inherited Create;
fSnippet := Snippet;
end;
function TSnippetHTML.Depends: string;
begin
Result := SnippetList(fSnippet.RequiredSnippets);
end;
function TSnippetHTML.Description: string;
begin
Result := RenderActiveText(fSnippet.Description);
end;
function TSnippetHTML.EmptyListSentence: string;
resourcestring
sEmpty = 'None';
begin
Result := THTML.Entities(StrMakeSentence(sEmpty));
end;
class function TSnippetHTML.JSALink(const JSFn, CSSClass, HTML: string):
string;
var
Attrs: IHTMLAttributes;
begin
Attrs := THTMLAttributes.Create([
THTMLAttribute.Create('href', '#'),
THTMLAttribute.Create('onclick', JSFn + '; return false;'),
THTMLAttribute.Create('class', CSSClass)
]);
Result := THTML.CompoundTag('a', Attrs, HTML);
end;
function TSnippetHTML.Language: string;
begin
Result := JSALink(
TJavaScript.LiteralFunc('displayLanguage', [fSnippet.LanguageID.ToString]),
'language-link',
THTML.Entities(
TConfig.Instance.SourceCodeLanguages[fSnippet.LanguageID].FriendlyName
)
);
Result := StrMakeSentence(Result);
end;
function TSnippetHTML.Notes: string;
begin
Result := RenderActiveText(fSnippet.Notes);
end;
function TSnippetHTML.RenderActiveText(ActiveText: IActiveText): string;
begin
Result := TActiveTextHTMLRenderer.Render(ActiveText);
end;
function TSnippetHTML.SnippetList(SnippetIDs: ISnippetIDList): string;
var
SnippetID: TSnippetID;
begin
if SnippetIDs.IsEmpty then
// There are no snippets: say so
Result := EmptyListSentence
else
begin
// Build comma separated list of snippet links
Result := '';
for SnippetID in SnippetIDs do
begin
if Result <> '' then
Result := Result + ', ';
Result := Result + SnippetALink(Database.LookupSnippet(SnippetID));
end;
Result := StrMakeSentence(Result);
end;
end;
function TSnippetHTML.SnippetTitle: string;
begin
Result := THTML.Entities(fSnippet.Title);
end;
class function TSnippetHTML.SnippetALink(Snippet: ISnippet): string;
begin
// Create javascript link enclosing snippet name
Result := JSALink(
TJavaScript.LiteralFunc('displaySnippet', [Snippet.ID.ToString]),
'snippet-link',
THTML.Entities(Snippet.Title)
);
end;
function TSnippetHTML.SnippetALink: string;
begin
Result := SnippetALink(fSnippet);
end;
function TSnippetHTML.SnippetKind: string;
begin
Result := THTML.Entities(
StrMakeSentence(Database.GetAllSnippetKinds[fSnippet.KindID].DisplayName)
);
end;
function TSnippetHTML.SourceCode: string;
var
Builder: THTMLBuilder; // object that assembles HTML
Renderer: IHiliteRenderer; // object that renders highlighted code as HTML
Lang: TSourceCodeLanguage; // source code language
Brush: TSyntaxHiliterBrush; // brush used to perform highlighting
begin
Lang := TConfig.Instance.SourceCodeLanguages[fSnippet.LanguageID];
Builder := THTMLBuilder.Create;
try
Brush := TSyntaxHiliterBrushes.CreateBrush(Lang.HiliterBrushID);
try
Renderer := THTMLHiliteRenderer.Create(
Builder,
Brush,
TConfig.Instance.HiliterThemes[Preferences.CurrentHiliteThemeIds[htkUI]]
);
TSyntaxHiliter.Hilite(
fSnippet.SourceCode,
Brush,
Renderer
);
finally
Brush.Free;
end;
Result := Builder.HTMLFragment;
finally
Builder.Free;
end;
end;
function TSnippetHTML.Star: string;
// Return the image tag that references the image used to indicate the
// snippet's starred state
function StarredImage: string;
resourcestring
sStarred = 'Starred.'#10'Click to un-star.';
sUnstarred = 'Not starred'#10'Click to star.';
const
ImgWidth = 16;
ImgHeight = 16;
ImgSrcs: array[Boolean] of record
ResName: string; // name of image resource
Title: string; // value of image tag title attribute
end =(
(ResName: 'unstarred-snippet.png'; Title: sUnstarred),
(ResName: 'starred-snippet.png'; Title: sStarred)
);
var
Attrs: IHTMLAttributes; // image's attributes
begin
Attrs := THTMLAttributes.Create;
Attrs.Add('src', MakeResourceURL(ImgSrcs[fSnippet.Starred].ResName));
Attrs.Add('title', THTML.Entities(ImgSrcs[fSnippet.Starred].Title));
Attrs.Add('class', 'testing-img');
Result := THTML.SimpleTag('img', Attrs);
end;
begin
Result := JSALink(
TJavaScript.LiteralFunc(
'changeSnippetStar',
[fSnippet.ID.ToString, not fSnippet.Starred]
),
'command-link',
StarredImage
);
end;
function TSnippetHTML.Tags: string;
// Creates HTML for a single tag.
function TagHTML(const Tag: TTag): string;
var
DisplayTagLink: string;
RemoveTagLink: string;
const
CloseGlyph = '&times;';
begin
DisplayTagLink := JSALink(
TJavaScript.LiteralFunc('displayTag', [Tag.ToString]),
'tag-link',
THTML.Entities(Tag.ToString)
);
RemoveTagLink := JSALink(
TJavaScript.LiteralFunc(
'removeTag', [fSnippet.ID.ToString, Tag.ToString]
),
'remove-tag command-link',
THTML.CompoundTag(
'span', THTMLAttributes.Create('class', 'close-tab-glyph'), CloseGlyph
)
);
Result := THTML.CompoundTag(
'span',
THTMLAttributes.Create('class', 'tag no-link-decoration'),
DisplayTagLink + RemoveTagLink
);
end;
resourcestring
sImgTitle = 'Tags';
var
Tag: TTag;
ImgHTML: string;
ImgAttrs: IHTMLAttributes;
TagsHTML: string;
begin
ImgAttrs := THTMLAttributes.Create;
ImgAttrs.Add('src', MakeResourceURL('tags.png'));
ImgAttrs.Add('title', THTML.Entities(sImgTitle));
ImgAttrs.Add('class', 'tags-img');
ImgHTML := THTML.SimpleTag('img', ImgAttrs);
if fSnippet.Tags.IsEmpty then
Exit(ImgHTML + EmptyListSentence);
TagsHTML := '';
for Tag in fSnippet.Tags do
begin
if TagsHTML <> '' then
TagsHTML := TagsHTML + ' ';
TagsHTML := TagsHTML + TagHTML(Tag);
end;
Result := ImgHTML + TagsHTML;
end;
function TSnippetHTML.TestingImage: string;
resourcestring
sTestingNone = 'Untested'#13#10'Use with care';
sTestingBasic = 'Passed simple tests';
sTestingAdvanced = 'Passed advanced / unit testing';
const
ImgWidth = 16;
ImgHeight = 16;
ImgSrcs: array[TSnippetTestInfo] of record
ResName: string; // name of image resource
Title: string; // value of image tag title attribute
end =(
(ResName: 'testing-none.png'; Title: sTestingNone),
(ResName: 'testing-basic.png'; Title: sTestingBasic),
(ResName: 'testing-advanced.png'; Title: sTestingAdvanced)
);
var
Attrs: IHTMLAttributes; // image's attributes
begin
Attrs := THTMLAttributes.Create;
Attrs.Add('src', MakeResourceURL(ImgSrcs[fSnippet.TestInfo].ResName));
Attrs.Add('title', THTML.Entities(ImgSrcs[fSnippet.TestInfo].Title));
Attrs.Add('class', 'testing-img');
Result := THTML.SimpleTag('img', Attrs);
end;
function TSnippetHTML.Units: string;
begin
if fSnippet.RequiredModules.IsEmpty then
Result := EmptyListSentence
else
Result := THTML.Entities(fSnippet.RequiredModules.GetText(', ', False));
end;
function TSnippetHTML.XRefs: string;
begin
Result := SnippetList(fSnippet.XRefs);
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.