Menu

[r1985]: / trunk / Src / USnippetHTML.pas  Maximize  Restore  History

Download this file

234 lines (203 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
{
* USnippetHTML.pas
*
* Classes that generates HTML used to display snippets in detail pane.
*
* $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 USnippetHTML.pas, formerly 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-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit USnippetHTML;
interface
uses
// Project
ActiveText.UMain, DB.USnippet;
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: TSnippet;
/// <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(const Snippets: TSnippetList): 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;
public
/// <summary>Object constructor. Sets up object to provide HTML for given
/// snippet.</summary>
constructor Create(const Snippet: TSnippet);
/// <summary>Returns snippet name as HTML.</summary>
function SnippetName: 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 link to category containing snippet.
/// </summary>
function Category: string;
/// <summary>Highlights snippet's source code and returns it as HTML.
/// </summary>
function SourceCode: 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
/// Extra property.</summary>
function Extra: string;
/// <summary>Returns HTML containing rows of a table representing snippet's
/// compilation results for each supported compiler.</summary>
function CompileResults: string;
end;
implementation
uses
// Project
ActiveText.UHTMLRenderer, DB.UCategory, DB.UMain, DB.USnippetKind,
Hiliter.UAttrs, Hiliter.UGlobals, Hiliter.UHiliters, UCompResHTML,
UHTMLBuilder, UHTMLDetailUtils, UHTMLUtils, UStrUtils;
{ TSnippetHTML }
function TSnippetHTML.Category: string;
var
Cat: TCategory; // category that snippet belongs to
begin
Cat := Database.Categories.Find(fSnippet.Category);
Assert(Assigned(Cat), ClassName + '.Category: Category not found');
Result := StrMakeSentence(
CategoryALink(Cat.ID, Cat.Description)
);
end;
function TSnippetHTML.CompileResults: string;
begin
Result := TCompResHTML.TableRows(fSnippet.Compatibility);
end;
constructor TSnippetHTML.Create(const Snippet: TSnippet);
begin
inherited Create;
fSnippet := Snippet;
end;
function TSnippetHTML.Depends: string;
begin
Result := SnippetList(fSnippet.Depends);
end;
function TSnippetHTML.Description: string;
begin
Result := RenderActiveText(fSnippet.Description);
end;
function TSnippetHTML.EmptyListSentence: string;
resourcestring
sEmpty = 'None';
begin
Result := MakeSafeHTMLText(StrMakeSentence(sEmpty));
end;
function TSnippetHTML.Extra: string;
begin
Result := RenderActiveText(fSnippet.Extra);
end;
function TSnippetHTML.RenderActiveText(ActiveText: IActiveText): string;
var
Renderer: TActiveTextHTML;
begin
Renderer := TActiveTextHTML.Create;
try
Result := Renderer.Render(ActiveText);
finally
Renderer.Free;
end;
end;
function TSnippetHTML.SnippetList(const Snippets: TSnippetList): string;
var
Snippet: TSnippet; // refers to each snippet in list
begin
if Snippets.IsEmpty 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 + SnippetALink(Snippet.Name, Snippet.UserDefined);
end;
Result := StrMakeSentence(Result);
end;
end;
function TSnippetHTML.SnippetName: string;
begin
Result := MakeSafeHTMLText(fSnippet.Name);
end;
function TSnippetHTML.SnippetKind: string;
begin
Result := MakeSafeHTMLText(
StrMakeSentence(TSnippetKindInfoList.Items[fSnippet.Kind].DisplayName)
);
end;
function TSnippetHTML.SourceCode: string;
var
Builder: THTMLBuilder; // object that assembles HTML
Renderer: IHiliteRenderer; // object that renders highlighted code as HTML
begin
Builder := THTMLBuilder.Create;
try
Renderer := THTMLHiliteRenderer.Create(
Builder, THiliteAttrsFactory.CreateDisplayAttrs
);
TSyntaxHiliter.Hilite(fSnippet.SourceCode, Renderer);
Result := Builder.HTMLFragment;
finally
Builder.Free;
end;
end;
function TSnippetHTML.Units: string;
begin
if fSnippet.Units.Count = 0 then
Result := EmptyListSentence
else
Result := MakeSafeHTMLText(StrJoin(fSnippet.Units, ', ', False) + '.');
end;
function TSnippetHTML.XRefs: string;
begin
Result := SnippetList(fSnippet.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.