Menu

[r111]: / trunk / Src / UREMLDataIO.pas  Maximize  Restore  History

Download this file

1020 lines (943 with data), 35.4 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
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
{
* UREMLDataIO.pas
*
* Implements classes that render and parse Routine Extra Markup Language (REML)
* code. This markup is used to read and store active text objects as used by
* the Extra property of a TRoutine object. Also includes helper classes.
*
* v1.0 of 30 Dec 2008 - Original version.
* v1.1 of 23 Jun 2009 - Added new REML tags: em, var, p, warning, heading.
* - Added new REML entity: &copy.
* v1.2 of 05 Jul 2009 - Added new TREMLVersion type.
* - Added new TREMLAnalyser static class provides latest
* REML version number and lowest possible version to
* use for specified active text.
* - Changed to be able to recognise version of REML where
* REML tags and entities were introduced and to generate
* REML in a specified version.
* v1.3 of 10 Jul 2009 - Removed TREMLInfo's Init and TidyUp methods. Tags and
* Entities fields are now created when required and
* released automatically when unit goes out of scope.
*
*
* ***** 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 UREMLDataIO.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-2009 Peter
* Johnson. All Rights Reserved.
*
* ***** END LICENSE BLOCK *****
}
unit UREMLDataIO;
interface
uses
// Project
UActiveText, UBaseObjects, UStacks, UTaggedTextLexer;
type
{
TREMLVersion:
Range of valid REML version numbers.
}
TREMLVersion = 1..3;
{
TREMLReader:
Class that parses markup used in Extra element read from snippets data
files. Markup is translated into active text. The Extra element may occur in
main database files and v2 of the user database and export files.
}
TREMLReader = class(TInterfacedObject, IActiveTextParser)
strict private
fLexer: TTaggedTextLexer;
{Object that analysis REML markup}
fParamStack: TStringStack;
{Stack used to store REML tag parameters for use when closing tags}
function TagInfo(const TagIdx: Integer; out TagName: string;
out TagCode: Word; out IsContainer: WordBool): Boolean;
{Callback that provides lexer with information about supported tags. Lexer
calls continually until False is returned.
@param TagIdx [in] Index of tag for which information is requested.
@param TagName [out] Set to name of tag.
@param TagCode [out] Set to unique code that identifies tag.
@param IsContainer [out] Always set to True to indicate container tag.
@return True if tag information was provided or False to indicate no
more tags.
}
function EntityInfo(const EntityIdx: Integer; out EntityName: string;
out EntityChar: AnsiChar): Boolean;
{Callback that provides lexer with information about supported character
entities. Lexer calls continually until False is returned.
@param EntityIdx [in] Index of entity for which information is
requested.
@param EntityName [out] Set to name of character entity.
@param EntityChar [out] Set to character associated with entity.
@return True if entity information was provided or False to indicate no
more entities.
}
protected // do not make strict
{ IActiveTextParser method }
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 updated by parser.
}
public
constructor Create;
{Class constructor. Sets up object.
}
destructor Destroy; override;
{Class destructor. Finalises object.
}
end;
{
TREMLWriter:
Class that creates a REML markup representation of an active text object.
}
TREMLWriter = class(TNoPublicConstructObject)
strict private
fVersion: TREMLVersion; // version of REML being written
function TextToREMLText(const Text: string): string;
{Converts plain text to REML compatible text by replacing illegal
characters with related character entities.
@param Text [in] Plain text to be converted.
@return Converted text.
}
function RenderTag(const TagElem: IActiveTextActionElem): string;
{Renders an active text action element as a REML tag.
@param TagElem [in] Active text action element to be rendered.
@return Required REML tag.
}
function RenderText(const TextElem: IActiveTextTextElem): string;
{Renders an active text text element. Illegal characters are converted to
REML character entities.
@param TextElem [in] Active text text element.
@return REML-safe text containing necessary character entities.
}
strict protected
constructor InternalCreate(const Version: TREMLVersion);
{Internal class constructor. Sets up object to render using a required
version of REML.
@param Version [in] Version of REML to write.
}
public
class function Render(const ActiveText: IActiveText;
const REMLVer: TREMLVersion): string;
{Renders REML representation of an active text object.
@param ActiveText [in] Active text to be rendered.
@param REMLVer [in] Version of REML to be written.
@return String containing REML markup.
}
end;
{
TREMLAnalyser:
Static class that analyses active text and provides information about latest
version and lowest possible version that can be used to render the REML.
}
TREMLAnalyser = class(TNoConstructObject)
public
const FIRST_VERSION = Low(TREMLVersion); // first version of REML
const LATEST_VERSION = High(TREMLVersion); // latest version of REML
class function LowestWriterVersion(
const ActiveText: IActiveText): TREMLVersion;
{Determines lowest possible version REML that can be used to write some
active text.
@param ActiveText [in] Active text to be analysed.
@return Minimum required REML version.
}
end;
implementation
{
About REML (Routine Extra Markup Language)
-----------------------------------------
The markup is simplified form of (X)HTML.
It comprises plain text with limited inline and block level formatting and
hyperlink specified by HTML like tags.
Supported tags are as follows. Unless otherwise specified, no tags may have
any attributes:
Inline:
<a href="url">xxxx</a> - Hyperlink: must have an href attribute that
specifies the link destination as a valid URL.
URLs must not be URL encoded. No other attributes
may be specified.
<strong>..</strong> - Renders enclosed text with strong emphasis.
<em>..</em> - Renders enclosed text emphasised.
<var>..</var> - Renders enclosed text as a programming variable.
<warning>..</warning> - Renders enclosed text as a warning.
<mono>..</mono> - Renders enclosed text as mono spaced.
Block:
<p>..</p> - Enclosed text is formatted as a paragraph.
<heading>..</heading> - Enclosed text is formatted as a heading.
Certain characters in plain text or in attribute values must be encoded as
HTML-like character entities. Attribute names must not contain any of these
characters. The characters that must be encoded are:
Character Entity
> &gt;
< &lt;
" &quot;
& &amp;
(C) &copy;
No other entities are supported. Any other character can be encoded using its
unicode or ascii value. For example, the @ symbol (ascii 64) is encoded as
&#64;
Example:
<heading>Hello</heading>
<p>&quot;<strong>Hello</strong>&quot; to
<a href="http://example.com">you</a></p>
This example specifes a heading "Hello" followed by a single paragraph. In the
paragraph, "Hello" will be bold, "to" should be plain text and "you" should
hyperlink to "example.com".
There are several versions of REML as follows:
v1 - supported tags: <strong> and <a>. The href attribute of the <a> tag
required the http:// protocol.
- supported entities: &gt;, &lt, &quot;, &amp;.
v2 - added tags: <em>, <var>, <warning>, <mono>, <p> and <heading>.
- added entity: &copy;.
v3 - changed <a> tag to accept file:// protocol in additions to http://
protocol in href attribute.
}
uses
// Delphi
SysUtils, StrUtils,
// Project
UAutoFree, UExceptions, UUtils;
type
{
TREMLTags:
Class that provides information about REML tags.
}
TREMLTags = class(TObject)
strict private
type
{
TREMLTag:
Record that stores information a REML tag.
}
TREMLTag = record
Id: TActiveTextElemKind; // active text element kind
Version: TREMLVersion; // REML version where tag introduced
TagName: string; // corresponding REML tag name
ParamName: string; // name of any REML parameter
constructor Create(const AId: TActiveTextElemKind;
const AVersion: TREMLVersion; const ATagName: string;
const AParamName: string = '');
{Record contructor. Initialises fields.
@param AId [in] Active text element kind.
@param AVersion [in] REML version where tag introduced.
@param ATagName [in] REML tag name.
@param AParamName [in] Optional name of parameter.
}
end;
var
fTagMap: array of TREMLTag; // Details of all supported tags
function IndexOfTagId(const Id: TActiveTextElemKind): Integer;
{Finds index of a tag id in tag map.
@param Id [in] Tag id to be found.
@return Index of tag id or -1 if tag id not found.
}
function GetCount: Integer;
{Read accessor for Count property.
@return Number of supported tags.
}
function GetId(Idx: Integer): TActiveTextElemKind;
{Read accessor for Ids[] property.
@param Idx [in] Zero based index of required id.
@return Required id.
}
function GetName(Idx: Integer): string;
{Read accessor for Names[] property,
@param Idx [in] Zero based index of required tag name.
@return Required tag name.
}
public
constructor Create;
{Class constructor. Sets up map of REML tags.
}
destructor Destroy; override;
{Class destructor. Clears tag map.
}
function LookupTagName(const Id: TActiveTextElemKind;
out TagName: string): Boolean;
{Looks up name of a tag.
@param Id [in] Id of tag.
@param TagName [out] Name of tag or '' if unknown id.
@return True if tag id is valid, False if not.
}
function LookupParamName(const Id: TActiveTextElemKind;
out ParamName: string): Boolean;
{Looks up a parameter name of an identified REML tag.
@param Id [in] Id of required tag.
@param ParamName [out] Set to name of parameter name. '' if tag has no
parameter or if tag id is not valid.
@return True if tag is valid, False if not.
}
function LookupTagVersion(const Id: TActiveTextElemKInd;
out Version: TREMLVersion): Boolean;
{Looks up REML version when tag was introduced.
@param Id [in] Id of tag.
@param Version [out] REML version when tag was introduced.
@return True if tag id is valid, False if not.
}
property Count: Integer read GetCount;
{Number of supported tags}
property Ids[Idx: Integer]: TActiveTextElemKind read GetId;
{List of tag ids}
property Names[Idx: Integer]: string read GetName;
{List of tag names}
end;
{
TREMLEntities:
Class provides information about character entities.
}
TREMLEntities = class(TObject)
strict private
type
{
TREMLEntity:
Record that associates a character with its REML mnemonic entity.
}
TREMLEntity = record
Entity: string; // Mnemonic entity
Ch: Char; // Character equivalent
Version: TREMLVersion; // REML version where entity introduced
constructor Create(const AEntity: string; const ACh: Char;
const AVersion: TREMLVersion);
{Record constructor. Initialises record.
@param AEntity [in] Mnemonic entity.
@param ACh [in] Equivalent character.
@param AVersion [in] REML version where entity introduced.
}
end;
var
fEntityMap: array of TREMLEntity; // Entities mapped to equivalent chars
function CharToMnemonicEntity(const Ch: Char;
const Ver: TREMLVersion): string;
{Gets the mnemonic character entity that represents a character.
@param Entity [in] Character for which equivalent entity is required.
@param Ver [in] Version of REML for which entity is required.
@return Required entity or '' if character has no matching mnemonic
entity.
}
function GetCount: Integer;
{Read accessor for Count property.
@return Number of supported tags.
}
function GetEntity(Idx: Integer): string;
{Read accessor for Entities[] property.
@param Idx [in] Zero based index of required entity.
@return Required entity.
}
function GetChar(Idx: Integer): Char;
{Read accessor for Chars[] property.
@param Idx [in] Zero based index of required character.
@return Required character.
}
public
constructor Create;
{Class constructor. Creates map of mnemonic entities to equivalent
characters.
}
destructor Destroy; override;
{Class destructor. Tidies up object.
}
function MapToEntity(const Ch: Char; const Ver: TREMLVersion): string;
{Maps a character to a character entity if appropriate.
@param Ch [in] Character to be mapped.
@param Ver [in] Version of REML for which entity is required.
@return Mnemonic entity if one exists, character itself if it is
printable and has ascii value less than 127, or a numeric character
otherwise.
}
property Count: Integer read GetCount;
{Number of supported tags}
property Entities[Idx: Integer]: string read GetEntity;
{List of character entities}
property Chars[Idx: Integer]: Char read GetChar;
{List of characters that match entities}
end;
{
EREMLEntities:
Class of exception raised when error encountered in TREMLEntities.
}
EREMLEntities = class(ECodeSnip);
{
TREMLInfo:
Static helper class that provides information about REML tags and converts
standard text to REML text and vice versa. This class simply maintains and
provides easy access to TREMLTags and TREMLEntities.
}
TREMLInfo = class(TNoConstructObject)
strict private
class var fTags: TREMLTags;
{Provides information about tags}
class var fTagsWrapper: IInterface;
{Wrapper object for fTags instanace that automatically frees it when unit
goes out of scope}
class var fEntities: TREMLEntities;
{Converts REML char entities <=> chars}
class var fEntitiesWrapper: IInterface;
{Wrapper object for fEntities instanace that automatically frees it when
unit goes out of scope}
public
class function TagInfo: TREMLTags;
{Provides a reference to object that provides information about a REML
tag.
@return Required object reference.
}
class function EntityInfo: TREMLEntities;
{Provides a reference to object that provides information about REML
character entities.
@return Required object reference.
}
end;
{ TREMLReader }
constructor TREMLReader.Create;
{Class constructor. Initialises object.
}
begin
inherited Create;
fLexer := TTaggedTextLexer.Create(TagInfo, EntityInfo);
fParamStack := TStringStack.Create;
end;
destructor TREMLReader.Destroy;
{Class destructor. Finalises object.
}
begin
FreeAndNil(fParamStack);
FreeAndNil(fLexer);
inherited;
end;
function TREMLReader.EntityInfo(const EntityIdx: Integer;
out EntityName: string; out EntityChar: AnsiChar): Boolean;
{Callback that provides lexer with information about supported character
entities. Lexer calls continually until False is returned.
@param EntityIdx [in] Index of entity for which information is requested.
@param EntityName [out] Set to name of character entity.
@param EntityChar [out] Set to character associated with entity.
@return True if entity information was provided or False to indicate no
more entities.
}
begin
Result := EntityIdx < TREMLInfo.EntityInfo.Count;
if not Result then
Exit;
EntityName := TREMLInfo.EntityInfo.Entities[EntityIdx];
EntityChar := TREMLInfo.EntityInfo.Chars[EntityIdx];
end;
procedure TREMLReader.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 updated by parser.
}
var
ParamName: string; // name of a parameter
ParamValue: string; // value of a parameter
TagId: TActiveTextElemKind; // id of a tag
resourcestring
// Error message
sErrMissingParam = 'Expected a "%0:s" parameter value in tag "%1:s"';
begin
Assert(Assigned(ActiveText), ClassName + '.Parse: ActiveText is nil');
try
// Nothing to do if there is no markup
if Markup = '' then
Exit;
// Use lexer to process markup
fLexer.TaggedText := Markup;
// Scan REML a token at a time
while fLexer.NextItem <> ttsEOF do
begin
case fLexer.Kind of
ttsText:
begin
// Plain text: add text element (lexer will have replaced character
// entities with actual characters
ActiveText.AddElem(
TActiveTextFactory.CreateTextElem(fLexer.PlainText)
);
end;
ttsCompoundStartTag:
begin
// Start of an action element
// Get tag id and any parameter
TagId := TActiveTextElemKind(fLexer.TagCode);
TREMLInfo.TagInfo.LookupParamName(TagId, ParamName);
if ParamName <> '' then
begin
// We have a parameter: must not be empty
ParamValue := fLexer.TagParams.Values[ParamName];
if ParamValue = '' then
raise EActiveTextParserError.CreateFmt(
sErrMissingParam, [ParamName, fLexer.TagName]
);
// Record param for use by closing tag
fParamStack.Push(ParamValue);
// Add opening action element
ActiveText.AddElem(
TActiveTextFactory.CreateActionElem(TagId, ParamValue, fsOpen)
);
end
else
begin
// No parameter: simply add opening parameterless action element
ActiveText.AddElem(
TActiveTextFactory.CreateActionElem(TagId, fsOpen)
);
end;
end;
ttsCompoundEndTag:
begin
// End of an action element
// Get tag id and note if tag should have a parameter
TagId := TActiveTextElemKind(fLexer.TagCode);
TREMLInfo.TagInfo.LookupParamName(TagId, ParamName);
if ParamName <> '' then
begin
// We should have a param which must be stored in closing action
// element, but closing REML tags have no parameters. We solve this
// by poppong the parameter value from the stack. This works because
// we use a stack for params and opening and closing tags are
// matched.
ParamValue := fParamStack.Pop;
// Add closing action element
ActiveText.AddElem(
TActiveTextFactory.CreateActionElem(TagId, ParamValue, fsClose)
);
end
else
begin
// No parameter: simple add closing parameterless action element
ActiveText.AddElem(
TActiveTextFactory.CreateActionElem(TagId, fsClose)
);
end;
end;
end;
end;
except
// Handle exceptions: convert expected exceptions to EActiveTextParserError
on E: ETaggedTextLexer do
raise EActiveTextParserError.Create(E.Message);
on E: EREMLEntities do
raise EActiveTextParserError.Create(E.Message);
else
raise;
end;
end;
function TREMLReader.TagInfo(const TagIdx: Integer; out TagName: string;
out TagCode: Word; out IsContainer: WordBool): Boolean;
{Callback that provides lexer with information about supported tags. Lexer
calls continually until False is returned.
@param TagIdx [in] Index of tag for which information is requested.
@param TagName [out] Set to name of tag.
@param TagCode [out] Set to unique code that identifies tag.
@param IsContainer [out] Always set to True to indicate container tag.
@return True if tag information was provided or False to indicate no more
tags.
}
begin
Result := TagIdx < TREMLInfo.TagInfo.Count;
if Result then
begin
TagName := TREMLInfo.TagInfo.Names[TagIdx];
TagCode := Ord(TREMLInfo.TagInfo.Ids[TagIdx]);
IsContainer := True;
end;
end;
{ TREMLWriter }
constructor TREMLWriter.InternalCreate(const Version: TREMLVersion);
{Internal class constructor. Sets up object to render using a required
version of REML.
@param Version [in] Version of REML to write.
}
begin
inherited InternalCreate;
fVersion := Version;
end;
class function TREMLWriter.Render(const ActiveText: IActiveText;
const REMLVer: TREMLVersion): string;
{Renders REML representation of an active text object.
@param ActiveText [in] Active text to be rendered.
@param REMLVer [in] Version of REML to be written.
@return String containing REML markup.
}
var
Elem: IActiveTextElem; // each element in active text object
TextElem: IActiveTextTextElem; // an active text text element
TagElem: IActiveTextActionElem; // an active text action element
begin
with InternalCreate(REMLVer) do
try
Result := '';
for Elem in ActiveText do
begin
if Supports(Elem, IActiveTextTextElem, TextElem) then
Result := Result + RenderText(TextElem)
else if Supports(Elem, IActiveTextActionElem, TagElem) then
Result := Result + RenderTag(TagElem);
end;
finally
Free;
end;
end;
function TREMLWriter.RenderTag(
const TagElem: IActiveTextActionElem): string;
{Renders an active text action element as a REML tag.
@param TagElem [in] Active text action element to be rendered.
@return Required REML tag.
}
var
TagName: string; // name of tag
ParamName: string; // name of any parameter
begin
if not TREMLInfo.TagInfo.LookupTagName(TagElem.Kind, TagName) then
raise EBug.CreateFmt('%s.RenderTag: Invalid REML tag id', [ClassName]);
Result := '';
TREMLInfo.TagInfo.LookupParamName(TagElem.Kind, ParamName);
case TagElem.State of
fsClose:
// closing tag
Result := Format('</%s>', [TagName]);
fsOpen:
begin
// opening tag: may have a parameter
if ParamName ='' then
Result := Format('<%s>', [TagName])
else
// have a parameter: value must be safely encoded
Result := Format(
'<%0:s %1:s="%2:s">',
[TagName, ParamName, TextToREMLText(TagElem.Param)]
);
end;
end;
end;
function TREMLWriter.RenderText(
const TextElem: IActiveTextTextElem): string;
{Renders an active text text element. Illegal characters are converted to
REML character entities.
@param TextElem [in] Active text text element.
@return REML-safe text containing necessary character entities.
}
begin
Result := TextToREMLText(TextElem.Text);
end;
function TREMLWriter.TextToREMLText(const Text: string): string;
{Converts plain text to REML compatible text by replacing illegal characters
with related character entities.
@param Text [in] Plain text to be converted.
@return Converted text.
}
var
Ch: Char; // each character in plain text
Entity: string; // stores each required entity
begin
Result := '';
for Ch in Text do
begin
Entity := TREMLInfo.EntityInfo.MapToEntity(Ch, fVersion);
if Entity = '' then
Result := Result + Ch
else
Result := Result + '&' + Entity + ';';
end;
end;
{ TREMLInfo }
class function TREMLInfo.EntityInfo: TREMLEntities;
{Provides a reference to object that provides information about REML character
entities.
@return Required object reference.
}
begin
if not Assigned(fEntities) then
begin
fEntities := TREMLEntities.Create;
fEntitiesWrapper := TAutoObjFree.Create(fEntities);
end;
Result := fEntities;
end;
class function TREMLInfo.TagInfo: TREMLTags;
{Provides a reference to object that provides information about a REML tag.
@return Required object reference.
}
begin
if not Assigned(fTags) then
begin
fTags := TREMLTags.Create;
fTagsWrapper := TAutoObjFree.Create(fTags);
end;
Result := fTags;
end;
{ TREMLTags }
constructor TREMLTags.Create;
{Class constructor. Sets up map of REML tags.
}
begin
inherited Create;
// Record all supported tags
SetLength(fTagMap, 8);
fTagMap[0] := TREMLTag.Create(ekLink, 1, 'a', 'href');
fTagMap[1] := TREMLTag.Create(ekStrong, 1, 'strong');
fTagMap[2] := TREMLTag.Create(ekEm, 2, 'em');
fTagMap[3] := TREMLTag.Create(ekVar, 2, 'var');
fTagMap[4] := TREMLTag.Create(ekPara, 2, 'p');
fTagMap[5] := TREMLTag.Create(ekWarning, 2, 'warning');
fTagMap[6] := TREMLTag.Create(ekHeading, 2, 'heading');
fTagMap[7] := TREMLTag.Create(ekMono, 2, 'mono');
end;
destructor TREMLTags.Destroy;
{Class destructor. Clears tag map.
}
begin
SetLength(fTagMap, 0);
inherited;
end;
function TREMLTags.GetCount: Integer;
{Read accessor for Count property.
@return Number of supported tags.
}
begin
Result := Length(fTagMap);
end;
function TREMLTags.GetId(Idx: Integer): TActiveTextElemKind;
{Read accessor for Ids[] property.
@param Idx [in] Zero based index of required id.
@return Required id.
}
begin
Result := fTagMap[Idx].Id;
end;
function TREMLTags.GetName(Idx: Integer): string;
{Read accessor for Names[] property,
@param Idx [in] Zero based index of required tag name.
@return Required tag name.
}
begin
Result := fTagMap[Idx].TagName;
end;
function TREMLTags.IndexOfTagId(const Id: TActiveTextElemKind): Integer;
{Finds index of a tag id in tag map.
@param Id [in] Tag id to be found.
@return Index of tag id or -1 if tag id not found.
}
var
Idx: Integer; // loops through tag map
begin
Result := -1;
for Idx := 0 to Pred(Length(fTagMap)) do
begin
if fTagMap[Idx].Id = Id then
begin
Result := Idx;
Exit;
end;
end;
end;
function TREMLTags.LookupParamName(const Id: TActiveTextElemKind;
out ParamName: string): Boolean;
{Looks up a parameter name of an identified REML tag.
@param Id [in] Id of required tag.
@param ParamName [out] Set to name of parameter name. '' if tag has no
parameter or if tag id is not valid.
@return True if tag is valid, False if not.
}
var
Idx: Integer; // Index of tag in map
begin
Idx := IndexOfTagId(Id);
Result := Idx >= 0;
if Result then
ParamName := fTagMap[Idx].ParamName
else
ParamName := '';
end;
function TREMLTags.LookupTagName(const Id: TActiveTextElemKind;
out TagName: string): Boolean;
{Looks up name of a tag.
@param Id [in] Id of tag.
@param TagName [out] Name of tag or '' if unknown id.
@return True if tag id is valid, False if not.
}
var
Idx: Integer; // Index of tag in map
begin
Idx := IndexOfTagId(Id);
Result := Idx >= 0;
if Result then
TagName := fTagMap[Idx].TagName
else
TagName := '';
end;
function TREMLTags.LookupTagVersion(const Id: TActiveTextElemKInd;
out Version: TREMLVersion): Boolean;
{Looks up REML version when tag was introduced.
@param Id [in] Id of tag.
@param Version [out] REML version when tag was introduced.
@return True if tag id is valid, False if not.
}
var
Idx: Integer; // Index of tag in map
begin
Idx := IndexOfTagId(Id);
Result := Idx >= 0;
if Result then
Version := fTagMap[Idx].Version
else
Version := Low(TREMLVersion);
end;
{ TREMLTags.TREMLTag }
constructor TREMLTags.TREMLTag.Create(const AId: TActiveTextElemKind;
const AVersion: TREMLVersion; const ATagName, AParamName: string);
{Record contructor. Initialises fields.
@param AId [in] Active text element kind.
@param AVersion [in] REML version where tag introduced.
@param ATagName [in] REML tag name.
@param AParamName [in] Optional name of parameter.
}
begin
Id := AId;
Version := AVersion;
TagName := ATagName;
ParamName := AParamName;
end;
{ TREMLEntities }
function TREMLEntities.CharToMnemonicEntity(const Ch: Char;
const Ver: TREMLVersion): string;
{Gets the mnemonic character entity that represents a character.
@param Entity [in] Character for which equivalent entity is required.
@param Ver [in] Version of REML for which entity is required.
@return Required entity or '' if character has no matching mnemonic entity.
}
var
Idx: Integer; // loops thru table of entity / characters
begin
Result := '';
for Idx := Low(fEntityMap) to High(fEntityMap) do
begin
if (fEntityMap[Idx].Version <= Ver) and (fEntityMap[Idx].Ch = Ch) then
begin
Result := fEntityMap[Idx].Entity;
Break;
end;
end;
end;
constructor TREMLEntities.Create;
{Class constructor. Creates map of mnemonic entities to equivalent characters.
}
begin
inherited Create;
SetLength(fEntityMap, 5);
// Record all supported character entities
fEntityMap[0] := TREMLEntity.Create('amp', '&', 1);
fEntityMap[1] := TREMLEntity.Create('quot', '"', 1);
fEntityMap[2] := TREMLEntity.Create('gt', '>', 1);
fEntityMap[3] := TREMLEntity.Create('lt', '<', 1);
fEntityMap[4] := TREMLEntity.Create('copy', #$A9, 2);
end;
destructor TREMLEntities.Destroy;
{Class destructor. Tidies up object.
}
begin
SetLength(fEntityMap, 0);
inherited;
end;
function TREMLEntities.GetChar(Idx: Integer): Char;
{Read accessor for Chars[] property.
@param Idx [in] Zero based index of required character.
@return Required character.
}
begin
Result := fEntityMap[Idx].Ch;
end;
function TREMLEntities.GetCount: Integer;
{Read accessor for Count property.
@return Number of supported tags.
}
begin
Result := Length(fEntityMap);
end;
function TREMLEntities.GetEntity(Idx: Integer): string;
{Read accessor for Entities[] property.
@param Idx [in] Zero based index of required entity.
@return Required entity.
}
begin
Result := fEntityMap[Idx].Entity;
end;
function TREMLEntities.MapToEntity(const Ch: Char;
const Ver: TREMLVersion): string;
{Maps a character to a character entity if appropriate.
@param Ch [in] Character to be mapped.
@param Ver [in] Version of REML for which entity is required.
@return Mnemonic entity if one exists, character itself if it is printable
and has ascii value less than 127, or a numeric character otherwise.
}
begin
Result := CharToMnemonicEntity(Ch, Ver);
if (Result = '') and ( (Ord(Ch) <= 31) or (Ord(Ch) >= 127) ) then
Result := '#' + IntToStr(Ord(Ch));
end;
{ TREMLEntities.TREMLEntity }
constructor TREMLEntities.TREMLEntity.Create(const AEntity: string;
const ACh: Char; const AVersion: TREMLVersion);
{Record constructor. Initialises record.
@param AEntity [in] Mnemonic entity.
@param ACh [in] Equivalent character.
@param AVersion [in] REML version where entity introduced.
}
begin
Entity := AEntity;
Ch := ACh;
Version := AVersion;
end;
{ TREMLAnalyser }
class function TREMLAnalyser.LowestWriterVersion(
const ActiveText: IActiveText): TREMLVersion;
{Determines lowest possible version REML that can be used to write some
active text.
@param ActiveText [in] Active text to be analysed.
@return Minimum required REML version.
}
var
Elem: IActiveTextElem; // each element in active text object
TagElem: IActiveTextActionElem; // an active text action element
TagVer: TREMLVersion; // REML version when action element introduced
begin
// Note: we can ignore checking for &copy; entity, introduced at ver 2, since
// equivalent character in active text can be written in &#999; format by
// ealier versions. Presence of &copy; is only critical when reading REML.
Result := FIRST_VERSION;
for Elem in ActiveText do
begin
if Supports(Elem, IActiveTextActionElem, TagElem) and
(TagElem.State = fsOpen) then
begin
if not TREMLInfo.TagInfo.LookupTagVersion(Elem.Kind, TagVer) then
raise EBug.Create(ClassName + '.LowestWriterVersion: TagVer not found');
if TagVer > Result then
Result := TagVer;
// special case of <a href="file://...">
if (Result < 3) and (TagElem.Kind = ekLink) and
AnsiStartsText('file://', TagElem.Param) then
Result := 3;
end;
if Result = LATEST_VERSION then
Exit;
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.