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 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
|
package HTML::LinkList;
$HTML::LinkList::VERSION = '0.1701';
use strict;
use warnings;
=head1 NAME
HTML::LinkList - Create a 'smart' list of HTML links.
=head1 VERSION
version 0.1701
=head1 SYNOPSIS
use HTML::LinkList qw(link_list);
# default formatting
my $html_links = link_list(current_url=>$url,
urls=>\@links_in_order,
labels=>\%labels,
descriptions=>\%desc);
# paragraph with ' :: ' separators
my $html_links = link_list(current_url=>$url,
urls=>\@links_in_order,
labels=>\%labels,
descriptions=>\%desc,
links_head=>'<p>',
links_foot=>'</p>',
pre_item=>'',
post_item=>''
pre_active_item=>'<em>',
post_active_item=>'</em>',
item_sep=>" :: ");
# multi-level list
my $html_links = link_tree(
current_url=>$url,
link_tree=>\@list_of_lists,
labels=>\%labels,
descriptions=>\%desc);
=head1 DESCRIPTION
This module contains a number of functions for taking sets of URLs and
labels and creating suitably formatted HTML. These links are "smart"
because, if given the url of the current page, if any of the links in
the list equal it, that item in the list will be formatted as a special
label, not as a link; this is a Good Thing, since the user would be
confused by clicking on a link back to the current page.
While many website systems have plugins for "smart" navbars, they are
specialized for that system only, and can't be reused elsewhere, forcing
people to reinvent the wheel. I hereby present one wheel, free to be
reused by anybody; just the simple functions, a backend, which can be
plugged into whatever system you want.
The default format for the HTML is to make an unordered list, but there
are many options, enabling one to have a flatter layout with any
separators you desire, or a more complicated list with differing
formats for different levels.
The "link_list" function uses a simple list of links -- good for a
simple navbar.
The "link_tree" function takes a set of nested links and makes the HTML
for them -- good for making a table of contents, or a more complicated
navbar.
The "full_tree" function takes a list of paths and makes a full tree of
all the pages and index-pages in those paths -- good for making a site
map.
The "breadcrumb_trail" function takes a url and makes a "breadcrumb trail"
from it.
The "nav_tree" function creates a set of nested links to be
used as a multi-level navbar; one can give it a list of paths
(as for full_tree) and it will only show the links related
to the current URL.
=cut
=head1 FUNCTIONS
To export a function, add it to the 'use' call.
use HTML::LinkList qw(link_list);
To export all functions do:
use HTML::LinkList ':all';
=cut
use Data::Dumper;
require Exporter;
our @ISA = qw(Exporter);
# Items which are exportable.
#
# This allows declaration use HTML::LinkList ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
link_list
link_tree
full_tree
breadcrumb_trail
nav_tree
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
our @EXPORT = qw(
);
=head2 link_list
$links = link_list(
current_url=>$url,
urls=>\@links_in_order,
labels=>\%labels,
descriptions=>\%desc,
pre_desc=>' ',
post_desc=>'',
links_head=>'<ul>',
links_foot=>'</ul>',
pre_item=>'<li>',
post_item=>'</li>'
pre_active_item=>'<em>',
post_active_item=>'</em>',
item_sep=>"\n");
Generates a simple list of links, from list of urls
(and optional labels) taking into account of the "current" URL.
This provides a large number of options to customize the appearance
of the list. The default setup is for a simple UL list, but setting
the options can enable you to make it something other than a list
altogether, or add in CSS styles or classes to make it look just
like you want.
Required:
=over
=item urls
The urls in the order you want them displayed. If this list
is empty, then nothing will be generated.
=back
Options:
=over
=item current_url
The link to the current page. If one of the links equals this,
then that is deemed to be the "active" link and is just displayed
as a label rather than a link.
=item descriptions
Optional hash of descriptions, to put next to the links. The keys
of this hash are the urls.
=item hide_ext
If a site is hiding link extensions (such as using MultiViews with
Apache) you may wish to hide the extensions (while using the full URLs
to check various things). (default: 0 (false))
=item item_sep
String to put between items.
=item labels
A hash whose keys are links and whose values are labels.
These are the labels for the links; if no label
is given, then the last part of the link is used
for the label, with some formatting.
=item links_head
String to begin the list with.
=item links_foot
String to end the list with.
=item pre_desc
String to prepend to each description.
=item post_desc
String to append to each description.
=item pre_item
String to prepend to each item.
=item post_item
String to append to each item.
=item pre_active_item
An additional string to put in front of each "active" item, after pre_item.
The "active" item is the link which matches 'current_url'.
=item pre_item_active
INSTEAD of the "pre_item" string, use this string for active items
=item post_active_item
An additional string to append to each active item, before post_item.
=item prefix_url
A prefix to prepend to all the links. (default: empty string)
=back
=cut
sub link_list {
my %args = (
current_url=>'',
prefix_url=>'',
labels=>undef,
urls=>undef,
links_head=>'<ul>',
links_foot=>"\n</ul>",
pre_item=>'<li>',
post_item=>'</li>',
pre_active_item=>'<em>',
post_active_item=>'</em>',
pre_current_parent=>'',
post_current_parent=>'',
item_sep=>"\n",
hide_ext=>0,
@_
);
my @link_order = @{$args{urls}};
if (!defined $args{urls}
or !@{$args{urls}})
{
return '';
}
my %format = (exists $args{format}
? %{$args{format}}
: make_default_format(%args));
# correct the current_url
$args{current_url} = make_canonical($args{current_url});
my %current_parents = extract_current_parents(%args);
my @items = ();
foreach my $link (@link_order)
{
my $label = (exists $args{labels}->{$link}
? $args{labels}->{$link} : '');
my $item = make_item(%args,
format=>\%format,
current_parents=>\%current_parents,
this_link=>$link,
this_label=>$label);
push @items, $item;
}
my $list = join($format{item_sep}, @items);
return ($list
? join('', $args{links_head}, $list, $args{links_foot})
: '');
} # link_list
=head2 link_tree
$links = link_tree(
current_url=>$url,
link_tree=>\@list_of_lists,
labels=>\%labels,
descriptions=>\%desc,
pre_desc=>' ',
post_desc=>'',
links_head=>'<ul>',
links_foot=>'</ul>',
subtree_head=>'<ul>',
subtree_foot=>'</ul>',
pre_item=>'<li>',
post_item=>'</li>'
pre_active_item=>'<em>',
post_active_item=>'</em>',
item_sep=>"\n",
tree_sep=>"\n",
formats=>\%formats);
Generates nested lists of links from a list of lists of links.
This is useful for things such as table-of-contents or
site maps.
By default, this will return UL lists, but this is highly
configurable.
Required:
=over
=item link_tree
A list of lists of urls, in the order you want them displayed.
If a url is not in this list, it will not be displayed.
=back
Options:
=over
=item current_url
The link to the current page. If one of the links equals this,
then that is deemed to be the "active" link and is just displayed
as a label rather than a link.
=item descriptions
Optional hash of descriptions, to put next to the links. The keys
of this hash are the urls.
=item exclude_root_parent
If this is true, then the "current_parent" display options are
not used for the "root" ("/") path, it isn't counted as a "parent"
of the current_url.
=item formats
A reference to a hash containing advanced format settings. For example:
my %formats = (
# level 1 and onwards
'1' => {
tree_head=>"<ol>",
tree_foot=>"</ol>\n",
},
# level 2 and onwards
'2' => {
tree_head=>"<ul>",
tree_foot=>"</ul>\n",
},
# level 3 and onwards
'3' => {
pre_item=>'(',
post_item=>')',
item_sep=>",\n",
tree_sep=>' -> ',
tree_head=>"<br/>\n",
tree_foot=>"",
}
);
The formats hash enables you to control the formatting on a per-level basis.
Each key of the hash corresponds to a level-number; the sub-hashes contain
format arguments which will apply from that level onwards. If an argument
isn't given in the sub-hash, then it will fall back to the previous level
(or to the default, if there is no setting for that format-argument
for a previous level).
The only difference between the names of the arguments in the sub-hash and
in the global format arguments is that instead of 'subtree_head' and subtree_foot'
it uses 'tree_head' and 'tree_foot'.
=item hide_ext
If a site is hiding link extensions (such as using MultiViews with
Apache) you may wish to hide the extensions (while using the full URLs
to check various things). (default: 0 (false))
=item item_sep
The string to separate each item.
=item labels
A hash whose keys are links and whose values are labels.
These are the labels for the links; if no label
is given, then the last part of the link is used
for the label, with some formatting.
=item links_head
The string to prepend the top-level tree with.
(default: <ul>)
=item links_foot
The string to append to the top-level tree.
(default: </ul>)
=item pre_desc
String to prepend to each description.
=item post_desc
String to append to each description.
=item pre_item
String to prepend to each item.
(default: <li>)
=item post_item
String to append to each item.
(default: </li>)
=item pre_active_item
An additional string to put in front of each "active" item, after pre_item.
The "active" item is the link which matches 'current_url'.
(default: <em>)
=item pre_item_active
INSTEAD of the "pre_item" string, use this string for active items
=item post_active_item
An additional string to append to each active item, before post_item.
(default: </em>)
=item pre_current_parent
An additional string to put in front of a link which is a parent
of the 'current_url' link, after pre_item.
=item pre_item_current_parent
INSTEAD of the "pre_item" string, use this for links which are parents
of the 'current_url' link.
=item post_current_parent
An additional string to append to a link which is a parent
of the 'current_url' link, before post_item.
=item prefix_url
A prefix to prepend to all the links. (default: empty string)
=item subtree_head
The string to prepend to lower-level trees.
(default: <ul>)
=item subtree_foot
The string to append to lower-level trees.
(default: </ul>)
=item tree_sep
The string to separate each tree.
=back
=cut
sub link_tree {
my %args = (
current_url=>'',
prefix_url=>'',
link_tree=>undef,
links_head=>'<ul>',
links_foot=>"\n</ul>",
subtree_head=>'<ul>',
subtree_foot=>"\n</ul>",
last_subtree_head=>'<ul>',
last_subtree_foot=>"\n</ul>",
pre_item=>'<li>',
post_item=>'</li>',
pre_active_item=>'<em>',
post_active_item=>'</em>',
pre_current_parent=>'',
post_current_parent=>'',
item_sep=>"\n",
tree_sep=>"\n",
@_
);
# correct the current_url
$args{current_url} = make_canonical($args{current_url});
my %current_parents = extract_current_parents(%args);
$args{tree_depth} = 0;
$args{end_depth} = 0;
if (defined $args{link_tree}
and @{$args{link_tree}})
{
my %default_format = make_default_format(%args);
my %formats = make_extra_formats(%args);
my @link_tree = @{$args{link_tree}};
my $list = traverse_lol(\@link_tree,
%args,
formats=>\%formats,
current_format=>\%default_format,
current_parents=>\%current_parents);
return $list if $list;
}
return '';
} # link_tree
=head2 full_tree
$links = full_tree(
paths=>\@list_of_paths,
labels=>\%labels,
descriptions=>\%desc,
hide=>$hide_regex,
nohide=>$nohide_regex,
start_depth=>0,
end_depth=>0,
top_level=>0,
preserve_order=>0,
preserve_paths=>0,
...
);
Given a set of paths this will generate a tree of links in the style of
I<link_tree>. This will figure out all the intermediate paths and construct
the nested structure for you, clustering parents and children together.
The formatting options are as for L</link_tree>.
Required:
=over
=item paths
A reference to a list of paths: that is, URLs relative to the top
of the site.
For example, if the full URL is http://www.example.com/foo.html
then the path is /foo.html
If the full URL is http://www.example.com/~frednurk/foo.html
then the path is /foo.html
This does not require that every possible path be given; all the intermediate
paths will be figured out from the list.
=back
Options:
=over
=item append_list
Array of paths to append to the top-level links. They are used
as-is, and are not part of the processing done to the "paths" list
of paths. (see L</prepend_list>)
=item descriptions
Optional hash of descriptions, to put next to the links. The keys
of this hash are the paths.
=item end_depth
End your tree at this depth. If zero, then go all the way.
(see L</start_depth>)
=item exclude_root_parent
If this is true, then the "current_parent" display options are
not used for the "root" ("/") path, it isn't counted as a "parent"
of the current_url.
=item hide
If the path matches this string, don't include it in the tree.
=item hide_ext
If a site is hiding link extensions (such as using MultiViews with
Apache) you may wish to hide the extensions (while using the full URLs
to check various things). (default: 0 (false))
=item labels
Hash containing replacement labels for one or more paths.
If no label is given for '/' (the root path) then 'Home' will
be used.
=item last_subtree_head
The string to prepend to the last lower-level tree.
Only used if end_depth is not zero.
=item last_subtree_foot
The string to append to the last lower-level tree.
Only used if end_depth is not zero.
=item nohide
If the path matches this string, it will be included even if it matches
the 'hide' string.
=item prefix_url
A prefix to prepend to all the links. (default: empty string)
=item prepend_list
Array of paths to prepend to the top-level links. They are used
as-is, and are not part of the processing done to the "paths" list
of paths.
=item preserve_order
Preserve the ordering of the paths in the input list of paths;
otherwise the links will be sorted alphabetically. Note that if
preserve_order is true, the structure is at the whims of the order
of the original list of paths, and so could end up odd-looking.
(default: false)
=item preserve_paths
Do not extract intermediate paths or reorder the input list of paths.
This speeds things up, but assumes that the input paths are complete
and in good order.
(default: false)
=item start_depth
Start your tree at this depth. Zero is the root, level 1 is the
files/sub-folders in the root, and so on.
(default: 0)
=item top_level
Decide which level is the "top" level. Useful when you
set the start_depth to something greater than 1.
=back
=cut
sub full_tree {
my %args = (
paths=>undef,
current_url=>'',
links_head=>'<ul>',
links_foot=>"\n</ul>",
subtree_head=>'<ul>',
subtree_foot=>"\n</ul>",
last_subtree_head=>'<ul>',
last_subtree_foot=>"\n</ul>",
pre_item=>'<li>',
post_item=>'</li>',
pre_active_item=>'<em>',
post_active_item=>'</em>',
pre_current_parent=>'',
post_current_parent=>'',
item_sep=>"\n",
tree_sep=>"\n",
hide=>'',
nohide=>'',
preserve_order=>0,
preserve_paths=>0,
labels=>{},
start_depth=>0,
end_depth=>0,
top_level=>0,
@_
);
# correct the current_url
$args{current_url} = make_canonical($args{current_url});
my %current_parents = extract_current_parents(%args);
# set the root label
if (!$args{labels}->{'/'})
{
$args{labels}->{'/'} = 'Home';
}
my @path_list = ();
if ($args{preserve_paths})
{
@path_list = filter_out_paths(%args, paths=>$args{paths});
}
else
{
@path_list = extract_all_paths(paths=>$args{paths},
preserve_order=>$args{preserve_order});
@path_list = filter_out_paths(%args, paths=>\@path_list);
}
my @list_of_lists = build_lol(%args, paths=>\@path_list,
depth=>0);
$args{tree_depth} = 0;
$args{end_depth} = 0;
my %default_format = make_default_format(%args);
my %formats = make_extra_formats(%args);
my $list = traverse_lol(\@list_of_lists,
%args,
formats=>\%formats,
current_format=>\%default_format,
current_parents=>\%current_parents);
return $list if $list;
return '';
} # full_tree
=head2 breadcrumb_trail
$links = breadcrumb_trail(
current_url=>$url,
labels=>\%labels,
descriptions=>\%desc,
links_head=>'<p>',
links_foot=>"\n</p>",
subtree_head=>'',
subtree_foot=>"\n",
pre_item=>'',
post_item=>'',
pre_active_item=>'<em>',
post_active_item=>'</em>',
item_sep=>"\n",
tree_sep=>' > ',
...
);
Given the current url, make a breadcrumb trail from it.
By default, this is laid out with '>' separators, but it can
be set up to give a nested set of UL lists (as for L</full_tree>).
The formatting options are as for L</link_tree>.
Required:
=over
=item current_url
The current url to be made into a breadcrumb-trail.
=back
Options:
=over
=item descriptions
Optional hash of descriptions, to put next to the links. The keys
of this hash are the urls.
=item exclude_root_parent
If this is true, then the "current_parent" display options are
not used for the "root" ("/") path, it isn't counted as a "parent"
of the current_url.
=item hide_ext
If a site is hiding link extensions (such as using MultiViews with
Apache) you may wish to hide the extensions (while using the full URLs
to check various things). (default: 0 (false))
=item labels
Hash containing replacement labels for one or more URLS.
If no label is given for '/' (the root path) then 'Home' will
be used.
=back
=cut
sub breadcrumb_trail {
my %args = (
current_url=>'',
links_head=>'<p>',
links_foot=>"\n</p>",
subtree_head=>'',
subtree_foot=>'',
last_subtree_head=>'{',
last_subtree_foot=>'}',
pre_item=>'',
post_item=>'',
pre_active_item=>'<em>',
post_active_item=>'</em>',
pre_current_parent=>'',
post_current_parent=>'',
item_sep=>"\n",
tree_sep=>' > ',
hide=>'',
nohide=>'',
labels=>{},
paths=>[],
start_depth=>0,
end_depth=>undef,
top_level=>0,
@_
);
# correct the current_url
$args{current_url} = make_canonical($args{current_url});
# set the root label
if (!$args{labels}->{'/'})
{
$args{labels}->{'/'} = 'Home';
}
# make a list of paths consisting only of the current_url
my @paths = ($args{current_url});
my @path_list = extract_all_paths(paths=>\@paths);
@path_list = filter_out_paths(%args, paths=>\@path_list);
my @list_of_lists = build_lol(%args, paths=>\@path_list,
depth=>0);
$args{tree_depth} = 0;
$args{end_depth} = 0;
my %default_format = make_default_format(%args);
my %formats = make_extra_formats(%args);
my $list = traverse_lol(\@list_of_lists,
%args,
formats=>\%formats,
current_format=>\%default_format,
);
return $list if $list;
return '';
} # breadcrumb_trail
=head2 nav_tree
$links = nav_tree(
paths=>\@list_of_paths,
labels=>\%labels,
current_url=>$url,
hide=>$hide_regex,
nohide=>$nohide_regex,
preserve_order=>1,
descriptions=>\%desc,
...
);
This takes a list of links, and the current URL, and makes a nested navigation
tree, consisting of (a) the top-level links (b) the links leading to the
current URL (c) the links on the same level as the current URL,
(d) the related links just above this level, depending on whether
this is an index-page or a content page.
Optionally one can hide links which match match the 'hide' option.
The formatting options are as for L</link_tree>, with some additions.
Required:
=over
=item current_url
The link to the current page. If one of the links equals this, then that
is deemed to be the "active" link and is just displayed as a label rather
than a link. This is also used to determine which links to show and which
ones to filter out.
=item paths
A reference to a list of paths: that is, URLs relative to the top
of the site.
For example, if the full URL is http://www.example.com/foo.html
then the path is /foo.html
This does not require that every possible path be given; all the intermediate
paths will be figured out from the list.
=back
Options:
=over
=item append_list
Array of paths to append to the top-level links. They are used
as-is, and are not part of the processing done to the "paths" list
of paths. (see L</prepend_list>)
=item descriptions
Optional hash of descriptions, to put next to the links. The keys
of this hash are the paths.
=item end_depth
End your tree at this depth. If zero, then go all the way.
By default this is set to the depth of the current_url.
=item exclude_root_parent
If this is true, then the "current_parent" display options are
not used for the "root" ("/") path, it isn't counted as a "parent"
of the current_url.
=item hide
If a path matches this string, don't include it in the tree.
=item hide_ext
If a site is hiding link extensions (such as using MultiViews with
Apache) you may wish to hide the extensions (while using the full URLs
to check various things). (default: 0 (false))
=item labels
Hash containing replacement labels for one or more paths.
If no label is given for '/' (the root path) then 'Home' will
be used.
=item last_subtree_head
The string to prepend to the last lower-level tree.
=item last_subtree_foot
The string to append to the last lower-level tree.
=item nohide
If the path matches this string, it will be included even if it matches
the 'hide' string.
=item prefix_url
A prefix to prepend to all the links. (default: empty string)
=item prepend_list
Array of paths to prepend to the top-level links. They are used
as-is, and are not part of the processing done to the "paths" list
of paths.
=item preserve_order
Preserve the ordering of the paths in the input list of paths;
otherwise the links will be sorted alphabetically.
(default: true)
=item preserve_paths
Do not extract intermediate paths or reorder the input list of paths.
This speeds things up, but assumes that the input paths are complete
and in good order.
(default: false)
=item start_depth
Start your tree at this depth. Zero is the root, level 1 is the
files/sub-folders in the root, and so on.
(default: 1)
=item top_level
Decide which level is the "top" level. Useful when you
set the start_depth to something greater than 1.
=back
=cut
sub nav_tree {
my %args = (
paths=>undef,
current_url=>'',
links_head=>'<ul>',
links_foot=>"\n</ul>",
subtree_head=>'<ul>',
subtree_foot=>"\n</ul>",
last_subtree_head=>'<ul>',
last_subtree_foot=>"\n</ul>",
pre_item=>'<li>',
post_item=>'</li>',
pre_active_item=>'<em>',
post_active_item=>'</em>',
pre_current_parent=>'',
post_current_parent=>'',
item_sep=>"\n",
tree_sep=>"\n",
hide=>'',
nohide=>'',
preserve_order=>1,
preserve_paths=>0,
include_home=>0,
labels=>{},
start_depth=>1,
end_depth=>undef,
top_level=>1,
navbar_type=>'normal',
@_
);
# correct the current_url
$args{current_url} = make_canonical($args{current_url});
my $current_is_index = ($args{current_url} =~ m!/$!o);
my %current_parents = extract_current_parents(%args);
# set the end depth if is not already set
# if this is an index-page, then make the depth its depth + 1
# if this is a content-page, make the depth its depth
my $current_url_depth = path_depth($args{current_url});
$args{end_depth} = ($current_is_index
? $current_url_depth + 1 : $current_url_depth)
if (!defined $args{end_depth});
# set the root label
if (!$args{labels}->{'/'})
{
$args{labels}->{'/'} = 'Home';
}
my @path_list = ();
if ($args{preserve_paths})
{
@path_list = filter_out_paths(%args, paths=>$args{paths});
}
else
{
@path_list = extract_all_paths(paths=>$args{paths},
preserve_order=>$args{preserve_order});
@path_list = filter_out_paths(%args, paths=>\@path_list);
}
my @list_of_lists = build_lol(%args, paths=>\@path_list,
depth=>0);
$args{tree_depth} = 0;
my %default_format = make_default_format(%args);
my %formats = make_extra_formats(%args);
my $list = traverse_lol(\@list_of_lists,
%args,
formats=>\%formats,
current_format=>\%default_format,
current_parents=>\%current_parents);
return $list if $list;
return '';
} # nav_tree
=head1 Private Functions
These functions cannot be exported.
=head2 make_item
$item = make_item(
this_label=>$label,
this_link=>$link,
hide_ext=>0,
current_url=>$url,
current_parents=>\%current_parents,
descriptions=>\%desc,
format=>\%format,
);
%format = (
pre_desc=>' ',
post_desc=>'',
pre_item=>'<li>',
post_item=>'</li>'
pre_active_item=>'<em>',
post_active_item=>'</em>',
pre_current_parent=>'<em>',
post_current_parent=>'</em>',
item_sep=>"\n");
);
Format a link item.
See L</link_list> for the formatting options.
=over
=item this_label
The label of the required link. If there is no label,
this uses the base-name of the last part of the link,
capitalizing it and replacing underscores and dashes with spaces.
=item this_link
The URL of the required link.
=item current_url
The link to the current page. If one of the links equals this,
then that is deemed to be the "active" link and is just displayed
as a label rather than a link.
=item current_parents
URLs of the parents of the current item.
=item descriptions
Optional hash of descriptions, to put next to the links. The keys
of this hash are the links (not the labels).
=item defer_post_item
Don't add the 'post_item' string if this is true.
(needed for nested lists)
(default: false)
=item no_link
Don't make a link for this, just a label.
=back
=cut
sub make_item {
my %args = (
this_link=>'',
this_label=>'',
hide_ext=>0,
current_url=>'',
current_parents=>{},
prefix_url=>'',
defer_post_item=>0,
no_link=>0,
@_
);
my $link = $args{this_link};
my $prefix_url = $args{prefix_url};
my $label = $args{this_label};
my %format = %{$args{format}};
if (!$label)
{
$label = $link if !$label;
if ($link =~ /([-\w]+)\.\w+$/o) # file
{
$label = $1;
}
elsif ($link =~ /([-\w]+)\/?$/o) # dir
{
$label = $1;
}
else # give up
{
$label = $link;
$label =~ s#/# :: #go;
}
# prettify
$label =~ s#_# #go;
$label =~ s#-# #go;
$label =~ s/(\b[a-z][-\w]+)/\u\L$1/go;
}
# if we are hiding the extensions of files
# we need to display an extensionless link
# while doing checks with the original link
my $display_link = $link;
if ($args{hide_ext})
{
if ($link =~ /(.*)\.[-\w]+$/o) # file
{
$display_link = $1;
}
}
my $item = '';
my $desc = '';
if (exists $args{descriptions}->{$link}
and defined $args{descriptions}->{$link}
and $args{descriptions}->{$link})
{
$desc = join('', $format{pre_desc},
$args{descriptions}->{$link},
$format{post_desc});
}
if (link_is_active(this_link=>$link,
current_url=>$args{current_url}))
{
$item = join('', $format{pre_item_active},
$format{pre_active_item},
$label,
$format{post_active_item},
$desc,
);
}
elsif ($args{no_link})
{
$item = join('', $format{pre_item},
$label,
$desc);
}
elsif ($args{current_url}
and exists $args{current_parents}->{$link}
and $args{current_parents}->{$link})
{
$item = join('', $format{pre_item_current_parent},
$format{pre_current_parent},
'<a href="', $prefix_url, $display_link, '">',
$label, '</a>',
$format{post_current_parent},
$desc);
}
else
{
$item = join('', $format{pre_item},
'<a href="', $prefix_url, $display_link, '">',
$label, '</a>',
$desc);
}
if (!$args{defer_post_item})
{
$item = join('', $item, $format{post_item});
}
return $item;
} # make_item
=head2 make_canonical
my $new_url = make_canonical($url);
Make a URL canonical; remove the 'index.*' and add on a needed
'/' -- this assumes that directory names never have a '.' in them.
=cut
sub make_canonical {
my $url = shift;
return $url if (!$url);
if ($url =~ m{^/index\.\w+$}o)
{
$url = '/';
}
elsif ($url =~ m{^(.*/)index\.\w+$}o)
{
$url = $1;
}
elsif ($url =~ m{/[-\w]+$}o) # no dots; a directory
{
$url = join('', $url, '/'); # add the slash
}
return $url;
} # make_canonical
=head2 get_index_path
my $new_url = get_index_path($url);
Get the "index" part of this path. That is, if this path
is not for an index-page, then get the parent index-page
path for this path.
(Removes the trailing slash).
=cut
sub get_index_path {
my $url = shift;
return $url if (!$url);
$url = make_canonical($url);
if ($url =~ m{^(.*)/[-\w]+\.\w+$}o)
{
$url = $1;
}
elsif ($url ne '/' and $url =~ m{/$}o)
{
chop $url;
}
return $url;
} # get_index_path
=head2 get_index_parent
my $new_url = get_index_parent($url);
Get the parent of the "index" part of this path.
(Removes the trailing slash).
=cut
sub get_index_parent {
my $url = shift;
return $url if (!$url);
$url = get_index_path($url);
if ($url =~ m#^(.*)/[-\w]+$#o)
{
$url = $1;
}
return $url;
} # get_index_parent
=head2 path_depth
my $depth = path_depth($url);
Calculate the "depth" of the given path.
=cut
sub path_depth {
my $url = shift;
return 0 if ($url eq '/'); # root is zero
if ($url =~ m!/$!o) # remove trailing /
{
chop $url;
}
return scalar ($url =~ tr!/!/!);
} # path_depth
=head2 link_is_active
if (link_is_active(this_link=>$link, current_url=>$url))
...
Check if the given link is "active", that is, if it
matches the 'current_url'.
=cut
sub link_is_active {
my %args = (
this_link=>'',
current_url=>'',
@_
);
# if there is no current link, is not active.
return 0 if (!$args{current_url});
my $link = make_canonical($args{this_link});
return 1 if ($link eq $args{current_url});
return 0;
} # link_is_active
=head2 traverse_lol
$links = traverse_lol(\@list_of_lists,
labels=>\%labels,
tree_depth=>$depth
current_format=>\%format,
...
);
Traverse the list of lists (of urls) to produce
a nested collection of links.
This consumes the list_of_lists!
=cut
sub traverse_lol {
my $lol_ref = shift;
my %args = (
current_url=>'',
labels=>undef,
prefix_url=>'',
hide_ext=>0,
@_
);
my $tree_depth = $args{tree_depth};
my %format = (
%{$args{current_format}},
(exists $args{formats}->{$tree_depth}
? %{$args{formats}->{$tree_depth}}
: ())
);
my @items = ();
while (@{$lol_ref})
{
my $ll = shift @{$lol_ref};
if (!ref $ll) # an item
{
my $link = $ll;
my $label = (exists $args{labels}->{$link}
? $args{labels}->{$link} : '');
my $item = make_item(this_link=>$link,
this_label=>$label,
defer_post_item=>1,
%args,
format=>\%format);
if (ref $lol_ref->[0]) # next one is a list
{
$ll = shift @{$lol_ref};
my $sublist = traverse_lol($ll, %args,
tree_depth=>$tree_depth + 1,
current_format=>\%format);
$item = join($format{tree_sep}, $item, $sublist);
}
$item = join('', $item, $format{post_item});
push @items, $item;
}
else # a reference to a list
{
if (defined $args{start_depth}
&& $args{tree_depth} < $args{start_depth})
{
return traverse_lol($ll, %args, current_format=>\%format);
}
else
{
my $sublist = traverse_lol($ll, %args,
tree_depth=>$tree_depth + 1,
current_format=>\%format);
my $item = join($format{tree_sep}, $format{pre_item}, $sublist);
$item = join('', $item, $format{post_item});
push @items, $item;
}
}
}
my $list = join($format{item_sep}, @items);
return join('',
(($args{end_depth} && $tree_depth == $args{end_depth} )
? $args{last_subtree_head}
: $format{tree_head}),
$list,
(($args{end_depth} && $tree_depth == $args{end_depth} )
? $args{last_subtree_foot}
: $format{tree_foot})
);
} # traverse_lol
=head2 extract_all_paths
my @all_paths = extract_all_paths(paths=>\@paths,
preserve_order=>0);
Extract all possible paths out of a list of paths.
Thus, if one has
/foo/bar/baz.html
then that would make
/
/foo/
/foo/bar/
/foo/bar/baz.html
If 'preserve_order' is true, this preserves the ordering of
the paths in the input list; otherwise the output paths
are sorted alphabetically.
=cut
sub extract_all_paths {
my %args = (
paths=>undef,
preserve_order=>0,
@_
);
my %paths = ();
# keep track of the order of the paths in the list of paths
my $order = 1;
foreach my $path (@{$args{paths}})
{
my @path_split = split('/', $path);
# first path as-is
$paths{$path} = $order;
pop @path_split;
while (@path_split)
{
# these paths are index-pages. should end in '/'
my $newpath = join('/', @path_split, '');
# give this path the same order-num as the full path
# but only if it hasn't already been added
$paths{$newpath} = $order if (!exists $paths{$newpath});
pop @path_split;
}
$order++ if ($args{preserve_order});
}
return sort {
return $a cmp $b if ($paths{$a} == $paths{$b});
return $paths{$a} <=> $paths{$b};
} keys %paths;
} # extract_all_paths
=head2 extract_current_parents
my %current_parents = extract_current_parents(current_url=>$url,
exclude_root_parent=>0);
Extract the "parent" paths of the current url
/foo/bar/baz.html
then that would make
/
/foo/
/foo/bar/
If 'exclude_root_parent' is true, then the '/' is excluded from the
list of parents.
=cut
sub extract_current_parents {
my %args = (
current_url=>undef,
exclude_root_parent=>0,
@_
);
my %paths = ();
if ($args{current_url})
{
my $current_url = $args{current_url};
my @path_split = split('/', $current_url);
pop @path_split; # remove the current url
while (@path_split)
{
# these paths are index-pages. should end in '/'
my $newpath = join('/', @path_split, '');
$paths{$newpath} = 1;
pop @path_split;
}
if ($args{exclude_root_parent})
{
delete $paths{"/"};
}
}
return %paths;
} # extract_current_parents
=head2 build_lol
my @lol = build_lol(
paths=>\@paths,
current_url=>$url,
navbar_type=>'',
);
Build a list of lists of paths, given a simple list of paths.
Assumes that this list has already been filtered.
=over
=item paths
Reference to list of paths; this is consumed.
=back
=cut
sub build_lol {
my %args = (
paths=>undef,
depth=>0,
start_depth=>0,
end_depth=>0,
current_url=>'',
navbar_type=>'',
prepend_list=>undef,
append_list=>undef,
@_
);
my $paths_ref = $args{paths};
my $depth = $args{depth};
my @list_of_lists = ();
while (@{$paths_ref})
{
my $path = $paths_ref->[0];
my $can_path = make_canonical($path);
my $path_depth = path_depth($can_path);
my $path_is_index = ($can_path =~ m#/$#o);
if ($path_depth == $depth)
{
shift @{$paths_ref}; # use this path
push @list_of_lists, $path;
}
elsif ($path_depth > $depth)
{
push @list_of_lists, [build_lol(
%args,
prepend_list=>undef,
append_list=>undef,
paths=>$paths_ref,
depth=>$path_depth,
navbar_type=>$args{navbar_type},
current_url=>$args{current_url},
)];
}
elsif ($path_depth < $depth)
{
return @list_of_lists;
}
}
# prepend the given list to the top level
if (defined $args{prepend_list} and @{$args{prepend_list}})
{
# if the list of lists is a single item which is a list
# then add the extra list to that item
if ($#list_of_lists == 0
and ref($list_of_lists[0]) eq "ARRAY")
{
unshift @{$list_of_lists[0]}, @{$args{prepend_list}};
}
else
{
unshift @list_of_lists, @{$args{prepend_list}};
}
}
# append the given list to the top level
if (defined $args{append_list} and @{$args{append_list}})
{
# if the list of lists is a single item which is a list
# then add the extra list to that item
if ($#list_of_lists == 0
and ref($list_of_lists[0]) eq "ARRAY")
{
push @{$list_of_lists[0]}, @{$args{append_list}};
}
else
{
push @list_of_lists, @{$args{append_list}};
}
}
return @list_of_lists;
} # build_lol
=head2 filter_out_paths
my @filtered_paths = filter_out_paths(
paths=>\@paths,
current_url=>$url,
hide=>$hide,
nohide=>$nohide,
start_depth=>$start_depth,
end_depth=>$end_depth,
top_level=>$top_level,
navbar_type=>'',
);
Filter out the paths we don't want from our list of paths.
Returns a list of the paths we want.
=cut
sub filter_out_paths {
my %args = (
paths=>undef,
start_depth=>0,
end_depth=>0,
top_level=>0,
current_url=>'',
navbar_type=>'',
hide=>'',
nohide=>'',
@_
);
my $paths_ref = $args{paths};
my $hide = $args{hide};
my $nohide = $args{nohide};
my %canon_paths = ();
my @wantedpaths1 = ();
my %path_depth = ();
# filter out common things
# remember canonical paths and path depths
foreach my $path (@{$paths_ref})
{
my $can_path = make_canonical($path);
my $path_depth = path_depth($can_path);
if ($hide and $nohide
and not($path =~ /$nohide/)
and $path =~ /$hide/)
{
# skip this one
}
elsif ($hide and !$nohide and $path =~ /$hide/)
{
# skip this one
}
elsif ($path_depth < $args{start_depth})
{
# skip this one
}
elsif ($args{end_depth}
and $path_depth > $args{end_depth})
{
# skip this one
}
else
{
$path_depth{$path} = $path_depth;
$canon_paths{$path} = $can_path;
push @wantedpaths1, $path;
}
}
my @wantedpaths = ();
if ($args{current_url})
{
my $current_url = $args{current_url};
my $current_url_depth = path_depth($args{current_url});
my $current_url_is_index = ($args{current_url} =~ m{/$}o);
my $parent = make_canonical($current_url_is_index
? get_index_parent($args{current_url})
: get_index_path($args{current_url})
);
my $parent_depth = path_depth($parent);
my $grandparent = ($parent_depth == 1
? '/'
: make_canonical(get_index_parent($parent)));
my $greatgrandparent = ($parent_depth <= 1
? ''
: ($parent_depth == 2
? '/'
: make_canonical(get_index_parent($grandparent))
)
);
my $current_index_path = get_index_path($args{current_url});
my $current_index_parent = get_index_parent($args{current_url});
if ($args{navbar_type} eq 'breadcrumb')
{
foreach my $path (@wantedpaths1)
{
my $pd = $path_depth{$path};
# a breadcrumb-navbar shows the parent, self,
# and the children the parent
if ($pd <= $current_url_depth
and $args{current_url} =~ /^$path/)
{
push @wantedpaths, $path;
}
elsif ($path eq $args{current_url})
{
push @wantedpaths, $path;
}
elsif ($pd >= $current_url_depth
and $path =~ m{^${current_url}})
{
push @wantedpaths, $path;
}
elsif ($parent
and $pd >= $current_url_depth
and $path =~ m{^$parent})
{
push @wantedpaths, $path;
}
}
}
elsif ($args{navbar_type} or $args{do_navbar})
{
# Rules for navbars:
# * if I am a leaf node, see my (great)uncles and siblings
# * if have children, use the same data as my parent,
# plus my immediate children
foreach my $path (@wantedpaths1)
{
my $pd = $path_depth{$path};
if ($pd > $current_url_depth + 1)
{
next;
}
if ($pd == $current_url_depth + 1
and $path =~ m{^${current_url}})
{
push @wantedpaths, $path;
}
elsif ($pd == $current_url_depth
and $path =~ m{^${parent}})
{
push @wantedpaths, $path;
}
elsif ($grandparent
and $pd == $parent_depth
and $path =~ m{^$grandparent})
{
push @wantedpaths, $path;
}
elsif ($greatgrandparent
and $pd == $parent_depth - 1
and $path =~ m{^$greatgrandparent})
{
push @wantedpaths, $path;
}
}
}
else
{
push @wantedpaths, @wantedpaths1;
}
}
else
{
push @wantedpaths, @wantedpaths1;
}
return @wantedpaths;
} # filter_out_paths
=head2 make_default_format
my %default_format = make_default_format(%args);
Make the default format hash from the args.
Returns a hash of format options.
=cut
sub make_default_format {
my %args = (
links_head=>'<ul>',
links_foot=>"\n</ul>",
subtree_head=>'<ul>',
subtree_foot=>"\n</ul>",
last_subtree_head=>'<ul>',
last_subtree_foot=>"\n</ul>",
pre_item=>'<li>',
post_item=>'</li>',
pre_active_item=>'<em>',
post_active_item=>'</em>',
pre_current_parent=>'',
post_current_parent=>'',
item_sep=>"\n",
tree_sep=>"\n",
@_
);
my %default_format = (
pre_item=>$args{pre_item},
post_item=>$args{post_item},
pre_active_item=>$args{pre_active_item},
post_active_item=>$args{post_active_item},
pre_current_parent=>$args{pre_current_parent},
post_current_parent=>$args{post_current_parent},
pre_desc=>$args{pre_desc},
post_desc=>$args{post_desc},
item_sep=>$args{item_sep},
tree_sep=>$args{tree_sep},
tree_head=>$args{links_head},
tree_foot=>$args{links_foot},
pre_item_active=>($args{pre_item_active}
? $args{pre_item_active}
: $args{pre_item}),
pre_item_current_parent=>
($args{pre_item_current_parent}
? $args{pre_item_current_parent}
: $args{pre_item}),
);
return %default_format;
} # make_default_format
=head2 make_extra_formats
my %formats = make_extra_formats(%args);
Transforms the subtree_head and subtree_foot into the "formats"
method of formatting.
Returns a hash of hashes of format options.
=cut
sub make_extra_formats {
my %args = (
formats=>undef,
links_head=>'<ul>',
links_foot=>"\n</ul>",
subtree_head=>'<ul>',
subtree_foot=>"\n</ul>",
last_subtree_head=>'<ul>',
last_subtree_foot=>"\n</ul>",
pre_item=>'<li>',
post_item=>'</li>',
pre_item_active=>'<li>',
pre_item_current_parent=>'<li>',
pre_active_item=>'<em>',
post_active_item=>'</em>',
pre_current_parent=>'',
post_current_parent=>'',
item_sep=>"\n",
tree_sep=>"\n",
@_
);
my %formats = ();
if (defined $args{formats})
{
%formats = %{$args{formats}};
}
if ($args{links_head} ne $args{subtree_head}
|| $args{links_foot} ne $args{subtree_foot})
{
if (!exists $formats{1})
{
$formats{1} = {};
}
$formats{1}->{tree_head} = $args{subtree_head};
$formats{1}->{tree_foot} = $args{subtree_foot};
}
return %formats;
} # make_extra_formats
=head1 REQUIRES
Test::More
=head1 INSTALLATION
To install this module, run the following commands:
perl Build.PL
./Build
./Build test
./Build install
Or, if you're on a platform (like DOS or Windows) that doesn't like the
"./" notation, you can do this:
perl Build.PL
perl Build
perl Build test
perl Build install
In order to install somewhere other than the default, such as
in a directory under your home directory, like "/home/fred/perl"
go
perl Build.PL --install_base /home/fred/perl
as the first step instead.
This will install the files underneath /home/fred/perl.
You will then need to make sure that you alter the PERL5LIB variable to
find the modules.
Therefore you will need to change the PERL5LIB variable to add
/home/fred/perl/lib
PERL5LIB=/home/fred/perl/lib:${PERL5LIB}
=head1 SEE ALSO
perl(1).
=head1 BUGS
Please report any bugs or feature requests to the author.
=head1 AUTHOR
Kathryn Andersen (RUBYKAT)
perlkat AT katspace dot com
http://www.katspace.com/tools/html_linklist/
=head1 COPYRIGHT AND LICENCE
Copyright (c) 2006 by Kathryn Andersen
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of HTML::LinkList
__END__
|