Menu

[r2009]: / trunk / Src / Hiliter.UHiliters.pas  Maximize  Restore  History

Download this file

595 lines (537 with data), 20.2 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
{
* Hiliter.UHiliters.pas
*
* Provides highlighter classes used to format and highlight source code in
* various file formats. Contains a factory object and implementation of various
* different highlighter objects.
*
* $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 Hiliter.UHiliters.pas, formerly USyntaxHiliters.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit Hiliter.UHiliters;
interface
uses
// Project
Hiliter.UGlobals, Hiliter.UPasParser, UBaseObjects, UEncodings, UHTMLBuilder,
URTFBuilder;
type
/// <summary>
/// Static class that pmplements a syntax highlighter for Pascal source code.
/// </summary>
/// <remarks>
/// Uses a user-provided rendering object to create highlighted output in
/// various formats.
/// </remarks>
TSyntaxHiliter = class sealed(TNoPublicConstructObject)
strict private
var
/// <summary>Object used to render parsed code in required format.
/// </summary>
fRenderer: IHiliteRenderer;
/// <summary>Handles Pascal parser's OnElement event.</summary>
/// <param name="Parser">THilitePasParser [in] Reference to parser object.
/// Not used.</param>
/// <param name="Elem">THiliteElement [in] Type of element to be output.
/// </param>
/// <param name="ElemText">string [in] Name of element to be output.
/// </param>
/// <remarks>Event triggered by parser for each different source code
/// element encountered.</remarks>
procedure ElementHandler(Parser: THilitePasParser; Elem: THiliteElement;
const ElemText: string);
/// <summary>Handles Pascal parser's OnLineBegin event.</summary>
/// <param name="Parser">THilitePasParser [in] Reference to parser object.
/// Not used.</param>
/// <remarks>Called when a new line of source code is about to be started.
/// </remarks>
procedure LineBeginHandler(Parser: THilitePasParser);
/// <summary>Handles Pascal parser's OnLineEnd event.</summary>
/// <param name="Parser">THilitePasParser [in] Reference to parser object.
/// Not used.</param>
/// <remarks>Called when a new line of source code has ended.</remarks>
procedure LineEndHandler(Parser: THilitePasParser);
/// <summary>Performs syntax highlighting of given source code.</summary>
procedure DoHilite(const RawCode: string);
strict protected
/// <summary>Internal object constructor. Sets up object to perform
/// highlighting using given renderer object.</summary>
constructor InternalCreate(Renderer: IHiliteRenderer);
public
/// <summary>Syntax highlights source code in an output format specified by
/// caller.</summary>
/// <param name="RawCode">string [in] Plain text source code to be
/// highlighted.</param>
/// <param name="Renderer">IHiliteRenderer [in] Object used to format and
/// record output.</param>
/// <remarks>Output is written via renderer in some user defined way. This
/// may update an object associated with the renderer.</remarks>
class procedure Hilite(const RawCode: string; Renderer: IHiliteRenderer);
end;
type
/// <summary>
/// Pure abstract base class for classes that create complete highlighted
/// source code documents.
/// </summary>
TDocumentHiliter = class abstract(TNoConstructObject)
public
/// <summary>Creates document containing highlighted source code.</summary>
/// <param name="RawCode">string [in] Source code to be processed.</param>
/// <param name="Attrs">IHiliteAttrs [in] Specifies required highlighting
/// style. If nil document is not highlighted.</param>
/// <param name="Title">string [in] Title of document to be included in
/// document as meta data. Defaults may be used if Title not specified.
/// </param>
/// <returns>TEncodedData. Highlighted source code in format applicable to
/// output type.</returns>
/// <remarks>
/// <para>Not all document types support formatting, in which case Attrs
/// will be ignored.</para>
/// <para>Not all document types support meta data, in which case Title
/// will be ignored.</para>
/// </remarks>
class function Hilite(const RawCode: string;
Attrs: IHiliteAttrs = nil; const Title: string = ''): TEncodedData;
virtual; abstract;
end;
type
TDocumentHiliterClass = class of TDocumentHiliter;
type
/// <summary>
/// Creates a Unicode plain text source code document.
/// </summary>
TNulDocumentHiliter = class sealed(TDocumentHiliter)
public
/// <summary>Creates a plain text document containing source code.
/// </summary>
/// <param name="RawCode">string [in] Source code to be processed.</param>
/// <param name="Attrs">IHiliteAttrs [in] Required highlighting style.
/// Ignored because plain text documents do not support formatting.
/// </param>
/// <param name="Title">string [in] Title of document. Ignored because
/// plain text documents do not support meta-data.</param>
/// <returns>TEncodedData. Plain text in Unicode LE format.
/// </returns>
class function Hilite(const RawCode: string;
Attrs: IHiliteAttrs = nil; const Title: string = ''): TEncodedData;
override;
end;
type
/// <summary>
/// Creates a highlighted source code document in XHTML format.
/// </summary>
TXHTMLDocumentHiliter = class sealed(TDocumentHiliter)
strict private
/// <summary>Generates the CSS rules to be used in the document.</summary>
/// <param name="Attrs">IHiliteAttrs [in] Highlighting styles used in
/// document.</param>
/// <returns>string. CSS rules that apply styles specified in Attrs.
/// </returns>
class function GenerateCSSRules(Attrs: IHiliteAttrs): string;
public
/// <summary>Creates XHTML document containing highlighted source code.
/// </summary>
/// <param name="RawCode">string [in] Source code to be processed.</param>
/// <param name="Attrs">IHiliteAttrs [in] Specifies required highlighting
/// style. If nil document is not highlighted.</param>
/// <param name="Title">string [in] Title of document to be included in
/// document header. If empty string a default title is used.</param>
/// <returns>TEncodedData. XHTML code in UTF-8 format.</returns>
class function Hilite(const RawCode: string;
Attrs: IHiliteAttrs = nil; const Title: string = ''): TEncodedData;
override;
end;
type
/// <summary>
/// Creates a highlighted source code document in rich text format.
/// </summary>
TRTFDocumentHiliter = class sealed(TDocumentHiliter)
public
/// <summary>Creates rich text format document containing highlighted
/// source code.</summary>
/// <param name="RawCode">string [in] Source code to be processed.</param>
/// <param name="Attrs">IHiliteAttrs [in] Specifies required highlighting
/// style. If nil document is not highlighted.</param>
/// <param name="Title">string [in] Title of document to be included in
/// document header. No title written if Title is empty string.
/// </param>
/// <returns>TEncodedData. RTF code in ASCII format.</returns>
class function Hilite(const RawCode: string;
Attrs: IHiliteAttrs = nil; const Title: string = ''): TEncodedData;
override;
end;
type
/// <summary>Base class for highlighter rendering objects. Provides common
/// functionality needed by all implementations.</summary>
/// <remarks>Marked as "abstract" because this class is meaningless if
/// instantiated. It is intended only as a sub-class.</remarks>
THiliteRenderer = class abstract(TInterfacedObject)
strict private
var
/// <summary>Specifies highlighting styles to be used.</summary>
fAttrs: IHiliteAttrs;
strict protected
/// <summary>Highlighting styles to be used in rendering.</summary>
property Attrs: IHiliteAttrs read fAttrs;
public
/// <summary>Object constructor. Records a copy of highlighter style
/// attributes passed in Attrs parameter.</summary>
constructor Create(Attrs: IHiliteAttrs);
end;
type
/// <summary>
/// Renders highlighted source code in rich text format. Generated code is
/// recorded in a given rich text code builder object.
/// </summary>
/// <remarks>
/// Designed for use with TSyntaxHiliter objects.
/// </remarks>
TRTFHiliteRenderer = class(THiliteRenderer, IHiliteRenderer)
strict private
var
/// <summary>Object used to record generated RTF code.</summary>
fBuilder: TRTFBuilder;
public
/// <summary>Object constructor. Sets up object to render documents.
/// </summary>
/// <param name="Builder">TRTFBuilder [in] Object that receives generated
/// RTF code.</param>
/// <param name="Attrs">IHiliteAttrs [in] Specifies required highlighting
/// style. If nil document is not highlighted.</param>
constructor Create(const Builder: TRTFBuilder;
const Attrs: IHiliteAttrs = nil);
/// <summary>Initialises RTF ready to receive highlighted code.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure Initialise;
/// <summary>Tidies up RTF after all highlighted code processed.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure Finalise;
/// <summary>Resets styles ready for a new line (paragraph) of highlighted
/// code.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure BeginLine;
/// <summary>Closes current RTF paragraph.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure EndLine;
/// <summary>Sets any highlighting style required for following source code
/// element as specified by Elem.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure BeforeElem(Elem: THiliteElement);
/// <summary>Writes given source code element text.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure WriteElemText(const Text: string);
/// <summary>Switches off any highlighting styles used for source code
/// element specified by Elem.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure AfterElem(Elem: THiliteElement);
end;
type
/// <summary>
/// Renders highlighted source code in XHTML format. Generated code is
/// recorded in a given HTML code builder object.
/// </summary>
/// <remarks>
/// Designed for use with TSyntaxHiliter objects.
/// </remarks>
THTMLHiliteRenderer = class(THiliteRenderer, IHiliteRenderer)
strict private
var
/// <summary>Object used to record generated XHTML code.</summary>
fBuilder: THTMLBuilder;
/// <summary>Flag indicating if writing first line of output.</summary>
fIsFirstLine: Boolean;
public
/// <summary>Object constructor. Sets up object to render documents.
/// </summary>
/// <param name="Builder">THTMLBuilder [in] Object that receives generated
/// XHTML code.</param>
/// <param name="Attrs">IHiliteAttrs [in] Specifies required highlighting
/// style. If nil document is not highlighted.</param>
constructor Create(const Builder: THTMLBuilder;
const Attrs: IHiliteAttrs = nil);
/// <summary>Initialises XHTML ready to receive highlighted code.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure Initialise;
/// <summary>Tidies up XHTML after all highlighted code processed.
/// </summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure Finalise;
/// <summary>Emits new line if necessary.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure BeginLine;
/// <summary>Does nothing.</summary>
/// <remarks>
/// <para>Handling of new lines is all done by BeginLine.</para>
/// <para>Method of IHiliteRenderer.</para>
/// </remarks>
procedure EndLine;
/// <summary>Emits any span tag required to style following source code
/// element as specified by Elem.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure BeforeElem(Elem: THiliteElement);
/// <summary>Writes given source code element text.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure WriteElemText(const Text: string);
/// <summary>Closes any span tag used to style source code element
/// specified by Elem.</summary>
/// <remarks>Method of IHiliteRenderer.</remarks>
procedure AfterElem(Elem: THiliteElement);
end;
implementation
uses
// Delphi
SysUtils, Graphics,
// Project
Hiliter.UAttrs, Hiliter.UCSS, IntfCommon, UCSSBuilder, URTFStyles;
{ TSyntaxHiliter }
procedure TSyntaxHiliter.DoHilite(const RawCode: string);
var
Parser: THilitePasParser; // object used to parse source code
begin
// Set up parser
Parser := THilitePasParser.Create;
try
Parser.OnElement := ElementHandler;
Parser.OnLineBegin := LineBeginHandler;
Parser.OnLineEnd := LineEndHandler;
// Parse the document:
fRenderer.Initialise;
Parser.Parse(RawCode);
fRenderer.Finalise;
finally
Parser.Free;
end;
end;
procedure TSyntaxHiliter.ElementHandler(Parser: THilitePasParser;
Elem: THiliteElement; const ElemText: string);
begin
fRenderer.BeforeElem(Elem);
fRenderer.WriteElemText(ElemText);
fRenderer.AfterElem(Elem);
end;
class procedure TSyntaxHiliter.Hilite(const RawCode: string;
Renderer: IHiliteRenderer);
begin
Assert(Assigned(Renderer), ClassName + '.Create: Renderer is nil');
with InternalCreate(Renderer) do
try
DoHilite(RawCode);
finally
Free;
end;
end;
procedure TSyntaxHiliter.LineBeginHandler(Parser: THilitePasParser);
begin
fRenderer.BeginLine;
end;
procedure TSyntaxHiliter.LineEndHandler(Parser: THilitePasParser);
begin
fRenderer.EndLine;
end;
constructor TSyntaxHiliter.InternalCreate(Renderer: IHiliteRenderer);
begin
inherited InternalCreate;
fRenderer := Renderer;
end;
{ TNulDocumentHiliter }
class function TNulDocumentHiliter.Hilite(const RawCode: string;
Attrs: IHiliteAttrs; const Title: string): TEncodedData;
begin
Result := TEncodedData.Create(RawCode, etUnicode);
end;
{ TXHTMLDocumentHiliter }
class function TXHTMLDocumentHiliter.GenerateCSSRules(Attrs: IHiliteAttrs):
string;
var
CSSBuilder: TCSSBuilder; // builds CSS code
HiliterCSS: THiliterCSS; // generates CSS names and properties for hiliter
begin
if not Assigned(Attrs) then
Attrs := THiliteAttrsFactory.CreateNulAttrs;
HiliterCSS := THiliterCSS.Create(Attrs);
try
CSSBuilder := TCSSBuilder.Create;
try
HiliterCSS.BuildCSS(CSSBuilder);
Result := CSSBuilder.AsString;
finally
CSSBuilder.Free;
end;
finally
HiliterCSS.Free;
end;
end;
class function TXHTMLDocumentHiliter.Hilite(const RawCode: string;
Attrs: IHiliteAttrs; const Title: string): TEncodedData;
resourcestring
// Default document title
sDefaultTitle = 'DelphiDabbler CodeSnip Database';
var
Renderer: IHiliteRenderer; // XHTML renderer object
Builder: THTMLBuilder; // object used to construct XHTML document
begin
Builder := THTMLBuilder.Create;
try
if Title <> '' then
Builder.Title := Title
else
Builder.Title := sDefaultTitle;
Builder.CSS := GenerateCSSRules(Attrs);
Renderer := THTMLHiliteRenderer.Create(Builder, Attrs);
TSyntaxHiliter.Hilite(RawCode, Renderer);
Result := TEncodedData.Create(Builder.HTMLDocument, etUTF8);
finally
Builder.Free;
end;
end;
{ TRTFDocumentHiliter }
class function TRTFDocumentHiliter.Hilite(const RawCode: string;
Attrs: IHiliteAttrs; const Title: string): TEncodedData;
var
Renderer: IHiliteRenderer; // RTF renderer object
Builder: TRTFBuilder; // object used to construct RTF document
begin
Builder := TRTFBuilder.Create(0);
try
Builder.DocProperties.Title := Title;
Renderer := TRTFHiliteRenderer.Create(Builder, Attrs);
TSyntaxHiliter.Hilite(RawCode, Renderer);
Result := TEncodedData.Create(Builder.Render.ToBytes, etASCII);
finally
Builder.Free;
end;
end;
{ THiliteRenderer }
constructor THiliteRenderer.Create(Attrs: IHiliteAttrs);
begin
inherited Create;
fAttrs := THiliteAttrsFactory.CreateNulAttrs;
(fAttrs as IAssignable).Assign(Attrs);
end;
{ TRTFHiliteRenderer }
procedure TRTFHiliteRenderer.AfterElem(Elem: THiliteElement);
begin
if not Attrs[Elem].IsNul then
fBuilder.EndGroup;
end;
procedure TRTFHiliteRenderer.BeforeElem(Elem: THiliteElement);
begin
if not Attrs[Elem].IsNul then
begin
fBuilder.BeginGroup;
if Attrs[Elem].ForeColor <> clNone then
fBuilder.SetColour(Attrs[Elem].ForeColor);
if Attrs[Elem].FontStyle <> [] then
fBuilder.SetFontStyle(Attrs[Elem].FontStyle);
end;
end;
procedure TRTFHiliteRenderer.BeginLine;
begin
fBuilder.ResetCharStyle;
fBuilder.SetFont(Attrs.FontName);
fBuilder.SetFontSize(Attrs.FontSize);
end;
constructor TRTFHiliteRenderer.Create(const Builder: TRTFBuilder;
const Attrs: IHiliteAttrs = nil);
begin
Assert(Assigned(Builder), ClassName + '.Create: Builder is nil');
inherited Create(Attrs);
fBuilder := Builder;
end;
procedure TRTFHiliteRenderer.EndLine;
begin
fBuilder.EndPara;
end;
procedure TRTFHiliteRenderer.Finalise;
begin
// End group containing all source code
fBuilder.EndGroup;
end;
procedure TRTFHiliteRenderer.Initialise;
var
Elem: THiliteElement; // loops thru all highlight elements
begin
// Set up font table
fBuilder.FontTable.Add(Attrs.FontName, rgfModern, 0);
// Set up colour table
for Elem := Low(THiliteElement) to High(THiliteElement) do
fBuilder.ColourTable.Add(Attrs[Elem].ForeColor);
// Begin group containing all source code
fBuilder.BeginGroup;
// Clear any paragraph formatting
fBuilder.ClearParaFormatting;
end;
procedure TRTFHiliteRenderer.WriteElemText(const Text: string);
begin
fBuilder.AddText(Text);
end;
{ THTMLHiliteRenderer }
procedure THTMLHiliteRenderer.AfterElem(Elem: THiliteElement);
begin
if not Attrs[Elem].IsNul then
fBuilder.CloseSpan;
end;
procedure THTMLHiliteRenderer.BeforeElem(Elem: THiliteElement);
begin
if not Attrs.Elements[Elem].IsNul then
fBuilder.OpenSpan(THiliterCSS.GetElemCSSClassName(Elem));
end;
procedure THTMLHiliteRenderer.BeginLine;
begin
// Note we don't emit CRLF before first line since it must be on same line as
// <pre> tag that will have been written.
if fIsFirstLine then
fIsFirstLine := False
else
fBuilder.NewLine;
end;
constructor THTMLHiliteRenderer.Create(const Builder: THTMLBuilder;
const Attrs: IHiliteAttrs);
begin
Assert(Assigned(Builder), ClassName + '.Create: Builder is nil');
inherited Create(Attrs);
fBuilder := Builder;
end;
procedure THTMLHiliteRenderer.EndLine;
begin
// Do nothing: we use BeginLine() to start new lines in preformatted text
end;
procedure THTMLHiliteRenderer.Finalise;
begin
fBuilder.ClosePre;
end;
procedure THTMLHiliteRenderer.Initialise;
begin
// Note that we are about to write first line
fIsFirstLine := True;
fBuilder.OpenPre(THiliterCSS.GetMainCSSClassName);
end;
procedure THTMLHiliteRenderer.WriteElemText(const Text: string);
begin
fBuilder.AddText(Text);
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.