Menu

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

Download this file

287 lines (235 with data), 6.7 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
{
* 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, 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;
// TODO: Rationalise this page with USnippetHTML and UDetailPageHTML
interface
uses
DB.USnippet, USnippetHTML, USnippetPageStructure;
type
TSnippetPageHTML = record
public
class function Render(const Snippet: TSnippet): string; static;
end;
type
TSnippetHTMLFragment = class abstract(TObject)
strict private
var
fSnippetHTML: TSnippetHTML;
strict protected
property SnippetHTML: TSnippetHTML read fSnippetHTML;
public
constructor Create(Snippet: TSnippet);
destructor Destroy; override;
end;
type
TSnippetHTMLFragmentFactory = record
public
class function Create(FragKind: TSnippetPagePartId; Snippet: TSnippet):
TSnippetHTMLFragment; static;
end;
implementation
uses
SysUtils,
UHTMLUtils, UPreferences;
type
TSnippetHTMLFragmentClass = class of TSnippetHTMLFragment;
type
TPrefixedSnippetHTMLFragment = class abstract(TSnippetHTMLFragment)
strict protected
class function Render(const Prefix, Id, Content: string): string;
end;
type
TSnippetDescHTMLFragment = class(TSnippetHTMLFragment)
public
function ToString: string; override;
end;
type
TSnippetSourceCodeHTMLFragment = class(TSnippetHTMLFragment)
public
function ToString: string; override;
end;
type
TSnippetKindHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
function ToString: string; override;
end;
type
TSnippetCategoryHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
function ToString: string; override;
end;
type
TSnippetUnitsHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
function ToString: string; override;
end;
type
TSnippetDependsHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
function ToString: string; override;
end;
type
TSnippetXRefsHTMLFragment = class(TPrefixedSnippetHTMLFragment)
public
function ToString: string; override;
end;
type
TSnippetCompileResultsHTMLFragment = class(TSnippetHTMLFragment)
public
function ToString: string; override;
end;
type
TSnippetExtraHTMLFragment = class(TSnippetHTMLFragment)
public
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 := MakeCompoundTag(
'p',
MakeCompoundTag('strong', Prefix) + ' ' +
MakeCompoundTag('span', THTMLAttributes.Create('id', Id), Content)
);
end;
{ TSnippetDescHTMLFragment }
function TSnippetDescHTMLFragment.ToString: string;
begin
Result := MakeCompoundTag(
'div', THTMLAttributes.Create('id', 'description'), SnippetHTML.Description
);
end;
{ TSnippetSourceCodeHTMLFragment }
function TSnippetSourceCodeHTMLFragment.ToString: string;
begin
Result := MakeCompoundTag(
'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 := MakeCompoundTag(
'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 := MakeCompoundTag(
'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.