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 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
|
{
Copyright 1998-2018 PasDoc developers.
This file is part of "PasDoc".
"PasDoc" is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"PasDoc" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with "PasDoc"; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
----------------------------------------------------------------------------
}
{ @abstract(Collects information about available @@-tags and can parse text
with tags.) }
unit PasDoc_TagManager;
{$I pasdoc_defines.inc}
interface
uses
SysUtils,
Classes,
PasDoc_Types,
PasDoc_ObjectVector;
type
TTagManager = class;
TTag = class;
{ @seealso TTag.Execute }
TTagExecuteEvent = procedure(ThisTag: TTag; var ThisTagData: TObject;
EnclosingTag: TTag; var EnclosingTagData: TObject;
const TagParameter: string; var ReplaceStr: string) of object;
{ @seealso TTag.AllowedInside }
TTagAllowedInsideEvent = procedure(
ThisTag: TTag; EnclosingTag: TTag; var Allowed: boolean) of object;
TStringConverter = function(const s: string): string of object;
TTagOption = (
{ This means that tag expects parameters. If this is not included
in TagOptions then tag should not be given any parameters,
i.e. TagParameter passed to @link(TTag.Execute) should be ''.
We will display a warning if user will try to give
some parameters for such tag. }
toParameterRequired,
{ This means that parameters of this tag will be expanded
before passing them to @link(TTag.Execute).
This means that we will expand recursive tags inside
parameters, that we will ConvertString inside parameters,
that we will handle paragraphs inside parameters etc. ---
all that does @link(TTagManager.Execute).
If toParameterRequired is not present in TTagOptions then
it's not important whether you included toRecursiveTags.
It's useful for some tags to include toParameterRequired
without including toRecursiveTags, e.g. @@longcode or @@html,
that want to get their parameters "verbatim", not processed.
@bold(If toRecursiveTags is not included in tag options:)
Then @italic(everything) is allowed within parameter of this tag,
but nothing is interpreted. E.g. you can freely use @@ char,
and even write various @@-tags inside @@html tag --- this doesn't
matter, because @@-tags will not be interpreted (they will
not be even searched !) inside @@html tag. In other words,
@@ character means literally "@@" inside @@html, nothing more.
The only exception are double @@@@, @@( and @@): we still treat them
specially, to allow escaping the default parenthesis matching rules.
Unless toRecursiveTagsManually is present. }
toRecursiveTags,
{ Use this, instead of toRecursiveTags, if the implementation of your
tag calls (always!) TagManager.CoreExecute on given TagParameter.
This means that your tag is expanded recursively (it handles @-tags inside),
but you do it manually (instead of allowing toRecursiveTags to do the job).
In this case, TagParameter given will be really absolutely unmodified
(even the special @@@@, @@( and @@) will not be handled),
because we know that it will be handled later by special CoreExecute call.
Never use both flags toRecursiveTags and toRecursiveTagsManually. }
toRecursiveTagsManually,
{ This is meaningful only if toRecursiveTags is included.
Then toAllowOtherTagsInsideByDefault determines
are other tags allowed by the default implementation
of @link(TTag.AllowedInside). }
toAllowOtherTagsInsideByDefault,
{ This is meaningful only if toRecursiveTags is included.
Then @name says that normal text is allowed
inside parameter of this tag.
@italic("Normal text") is anything except other @@-tags:
normal text, paragraph breaks, various dashes, URLs,
and literal @@ character (expressed by @@@@ in descriptions).
If @name will not be included,
then normal text (not enclosed within other @@-tags) will
not be allowed inside. Only whitespace will be allowed, and
it will be ignored anyway (i.e. will not be passed to
ConvertString, empty line will not produce any Paragraph etc.).
This is useful for tags like @@orderedList that should only contain
other @@item tags inside. }
toAllowNormalTextInside,
{ This is useful for tags like @@raises and @@param that treat
1st word of their descriptions very specially
(where "what exactly is the 1st word" is defined by the
@link(ExtractFirstWord) function). This tells pasdoc to
leave the beginning of tag parameter (the first word and
the eventual whitespace before it) as it is in the parameter.
Don't search there for @@-tags, URLs, @-- or other special dashes,
don't insert paragraphs, don't try to auto-link it.
This is meaningful only if toRecursiveTags is included
(otherwise the whole tag parameters are always preserved "verbatim").
TODO: in the future TTagExecuteEvent should just get this
"first word" as a separate parameter, separated from TagParameters.
Also, this word should not be converted by ConvertString. }
toFirstWordVerbatim);
TTagOptions = set of TTagOption;
TTag = class
private
FOnPreExecute: TTagExecuteEvent;
FOnExecute: TTagExecuteEvent;
FTagOptions: TTagOptions;
FName: string;
FTagManager: TTagManager;
FOnAllowedInside: TTagAllowedInsideEvent;
public
{ Note that AName will be converted to lowercase before assigning
to Name. }
constructor Create(ATagManager: TTagManager;
const AName: string;
AOnPreExecute: TTagExecuteEvent;
AOnExecute: TTagExecuteEvent;
const ATagOptions: TTagOptions);
property TagOptions: TTagOptions read FTagOptions write FTagOptions;
{ TagManager that will recognize and handle this tag.
Note that the tag instance is owned by this tag manager
(i.e. it will be freed inside this tag manager).
It can be nil if no tag manager currently owns this tag.
Note that it's very useful in @link(Execute) or
@link(OnExecute) implementations.
E.g. you can use it to report a message
by @code(TagManager.DoMessage(...)), this is e.g. used
by implementation of TPasItem.StoreAbstractTag.
You could also use this to manually force recursive
behavior of a given tag. I.e let's suppose that you
have a tag with TagOptions = [toParameterRequired],
so the TagParameter parameter passed to handler was
not recursively expanded. Then you can do inside your handler
@longcode# NewTagParameter := TagManager.Execute(TagParameter, ...) #
and this way you have explicitly recursively expanded the tag.
Scenario above is actually used in implementation of @@noAutoLink
tag. There I call TagManager.Execute with parameter
@code(AutoLink) set to false thus preventing auto-linking
inside text within @@noAutoLink. }
property TagManager: TTagManager read FTagManager;
{ Name of the tag, that must be specified by user after the "@@" sign.
Value of this property must always be lowercase. }
property Name: string read FName write FName;
property OnPreExecute: TTagExecuteEvent
read FOnPreExecute write FOnPreExecute;
property OnExecute: TTagExecuteEvent
read FOnExecute write FOnExecute;
{ This is completely analogous to @link(Execute) but used when
@link(TTagManager.PreExecute) is @true.
In this class this simply calls @link(OnPreExecute). }
procedure PreExecute(var ThisTagData: TObject;
EnclosingTag: TTag; var EnclosingTagData: TObject;
const TagParameter: string; var ReplaceStr: string); virtual;
{ This will be used to do main work when this
@@-tag occured in description.
EnclosingTag parameter specifies enclosing tag. This
is useful for tags that must behave differently in different
contexts, e.g. in plain-text output @@item tag will behave
differently inside @@orderedList and @@unorderedList.
EnclosingTag is nil when the tag occured at top level of the
description.
ThisTagData and EnclosingTagData form a mechanism to pass
arbitraty data between child tags enclosed within one
parent tag. Example uses:
@unorderedList(
@item(This is the way for multiple @@item tags
inside @@orderedList tag to count themselves (to provide
list item numbers, for pasdoc output formats that can't
automatically number list items).)
@item(This is the way for
@@itemSpacing tag to communicate with enclosing
@@orderedList tag to specify list style. )
@item(And this is the way for @@cell tags to be collected
inside rows data and then @@rows tags to be collected
inside table data. Thanks to such collecting
@link(TDocGenerator.FormatTable) receives at once all
information about given table, and can use it to format
table.)
)
How does this XxxTagData mechanism work:
When we start parsing parameter of some tag with
toRecursiveTags, we create a new pointer inited to
@link(CreateOccurenceData).
When @@-tags occur inside this parameter, we pass them
this pointer as EnclosingTagData (this way all @@-tags
with the same parent can use this pointer to communicate
with each other). At the end, when parameter was parsed,
we call given tag's Execute method passing the resulting
pointer as ThisTagData (this way @@-tags with the same parent
can use this pointer to pass some data to their parent).
In this class this method simply calls @link(OnExecute)
(if assigned). }
procedure Execute(var ThisTagData: TObject;
EnclosingTag: TTag; var EnclosingTagData: TObject;
const TagParameter: string; var ReplaceStr: string); virtual;
property OnAllowedInside: TTagAllowedInsideEvent
read FOnAllowedInside write FOnAllowedInside;
{ This will be checked always when this tag occurs within description.
Given EnclosingTag is enclosing tag, nil if we're in top level.
If this returns false then this tag will not be allowed inside
EnclosingTag.
In this class this method
@orderedList(
@item(
Assumes that Result = true if we're at top level
or EnclosingTag.TagOptions contains
toAllowOtherTagsInsideByDefault.
Else it assumes Result = false.)
@item(
Then it calls @link(OnAllowedInside
OnAllowedInside(Self, EnclosingTag, Result))
(if OnAllowedInside is assigned).)
) }
function AllowedInside(EnclosingTag: TTag): boolean; virtual;
{ In this class this simply returns @nil. }
function CreateOccurenceData: TObject; virtual;
{ In this class this simply does @code(Value.Free). }
procedure DestroyOccurenceData(Value: TObject); virtual;
end;
TTopLevelTag = class(TTag)
{ This returns just @code(EnclosingTag = nil).
Which means that this tag is allowed only at top level of
description, never inside parameter of some tag. }
function AllowedInside(EnclosingTag: TTag): boolean; override;
end;
TNonSelfTag = class(TTag)
{ This returns just @code(inherited and (EnclosingTag <> Self)).
Which means that (assuming that @link(OnAllowedInside)
is not assigned) this tag is allowed at top level of
description and inside parameter of any tag
@italic(but not within itself and not within tags
without toAllowOtherTagsInsideByDefault).
This is currently not used by any tag. }
function AllowedInside(EnclosingTag: TTag): boolean; override;
end;
{ All Items of this list must be non-nil TTag objects. }
TTagVector = class(TObjectVector)
{ Case of Name does @italic(not) matter (so don't bother converting it to
lowercase or something like that before using this method).
Returns nil if not found.
Maybe in the future it will use hashlist, for now it's not needed. }
function FindByName(const Name: string): TTag;
end;
TTryAutoLinkEvent = procedure(TagManager: TTagManager;
const QualifiedIdentifier: TNameParts;
out QualifiedIdentifierReplacement: string;
var AutoLinked: boolean) of object;
TTagManager = class
private
FTags: TTagVector;
FConvertString: TStringConverter;
FAbbreviations: TStringList;
FOnMessage: TPasDocMessageEvent;
FParagraph: string;
FSpace: string;
FShortDash, FEnDash, FEmDash: string;
FURLLink: TStringConverter;
FOnTryAutoLink: TTryAutoLinkEvent;
FPreExecute: boolean;
FMarkdown: boolean;
function DoConvertString(const s: string): string;
function DoURLLink(const s: string): string;
procedure Unabbreviate(var s: string);
function TryAutoLink(const QualifiedIdentifier: TNameParts;
out QualifiedIdentifierReplacement: string): boolean;
public
constructor Create;
destructor Destroy; override;
{ Call OnMessage (if assigned) with given params. }
procedure DoMessage(const AVerbosity: Cardinal;
const MessageType: TPasDocMessageType; const AMessage: string;
const AArguments: array of const);
{ Call @link(DoMessage) only if @link(PreExecute) is @false. }
procedure DoMessageNonPre(const AVerbosity: Cardinal;
const MessageType: TPasDocMessageType; const AMessage: string;
const AArguments: array of const);
{ This will be used to print messages from within @link(Execute).
Note that in this unit we essentialy "don't know"
that parsed Description string is probably attached to some TPasItem.
It's good that we don't know it (because it makes this class more flexible).
But it also means that OnMessage that you assign here may want to add
to passed AMessage something like + ' (Expanded_TPasItem_Name)',
see e.g. TDocGenerator.DoMessageFromExpandDescription.
Maybe in the future we will do some descendant of this class,
like TTagManagerForPasItem. }
property OnMessage: TPasDocMessageEvent read FOnMessage write FOnMessage;
{ This will be inserted on paragraph marker (two consecutive newlines,
see wiki page WritingDocumentation) in the text.
This should specify how paragraphs are marked in particular
output format, e.g. html generator may set this to '<p>'.
Default value is ' ' (one space). }
property Paragraph: string read FParagraph write FParagraph;
{ This will be inserted on each whitespace sequence (but not on
paragraph break). This is consistent with
[https://github.com/pasdoc/pasdoc/wiki/WritingDocumentation]
that clearly says that "amount of whitespace does not matter".
Although in some pasdoc output formats amount of whitespace also
does not matter (e.g. HTML and LaTeX) but in other (e.g. plain text)
it matters, so such space compression is needed.
In other output formats (no examples yet) it may need to be expressed
by something else than simple space, that's why this property
is exposed.
Default value is ' ' (one space). }
property Space: string read FSpace write FSpace;
{ This will be inserted on @code(@@@-) in description,
and on a normal single dash in description that is not a part
of en-dash or em-dash.
This should produce just a short dash.
Default value is '-'.
You will never get any '-' character to be converted by ConvertString.
Convertion of '-' is controlled solely by XxxDash properties of
tag manager.
@seealso EnDash
@seealso EmDash }
property ShortDash: string read FShortDash write FShortDash;
{ This will be inserted on @code(@--) in description.
This should produce en-dash (as in LaTeX).
Default value is '@--'. }
property EnDash: string read FEnDash write FEnDash;
{ This will be inserted on @code(@-@--) in description.
This should produce em-dash (as in LaTeX).
Default value is '@-@--'. }
property EmDash: string read FEmDash write FEmDash;
{ This will be called from @link(Execute) when URL will be found
in Description. Note that passed here URL will @italic(not) be processed by
@link(ConvertString).
This tells what to put in result on URL.
If this is not assigned, then ConvertString(URL) will be appended
to Result in @link(Execute). }
property URLLink: TStringConverter read FURLLink write FURLLink;
{ This should check does QualifiedIdentifier looks like a name
of some existing identifier. If yes, sets AutoLinked to true and
sets QualifiedIdentifierReplacement to a link to
QualifiedIdentifier (QualifiedIdentifierReplacement should be
ready to be put in final documentation, i.e. already in the
final output format). By default AutoLinked is false. }
property OnTryAutoLink: TTryAutoLinkEvent
read FOnTryAutoLink write FOnTryAutoLink;
{ This method is the very essence of this class and this unit.
It expands Description, which means that it processes Description
(text supplied by user in some comment in parsed unit)
into something ready to be included in output documentation.
This means that this handles parsing @@-tags, inserting
paragraph markers, recognizing URLs in Description and
correctly translating it, and translating rest of the "normal" text
via ConvertString.
If WantFirstSentenceEnd then we will look for '.' char
followed by any whitespace in Description.
Moreover, this '.' must be outside of any @@-tags
parameter. Under FirstSentenceEnd we will return the number
of beginning characters @italic(in the output string) that will
include correspong '.' character (note that this definition
takes into account that ConvertString may translate '.' into
something longer).
If no such character exists in Description, FirstSentenceEnd will
be set to Length(Result), so the whole Description will be treated
as it's first sentence.
If WantFirstSentenceEnd, FirstSentenceEnd will not be set. }
function Execute(const Description: string;
AutoLink: boolean;
WantFirstSentenceEnd: boolean;
out FirstSentenceEnd: Integer): string; overload;
{ This is equivalent to Execute(Description, AutoLink, false, Dummy) }
function Execute(const Description: string;
AutoLink: boolean): string; overload;
{ This is the underlying version of Execute. Use with caution!
If EnclosingTag = nil then this is understood to be
toplevel of description, which means that all tags are allowed inside.
If EnclosingTag <> nil then this is not toplevel.
EnclosingTagData returns collected data for given EnclosingTag.
You should init it to EnclosingTag.CreateOccurenceData.
It will be passed as EnclosingTagData to each of @@-tags
found inside Description. }
function CoreExecute(const Description: string;
AutoLink: boolean;
EnclosingTag: TTag; var EnclosingTagData: TObject;
WantFirstSentenceEnd: boolean;
out FirstSentenceEnd: Integer): string; overload;
function CoreExecute(const Description: string;
AutoLink: boolean;
EnclosingTag: TTag; var EnclosingTagData: TObject): string; overload;
property ConvertString: TStringConverter
read FConvertString write FConvertString;
property Abbreviations: TStringList read FAbbreviations write FAbbreviations;
{ When @name is @true, tag manager will work a little differently than usual:
@unorderedList(
@item(Instead of @link(TTag.Execute),
@link(TTag.PreExecute) will be called.)
@item(Various warnings will @italic(not) be reported.
Assumption is that you will later process the same text
with @name set to @false to get all the warnings.)
@item(AutoLink will not be used (like it was always false).
Also the result of @link(Execute) will be pretty much
random and meaningless (so you should ignore it).
Also this means that the TagParameter for tags with
toRecursiveTags should be ignored, because it will be
something incorrect. This means that only tags
without toRecursiveTags should actually use
TagParameter in their OnPreExecute handlers.
Assumption is that you actually don't care about the
result of @link(Execute) methods,
and you will later process the same text
with @name set to @false to get the proper output.
The goal is to make execution with PreExecute set to @true
as fast as possible.)
) }
property PreExecute: boolean
read FPreExecute write FPreExecute;
{ When @name is @true, Markdown syntax is considered }
property Markdown: boolean
read FMarkdown write FMarkdown default false;
end;
implementation
uses PasDoc_Utils, StrUtils;
type
TMarkdownBlock = record
Open, Close: string;
PasDocTag: string;
end;
const
MarkDownEscapeChar = '\';
// Defines for markdown blocks
// Markdown blocks are parts of text enclosed in two special fragments, identical or not.
// Markdown blocks are rendered only when:
// - Opening tag is preceded by ASCII whitespace or ASCII punctuation
// - Closing tag is followed by ASCII whitespace or ASCII punctuation
// One-char Markdown blocks (italic *, italic _, code `) must meet additional condition:
// - Opening tag is followed by non-ASCII whitespace
// to minimize false positives when block characters are used not for Markdown
// ( 'X = Y * Z', 'underscore _ is used to name some deprecated thing: something_').
// Logic beneath follows Occam's razor principle - as these blocks add styling to
// some content, this content shouldn't start and end with whitespaces.
// No Unicode categories used, just plain ASCII.
MarkdownBlocks: array [0..6] of TMarkdownBlock =
(
(Open: '```pascal'; Close: '```'; PasDocTag: 'longcode'), // must be checked first to not mess with ``` or single `
(Open: '```'; Close: '```'; PasDocTag: 'preformatted'), // must be checked first to not mess with single `
(Open: '`'; Close: '`'; PasDocTag: 'code'),
(Open: '**'; Close: '**'; PasDocTag: 'bold'),
(Open: '__'; Close: '__'; PasDocTag: 'bold'),
(Open: '*'; Close: '*'; PasDocTag: 'italic'),
(Open: '_'; Close: '_'; PasDocTag: 'italic')
);
ASCIIPunctuation = ['!', '"', '#', '$', '%', '&', '''', '(', ')', '*', '+', ',',
'-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\', ']', '^', '_',
'`', '{', '|', '}', '~'];
MarkdownBlockBoundaries = WhiteSpace + ASCIIPunctuation;
// Set of chars markdown blocks could start with. To speedup checking
// All first chars of MarkdownBlocks[x].Open must be here. This could be automated
// but it's not so complicated to keep this set actual
MarkdownBlockStartChars = ['`', '*', '_'];
// Defines for markdown URLs
// Format is [Description](http://...)
MarkdownURLDescrOpen = '[';
MarkdownURLDescrClose = ']';
MarkdownURLOpen = '(';
MarkdownURLClose = ')';
PasDocURLTag = 'url';
// Defines for markdown lists
MarkdownUListMarkers = ['-', '*'];
MarkdownOListMarkers = ['0'..'9'];
// Set of chars markdown lists could start with. To speedup checking
MarkdownListStartChars = MarkdownUListMarkers + MarkdownOListMarkers + WhiteSpaceNotNL;
MarkdownOListCloseChars = ['.', ')'];
PasDocListTags: array[boolean] of string =
('unorderedlist', 'orderedlist');
{ This checks whether we are looking (i.e. Description[Offset]
starts with) at a paragraph marker
(i.e. newline +
optional whitespace +
newline +
some more optional whitespaces and newlines)
and if it is so, returns true and sets OffsetEnd to the next
index in Description after this paragraph marker. }
function FindParagraph(const Description: string; Offset: Integer;
out OffsetEnd: Integer): boolean;
var i: Integer;
begin
Result := false;
i := Offset;
while SCharIs(Description, i, WhiteSpaceNotNL) do Inc(i);
if not SCharIs(Description, i, WhiteSpaceNL) then Exit;
{ In case newline is two-characters wide, read it to the end
(to not accidentally take #13#10 as two newlines.) }
Inc(i);
if (i <= Length(Description)) and
( ((Description[i-1] = #10) and (Description[i] = #13)) or
((Description[i-1] = #13) and (Description[i] = #10))
) then
Inc(i);
while SCharIs(Description, i, WhiteSpaceNotNL) do Inc(i);
if not SCharIs(Description, i, WhiteSpaceNL) then Exit;
{ OK, so we found 2nd newline. So we got paragraph marker.
Now read it to the end. }
Result := true;
while SCharIs(Description, i, WhiteSpace) do Inc(i);
OffsetEnd := i;
end;
{ This checks whether we are looking (i.e. Description[Offset]
starts with) at some char from Chars set.
If true, then it also sets OffsetEnd to next index after all chars from set. }
function FindChars(const Description: string; Offset: Integer;
const Chars: TCharSet; out OffsetEnd: Integer): boolean;
begin
Result := SCharIs(Description, Offset, Chars);
if Result then
begin
OffsetEnd := Offset + 1;
while SCharIs(Description, OffsetEnd, Chars) do Inc(OffsetEnd);
end;
end;
{ This checks whether we are looking (i.e. Description[Offset]
starts with) at some char NOT from Chars set.
If true, then it also sets OffsetEnd to next index after all chars NOT from set.
If no such char found, OffsetEnd = Length(Description) + 1 }
function FindCharsNot(const Description: string; Offset: Integer;
const Chars: TCharSet; out OffsetEnd: Integer): boolean;
var
StrLen: Integer;
begin
Result := not SCharIs(Description, Offset, Chars);
if Result then
begin
StrLen := Length(Description);
OffsetEnd := Offset + 1;
while (OffsetEnd <= StrLen) and not SCharIs(Description, OffsetEnd, Chars) do Inc(OffsetEnd);
end;
end;
{ This checks whether we are looking (i.e. Description[Offset]
starts with) at some markdown special char, considering escaping }
function IsMarkdownSpecialChar(const Description: string; Offset: Integer;
const Chars: TCharSet): boolean;
begin
Result := False;
if not SCharIs(Description, Offset, Chars) then Exit;
if (Offset > 1) and SCharIs(Description, Offset - 1, MarkDownEscapeChar) then Exit; // skip escaped chars
Result := True;
end;
{ This searches for markdown special sign, considering escaping.
Search starts from position Offset.
If found, then it also sets OffsetEnd to position of found sign.}
function FindMarkdownSpecialSign(const Description: string; Offset: Integer;
const Sign: string; out OffsetEnd: Integer): boolean;
// Check if special sign in Description at Offset is escaped
// (has strictly single `\` before)
function IsEscaped(const Description: string; Offset: Integer): Boolean;
var EscapesCount: Integer;
begin
case Offset of
1: Result := False; // 1st char
2: Result := Description[Offset - 1] = MarkDownEscapeChar; // 2nd char
else // 3rd char and further - check for escaped escape
begin
// Count consecutive escape chars. Each two mean '\' char
EscapesCount := 0;
while Offset > 1 do
begin
if Description[Offset - 1] = MarkDownEscapeChar then
Inc(EscapesCount)
else
Break;
Dec(Offset);
end;
Result := Odd(EscapesCount);
end;
end;
end;
var CurrOffset, SignPos: Integer;
begin
Result := False;
CurrOffset := Offset;
repeat
SignPos := PosEx(Sign, Description, CurrOffset);
if SignPos = 0 then Exit;
// skip escaped chars
if IsEscaped(Description, SignPos) then
begin
CurrOffset := SignPos + 1;
Continue;
end;
Result := True;
OffsetEnd := SignPos;
Break;
until False;
end;
{ This removes escaped special chars from Markdown string.
Algo is extremely primitive: just remove one "\" before any char.
This means no "smart" non-escape detection (like `AC\DC`) so escape char
must be escaped always (`AC\\DC`).
NOTE: currently this function is intended to run only for URL description. }
function MarkdownUnescape(const Description: string): string;
var
SrcIdx, DestIdx: Integer;
begin
// First check if we got any escape
if PosEx(MarkDownEscapeChar, Description) = 0 then
begin
Result := Description;
Exit;
end;
// Just copy src chars to dest one by one. Dumb and non-optimal but very simple.
// Loop until the char before the last one (!) to simplify code
SetLength(Result, Length(Description));
DestIdx := 1;
SrcIdx := 1;
while SrcIdx < Length(Description) do
begin
if Description[SrcIdx] = MarkDownEscapeChar then
Inc(SrcIdx);
Result[DestIdx] := Description[SrcIdx];
Inc(DestIdx);
Inc(SrcIdx);
end;
// Copy last char if not processed yet
if SrcIdx = Length(Description) then
begin
Result[DestIdx] := Description[SrcIdx];
Inc(DestIdx);
end;
SetLength(Result, DestIdx - 1);
end;
{
This checks if a known Markdown block starts at Description[Offset].
If yes, it returns true and sets
-- PasDocTagName to name of PasDoc tag corresponding to current Markdown block
-- Parameters to contents of this block
-- OffsetEnd to the index of *next* character in Description right
after this block
}
function CheckMarkdownBlock(const Description: string; Offset: Integer;
out PasDocTagName: string; out Parameters: string; out OffsetEnd: Integer): Boolean;
var
CurrOffset, BlockEndPos, MdBlockIdx: Integer;
begin
Result := False;
// Fast check if we have markdown block opening
if not IsMarkdownSpecialChar(Description, Offset, MarkdownBlockStartChars) then Exit;
if (Offset > 1) and not SCharIs(Description, Offset - 1, MarkdownBlockBoundaries) then Exit; // opening block must be preceded by a block delimiter char
MdBlockIdx := -1;
for BlockEndPos := Low(MarkdownBlocks) to High(MarkdownBlocks) do
if Copy(Description, Offset, Length(MarkdownBlocks[BlockEndPos].Open)) = MarkdownBlocks[BlockEndPos].Open then
begin
MdBlockIdx := BlockEndPos;
Break;
end;
if MdBlockIdx = -1 then Exit; { exit with false }
CurrOffset := Offset + Length(MarkdownBlocks[MdBlockIdx].Open);
// Check if this is one-char block and a whitespace follows - skip these cases
if (Length(MarkdownBlocks[MdBlockIdx].Open) = 1) and
SCharIs(Description, CurrOffset, WhiteSpace) then
Exit; { exit with false }
// now search for markdown block end
repeat
if not FindMarkdownSpecialSign(Description, CurrOffset, MarkdownBlocks[MdBlockIdx].Close, BlockEndPos) then
Exit; { exit with false }
// block must close with a block delimiter char or end of string
if BlockEndPos + Length(MarkdownBlocks[MdBlockIdx].Close) - 1 = Length(Description) then
Break;
if not SCharIs(Description, BlockEndPos + Length(MarkdownBlocks[MdBlockIdx].Close), MarkdownBlockBoundaries) then
begin
CurrOffset := BlockEndPos + 1;
Continue;
end;
Break;
until False;
PasDocTagName := MarkdownBlocks[MdBlockIdx].PasDocTag;
OffsetEnd := BlockEndPos + Length(MarkdownBlocks[MdBlockIdx].Close);
Parameters := Copy(Description, Offset + Length(MarkdownBlocks[MdBlockIdx].Open), BlockEndPos - (Offset + Length(MarkdownBlocks[MdBlockIdx].Open)));
Result := True;
end;
{
This checks if Markdown URL starts at Description[Offset].
If yes, it returns true and sets
-- PasDocTagName to name of PasDoc tag corresponding to current Markdown block
-- Parameters to contents of this block
-- OffsetEnd to the index of *next* character in Description right
after this block
}
function CheckMarkdownURL(const Description: string; Offset: Integer;
out PasDocTagName: string; out Parameters: string; out OffsetEnd: Integer): Boolean;
var
CurrOffset, BlockEndPos, Level: Integer;
URLDescr, URL: string;
Found: Boolean;
const
DescrEnd = MarkdownURLDescrClose + MarkdownURLOpen;
begin
Result := False;
// Fast check if we have markdown URL descr opening
if not IsMarkdownSpecialChar(Description, Offset, [MarkdownURLDescrOpen]) then Exit;
// Scan for contents
CurrOffset := Offset;
repeat
// Scan for end of description and start of URL
if not FindMarkdownSpecialSign(Description, CurrOffset, DescrEnd, BlockEndPos) then
Exit; { exit with false }
URLDescr := Copy(Description, CurrOffset + 1, BlockEndPos - Offset - 1);
CurrOffset := BlockEndPos + Length(DescrEnd);
// Scan for end of URL i.e. closing bracket. If opening bracket is encountered, skip to next
// closing bracket as it's part of an URL
Level := 0; Found := False; BlockEndPos := CurrOffset;
while BlockEndPos < Length(Description) do
case Description[BlockEndPos] of
MarkdownURLOpen:
begin
Inc(Level);
Inc(BlockEndPos);
Continue;
end;
MarkdownURLClose:
if Level = 0 then
begin
Found := True;
Break;
end
else
begin
Dec(Level);
Inc(BlockEndPos);
Continue;
end;
else
Inc(BlockEndPos);
end;
if not Found then
Exit; { exit with false }
URL := Copy(Description, CurrOffset, BlockEndPos - CurrOffset);
Break;
until False;
PasDocTagName := PasDocURLTag;
OffsetEnd := BlockEndPos + 1;
Parameters := URL + ' ' + MarkdownUnescape(URLDescr);
Result := True;
end;
{
This checks if Markdown list item starts at Description[Offset].
Whitespaces are the part of list so Offset could point at one or more whitespaces.
If yes, it returns true and sets
-- Ordered to true of list is ordered, false otherwise
-- OffsetEnd to the index of *next* non-whitespace character in Description right
after list marker
-- IndentLen to count of whitespace chars between start of line and list marker
(for tracking nested lists).
Markdown list items have the following pattern:
[WSP]MARKER[contents]
MARKER is:
'*'|'-'WSP for ordered lists
DIGIT*N'.'|')'WSP for ordered lists
where
WSP = tab #9 or space #32
DIGIT = 0..9
}
function CheckMarkdownListItemStart(const Description: string; Offset: Integer;
out Ordered: boolean; out OffsetEnd: Integer; out IndentLen: Integer): boolean;
var MarkerOffset, NewOffsetEnd: Integer;
begin
Result := False;
// List item must start from new line
if (Offset > 1) and not SCharIs(Description, Offset - 1, WhiteSpaceNL) then Exit;
// Skip whitespaces also getting indent length
MarkerOffset := Offset;
FindChars(Description, Offset, WhiteSpaceNotNL, MarkerOffset);
// Unordered list - "* item"
if SCharIs(Description, MarkerOffset, MarkdownUListMarkers) then
begin
// Whitespace is obligatory
if not FindChars(Description, MarkerOffset + 1, WhiteSpaceNotNL, NewOffsetEnd) then
Exit;
Ordered := False;
OffsetEnd := NewOffsetEnd;
IndentLen := MarkerOffset - Offset;
Result := True;
end
// Ordered list - "1. item"
else if SCharIs(Description, MarkerOffset, MarkdownOListMarkers) then
begin
// Skip probably multiple digits
FindChars(Description, MarkerOffset, MarkdownOListMarkers, NewOffsetEnd);
// Closing char and Whitespace are obligatory
if not SCharIs(Description, NewOffsetEnd, MarkdownOListCloseChars) then
Exit;
Inc(NewOffsetEnd);
if not FindChars(Description, NewOffsetEnd, WhiteSpaceNotNL, NewOffsetEnd) then
Exit;
Ordered := True;
OffsetEnd := NewOffsetEnd;
IndentLen := MarkerOffset - Offset;
Result := True;
end;
end;
{
This checks if Markdown list item starts at Description[Offset].
Whitespaces are the part of list so Offset could point at one or more whitespaces.
If yes, it returns true and sets
-- PasDocTagName to name of PasDoc tag corresponding to current Markdown block
-- Parameters to contents of this block
-- OffsetEnd to the index of *next* character in Description right
after this block
-- LastItemInList to true if list end signature was found
}
function CheckMarkdownListItem(const Description: string; Offset: Integer;
out PasDocTagName: string; out Parameters: string; out OffsetEnd: Integer;
out LastItemInList: boolean): boolean;
var
BDummy, WasDoubleNL: boolean;
StrLen, NewOffsetEnd, IndentLen, IndentWithMarkerLen, OtherIndentLen: Integer;
begin
Result := CheckMarkdownListItemStart(Description, Offset, BDummy, OffsetEnd, IndentLen);
if not Result then Exit;
PasDocTagName := 'item';
LastItemInList := False;
IndentWithMarkerLen := OffsetEnd - Offset;
Offset := OffsetEnd; // start index of item contents
StrLen := Length(Description);
// consume line
FindCharsNot(Description, Offset, WhiteSpaceNL, OffsetEnd);
// now search for end of item content - it must be
// - signature of a next list item with the same (next item of the same list)
// or lesser (item of parent list / start of a new list) indent
// - empty line and optionally a text with lesser indent length
// - end of string
repeat
// check if we have double-NL which could finalize the list (or not)
WasDoubleNL := FindParagraph(Description, OffsetEnd, NewOffsetEnd);
if WasDoubleNL then
begin
// FindParagraph eats whitespaces after newline so we've to take them back
while (NewOffsetEnd > 1) and not SCharIs(Description, NewOffsetEnd - 1, WhiteSpaceNL) do
Dec(NewOffsetEnd);
OffsetEnd := NewOffsetEnd;
end
else
begin
// consume all NL's
FindChars(Description, OffsetEnd, WhiteSpaceNL, OffsetEnd);
if OffsetEnd > StrLen then
Break;
end;
// check for list item start
if CheckMarkdownListItemStart(Description, OffsetEnd, BDummy, NewOffsetEnd, OtherIndentLen) then
begin
// indents equal => it's the next item of current list, finish the loop
if IndentLen = OtherIndentLen then
Break
else
// indent of current item greater than new => it's the next item of parent list, finish the loop
if IndentLen > OtherIndentLen then
begin
LastItemInList := True;
Break;
end
else
// new indent is greater => nested list. Consume line
begin
FindCharsNot(Description, OffsetEnd, WhiteSpaceNL, OffsetEnd);
Continue;
end;
end
else
// it's just a text. Check if it belongs to current item
begin
// All text not separated with NL*2 goes to current item
if not WasDoubleNL then
begin
FindCharsNot(Description, OffsetEnd, WhiteSpaceNL, OffsetEnd);
Continue;
end;
// Text separated with NL*2 is checked for indent length. Those which have
// indent of the same or greater length go to current item
FindChars(Description, OffsetEnd, WhiteSpaceNotNL, NewOffsetEnd);
OtherIndentLen := NewOffsetEnd - OffsetEnd;
if OtherIndentLen >= IndentWithMarkerLen then
begin
FindCharsNot(Description, OffsetEnd, WhiteSpaceNL, OffsetEnd);
Continue;
end;
// NL*2 + text with lesser indent - the list is finished
LastItemInList := True;
Break;
end;
until False;
if OffsetEnd > StrLen then
LastItemInList := True;
// OffsetEnd includes newlines after item content so trim them
Parameters := TrimRight(Copy(Description, Offset, OffsetEnd - Offset));
end;
{
This checks if Markdown list starts at Description[Offset].
Whitespaces are the part of list so Offset could point at one or more whitespaces.
If yes, it returns true and sets
-- PasDocTagName to name of PasDoc tag corresponding to current Markdown block
-- Parameters to contents of this block
-- OffsetEnd to the index of *next* character in Description right
after this block
}
function CheckMarkdownList(const Description: string; Offset: Integer;
out PasDocTagName: string; out Parameters: string; out OffsetEnd: Integer): boolean;
var
Ordered, LastItemInList: boolean;
IDummy, CurrOffset: Integer;
SDummy: string;
begin
// Quick check
Result := False;
if not SCharIs(Description, Offset, MarkdownListStartChars) then Exit;
// Parser eats whitespaces after newline so we've to take them back
while (Offset > 1) and not SCharIs(Description, Offset - 1, WhiteSpaceNL) do
Dec(Offset);
Result := CheckMarkdownListItemStart(Description, Offset, Ordered, OffsetEnd, IDummy);
if not Result then Exit;
PasDocTagName := PasDocListTags[Ordered];
CurrOffset := Offset;
while CheckMarkdownListItem(Description, CurrOffset, SDummy, SDummy, OffsetEnd, LastItemInList) and
not LastItemInList do
CurrOffset := OffsetEnd;
Parameters := TrimRight(Copy(Description, Offset, OffsetEnd - Offset));
end;
{ TTag ------------------------------------------------------------ }
constructor TTag.Create(ATagManager: TTagManager;
const AName: string;
AOnPreExecute: TTagExecuteEvent;
AOnExecute: TTagExecuteEvent;
const ATagOptions: TTagOptions);
begin
inherited Create;
FName := LowerCase(AName);
FOnPreExecute := AOnPreExecute;
FOnExecute := AOnExecute;
FTagOptions := ATagOptions;
FTagManager := ATagManager;
if TagManager <> nil then
TagManager.FTags.Add(Self);
end;
procedure TTag.PreExecute(var ThisTagData: TObject;
EnclosingTag: TTag; var EnclosingTagData: TObject;
const TagParameter: string; var ReplaceStr: string);
begin
if Assigned(OnPreExecute) then
OnPreExecute(Self, ThisTagData, EnclosingTag, EnclosingTagData,
TagParameter, ReplaceStr);
end;
procedure TTag.Execute(var ThisTagData: TObject;
EnclosingTag: TTag; var EnclosingTagData: TObject;
const TagParameter: string; var ReplaceStr: string);
begin
if Assigned(OnExecute) then
OnExecute(Self, ThisTagData, EnclosingTag, EnclosingTagData,
TagParameter, ReplaceStr);
end;
function TTag.AllowedInside(EnclosingTag: TTag): boolean;
begin
Result := (EnclosingTag = nil) or
(toAllowOtherTagsInsideByDefault in EnclosingTag.TagOptions);
if Assigned(OnAllowedInside) then
OnAllowedInside(Self, EnclosingTag, Result);
end;
function TTag.CreateOccurenceData: TObject;
begin
Result := nil;
end;
procedure TTag.DestroyOccurenceData(Value: TObject);
begin
Value.Free;
end;
{ TTopLevelTag ---------------------------------------------------------- }
function TTopLevelTag.AllowedInside(EnclosingTag: TTag): boolean;
begin
Result := EnclosingTag = nil;
end;
{ TNonSelfTag ----------------------------------------------------------- }
function TNonSelfTag.AllowedInside(EnclosingTag: TTag): boolean;
begin
Result := inherited AllowedInside(EnclosingTag) and (EnclosingTag <> Self);
end;
{ TTagVector ------------------------------------------------------------ }
function TTagVector.FindByName(const Name: string): TTag;
var
i: Integer;
NameLower: string;
begin
NameLower := LowerCase(Name);
for i := 0 to Count - 1 do
begin
Result := TTag(Items[i]);
if Result.Name = NameLower then Exit;
end;
Result := nil;
end;
{ TTagManager ------------------------------------------------------------ }
constructor TTagManager.Create;
begin
inherited Create;
FTags := TTagVector.Create(true);
FParagraph := ' ';
FSpace := ' ';
FShortDash := '-';
FEnDash := '--';
FEmDash := '---';
end;
destructor TTagManager.Destroy;
begin
FreeAndNil(FTags);
inherited;
end;
function TTagManager.DoConvertString(const s: string): string;
begin
if Assigned(FConvertString) then
Result := FConvertString(s)
else
Result := s;
end;
function TTagManager.DoURLLink(const s: string): string;
begin
if Assigned(FURLLink) then
Result := FURLLink(s)
else
Result := DoConvertString(s);
end;
procedure TTagManager.Unabbreviate(var s: string);
var
idx: Integer;
begin
if Assigned(Abbreviations) then begin
idx := Abbreviations.IndexOfName(s);
if idx>=0 then begin
s := Abbreviations.Values[s];
end;
end;
end;
procedure TTagManager.DoMessage(const AVerbosity: Cardinal; const
MessageType: TPasDocMessageType; const AMessage: string;
const AArguments: array of const);
begin
if Assigned(FOnMessage) then
FOnMessage(MessageType, Format(AMessage, AArguments), AVerbosity);
end;
procedure TTagManager.DoMessageNonPre(const AVerbosity: Cardinal;
const MessageType: TPasDocMessageType; const AMessage: string;
const AArguments: array of const);
begin
if not PreExecute then
DoMessage(AVerbosity, MessageType, AMessage, AArguments);
end;
function TTagManager.TryAutoLink(const QualifiedIdentifier: TNameParts;
out QualifiedIdentifierReplacement: string): boolean;
begin
Result := false;
if Assigned(OnTryAutoLink) then
OnTryAutoLink(Self, QualifiedIdentifier,
QualifiedIdentifierReplacement, Result);
if Result then
DoMessage(3, pmtInformation, 'Automatically linked identifier "%s"',
[GlueNameParts(QualifiedIdentifier)]);
end;
function TTagManager.CoreExecute(const Description: string;
AutoLink: boolean;
EnclosingTag: TTag; var EnclosingTagData: TObject;
WantFirstSentenceEnd: boolean;
out FirstSentenceEnd: Integer): string;
var
{ This is the position of next char in Description to work with,
i.e. first FOffset-1 chars in Description are considered "done"
("done" means that their converted version is appended to Result) }
FOffset: Integer;
{ This checks if some tag starts at Description[FOffset + 1].
If yes then it returns true and sets
-- Tag to given tag object
-- Parameters to params for this tag (text specified between '(' ')',
parsed to the matching parenthesis)
-- OffsetEnd to the index of *next* character in Description right
after this tag (including it's parameters, if there were any)
Note that it may also change it's out parameters even when it returns
false; this doesn't harm anything for now, so I don't think there's
a reason to correct this for now.
In case some string looking as tag name (A-Za-z*) is here,
but it's not a name of any existing tag,
it not only returns false but also emits a warning for user. }
function FindTag(out Tag: TTag;
out Parameters: string; out OffsetEnd: Integer): Boolean;
var
i: Integer;
BracketCount: integer;
TagName: string;
begin
Result := False;
Parameters := '';
i := FOffset + 1;
while (i <= Length(Description)) and
IsCharInSet(Description[i], ['A'..'Z', 'a'..'z']) do
Inc(i);
if i = FOffset + 1 then Exit; { exit with false }
TagName := Copy(Description, FOffset + 1, i - FOffset - 1);
Tag := FTags.FindByName(TagName);
OffsetEnd := i;
if Tag = nil then
begin
DoMessageNonPre(1, pmtWarning, 'Unknown tag name "%s"', [TagName]);
Exit;
end;
Result := true;
{ OK, we found the correct tag, Tag variable is already set.
Now lets get the parameters, setting Parameters and OffsetEnd. }
if (i <= Length(Description)) and (Description[i] = '(') then
begin
{ Read Parameters to a matching parenthesis.
Note that we didn't check here whether
toParameterRequired in Tag.TagOptions.
Caller of FindTag will give a warning for user if it will
receive some Parameters <> '' while
toParameterRequired is *not* in Tag.TagOptions }
Inc(i);
BracketCount := 1;
while not ((i > Length(Description)) or (BracketCount = 0)) do
begin
case Description[i] of
'@':
{ Inc(I) here means that we will skip to next character
when we will see @@, @( or @).
This means that @( and @) will correctly *not* change
BracketCount. And @@ will be properly avoided,
so that e.g. "@@(" will correctly increase BracketCount
(because "@@(" means one "at" character and then normal
opening paren). }
Inc(I);
'(': Inc(BracketCount);
')': Dec(BracketCount);
end;
Inc(i);
end;
if (BracketCount = 0) then begin
Parameters := Copy(Description, OffsetEnd + 1, i - OffsetEnd - 2);
OffsetEnd := i;
end else
DoMessageNonPre(1, pmtWarning,
'No matching closing parenthesis for tag "%s"', [TagName]);
end else
if toParameterRequired in Tag.TagOptions then
begin
{ Read Parameters to the end of Description or newline but consume newline
prefixed with "line feed" char \ (just like shell scripts, C lang etc.) }
while (i <= Length(Description)) do
begin
if IsCharInSet(Description[i], WhiteSpaceNL) then
if Description[i - 1] = '\' then
begin
// Copy currently consumed line and start reading from found NL
Parameters := Parameters + Trim(Copy(Description, OffsetEnd, i - OffsetEnd - 1));
OffsetEnd := i;
// Explicitly add all NL's because they will be trimmed otherwise
while SCharIs(Description, i, WhiteSpaceNL) do Inc(i);
Parameters := Parameters + Copy(Description, OffsetEnd, i - OffsetEnd);
OffsetEnd := i;
end
else
Break;
Inc(i);
end;
Parameters := Parameters + Trim(Copy(Description, OffsetEnd, i - OffsetEnd));
OffsetEnd := i;
end;
end;
{ Wrapper to check for Markdown tag }
function FindMarkdownTag(out Tag: TTag;
out Parameters: string; out OffsetEnd: Integer): Boolean;
var
TagName: string;
Dummy: Boolean;
begin
// Check for markdown list
// If parent tag is list, then it's contents of a single item.
if (EnclosingTag <> nil) and (IndexText(EnclosingTag.Name, PasDocListTags) <> -1) then
Result := CheckMarkdownListItem(Description, FOffset, TagName, Parameters, OffsetEnd, Dummy)
// otherwise check for whole list
else
Result := CheckMarkdownList(Description, FOffset, TagName, Parameters, OffsetEnd);
// Check for markdown URL
if not Result then
Result := CheckMarkdownURL(Description, FOffset, TagName, Parameters, OffsetEnd);
// Check for simple markdown block
if not Result then
Result := CheckMarkdownBlock(Description, FOffset, TagName, Parameters, OffsetEnd);
if not Result then Exit;
Tag := FTags.FindByName(TagName);
if Tag = nil then
begin
DoMessageNonPre(1, pmtWarning, 'Unknown tag name "%s"', [TagName]);
Result := False;
Exit;
end;
end;
{ Checks does Description[FOffset] may be a beginning of some URL.
(xxx://xxxx/.../).
If yes, returns true and sets OffsetEnd to the next
index in Description after this URL.
For your comfort, returns also URL (this is *always*
Copy(Description, FOffset, OffsetEnd - FOffset)). }
function FindURL(out OffsetEnd: Integer; out URL: string): boolean;
{ Here's how it works, and what is the meaning of constants below:
Include all continuous AlphaNum chars.
Then must be '://'.
Include all continuous FullLinkChars and HalfLinkChars chars after '://'
but then strip all HalfLinkChars from the end.
This means that HalfLinkChars are allowed in the middle of URL,
but only as long as there is some char after FullLinkChars
but not at the end.
}
const
AlphaNum = ['A'..'Z', 'a'..'z', '0'..'9'];
FullLinkChars = AlphaNum + ['_', '%', '/', '#', '~', '@'];
HalfLinkChars = ['.', ',', '-', ':', ';', '?', '=', '&'];
URLMiddle = '://';
var
i: Integer;
begin
Result := False;
i := FOffset;
while SCharIs(Description, i, AlphaNum) do Inc(i);
if not (Copy(Description, i, Length(URLMiddle)) = URLMiddle) then Exit;
Result := true;
i := i + Length(URLMiddle);
while SCharIs(Description, i, FullLinkChars + HalfLinkChars) do Inc(i);
Dec(i);
while IsCharInSet(Description[i], HalfLinkChars) do Dec(i);
Inc(i);
OffsetEnd := i;
URL := Copy(Description, FOffset, OffsetEnd - FOffset);
end;
{ Checks does Description[FOffset] may be a beginning of some
qualified identifier (identifier is [A-Za-z_]([A-Za-z_0-9])*,
qualified identifier is a sequence of identifiers delimited
by dots).
If yes, returns true and sets OffsetEnd to the next
index in Description after this qualified ident.
For your comfort, returns also QualifiedIdentifier
(this is *always* equal to SplitNameParts(
Copy(Description, FOffset, OffsetEnd - FOffset))). }
function FindQualifiedIdentifier(out OffsetEnd: Integer;
out QualifiedIdentifier: TNameParts): boolean;
const
FirstIdentChar = ['a'..'z', 'A'..'Z', '_'];
NonFirstIdentChar = FirstIdentChar + ['0'..'9'];
AnyQualifiedIdentChar = NonFirstIdentChar + ['.'];
var
NamePartBegin: Integer;
begin
Result :=
( (FOffset = 1) or
not IsCharInSet(Description[FOffset - 1], AnyQualifiedIdentChar) ) and
SCharIs(Description, FOffset, FirstIdentChar);
if Result then
begin
NamePartBegin := FOffset;
OffsetEnd := FOffset + 1;
SetLength(QualifiedIdentifier, 0);
repeat
{ skip a sequence of NonFirstIdentChar characters }
while SCharIs(Description, OffsetEnd, NonFirstIdentChar) do
Inc(OffsetEnd);
if Length(QualifiedIdentifier) = MaxNameParts then
begin
{ I can't add new item to QualifiedIdentifier.
So Result is false. }
Result := false;
Exit;
end;
{ Append next part to QualifiedIdentifier }
SetLength(QualifiedIdentifier, Length(QualifiedIdentifier) + 1);
QualifiedIdentifier[Length(QualifiedIdentifier) - 1] :=
Copy(Description, NamePartBegin, OffsetEnd - NamePartBegin);
if SCharIs(Description, OffsetEnd, '.') and
SCharIs(Description, OffsetEnd + 1, FirstIdentChar) then
begin
NamePartBegin := OffsetEnd + 1;
{ skip the dot and skip FirstIdentChar character }
Inc(OffsetEnd, 2);
end else
break;
until false;
end;
end;
function FindFirstSentenceEnd: boolean;
begin
Result := (Description[FOffset] = '.') and
SCharIs(Description, FOffset + 1, WhiteSpace);
end;
function IsNormalTextAllowed: boolean;
begin
Result := (EnclosingTag = nil) or
(toAllowNormalTextInside in EnclosingTag.TagOptions);
end;
function CheckNormalTextAllowed(const NormalText: string): boolean;
begin
Result := IsNormalTextAllowed;
if not Result then
DoMessageNonPre(1, pmtWarning,
'Such content, "%s", is not allowed '+
'directly within the tag @%s', [NormalText, EnclosingTag.Name]);
end;
{ Strip initial @ from @( and @). Do not touch other @ occurences.
This is only used for tags without toRecursiveTags
(for toRecursiveTags, the recursive call to CoreExecute
will already handle it). }
function HandleAtChar(const S: string): string;
var
PosAt, HandledCount: Integer;
begin
Result := '';
HandledCount := 0;
while HandledCount < Length(S) do
begin
PosAt := PosEx('@', S, HandledCount + 1);
if PosAt = 0 then
begin
Result := Result + SEnding(S, HandledCount + 1);
HandledCount := Length(S);
end else
if SCharIs(S, PosAt + 1, ['(', ')', '@']) then
begin
{ strip @, add the next ( or ) or @ }
Result := Result + Copy(S, HandledCount + 1, PosAt - HandledCount - 1) +
S[PosAt + 1];
HandledCount := PosAt + 1;
end else
begin
{ do not strip @ }
Result := Result + Copy(S, HandledCount + 1, PosAt - HandledCount);
HandledCount := PosAt;
end;
end;
end;
var
{ Always ConvertBeginOffset <= FOffset.
Description[ConvertBeginOffset ... FOffset - 1]
is the string that should be filtered by DoConvertString. }
ConvertBeginOffset: Integer;
{ This function increases ConvertBeginOffset to FOffset,
appending converted version of
Description[ConvertBeginOffset ... FOffset - 1]
to Result. }
procedure DoConvert;
var
ToAppend: string;
begin
ToAppend := Copy(Description, ConvertBeginOffset,
FOffset - ConvertBeginOffset);
if ToAppend <> '' then
begin
if (not PreExecute) and
CheckNormalTextAllowed(ToAppend) then
Result := Result + DoConvertString(ToAppend);
ConvertBeginOffset := FOffset;
end;
end;
var
ReplaceStr: string;
Params: string;
OffsetEnd: Integer;
FoundTag: TTag;
URL: string;
FoundTagData: TObject;
QualifiedIdentifier: TNameParts;
QualifiedIdentifierReplacement: string;
begin
Result := '';
FOffset := 1;
ConvertBeginOffset := 1;
if (EnclosingTag <> nil) and
(toFirstWordVerbatim in EnclosingTag.TagOptions) then
begin
{ Skip the first word in Description }
while SCharIs(Description, FOffset, WhiteSpace) do Inc(FOffset);
while SCharIs(Description, FOffset, AllChars - WhiteSpace) do Inc(FOffset);
end;
if WantFirstSentenceEnd then
FirstSentenceEnd := 0;
{ Description[FOffset] is the next char that must be processed
(we're "looking at it" right now). }
while FOffset <= Length(Description) do
begin
if ((Description[FOffset] = '@') and FindTag(FoundTag, Params, OffsetEnd)) or
(Markdown and FindMarkdownTag(FoundTag, Params, OffsetEnd)) then
begin
DoConvert;
{ Check is it allowed for this tag to be here }
if not FoundTag.AllowedInside(EnclosingTag) then
begin
if EnclosingTag = nil then
DoMessageNonPre(1, pmtWarning, 'The tag "@%s" cannot be used at the ' +
'top level of description, it must be used within some other @-tag',
[FoundTag.Name]) else
DoMessageNonPre(1, pmtWarning, 'The tag "@%s" cannot be used inside ' +
'parameter of tag "@%s"', [FoundTag.Name, EnclosingTag.Name]);
{ Assign dummy value for ReplaceStr.
We can't proceed with normal recursive expanding and
calling FoundTag.[Pre]Execute, because tag methods
(and callbacks, like TTag.On[Pre]Execute) may assume that the tag
is always enclosed only within allowed tags
(so e.g. EnclosingTag and EnclosingTagData values for
On[Pre]Execute are of appropriate classes etc.) }
ReplaceStr := '';
end else
begin
FoundTagData := FoundTag.CreateOccurenceData;
try
{ Process Params }
if Params <> '' then
begin
if toParameterRequired in FoundTag.TagOptions then
begin
Unabbreviate(Params);
if toRecursiveTags in FoundTag.TagOptions then
{ recursively expand Params }
Params := CoreExecute(Params, AutoLink, FoundTag, FoundTagData) else
if not (toRecursiveTagsManually in FoundTag.TagOptions) then
Params := HandleAtChar(Params);
end else
begin
{ Note that in this case we ignore whether
toRecursiveTags is in Tag.TagOptions,
we always behave like toRecursiveTags was not included.
This is reported as a serious warning,
because tag handler procedure will probably ignore
passed value of Params and will set ReplaceStr to something
unrelated to Params. This means that user input is completely
discarded. So user should really correct it.
I didn't mark this as an mtError only because some sensible
output will be generated anyway. }
DoMessageNonPre(1, pmtWarning,
'Tag "%s" is not allowed to have any parameters', [FoundTag.Name]);
end;
// This ReplaceStr value shoud be overridden by FoundTag.PreExecute/Execute
ReplaceStr := DoConvertString('PasDoc cannot expand tag @' + FoundTag.Name + '(') + Params + ConvertString(')');
end else
// This ReplaceStr value shoud be overridden by FoundTag.PreExecute/Execute
ReplaceStr := DoConvertString('PasDoc cannot expand tag @' + FoundTag.Name);
{ execute tag handler }
if PreExecute then
FoundTag.PreExecute(FoundTagData, EnclosingTag, EnclosingTagData,
Params, ReplaceStr) else
FoundTag.Execute(FoundTagData, EnclosingTag, EnclosingTagData,
Params, ReplaceStr);
finally
FoundTag.DestroyOccurenceData(FoundTagData)
end;
end;
Result := Result + ReplaceStr;
FOffset := OffsetEnd;
ConvertBeginOffset := FOffset;
end else
if Copy(Description, FOffset, 2) = '@(' then
begin
DoConvert;
{ convert '@(' to '(' }
if CheckNormalTextAllowed('@(') then
Result := Result + '(';
FOffset := FOffset + 2;
ConvertBeginOffset := FOffset;
end else
if Copy(Description, FOffset, 2) = '@)' then
begin
DoConvert;
{ convert '@)' to '(' }
if CheckNormalTextAllowed('@)') then
Result := Result + ')';
FOffset := FOffset + 2;
ConvertBeginOffset := FOffset;
end else
if Copy(Description, FOffset, 2) = '@@' then
begin
DoConvert;
{ convert '@@' to '@' }
if CheckNormalTextAllowed('@@') then
Result := Result + '@';
FOffset := FOffset + 2;
ConvertBeginOffset := FOffset;
end else
if Copy(Description, FOffset, 2) = '@-' then
begin
DoConvert;
{ convert '@-' to ShortDash }
if CheckNormalTextAllowed('@-') then
Result := Result + ShortDash;
FOffset := FOffset + 2;
ConvertBeginOffset := FOffset;
end else
{ Note that we must scan for '---' in Description before scanning for '--'. }
if Copy(Description, FOffset, 3) = '---' then
begin
DoConvert;
{ convert '---' to EmDash }
if CheckNormalTextAllowed('---') then
Result := Result + EmDash;
FOffset := FOffset + 3;
ConvertBeginOffset := FOffset;
end else
if Copy(Description, FOffset, 2) = '--' then
begin
DoConvert;
{ convert '--' to EnDash }
if CheckNormalTextAllowed('--') then
Result := Result + EnDash;
FOffset := FOffset + 2;
ConvertBeginOffset := FOffset;
end else
if Description[FOffset] = '-' then
begin
DoConvert;
{ So '-' is just a normal ShortDash }
if CheckNormalTextAllowed('-') then
Result := Result + ShortDash;
FOffset := FOffset + 1;
ConvertBeginOffset := FOffset;
end else
if FindParagraph(Description, FOffset, OffsetEnd) then
begin
DoConvert;
{ If normal text is allowed then append Paragraph to Result.
Otherwise just ignore any whitespace in Description. }
if IsNormalTextAllowed then
Result := Result + Paragraph;
FOffset := OffsetEnd;
ConvertBeginOffset := FOffset;
end else
{ FindWhitespace must be checked after FindParagraph,
otherwise we would take paragraph as just some whitespace. }
if FindChars(Description, FOffset, WhiteSpace, OffsetEnd) then
begin
DoConvert;
{ If normal text is allowed then append Space to Result.
Otherwise just ignore any whitespace in Description. }
if IsNormalTextAllowed then
Result := Result + Space;
FOffset := OffsetEnd;
ConvertBeginOffset := FOffset;
end else
if (not PreExecute) and
AutoLink and
FindQualifiedIdentifier(OffsetEnd, QualifiedIdentifier) and
TryAutoLink(QualifiedIdentifier, QualifiedIdentifierReplacement) then
begin
DoConvert;
if CheckNormalTextAllowed(GlueNameParts(QualifiedIdentifier)) then
Result := Result + QualifiedIdentifierReplacement;
FOffset := OffsetEnd;
ConvertBeginOffset := FOffset;
end else
if FindURL(OffsetEnd, URL) then
begin
DoConvert;
if CheckNormalTextAllowed(URL) then
Result := Result + DoURLLink(URL);
FOffset := OffsetEnd;
ConvertBeginOffset := FOffset;
end else
if WantFirstSentenceEnd and
(FirstSentenceEnd = 0) and
FindFirstSentenceEnd then
begin
DoConvert;
if CheckNormalTextAllowed('.') then
begin
Result := Result + ConvertString('.');
FirstSentenceEnd := Length(Result);
end;
Inc(FOffset);
ConvertBeginOffset := FOffset;
end else
Inc(FOffset);
end;
DoConvert;
if WantFirstSentenceEnd and (FirstSentenceEnd = 0) then
FirstSentenceEnd := Length(Result);
{ Only for testing:
Writeln('----');
Writeln('Description was "', Description, '"');
Writeln('Result is "', Result, '"');
Writeln('----');}
end;
function TTagManager.CoreExecute(const Description: string;
AutoLink: boolean;
EnclosingTag: TTag; var EnclosingTagData: TObject): string;
var Dummy: Integer;
begin
Result := CoreExecute(Description, AutoLink,
EnclosingTag, EnclosingTagData,
false, Dummy);
end;
function TTagManager.Execute(const Description: string;
AutoLink: boolean;
WantFirstSentenceEnd: boolean;
out FirstSentenceEnd: Integer): string;
var
EnclosingTagData: TObject;
begin
EnclosingTagData := nil;
Result := CoreExecute(Description, AutoLink,
nil, EnclosingTagData,
WantFirstSentenceEnd, FirstSentenceEnd);
{ Just ignore resulting EnclosingTagData }
end;
function TTagManager.Execute(const Description: string;
AutoLink: boolean): string;
var
Dummy: Integer;
begin
Result := Execute(Description, AutoLink, false, Dummy);
end;
end.
|