Menu

[r660]: / trunk / Src / UActiveText.pas  Maximize  Restore  History

Download this file

743 lines (678 with data), 23.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
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
{
* UActiveText.pas
*
* Provides interfaces, a factory class and implementation of "active text".
* Active text is text that can have actions performed on it. Actions may
* include formatting and clickable links.
*
* The active text object provides a list of a mixture of text and compound
* action elements. Text elements text to be displayed in the current context.
* Action elements occur in matched pairs and specify an action to be performed:
* the first action element switches on the action and the second switches it
* off.
*
* Active text does not define how it is rendered. It is up to the user to
* determine how to render the text by examining the elements. However some
* elements are defined as block level and some as inline.
*
* The active text object can be created by interpreting a textual markup
* language. The object is language agnostic. The user must provide a parser
* that can interpret the language and create the required elements.
*
* $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 UActiveText.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2008-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UActiveText;
interface
uses
// Delphi
SysUtils,
// Project
UBaseObjects, UExceptions;
type
{
TActiveTextElemKind:
Supported types of active text element.
}
TActiveTextElemKind = (
ekText, // a plain text element
ekLink, // a link element: has a URL (inline)
ekStrong, // text formatted as strong (inline)
ekEm, // text formatted as emphasised (inline)
ekVar, // text formatted as variable (inline)
ekPara, // delimits a paragraph (block level)
ekWarning, // text formatted as a warning (inline)
ekHeading, // delimits a heading (block level)
ekMono // text formatted as mono spaced (inline)
);
type
{
TActiveTextElemState:
Indicates whether a active text action element is opening or closing.
}
TActiveTextElemState = (
fsClose, // element is closing
fsOpen // element is opening
);
{
TActiveTextDisplayStyle:
Indicates how an active text action element is displayed - either inline or
as a block.
}
TActiveTextDisplayStyle = (
dsInline, // element is inline
dsBlock // element is block
);
{
IActiveTextElem:
Interface supported by all active text elements.
}
IActiveTextElem = interface(IInterface)
['{F08A9853-EDB6-4B14-8E21-F3AB10FAF7D9}']
function GetKind: TActiveTextElemKind;
{Gets kind of element this represents.
@return Element kind.
}
property Kind: TActiveTextElemKind read GetKind;
{Kind of element}
end;
{
IActiveTextTextElem:
Interface supported by plain text active text elements (i.e. with Kind =
ekText).
}
IActiveTextTextElem = interface(IActiveTextElem)
['{B20C56D2-4ACC-48C8-AB30-9979A1B148B3}']
function GetText: string;
{Gets plain text represented by element.
@return Element's text.
}
property Text: string read GetText;
{Text associated with element}
end;
{
IActiveTextActionElem:
Interface supported by active text action elements, i.e. those that specify
actions to be performed on text.
}
IActiveTextActionElem = interface(IActiveTextElem)
['{2956A28F-AED2-437E-A405-9A62077BD881}']
function GetState: TActiveTextElemState;
{Checks whether element is opening or closing: i.e. switching on or off
the element's operation.
@return Required state.
}
property State: TActiveTextElemState read GetState;
{Indicates whether element is opening or closing operation. Operation is
determined by inherited Kind property}
function GetParam: string;
{Gets value of any parameter associated with the element.
@return Required parameter value or '' if there is no parameter.
}
property Param: string read GetParam;
{Parameter associated with element. '' if element has no parameter}
function GetDisplayStyle: TActiveTextDisplayStyle;
{Checks whether element is displayed inline or as a block.
@return Required display style.
}
property DisplayStyle: TActiveTextDisplayStyle read GetDisplayStyle;
{Indicates whether element displays as a block or inline}
end;
{
IActiveTextEnum:
Defines an enumerator that can enumerate all the elements of an active text
object.
}
IActiveTextEnum = interface(IInterface)
['{08223D0F-1F4B-4F71-B115-C0613FFF47C5}']
function GetCurrent: IActiveTextElem;
{Gets current element in enumeration.
@return Current string.
}
function MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration completed.
}
property Current: IActiveTextElem read GetCurrent;
{Current element in enumeration}
end;
{
IActiveText:
Defines operations of active text objects.
}
IActiveText = interface(IInterface)
['{230228FB-355F-4EC9-9EA9-F8A6DE628972}']
function GetEnumerator: IActiveTextEnum;
{Gets object that can enumerate object's elements.
@return Required enumerator object.
}
function AddElem(const Elem: IActiveTextElem): Integer;
{Adds an element to the object.
@param Elem [in] Element to be added.
@return Index of new element in elements list.
}
procedure Append(const ActiveText: IActiveText);
{Appends elements from another active text object to this one.
@param ActiveText [in] Contains elements to be appended.
}
function IsEmpty: Boolean;
{Checks if the active text object contains any elements.
@return True if there are no elements, False otherwise.
}
function GetElem(Idx: Integer): IActiveTextElem;
{Gets an element from the Elems property.
@param Idx [in] Index of required element in Elems[].
@return Required element.
}
function GetCount: Integer;
{Gets number of elements in Elems property.
@return Number of elements.
}
property Elems[Idx: Integer]: IActiveTextElem read GetElem; default;
{List of elements in active text}
property Count: Integer read GetCount;
{Number of elements in Elems[]}
end;
{
IActiveTextParser:
Interface supported by objects that can build an active text object by
parsing mark-up.
}
IActiveTextParser = interface(IInterface)
procedure Parse(const Markup: string; const ActiveText: IActiveText);
{Parses markup and updates active text object with details.
@param Markup [in] Markup containing definition of active text. Must be
in format understood by parser.
@param ActiveText [in] Active text object to be updated by parser.
}
end;
{
EActiveTextParserError:
Class of exception raised when parsing active text markup.
}
EActiveTextParserError = class(ECodeSnip);
{
TActiveTextFactory:
Static factory class that can create instances of active text objects and
active text elements.
}
TActiveTextFactory = class(TNoConstructObject)
public
class function CloneActiveText(const Src: IActiveText): IActiveText;
{Creates a cloned copy of an active text object.
@param Src [in] Active text object to be cloned.
@return Cloned copy.
}
class function CreateActiveText: IActiveText; overload;
{Creates a new empty active text object.
@return Empty object.
}
class function CreateActiveText(const Markup: string;
const Parser: IActiveTextParser): IActiveText; overload;
{Create an active text object with contents obtained from parsing some
markup.
@param Markup [in] Markup that defines active text. "Language" used must
be suitable for processing by provided parser.
@param Parser [in] Object used to parse the markup and update active
text document.
@return New active text object.
}
class function CreateTextElem(const AText: string): IActiveTextTextElem;
{Creates a new active text text element.
@param AText [in] Text property of new element.
@return Required new element.
}
class function CreateActionElem(const Kind: TActiveTextElemKind;
const Param: string;
const State: TActiveTextElemState): IActiveTextActionElem; overload;
{Creates a new active text action element with a parameter.
@param Kind [in] Identifies kind of element.
@param Param [in] Value of element's Param property.
@param State [in] State of elememt: opening or closing.
@return Required new element.
}
class function CreateActionElem(const Kind: TActiveTextElemKind;
const State: TActiveTextElemState): IActiveTextActionElem; overload;
{Creates a new, parameterless, active text action element.
@param Kind [in] Identifies kind of element.
@param State [in] State of element: opening or closing.
@return Required new element.
}
end;
implementation
uses
// Delphi
Classes,
// Project
IntfCommon;
type
{
TActiveText:
Active text object implementation. Supports assignment and cloning and
provides an enumerator.
}
TActiveText = class(TInterfacedObject,
IActiveText, IAssignable, IClonable
)
strict private
fElems: TInterfaceList;
{List of object's active text elements}
type
TEnumerator = class(TInterfacedObject, IActiveTextEnum)
private
fActiveText: IActiveText;
{Reference to object being implemented}
fIndex: Integer;
{Index of current object in enumeration}
public
constructor Create(const ActiveText: IActiveText);
{Class constructor. Initialises enumeration.
@param ActiveText [in] Object to be enumerated.
}
{ IActiveTextEnum methods }
function GetCurrent: IActiveTextElem;
{Gets current element in enumeration.
@return Current string.
}
function MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration
completed.
}
end;
protected // do not make strict
{ IActiveText methods }
function GetEnumerator: IActiveTextEnum;
{Gets object that can enumerate object's elements.
@return Required enumerator object.
}
function AddElem(const Elem: IActiveTextElem): Integer;
{Adds an element to the object.
@param Elem [in] Element to be added.
@return Index of new element in elements list.
}
procedure Append(const ActiveText: IActiveText);
{Appends elements from another active text object to this one.
@param ActiveText [in] Contains elements to be appended.
}
function IsEmpty: Boolean;
{Checks if the active text object contains any elements.
@return True if there are no elements, False otherwise.
}
function GetElem(Idx: Integer): IActiveTextElem;
{Gets an element from the Elems property.
@param Idx [in] Index of required element in Elems[].
@return Required element.
}
function GetCount: Integer;
{Gets number of elements in Elems property.
@return Number of elements.
}
{ IAssignable method }
procedure Assign(const Src: IInterface);
{Assigns properties of another object to this object.
@param Src [in] Active text object whose properties are to be copied.
May be nil: clears this object's properties.
@except EBug raised if Src is not not nil or an active text object.
}
{ IClonable method }
function Clone: IInterface;
{Create a new instance of this object that is an extact copy.
@return New object's IInterface interface.
}
public
constructor Create;
{Class constructor. Sets up object.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
end;
{
TActiveTextElem:
Base class for active text elements.
}
TActiveTextElem = class(TInterfacedObject,
IActiveTextElem
)
strict private
fKind: TActiveTextElemKind;
{Kind of element}
protected // do not make strict
{ IActiveTextElem method }
function GetKind: TActiveTextElemKind;
{Gets element kind.
@return Required kind.
}
public
constructor Create(const Kind: TActiveTextElemKind);
{Class constructor. Sets up object of correct kind.
@param Kind [in] Element kind.
}
end;
{
TActiveTextTextElem:
Implements an active text text element.
}
TActiveTextTextElem = class(TActiveTextElem,
IActiveTextTextElem
)
strict private
fText: string;
{Text of element}
protected // do not make strict
function GetText: string;
{Gets element's text.
@return Required text.
}
public
constructor Create(const Text: string);
{Class constructor. Records element's text and specifies correct kind.
@param Text [in] Element's text.
}
end;
{
TActiveTextActionElem:
Implements an active text action element.
}
TActiveTextActionElem = class(TActiveTextElem,
IActiveTextActionElem
)
strict private
fState: TActiveTextElemState;
{State of element: opening or closing}
fParam: string;
{Any parameter associated with element}
protected // do not make strict
function GetState: TActiveTextElemState;
{Gets element state.
@return Required state.
}
function GetParam: string;
{Gets element's parameter as text.
@return Required parameter. '' if no parameter.
}
function GetDisplayStyle: TActiveTextDisplayStyle;
{Checks whether element is displayed inline or as a block.
@return Required display style.
}
public
constructor Create(const Kind: TActiveTextElemKind;
const Param: string; const State: TActiveTextElemState);
{Class constructor. Creates required action element.
@param Kind [in] Element kind.
@param Param [in] Any parameter associated with element. May be ''.
@param State [in] Element state: opening or closing.
}
end;
{ TActiveTextFactory }
class function TActiveTextFactory.CloneActiveText(
const Src: IActiveText): IActiveText;
{Creates a cloned copy of an active text object.
@param Src [in] Active text object to be cloned.
@return Cloned copy.
}
begin
Result := CreateActiveText;
(Result as IAssignable).Assign(Src);
end;
class function TActiveTextFactory.CreateActiveText: IActiveText;
{Creates a new empty active text object.
@return Empty object.
}
begin
Result := TActiveText.Create;
end;
class function TActiveTextFactory.CreateActionElem(
const Kind: TActiveTextElemKind; const Param: string;
const State: TActiveTextElemState): IActiveTextActionElem;
{Creates a new active text action element with a parameter.
@param Kind [in] Identifies kind of element.
@param Param [in] Value of element's Param property.
@param State [in] State of elememt: opening or closing.
@return Required new element.
}
begin
Result := TActiveTextActionElem.Create(Kind, Param, State);
end;
class function TActiveTextFactory.CreateActionElem(
const Kind: TActiveTextElemKind;
const State: TActiveTextElemState): IActiveTextActionElem;
{Creates a new, parameterless, active text action element.
@param Kind [in] Identifies kind of element.
@param State [in] State of element: opening or closing.
@return Required new element.
}
begin
Result := CreateActionElem(Kind, '', State);
end;
class function TActiveTextFactory.CreateActiveText(const Markup: string;
const Parser: IActiveTextParser): IActiveText;
{Create an active text object with contents obtained from parsing some
markup.
@param Markup [in] Markup that defines active text. "Language" used must be
suitable for processing by provided parser.
@param Parser [in] Object used to parse the markup and update active text
document.
@return New active text object.
}
begin
Result := CreateActiveText;
Parser.Parse(Markup, Result);
end;
class function TActiveTextFactory.CreateTextElem(
const AText: string): IActiveTextTextElem;
{Creates a new active text text element.
@param AText [in] Text property of new element.
@return Required new element.
}
begin
Result := TActiveTextTextElem.Create(AText);
end;
{ TActiveText }
function TActiveText.AddElem(const Elem: IActiveTextElem): Integer;
{Adds an element to the object.
@param Elem [in] Element to be added.
@return Index of new element in elements list.
}
begin
Result := fElems.Add(Elem);
end;
procedure TActiveText.Append(const ActiveText: IActiveText);
{Appends elements from another active text object to this one.
@param ActiveText [in] Contains elements to be appended.
}
var
Elem: IActiveTextElem; // references each element in elems
begin
for Elem in ActiveText do
AddElem(Elem);
end;
procedure TActiveText.Assign(const Src: IInterface);
{Assigns properties of another object to this object.
@param Src [in] Active text object whose properties are to be copied. May be
nil: clears this object's properties.
@except EBug raised if Src is not not nil or an active text object.
}
begin
if Assigned(Src) and not Supports(Src, IActiveText) then
raise EBug.Create(ClassName + '.Assign: Src must support IActiveText');
fElems.Clear;
if Assigned(Src) then
Append(Src as IActiveText);
end;
function TActiveText.Clone: IInterface;
{Create a new instance of this object that is an extact copy.
@return New object's IInterface interface.
}
begin
Result := Create;
(Result as IAssignable).Assign(Self);
end;
constructor TActiveText.Create;
{Class constructor. Sets up object.
}
begin
inherited Create;
fElems := TInterfaceList.Create;
end;
destructor TActiveText.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fElems);
inherited;
end;
function TActiveText.GetCount: Integer;
{Gets number of elements in Elems property.
@return Number of elements.
}
begin
Result := fElems.Count;
end;
function TActiveText.GetElem(Idx: Integer): IActiveTextElem;
{Gets an element from the Elems property.
@param Idx [in] Index of required element in Elems[].
@return Required element.
}
begin
Result := fElems[Idx] as IActiveTextElem;
end;
function TActiveText.GetEnumerator: IActiveTextEnum;
{Gets object that can enumerate object's elements.
@return Required enumerator object.
}
begin
Result := TEnumerator.Create(Self);
end;
function TActiveText.IsEmpty: Boolean;
{Checks if the active text object contains any elements.
@return True if there are no elements, False otherwise.
}
begin
Result := fElems.Count = 0;
end;
{ TActiveText.TEnumerator }
constructor TActiveText.TEnumerator.Create(const ActiveText: IActiveText);
{Class constructor. Initialises enumeration.
@param ActiveText [in] Object to be enumerated.
}
begin
inherited Create;
fActiveText := ActiveText;
fIndex := -1;
end;
function TActiveText.TEnumerator.GetCurrent: IActiveTextElem;
{Gets current element in enumeration.
@return Current string.
}
begin
Result := fActiveText[fIndex];
end;
function TActiveText.TEnumerator.MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration
completed.
}
begin
Result := fIndex < Pred(fActiveText.Count);
if Result then
Inc(fIndex);
end;
{ TActiveTextElem }
constructor TActiveTextElem.Create(const Kind: TActiveTextElemKind);
{Class constructor. Sets up object of correct kind.
@param Kind [in] Element kind.
}
begin
inherited Create;
fKind := Kind;
end;
function TActiveTextElem.GetKind: TActiveTextElemKind;
{Gets element kind.
@return Required kind.
}
begin
Result := fKind;
end;
{ TActiveTextTextElem }
constructor TActiveTextTextElem.Create(const Text: string);
{Class constructor. Records element's text and specifies correct kind.
@param Text [in] Element's text.
}
begin
inherited Create(ekText);
fText := Text;
end;
function TActiveTextTextElem.GetText: string;
{Gets element's text.
@return Required text.
}
begin
Result := fText;
end;
{ TActiveTextActionElem }
constructor TActiveTextActionElem.Create(const Kind: TActiveTextElemKind;
const Param: string; const State: TActiveTextElemState);
{Class constructor. Creates required action element.
@param Kind [in] Element kind.
@param Param [in] Any parameter associated with element. May be ''.
@param State [in] Element state: opening or closing.
}
begin
Assert(Kind <> ekText,
ClassName + '.Create: Kind is not valid for a compound element.');
inherited Create(Kind);
fParam := Param;
fState := State;
end;
function TActiveTextActionElem.GetDisplayStyle: TActiveTextDisplayStyle;
{Checks whether element is displayed inline or as a block.
@return Required display style.
}
begin
if GetKind in [ekPara, ekHeading] then
Result := dsBlock
else
Result := dsInline;
end;
function TActiveTextActionElem.GetParam: string;
{Gets element's parameter as text.
@return Required parameter. '' if no parameter.
}
begin
Result := fParam;
end;
function TActiveTextActionElem.GetState: TActiveTextElemState;
{Gets element state.
@return Required state.
}
begin
Result := fState;
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.