Menu

[r3189]: / trunk / Src / USnippetPageHTML.pas  Maximize  Restore  History

Download this file

362 lines (305 with data), 10.6 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
{
* 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) 2012-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines classes etc that render different fragments of information about a
* snippet as HTML for display in the detail pane. Page content is flexible and
* user defined.
}
unit USnippetPageHTML;
interface
uses
// Project
DB.USnippet, USnippetHTML, USnippetPageStructure;
type
/// <summary>Advanced record use to render a "page" of HTML containing all
/// required fragments of a snippet.</summary>
/// <remarks>This is not strictly a complete HTML page, just the user-
/// configured fragments of page. Constant page elements are rendered
/// elsewhere.</remarks>
TSnippetPageHTML = record
public
/// <summary>Renders HTML description of given snippet, using the fragments
/// specified in preferences.</summary>
class function Render(const Snippet: TSnippet): string; static;
end;
type
/// <summary>Base class for classes that render a snippet fragment as HTML.
/// </summary>
/// <remarks>Each sub class renders a different fragment of snippet
/// information and must return it via overridden ToString method.</remarks>
TSnippetHTMLFragment = class abstract(TObject)
strict private
var
/// <summary>Value of SnippetHTML property.</summary>
fSnippetHTML: TSnippetHTML;
strict protected
/// <summary>Object that renders snippet information in HTML.</summary>
/// <remarks>For use in sub-classes.</remarks>
property SnippetHTML: TSnippetHTML read fSnippetHTML;
public
/// <summary>Constructs object to render fragment of given snippet.
/// </summary>
constructor Create(Snippet: TSnippet);
/// <summary>Destroys fragement object.</summary>
destructor Destroy; override;
end;
type
/// <summary>Factory used to instantiate objects used to generate HTML
/// fragments describing a snippet.</summary>
TSnippetHTMLFragmentFactory = record
public
/// <summary>Generates a TSnippetHTMLFragment instance used to specify a
/// specified fragment of a specified snippet.</summary>
/// <param name="FragKind">TSnippetPagePartId [in] Id of required fragment.
/// </param>
/// <param name="Snippet">TSnippet [in] Snippet for which fragment is to be
/// rendered.</param>
/// <returns>TSnippetHTMLFragment. Required renderer object.</returns>
/// <remarks>Caller is responsible for freeing returned object.</remarks>
class function Create(FragKind: TSnippetPagePartId; Snippet: TSnippet):
TSnippetHTMLFragment; static;
end;
implementation
uses
// Delphi
SysUtils,
// Project
UHTMLUtils, UPreferences;
type
TSnippetHTMLFragmentClass = class of TSnippetHTMLFragment;
type
/// <summary>Base class for classes that render HTML describing a snippet
/// fragment comprising fixed prefix text following by some snippet
/// information.</summary>
TPrefixedSnippetHTMLFragment = class abstract(TSnippetHTMLFragment)
strict protected
/// <summary>Renders required prefixed HTML.</summary>
/// <param name="Prefix">string [in] Prefix as valid HTML text.</param>
/// <param name="Id">string [in] Unique id of HTML that spans Content.
/// </param>
/// <param name="Content">string [in] Snippet dependant content, as valid
/// HTML text.</param>
/// <returns>string. Required HTML.</returns>
class function Render(const Prefix, Id, Content: string): string;
end;
type
/// <summary>Class that renders "Description" HTML fragment for a snippet.
/// </summary>
TSnippetDescHTMLFragment = class(TSnippetHTMLFragment)
public
/// <summary>Renders "Description" fragment as HTML.</summary>
function ToString: string; override;
end;
type
/// <summary>Class that renders "Source Code" HTML fragment for a snippet.
/// </summary>
TSnippetSourceCodeHTMLFragment = class(TSnippetHTMLFragment)
public
/// <summary>Renders "Source Code" fragment as HTML.</summary>
function ToString: string; override;
end;
type
/// <summary>Class that renders "Type" HTML fragment for a snippet.
/// </summary>
TSnippetKindHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
/// <summary>Renders "Type" fragment as HTML.</summary>
function ToString: string; override;
end;
type
/// <summary>Class that renders "Category" HTML fragment for a snippet.
/// </summary>
TSnippetCategoryHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
/// <summary>Renders "Category" fragment as HTML.</summary>
function ToString: string; override;
end;
type
/// <summary>Class that renders "Required Units List" HTML fragment for a
/// snippet.</summary>
TSnippetUnitsHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
/// <summary>Renders "Required Units List" fragment as HTML.</summary>
function ToString: string; override;
end;
type
/// <summary>Class that renders "Required Snippets List" HTML fragment for a
/// snippet.</summary>
TSnippetDependsHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
/// <summary>Renders "Required Snippets List" fragment as HTML.</summary>
function ToString: string; override;
end;
type
/// <summary>Class that renders "Cross Reference List" HTML fragment for a
/// snippet.</summary>
TSnippetXRefsHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
/// <summary>Renders "Cross Reference List" fragment as HTML.</summary>
function ToString: string; override;
end;
type
/// <summary>Class that renders "Compile Results Table" HTML fragment for a
/// snippet.</summary>
TSnippetCompileResultsHTMLFragment = class(TSnippetHTMLFragment)
public
/// <summary>Renders "Compile Results Table" fragment as HTML.</summary>
function ToString: string; override;
end;
type
/// <summary>Class that renders "Extra Information" HTML fragment for a
/// snippet.</summary>
TSnippetExtraHTMLFragment = class(TSnippetHTMLFragment)
public
/// <summary>Renders "Extra Information" fragment as HTML.</summary>
function ToString: string; override;
end;
{ TSnippetHTMLFragment }
constructor TSnippetHTMLFragment.Create(Snippet: TSnippet);
begin
Assert(Assigned(Snippet), ClassName + '.Create: Snippet is nil');
inherited Create;
fSnippetHTML := TSnippetHTML.Create(Snippet);
end;
destructor TSnippetHTMLFragment.Destroy;
begin
fSnippetHTML.Free;
inherited;
end;
{ TPrefixedSnippetHTMLFragment }
class function TPrefixedSnippetHTMLFragment.Render(const Prefix, Id,
Content: string): string;
begin
Result := THTML.CompoundTag(
'p',
THTML.CompoundTag('strong', Prefix) + ' ' +
THTML.CompoundTag('span', THTMLAttributes.Create('id', Id), Content)
);
end;
{ TSnippetDescHTMLFragment }
function TSnippetDescHTMLFragment.ToString: string;
begin
Result := THTML.CompoundTag(
'div', THTMLAttributes.Create('id', 'description'), SnippetHTML.Description
);
end;
{ TSnippetSourceCodeHTMLFragment }
function TSnippetSourceCodeHTMLFragment.ToString: string;
begin
Result := THTML.CompoundTag(
'div', THTMLAttributes.Create('id', 'sourcecode'), SnippetHTML.SourceCode
);
end;
{ TSnippetKindHTMLFragment }
function TSnippetKindHTMLFragment.ToString: string;
resourcestring
sPrefix = 'Snippet type:';
begin
Result := Render(sPrefix, 'kind', SnippetHTML.SnippetKind);
end;
{ TSnippetCategoryHTMLFragment }
function TSnippetCategoryHTMLFragment.ToString: string;
resourcestring
sPrefix = 'Category:';
begin
Result := Render(sPrefix, 'category', SnippetHTML.Category);
end;
{ TSnippetUnitsHTMLFragment }
function TSnippetUnitsHTMLFragment.ToString: string;
resourcestring
sPrefix = 'Required units:';
begin
Result := Render(sPrefix, 'units', SnippetHTML.Units);
end;
{ TSnippetDependsHTMLFragment }
function TSnippetDependsHTMLFragment.ToString: string;
resourcestring
sPrefix = 'Required snippets:';
begin
Result := Render(sPrefix, 'depends', SnippetHTML.Depends);
end;
{ TSnippetXRefsHTMLFragment }
function TSnippetXRefsHTMLFragment.ToString: string;
resourcestring
sPrefix = 'See also:';
begin
Result := Render(sPrefix, 'xrefs', SnippetHTML.XRefs);
end;
{ TSnippetCompileResultsHTMLFragment }
function TSnippetCompileResultsHTMLFragment.ToString: string;
begin
Result := THTML.CompoundTag(
'div',
THTMLAttributes.Create('id', 'compile-results'),
THTML.CompoundTag(
'table',
THTMLAttributes.Create(
[
THTMLAttribute.Create('class', 'comptable'),
THTMLAttribute.Create('cellspacing', '1'),
THTMLAttribute.Create('cellpadding', '4')
]
),
SnippetHTML.CompileResults
)
);
end;
{ TSnippetExtraHTMLFragment }
function TSnippetExtraHTMLFragment.ToString: string;
begin
Result := THTML.CompoundTag(
'div', THTMLAttributes.Create('id', 'extra'), SnippetHTML.Extra
);
end;
{ TSnippetHTMLFragmentFactory }
class function TSnippetHTMLFragmentFactory.Create(FragKind: TSnippetPagePartId;
Snippet: TSnippet): TSnippetHTMLFragment;
const
Map: array[TSnippetPagePartId] of TSnippetHTMLFragmentClass = (
TSnippetDescHTMLFragment, // sppDescription,
TSnippetSourceCodeHTMLFragment, // sppSourceCode,
TSnippetKindHTMLFragment, // sppKind,
TSnippetCategoryHTMLFragment, // sppCategory,
TSnippetUnitsHTMLFragment, // sppUnits,
TSnippetDependsHTMLFragment, // sppDepends,
TSnippetXRefsHTMLFragment, // sppXRefs,
TSnippetCompileResultsHTMLFragment, // sppCompileResults,
TSnippetExtraHTMLFragment // sppExtra
);
begin
Result := Map[FragKind].Create(Snippet);
end;
{ TSnippetPageHTML }
class function TSnippetPageHTML.Render(const Snippet: TSnippet):
string;
var
SB: TStringBuilder;
PageStruct: TSnippetPageStructure;
Part: TSnippetPagePart;
Fragment: TSnippetHTMLFragment;
begin
PageStruct := Preferences.PageStructures[Snippet.Kind];
SB := TStringBuilder.Create;
try
for Part in PageStruct.Parts do
begin
Fragment := TSnippetHTMLFragmentFactory.Create(Part.Id, Snippet);
try
SB.AppendLine(Fragment.ToString);
finally
Fragment.Free;
end;
end;
Result := SB.ToString;
finally
SB.Free;
end;
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.