Menu

[r4761]: / branches / parsnip / Src / Main / CS.SourceCode.Hiliter.Renderers.pas  Maximize  Restore  History

Download this file

715 lines (651 with data), 25.9 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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
{
* 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) 2005-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* 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.
}
unit CS.SourceCode.Hiliter.Renderers;
interface
uses
// Delphi
Generics.Collections,
// Project
CS.SourceCode.Hiliter.Brushes,
CS.SourceCode.Hiliter.Parser,
CS.SourceCode.Hiliter.Themes,
UBaseObjects,
UEncodings,
UHTMLBuilder,
URTFBuilder,
USourceFileInfo;
// TODO: fix documentation comments re changes
type
/// <summary>Interface implemented by objects that format different source
/// code elements on behalf of syntax highlighter.</summary>
/// <remarks>Implement this interface for each required output format.
/// Syntax highlighter calls the methods of this interface.</remarks>
IHiliteRenderer = interface(IInterface)
['{17816D15-0565-449A-AFE3-D3CD74C3C6FC}']
/// <summary>Called by syntax highlighter before any source code is
/// processed.</summary>
procedure Initialise;
/// <summary>Called by syntax highlighter after all source code has been
/// processed.</summary>
procedure Finalise;
/// <summary>Called by syntax highlighter when a new line of source code
/// is started.</summary>
procedure BeginLine;
/// <summary>Called by syntax highlighter after a line of souce code is
/// complete.</summary>
procedure EndLine;
/// <summary>Called by syntax highlighter just before a source code
/// element is to be output.</summary>
/// <param name="Elem">THiliteElement [in] Type of element to be output.
/// </param>
procedure BeforeElem(const ElemInfo: TSyntaxHiliteElemInfo);
// TODO: revise comment for BeforeElem
/// <summary>Called by syntax highlighter for each element of source code
/// read. All the given text should be formatted in same style.</summary>
/// <remarks>Type of the element will have been specified in prior call to
/// BeforeElem.</remarks>
procedure WriteElemText(const Text: string);
/// <summary>Called by syntax highlighter just after an element of source
/// code has been written.</summary>
/// <param name="Elem">THiliteElement [in] Type of element that has just
/// been output.</param>
procedure AfterElem(const ElemInfo: TSyntaxHiliteElemInfo);
// TODO: revise comment for AfterElem
end;
type
/// <summary>
/// Static class that implements 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: TObject;
const ElemInfo: TSyntaxHiliteElemInfo; 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: TObject);
/// <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: TObject);
/// <summary>Performs syntax highlighting of given source code.</summary>
procedure DoHilite(const RawCode: string; const Brush: TSyntaxHiliterBrush);
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;
const Brush: TSyntaxHiliterBrush; 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="Brush">TSyntaxHiliterBrush [in] Specifies highlighter
/// brush to be used to perform highlighting.</param>
/// <param name="Theme">TSyntaxHiliteTheme [in] Specifies highlighter
/// styling to be used by highlighter brush.</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 Brush
/// and Theme 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;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme;
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="Brush">TSyntaxHiliterBrush [in] Specifies highlighter
/// brush to be used to perform highlighting. Ignored because plain text
/// documents do not support syntax highlighting.</param>
/// <param name="Theme">TSyntaxHiliteTheme [in] Specifies highlighter
/// styling to be used by highlighter brush. Ignored because plain text
/// documents do not support syntax highlighting.</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;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme;
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="Brush">TSyntaxHiliterBrush [in] Highlighter brush to be
/// used to perform highlighting.</param>
/// <param name="Theme">TSyntaxHiliteTheme [in] Styling to be used by
/// highlighter brush.</param>
/// <returns>string. CSS rules that apply styles specified in Attrs.
/// </returns>
class function GenerateCSSRules(const Brush: TSyntaxHiliterBrush;
const Theme: TSyntaxHiliteTheme): string;
public
/// <summary>Creates XHTML document containing highlighted source code.
/// </summary>
/// <param name="RawCode">string [in] Source code to be processed.</param>
/// <param name="Brush">TSyntaxHiliterBrush [in] Specifies highlighter
/// brush to be used to perform highlighting.</param>
/// <param name="Theme">TSyntaxHiliteTheme [in] Specifies highlighter
/// styling to be used by highlighter brush.</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;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme;
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="Brush">TSyntaxHiliterBrush [in] Specifies highlighter
/// brush to be used to perform highlighting.</param>
/// <param name="Theme">TSyntaxHiliteTheme [in] Specifies highlighter
/// styling to be used by highlighter brush.</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;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme;
const Title: string = ''): TEncodedData; override;
end;
type
/// <summary>Container for methods to help with highlighting source code for
/// various different output file types.</summary>
TDocumentHiliterHelper = record
public
/// <summary>Checks if syntax highlighting is supported for the given
/// output file type.</summary>
class function IsHilitingSupported(const FileType: TSourceOutputFileType):
Boolean; static;
/// <summary>Returns the highlighter class to be used to syntax highlight
/// source code for the given output file type.</summary>
/// <remarks>A null highlighter will be returned if syntax highlighting is
/// not supported for the given file type.</remarks>
class function GetHiliterClass(const FileType: TSourceOutputFileType):
TDocumentHiliterClass; static;
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
fTheme: TSyntaxHiliteTheme;
fBrush: TSyntaxHiliterBrush;
strict protected
/// <summary>Highlighting styles to be used in rendering.</summary>
property Theme: TSyntaxHiliteTheme read fTheme;
property Brush: TSyntaxHiliterBrush read fBrush;
public
/// <summary>Object constructor. Records a copy of highlighter style
/// attributes passed in Attrs parameter.</summary>
constructor Create(const Brush: TSyntaxHiliterBrush;
const Theme: TSyntaxHiliteTheme);
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;
function IsEmptyGroup(const Style: TSyntaxHiliteAttrStyle): Boolean;
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="Brush">TSyntaxHiliterBrush [in] Highlighter brush to be
/// used to perform highlighting.</param>
/// <param name="Theme">TSyntaxHiliteTheme [in] Styling to be used by
/// highlighter brush.</param>
constructor Create(const Builder: TRTFBuilder;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme);
/// <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(const ElemInfo: TSyntaxHiliteElemInfo);
// TODO: revise comment for BeforeElem
/// <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(const ElemInfo: TSyntaxHiliteElemInfo);
// TODO: revise comment for AfterElem
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="Brush">TSyntaxHiliterBrush [in] Highlighter brush to be
/// used to perform highlighting.</param>
/// <param name="Theme">TSyntaxHiliteTheme [in] Styling to be used by
/// highlighter brush.</param>
constructor Create(const Builder: THTMLBuilder;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme);
/// <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(const ElemInfo: TSyntaxHiliteElemInfo);
/// <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(const ElemInfo: TSyntaxHiliteElemInfo);
end;
implementation
uses
// Delphi
SysUtils, Graphics,
// Project
CS.SourceCode.Hiliter.Renderers.CSS,
IntfCommon,
UComparers,
UCSSBuilder,
URTFStyles;
{ TSyntaxHiliter }
procedure TSyntaxHiliter.DoHilite(const RawCode: string;
const Brush: TSyntaxHiliterBrush);
var
Parser: TSyntaxHiliteParser; // object used to parse source code
begin
// Set up parser
Parser := TSyntaxHiliteParser.Create;
try
Parser.OnElement := ElementHandler;
Parser.OnLineBegin := LineBeginHandler;
Parser.OnLineEnd := LineEndHandler;
// Parse the document:
fRenderer.Initialise;
Parser.Parse(RawCode, Brush);
fRenderer.Finalise;
finally
Parser.Free;
end;
end;
procedure TSyntaxHiliter.ElementHandler(Parser: TObject;
const ElemInfo: TSyntaxHiliteElemInfo; const ElemText: string);
begin
fRenderer.BeforeElem(ElemInfo);
fRenderer.WriteElemText(ElemText);
fRenderer.AfterElem(ElemInfo);
end;
class procedure TSyntaxHiliter.Hilite(const RawCode: string;
const Brush: TSyntaxHiliterBrush; Renderer: IHiliteRenderer);
begin
Assert(Assigned(Renderer), ClassName + '.Create: Renderer is nil');
with InternalCreate(Renderer) do
try
DoHilite(RawCode, Brush);
finally
Free;
end;
end;
procedure TSyntaxHiliter.LineBeginHandler(Parser: TObject);
begin
fRenderer.BeginLine;
end;
procedure TSyntaxHiliter.LineEndHandler(Parser: TObject);
begin
fRenderer.EndLine;
end;
constructor TSyntaxHiliter.InternalCreate(Renderer: IHiliteRenderer);
begin
inherited InternalCreate;
fRenderer := Renderer;
end;
{ TNulDocumentHiliter }
class function TNulDocumentHiliter.Hilite(const RawCode: string;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme;
const Title: string): TEncodedData;
begin
Result := TEncodedData.Create(RawCode, etUnicode);
end;
{ TXHTMLDocumentHiliter }
class function TXHTMLDocumentHiliter.GenerateCSSRules(
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme):
string;
var
CSSBuilder: TCSSBuilder; // builds CSS code
HiliterCSS: THiliterCSS; // generates CSS names and properties for hiliter
begin
HiliterCSS := THiliterCSS.Create(Theme);
try
CSSBuilder := TCSSBuilder.Create;
try
HiliterCSS.BuildBrushCSS(Brush, CSSBuilder);
Result := CSSBuilder.AsString;
finally
CSSBuilder.Free;
end;
finally
HiliterCSS.Free;
end;
end;
class function TXHTMLDocumentHiliter.Hilite(const RawCode: string;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme;
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(Brush, Theme);
Renderer := THTMLHiliteRenderer.Create(Builder, Brush, Theme);
TSyntaxHiliter.Hilite(RawCode, Brush, Renderer);
Result := TEncodedData.Create(Builder.HTMLDocument, etUTF8);
finally
Builder.Free;
end;
end;
{ TRTFDocumentHiliter }
class function TRTFDocumentHiliter.Hilite(const RawCode: string;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme;
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, Brush, Theme);
TSyntaxHiliter.Hilite(RawCode, Brush, Renderer);
Result := TEncodedData.Create(Builder.Render.ToBytes, etASCII);
finally
Builder.Free;
end;
end;
{ THiliteRenderer }
constructor THiliteRenderer.Create(const Brush: TSyntaxHiliterBrush;
const Theme: TSyntaxHiliteTheme);
begin
inherited Create;
fTheme := Theme;
fBrush := Brush;
end;
{ TRTFHiliteRenderer }
procedure TRTFHiliteRenderer.AfterElem(const ElemInfo: TSyntaxHiliteElemInfo);
begin
if not IsEmptyGroup(Theme.GetStyle(ElemInfo.BrushID, ElemInfo.AttrID)) then
fBuilder.EndGroup;
end;
procedure TRTFHiliteRenderer.BeforeElem(const ElemInfo: TSyntaxHiliteElemInfo);
var
AttrStyle: TSyntaxHiliteAttrStyle;
begin
AttrStyle := Theme.GetStyle(ElemInfo.BrushID, ElemInfo.AttrID);
if not IsEmptyGroup(AttrStyle) then
begin
fBuilder.BeginGroup;
// TODO: Find a way of displaying background colour in RTF
// if AttrStyle.Background <> clNone then
// fBuilder.SetColour(AttrStyle.Foreground);
if AttrStyle.Foreground <> Theme.DefaultForeground then
fBuilder.SetColour(AttrStyle.Foreground);
if AttrStyle.FontStyles.Styles <> [] then
fBuilder.SetFontStyle(AttrStyle.FontStyles);
end;
end;
procedure TRTFHiliteRenderer.BeginLine;
begin
fBuilder.ResetCharStyle;
fBuilder.SetFont(Theme.FontName);
fBuilder.SetFontSize(Theme.FontSize);
fBuilder.SetColour(Theme.DefaultForeground);
end;
constructor TRTFHiliteRenderer.Create(const Builder: TRTFBuilder;
const Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme);
begin
Assert(Assigned(Builder), ClassName + '.Create: Builder is nil');
inherited Create(Brush, Theme);
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
Attrs: TArray<TSyntaxHiliterAttr>;
Attr: TSyntaxHiliterAttr;
AttrStyle: TSyntaxHiliteAttrStyle;
begin
// Set up font table
fBuilder.FontTable.Add(Theme.FontName, rgfModern, 0);
// Set up colour table
Attrs := Brush.SupportedAttrs;
for Attr in Attrs do
begin
AttrStyle := Theme.GetStyle(Brush.ID, Attr.ID);
fBuilder.ColourTable.Add(AttrStyle.Foreground);
end;
// Begin group containing all source code
fBuilder.BeginGroup;
// Clear any paragraph formatting
fBuilder.ClearParaFormatting;
end;
function TRTFHiliteRenderer.IsEmptyGroup(
const Style: TSyntaxHiliteAttrStyle): Boolean;
begin
{ TODO: if and when Style.Background is supported, replace this with test on
Theme.IsBaseStyle }
Result := (Style.Foreground = Theme.DefaultForeground)
and Style.FontStyles.IsNull;
end;
procedure TRTFHiliteRenderer.WriteElemText(const Text: string);
begin
fBuilder.AddText(Text);
end;
{ THTMLHiliteRenderer }
procedure THTMLHiliteRenderer.AfterElem(const ElemInfo: TSyntaxHiliteElemInfo);
begin
if not Theme.IsBaseStyle(
Theme.GetStyle(ElemInfo.BrushID, ElemInfo.AttrID)
) then
fBuilder.CloseSpan;
end;
procedure THTMLHiliteRenderer.BeforeElem(const ElemInfo: TSyntaxHiliteElemInfo);
begin
if not Theme.IsBaseStyle(
Theme.GetStyle(ElemInfo.BrushID, ElemInfo.AttrID)
) then
fBuilder.OpenSpan(
THiliterCSS.GetElemCSSClassName(ElemInfo.BrushID, ElemInfo.AttrID)
);
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 Brush: TSyntaxHiliterBrush; const Theme: TSyntaxHiliteTheme);
begin
Assert(Assigned(Builder), ClassName + '.Create: Builder is nil');
inherited Create(Brush, Theme);
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;
{ TDocumentHiliterHelper }
class function TDocumentHiliterHelper.GetHiliterClass(
const FileType: TSourceOutputFileType): TDocumentHiliterClass;
begin
case FileType of
sfRTF: Result := TRTFDocumentHiliter;
sfHTML: Result := TXHTMLDocumentHiliter;
else
Result := TNulDocumentHiliter;
end;
end;
class function TDocumentHiliterHelper.IsHilitingSupported(
const FileType: TSourceOutputFileType): Boolean;
begin
Result := FileType in [sfRTF, sfHTML];
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.