Menu

[r166]: / branches / proxy-server / Src / USyntaxHiliters.pas  Maximize  Restore  History

Download this file

927 lines (841 with data), 32.1 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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
{
* USyntaxHiliters.pas
*
* Provides highlighter classes used to format and highlight source code in
* various file formats. Contains an interface supported by all highlighters, a
* factory object and implementation of various different highlighter objects.
*
* v0.1 of 17 Mar 2005 - Original version. This is a reworking of earlier
* syntax highlighter implementation in the former
* UBaseHiliter and UDetailHTMLHiliter units.
* v0.2 of 21 Apr 2005 - Added new IntfHilite unit that contains type
* declarations formerly in other units.
* v0.3 of 04 Jun 2005 - Changed invalid default font in
* TXHTMLHiliter.GetMainCSSStyle from mono to monospace.
* v1.0 of 25 May 2006 - Improved and corrected comments.
* - Localised literal HTML document title string.
* v1.1 of 07 Nov 2006 - Changed to use UCSSUtils methods to generate CSS
* attributes.
* - Completely rewrote RTF hiliter using new TRTFBuilder
* class. Also changed class to generate smaller RTF
* code.
* - Changed reference to fAttrs private field of parent
* class to reference to protected Attrs property in
* TXHTMLHiliter.
* v1.2 of 25 Nov 2006 - Changed to use new THiliterCSS class to handle CSS
* generation and class names. As a result removed all
* explicit CSS generation and interogation code from
* this unit.
* - Pushed BeforeElem() and AfterElem() methods from
* TDetailHTMLHiliter and TCHTMLHiliter up into super
* class TBaseHTMLHiliter. This leaves TDetailHTMLHiliter
* as an empty class that is retained for informational
* purposes.
* - TBaseHTMLHiliter now takes note of provided
* highlighter class when determining which highlighter
* elements are formatted. This means that
* TDetailHTMLHighlighter also now requires a suitable
* highlighter object. (Previously it ignored any
* highlighter and CSS classes were hard wired).
* v1.3 of 03 Dec 2006 - TDetailHTMLHighlighter changed to surround generated
* HTML in <pre>..</pre> tags. This means that
* TDetailHTMLHighlighter is no longer an empty class.
* v1.4 of 02 Jul 2007 - Added support for document titles where document type
* supports this.
* - Changed XHTML code generation to use THTMLBuilder.
* v1.5 of 04 Nov 2007 - Removed the IAssignable cast from the parameter to
* IAssignable.Assign method calls.
* v1.6 of 05 Oct 2008 - Changed TSyntaxHiliterFactory to derive from
* TNoConstructObject and hence prevented it from being
* constructed.
* - Made some private and protected sections of some
* classes strict.
*
*
* ***** 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 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-2008 Peter
* Johnson. All Rights Reserved.
*
* ***** END LICENSE BLOCK *****
}
unit USyntaxHiliters;
interface
uses
// Project
IntfHiliter, UBaseObjects;
type
{
TSyntaxHiliterFactory:
Factory class used to create syntax highlighter objects.
}
TSyntaxHiliterFactory = class(TNoConstructObject)
public
class function CreateHiliter(
const Kind: TSyntaxHiliterKind): ISyntaxHiliter;
{Creates syntax highlighter of required kind.
@param Kind [in] Kind of highlighter required.
@return Highlighter object.
}
end;
implementation
{
NOTES:
The class heirachy for syntax highlighter classes in this unit is:
TSyntaxHiliter - abstract base class
|
+-- * TNulHiliter - do nothing class that passes source thru unchanged
| ignores highlight attributes
|
+-- TParsedHiliter - abstract base class for classes that parse source code
|
+-- TBaseHTMLHiliter - base class for highlighters that generate HTML
| |
| +-- * TDetailHTMLHiliter - creates HTML fragment for Details pane
| | uses highlight attributes
| |
| +-- * TXHTMLHiliter - creates complete XHTML document
| uses highlight attributes
|
+-- * TRTFHiliter - creates complete RTF document
uses highlight attributes
* indicates a class constructed by factory class
}
uses
// Delphi
SysUtils, Classes, Graphics,
// Project
IntfCommon, UCSSBuilder, UHiliteAttrs, UHiliterCSS, UHilitePasParser,
UHTMLBuilder, URTFBuilder, UStrStreamWriter;
type
{
TSyntaxHiliterClass:
Class type for syntax highlighters. Used by factory class to create syntax
highlighter objects of different types.
}
TSyntaxHiliterClass = class of TSyntaxHiliter;
{
TSyntaxHiliter:
Abstract base class for all syntax highlighter classes. Provides virtual
abstract methods and a virtual constructor that descendants override. This
class is provided to give common base class that allows factory class to
use TSyntaxHiliterClass for object creation.
}
TSyntaxHiliter = class(TInterfacedObject)
protected
{ ISyntaxHiliter methods }
procedure Hilite(const Src, Dest: TStream; const Attrs: IHiliteAttrs = nil;
const Title: string = ''); overload; virtual; abstract;
{Highlights source code on an input stream and writes to output stream.
@param Src [in] Stream containing source code to be highlighted.
@param Dest [in] Stream that receives formatted / highlighted document.
@param Attrs [in] Attributes to be used by highlighter. Nil value causes
a nul highlighter to be used.
@param Title [in] Optional title to be used as meta data in output
document. Will be ignored if document type does not support title.
}
function Hilite(const RawCode: string; const Attrs: IHiliteAttrs = nil;
const Title: string = ''): string; overload; virtual; abstract;
{Creates string containing highlighted source code.
@param RawCode [in] Contains source code to be highlighted.
@param Attrs [in] Attributes to be used by highlighter. Nil value causes
a nul highlighter to be used.
@param Title [in] Optional title to be used as meta data in output
document. Will be ignored if document type does not support title.
@return Formatted / highlighted source code.
}
constructor Create; virtual;
{Class constructor. Instantiates object. This do-nothing virtual
constructor is required to enable polymorphism to work for descendant
classes.
}
end;
{
TNulHiliter:
A do nothing highlighter class that passes source code through unchanged.
}
TNulHiliter = class(TSyntaxHiliter,
ISyntaxHiliter
)
protected
{ ISyntaxHiliter methods }
procedure Hilite(const Src, Dest: TStream; const Attrs: IHiliteAttrs = nil;
const Title: string = ''); overload; override;
{Copies source code on an input stream to output stream unchanged.
@param Src [in] Stream containing source code.
@param Dest [in] Stream that receives unchanged copy of source code from
Src.
@param Attrs [in] Attributes to be used by highlighter. Ignored.
@param Title [in] Title of output document. Ignored.
}
function Hilite(const RawCode: string; const Attrs: IHiliteAttrs = nil;
const Title: string = ''): string; overload; override;
{Returns provided source code unchanged.
@param RawCode [in] Contains source code.
@param Attrs [in] Attributes to be used by highlighter. Ignored.
@param Title [in] Title of output document. Ignored.
@return Unchanged source code.
}
end;
{
TParsedHiliter:
Abstract base class for all highlighter classes that parse source code using
Pascal parser object. Handles parser events and calls virtual methods to
write the various document parts. Also provides a helper object to simplify
output of formatted code.
}
TParsedHiliter = class(TSyntaxHiliter)
strict private
fWriter: TStrStreamWriter;
{Helper object used to emit formatted source code}
fAttrs: IHiliteAttrs;
{Reference to highlighter attributes}
fTitle: string;
{Document title}
procedure ElementHandler(Parser: THilitePasParser; Elem: THiliteElement;
const ElemText: string);
{Handles parser's OnElement event. Calls virtual do nothing and abstract
methods that descendants override to write a document element in required
format.
@param Parser [in] Reference to parser that triggered event (unused).
@param Elem [in] Type of element to output.
@param ElemText [in] Text to be output for element.
}
procedure LineBeginHandler(Parser: THilitePasParser);
{Handles parser's OnLineBegin event. Calls virtual do nothing method that
descendants override to output data needed to start a new line.
@param Parser [in] Reference to parser that triggered event (unused).
}
procedure LineEndHandler(Parser: THilitePasParser);
{Handles parser's OnLineEnd event. Calls virtual do nothing method that
descendants override to output data needed to end a new line.
@param Parser [in] Reference to parser that triggered event (unused).
}
protected
{ ISyntaxHiliter methods }
procedure Hilite(const Src, Dest: TStream; const Attrs: IHiliteAttrs = nil;
const Title: string = ''); overload; override;
{Highlights source code on an input stream and writes to output stream.
@param Src [in] Stream containing source code to be highlighted.
@param Dest [in] Stream that receives formatted / highlighted document.
@param Attrs [in] Attributes to be used by highlighter. Nil value causes
a nul highlighter to be used.
@param Title [in] Optional title to be used as meta data in output
document. Will be ignored if document type does not support title.
}
function Hilite(const RawCode: string; const Attrs: IHiliteAttrs = nil;
const Title: string = ''): string; overload; override;
{Creates string containing highlighted source code.
@param RawCode [in] Contains source code to be highlighted.
@param Attrs [in] Attributes to be used by highlighter. Nil value causes
a nul highlighter to be used.
@param Title [in] Optional title to be used as meta data in output
document. Will be ignored if document type does not support title.
@return Formatted / highlighted source code.
}
strict protected
procedure BeginDoc; virtual;
{Called just before document is parsed. Used to initialise document.
}
procedure EndDoc; virtual;
{Called after parsing complete. Used to finalise document.
}
procedure BeginLine; virtual;
{Called when a new line in output is started. Used to initialise a line in
output.
}
procedure EndLine; virtual;
{Called when a line is ending. Used to terminate a line in output.
}
procedure WriteElem(const ElemText: string); virtual; abstract;
{Called for each different highlight element in document and is overridden
to output element's text.
@param ElemText [in] Text of the element.
}
procedure BeforeElem(Elem: THiliteElement); virtual;
{Called before a highlight element is output. Used to write code to
display element in required format.
@param Elem [in] Kind of highlight element.
}
procedure AfterElem(Elem: THiliteElement); virtual;
{Called after a highlight element is output. Used to write code to
finalise element formatting.
@param Elem [in] Kind of highlight element.
}
property Writer: TStrStreamWriter read fWriter;
{Helper object used to write formatted code to output}
property Attrs: IHiliteAttrs read fAttrs;
{Object storing attributes of highlighter. Defines appearance of document
and each highlight element}
property Title: string read fTitle;
{Title of document. May be ignored if document type doesn't support title
meta data}
public
constructor Create; override;
{Class constructor. Sets up object.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
end;
{
TRTFHiliter:
Highlighter object used to write highlighted rich text.
}
TRTFHiliter = class(TParsedHiliter,
ISyntaxHiliter
)
strict private
fRTFBuilder: TRTFBuilder;
{Object used to construct rich text document}
strict protected
procedure BeginDoc; override;
{Called just before document is parsed. Used to emit RTF header.
}
procedure EndDoc; override;
{Called after parsing complete. Writes closing brace that terminates RTF
document.
}
procedure BeginLine; override;
{Called when a new line in output is started. Used to initialise a line in
output.
}
procedure EndLine; override;
{Called when a line is ending. Writes end of line RTF control.
}
procedure WriteElem(const ElemText: string); override;
{Outputs element's text.
@param ElemText [in] Text of the element.
}
procedure BeforeElem(Elem: THiliteElement); override;
{Called before a highlight element is output. Used to emit RTF controls
that define style of element.
@param Elem [in] Kind of highlight element.
}
procedure AfterElem(Elem: THiliteElement); override;
{Called after a highlight element is output. Used to write code to
finalise element formatting.
@param Elem [in] Kind of highlight element.
}
public
constructor Create; override;
{Class constructor. Sets up object.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
end;
{
TBaseHTMLHiliter:
Base class for all highlighters that emit HTML. Provides some common
functionality. Emits HTML for highlighted source code only, not a complete
HTML document. No CSS definitions are written since these are expected to
have been provided elsewhere and match standard highlighter class names.
(THiliterCSS is used to provide CSS class names and definitions).
}
TBaseHTMLHiliter = class(TParsedHiliter)
strict private
fIsFirstLine: Boolean;
{Records whether we are about to write first line}
fHTMLBuilder: THTMLBuilder;
{Object used to construct XHTML document}
strict protected
procedure BeginDoc; override;
{Called just before document is parsed. Used to initialise document.
}
procedure BeginLine; override;
{Called when a new line in output is started. Writes new line where
required.
}
procedure WriteElem(const ElemText: string); override;
{Outputs element's text.
@param ElemText [in] Text of the element.
}
procedure BeforeElem(Elem: THiliteElement); override;
{Called before a highlight element is output. Used to write span tag for
any CSS class that is required.
@param Elem [in] Kind of highlight element.
}
procedure AfterElem(Elem: THiliteElement); override;
{Called after a highlight element is output. Writes closing span tag if
required.
@param Elem [in] Kind of highlight element.
}
property IsFirstLine: Boolean read fIsFirstLine;
{Flag true when line to be written is first line and false after that}
property HTMLBuilder: THTMLBuilder read fHTMLBuilder;
{Object used to construct XHTML document}
public
constructor Create; override;
{Class constructor. Sets up object.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
end;
{
TDetailHTMLHiliter:
Highlighter that is used to display source code in program's Details pane.
The class produces HTML suitable for use in HTML template document,
therefore it does not emit a complete document, just HTML between
<pre>..</pre> tags.
}
TDetailHTMLHiliter = class(TBaseHTMLHiliter,
ISyntaxHiliter
)
strict protected
procedure BeginDoc; override;
{Called just before document is parsed. Used to write opening pre tag.
}
procedure EndDoc; override;
{Called after parsing complete. Writes closing pre tag.
}
end;
{
TXHTMLHiliter:
Highlighter that emits a complete XHTML document containing the source code.
It creates an emebedded style sheet containing information from given
highlight attribute objects and then writes code that uses these classes for
formatting.
}
TXHTMLHiliter = class(TBaseHTMLHiliter,
ISyntaxHiliter
)
strict private
function GenerateCSSRules: string;
{Generates CSS rule for use in head section of HTML document.
@return List of CSS class rules.
}
strict protected
procedure BeginDoc; override;
{Called just before document is parsed. Used to write XHTML code for
document head section and first part of body.
}
procedure EndDoc; override;
{Called after parsing complete. Writes XHTML that closes document.
}
end;
{ TSyntaxHiliterFactory }
class function TSyntaxHiliterFactory.CreateHiliter(
const Kind: TSyntaxHiliterKind): ISyntaxHiliter;
{Creates syntax highlighter of required kind.
@param Kind [in] Kind of highlighter required.
@return Highlighter object.
}
const
// Map of highlighter kinds to highlighter classes
cHiliterMap: array[TSyntaxHiliterKind] of TSyntaxHiliterClass = (
TNulHiliter,
TDetailHTMLHiliter,
TXHTMLHiliter,
TRTFHiliter
);
var
Obj: TSyntaxHiliter; // created object
begin
Obj := cHiliterMap[Kind].Create; // create object
Result := Obj as ISyntaxHiliter; // return ISyntaxHiliter interface to object
end;
{ TSyntaxHiliter }
constructor TSyntaxHiliter.Create;
{Class constructor. Instantiates object. This do-nothing virtual constructor
is required to enable polymorphism to work for descendant classes.
}
begin
inherited;
// Do nothing ** Do not remove - required for polymorphism to work **
end;
{ TNulHiliter }
procedure TNulHiliter.Hilite(const Src, Dest: TStream;
const Attrs: IHiliteAttrs; const Title: string);
{Copies source code on an input stream to output stream unchanged.
@param Src [in] Stream containing source code.
@param Dest [in] Stream that receives unchanged copy of source code from
Src.
@param Attrs [in] Attributes to be used by highlighter. Ignored.
@param Title [in] Title of output document. Ignored.
}
begin
// Copy source from current location in input stream to end of stream into
// destination stream
Dest.CopyFrom(Src, Src.Size - Src.Position);
end;
function TNulHiliter.Hilite(const RawCode: string; const Attrs: IHiliteAttrs;
const Title: string): string;
{Returns provided source code unchanged.
@param RawCode [in] Contains source code.
@param Attrs [in] Attributes to be used by highlighter. Ignored.
@param Title [in] Title of output document. Ignored.
@return Unchanged source code.
}
begin
Result := RawCode;
end;
{ TParsedHiliter }
procedure TParsedHiliter.AfterElem(Elem: THiliteElement);
{Called after a highlight element is output. Used to write code to finalise
element formatting.
@param Elem [in] Kind of highlight element.
}
begin
// Do nothing: descendants override
end;
procedure TParsedHiliter.BeforeElem(Elem: THiliteElement);
{Called before a highlight element is output. Used to write code to display
element in required format.
@param Elem [in] Kind of highlight element.
}
begin
// Do nothing: descendants override
end;
procedure TParsedHiliter.BeginDoc;
{Called just before document is parsed. Used to initialise document.
}
begin
// Do nothing: descendants override
end;
procedure TParsedHiliter.BeginLine;
{Called when a new line in output is started. Used to initialise a line in
output.
}
begin
// Do nothing: descendants override
end;
constructor TParsedHiliter.Create;
{Class constructor. Sets up object.
}
begin
inherited;
// Create nul highlighter object used by default
fAttrs := THiliteAttrsFactory.CreateNulAttrs;
end;
destructor TParsedHiliter.Destroy;
{Class destructor. Tears down object.
}
begin
fAttrs := nil;
inherited;
end;
procedure TParsedHiliter.ElementHandler(Parser: THilitePasParser;
Elem: THiliteElement; const ElemText: string);
{Handles parser's OnElement event. Calls virtual do nothing and abstract
methods that descendants override to write a document element in required
format.
@param Parser [in] Reference to parser that triggered event (unused).
@param Elem [in] Type of element to output.
@param ElemText [in] Text to be output for element.
}
begin
BeforeElem(Elem);
WriteElem(ElemText);
AfterElem(Elem);
end;
procedure TParsedHiliter.EndDoc;
{Called after parsing complete. Used to finalise document.
}
begin
// Do nothing: descendants override
end;
procedure TParsedHiliter.EndLine;
{Called when a line is ending. Used to terminate a line in output.
}
begin
// Do nothing: descendants override
end;
procedure TParsedHiliter.Hilite(const Src, Dest: TStream;
const Attrs: IHiliteAttrs; const Title: string);
{Highlights source code on an input stream and writes to output stream.
@param Src [in] Stream containing source code to be highlighted.
@param Dest [in] Stream that receives formatted / highlighted document.
@param Attrs [in] Attributes to be used by highlighter. Nil value causes a
nul highlighter to be used.
@param Title [in] Optional title to be used as meta data in output document.
Will be ignored if document type does not support title.
}
var
Parser: THilitePasParser; // object used to parse source
begin
(fAttrs as IAssignable).Assign(Attrs); // Attrs may be nil
fTitle := Title;
fWriter := TStrStreamWriter.Create(Dest);
try
// Create parser
Parser := THilitePasParser.Create;
try
Parser.OnElement := ElementHandler;
Parser.OnLineBegin := LineBeginHandler;
Parser.OnLineEnd := LineEndHandler;
// Parse the document:
BeginDoc; // overridden in descendants to initialise document
Parser.Parse(Src);
EndDoc; // overridden in descendants to finalise document
finally
Parser.Free;
end;
finally
fWriter.Free;
end;
end;
function TParsedHiliter.Hilite(const RawCode: string; const Attrs: IHiliteAttrs;
const Title: string): string;
{Creates string containing highlighted source code.
@param RawCode [in] Contains source code to be highlighted.
@param Attrs [in] Attributes to be used by highlighter. Nil value causes a
nul highlighter to be used.
@param Title [in] Optional title to be used as meta data in output document.
Will be ignored if document type does not support title.
@return Formatted / highlighted source code.
}
var
SrcStm: TStringStream; // stream used to store raw source code
DestStm: TStringStream; // stream used to receive output
begin
DestStm := nil;
// Create string stream containing raw source code
SrcStm := TStringStream.Create(RawCode);
try
// Create string stream to received formatted / highlighted output
DestStm := TStringStream.Create('');
// Use stream version of method to perform highlighting
Hilite(SrcStm, DestStm, Attrs, Title);
// Return string stored in destination stream
Result := DestStm.DataString;
finally
DestStm.Free;
SrcStm.Free;
end;
end;
procedure TParsedHiliter.LineBeginHandler(Parser: THilitePasParser);
{Handles parser's OnLineBegin event. Calls virtual do nothing method that
descendants override to output data needed to start a new line.
@param Parser [in] Reference to parser that triggered event (unused).
}
begin
BeginLine;
end;
procedure TParsedHiliter.LineEndHandler(Parser: THilitePasParser);
{Handles parser's OnLineEnd event. Calls virtual do nothing method that
descendants override to output data needed to end a new line.
@param Parser [in] Reference to parser that triggered event (unused).
}
begin
EndLine;
end;
{ TRTFHiliter }
procedure TRTFHiliter.AfterElem(Elem: THiliteElement);
{Called after a highlight element is output. Used to write code to finalise
element formatting.
@param Elem [in] Kind of highlight element.
}
begin
if not Attrs[Elem].IsNul then
fRTFBuilder.EndGroup;
end;
procedure TRTFHiliter.BeforeElem(Elem: THiliteElement);
{Called before a highlight element is output. Used to emit RTF controls that
define style of element.
@param Elem [in] Kind of highlight element.
}
begin
if not Attrs[Elem].IsNul then
begin
fRTFBuilder.BeginGroup;
if Attrs[Elem].ForeColor <> clNone then
fRTFBuilder.SetColour(Attrs[Elem].ForeColor);
if Attrs[Elem].FontStyle <> [] then
fRTFBuilder.SetFontStyle(Attrs[Elem].FontStyle);
end;
end;
procedure TRTFHiliter.BeginDoc;
{Called just before document is parsed. Used to emit RTF header.
}
var
Elem: THiliteElement; // loops thru all highlight elements
begin
// Set up font table
fRTFBuilder.FontTable.Add(Attrs.FontName, rgfModern, 0);
// Set up colour table
for Elem := Low(THiliteElement) to High(THiliteElement) do
fRTFBuilder.ColourTable.Add(Attrs[Elem].ForeColor);
// Add any document title
fRTFBuilder.DocProperties.Title := Title;
// Clear any paragraph formatting
fRTFBuilder.ClearParaFormatting;
end;
procedure TRTFHiliter.BeginLine;
{Called when a new line in output is started. Used to initialise a line in
output.
}
begin
fRTFBuilder.ResetCharStyle;
fRTFBuilder.SetFont(Attrs.FontName);
fRTFBuilder.SetFontSize(Attrs.FontSize);
end;
constructor TRTFHiliter.Create;
{Class constructor. Sets up object.
}
begin
inherited;
fRTFBuilder := TRTFBuilder.Create;
end;
destructor TRTFHiliter.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fRTFBuilder);
inherited;
end;
procedure TRTFHiliter.EndDoc;
{Called after parsing complete. Writes closing brace that terminates RTF
document.
}
begin
Writer.WriteStrLn(fRTFBuilder.AsString);
end;
procedure TRTFHiliter.EndLine;
{Called when a line is ending. Writes end of line RTF control.
}
begin
fRTFBuilder.EndPara;
end;
procedure TRTFHiliter.WriteElem(const ElemText: string);
{Outputs element's text.
@param ElemText [in] Text of the element.
}
begin
fRTFBuilder.AddText(ElemText);
end;
{ TBaseHTMLHiliter }
procedure TBaseHTMLHiliter.AfterElem(Elem: THiliteElement);
{Called after a highlight element is output. Writes closing span tag if
required.
@param Elem [in] Kind of highlight element.
}
begin
if not Attrs[Elem].IsNul then
HTMLBuilder.CloseSpan;
end;
procedure TBaseHTMLHiliter.BeforeElem(Elem: THiliteElement);
{Called before a highlight element is output. Used to write span tag for any
CSS class that is required.
@param Elem [in] Kind of highlight element.
}
begin
if not Attrs.Elements[Elem].IsNul then
HTMLBuilder.OpenSpan(THiliterCSS.GetElemCSSClassName(Elem));
end;
procedure TBaseHTMLHiliter.BeginDoc;
{Called just before document is parsed. Used to initialise document.
}
begin
// Note that we are about to write first line
fIsFirstLine := True;
end;
procedure TBaseHTMLHiliter.BeginLine;
{Called when a new line in output is started. Writes new line where required.
}
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
HTMLBuilder.NewLine;
end;
constructor TBaseHTMLHiliter.Create;
{Class constructor. Sets up object.
}
begin
inherited;
fHTMLBuilder := THTMLBuilder.Create;
end;
destructor TBaseHTMLHiliter.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fHTMLBuilder);
inherited;
end;
procedure TBaseHTMLHiliter.WriteElem(const ElemText: string);
{Outputs element's text.
@param ElemText [in] Text of the element.
}
begin
HTMLBuilder.AddText(ElemText);
end;
{ TDetailHTMLHiliter }
procedure TDetailHTMLHiliter.BeginDoc;
{Called just before document is parsed. Used to write opening pre tag.
}
begin
inherited;
HTMLBuilder.OpenPre(THiliterCSS.GetMainCSSClassName);
end;
procedure TDetailHTMLHiliter.EndDoc;
{Called after parsing complete. Writes closing pre tag.
}
begin
inherited;
HTMLBuilder.ClosePre;
Writer.WriteStr(HTMLBuilder.HTMLFragment);
end;
{ TXHTMLHiliter }
procedure TXHTMLHiliter.BeginDoc;
{Called just before document is parsed. Used to write XHTML code for document
head section and first part of body.
}
resourcestring
// Default document title
sDefaultTitle = 'DelphiDabbler CodeSnip Database';
begin
inherited;
if Title <> '' then
HTMLBuilder.Title := Title
else
HTMLBuilder.Title := sDefaultTitle;
HTMLBuilder.CSS := GenerateCSSRules;
HTMLBuilder.OpenPre(THiliterCSS.GetMainCSSClassName);
end;
procedure TXHTMLHiliter.EndDoc;
{Called after parsing complete. Writes XHTML that closes document.
}
begin
inherited;
HTMLBuilder.ClosePre;
Writer.WriteStr(HTMLBuilder.HTMLDocument);
end;
function TXHTMLHiliter.GenerateCSSRules: string;
{Generates CSS rule for use in head section of HTML document.
@return List of CSS class rules.
}
var
CSSBuilder: TCSSBuilder; // builds CSS code
HiliterCSS: THiliterCSS; // generates CSS names and properties for hiliter
begin
HiliterCSS := THiliterCSS.Create(Attrs);
CSSBuilder := nil;
try
CSSBuilder := TCSSBuilder.Create;
HiliterCSS.BuildCSS(CSSBuilder);
Result := CSSBuilder.AsString;
finally
FreeAndNil(CSSBuilder);
FreeAndNil(HiliterCSS);
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.