This file is indexed.

/usr/share/perl5/Pod/Tree/Node.pm is in libpod-tree-perl 1.17-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
# Copyright (c) 1999-2004 by Steven McDougall.  This module is free
# software; you can redistribute it and/or modify it under the same
# terms as Perl itself.

package Pod::Tree::Node;

use strict;
use Pod::Escapes ();


$Pod::Tree::Node::VERSION = '1.10';


sub root  # ctor
{
    my($class, $children) = @_;

    my $node = { type     => 'root',
		 children => $children };

    bless $node, $class
}


sub code #ctor
{
    my($class, $paragraph) = @_;

    my $node = { type => 'code',
		 text => $paragraph };

    bless $node, $class
}


sub verbatim  # ctor
{
    my($class, $paragraph) = @_;

    my $node = { type => 'verbatim',
		 raw  => $paragraph,
		 text => $paragraph };

    bless $node, $class
}


my %Argumentative = map { $_ => 1 } qw(=over
				       =for =begin =end);

sub command  # ctor
{
    my($class, $paragraph) = @_;
    my($command, $arg, $text);

    ($command) = split(/\s/, $paragraph);

    if ($Argumentative{$command})
    {
	($command, $arg, $text) = split(/\s+/, $paragraph, 3);
    }
    else
    {
	($command,       $text) = split(/\s+/, $paragraph, 2);
	$arg = '';
    }

    $command =~ s/^=//;

    my $node = { type    => 'command',
		 raw	 => $paragraph,
		 command => $command,
		 arg     => $arg,
		 text    => $text };

    bless $node, $class
}


sub ordinary  # ctor
{
    my($class, $paragraph) = @_;

    my $node = { type => 'ordinary',
		 raw  => $paragraph,
		 text => $paragraph };

    bless $node, $class
}


sub letter  # ctor
{
    my($class, $token) = @_;

    my $node = { type   => 'letter',
		 letter => substr($token, 0, 1),
		 width  => $token =~ tr/</</     };

    bless $node, $class
}    


sub sequence  # ctor
{
    my($class, $letter, $children) = @_;

    my $node = {  type     => 'sequence',
		 'letter'  => $letter->{'letter'},
		  children => $children };

    bless $node, $class
}    


sub text  # ctor
{
    my($class, $text) = @_;

    my $node = { type => 'text',
		 text => $text };

    bless $node, $class
}


sub target  # ctor
{
    my($class, $children) = @_;

    my $node = bless { type     => 'target',
		       children => $children }, $class;

    $node->unescape;
    my $text = $node->get_deep_text;

    if ($text =~ m(^[A-Za-z]+:(?!:))) # a URL
    {
	$node->{page   } = $text;
	$node->{section} = '';
	$node->{domain } = 'HTTP';
    }
    else                              # a POD link
    {
	my($page, $section) = SplitTarget($text);
	$node->{page   } = $page;
	$node->{section} = $section;
	$node->{domain } = 'POD';
    }

    $node
}


sub SplitTarget
{
    my $text = shift;
    my($page, $section);

    if ($text =~ /^"(.*)"$/s)     # L<"sec">;
    {
	$page    = '';
	$section = $1;
    }
    else                          # all other cases
    {
	($page, $section) = (split(m(/), $text, 2), '', '');

	$page    =~ s/\s*\(\d\)$//;    # ls (1) -> ls
	$section =~ s( ^" | "$ )()xg;  # lose the quotes

	# L<section in this man page> (without quotes)
	if ($page !~ /^[\w.-]+(::[\w.-]+)*$/ and $section eq '')   
	{                            
	    $section = $page;
	    $page = '';
	}
    }

    $section =~ s(   \s*\n\s*   )( )xg;  # close line breaks
    $section =~ s( ^\s+ | \s+$  )()xg;   # clip leading and trailing WS
    
    ($page, $section)
}


sub link  # ctor
{
    my($class, $node, $page, $section) = @_;

    my $target = bless { type     => 'target',
			 domain   => 'POD',
			 children => [ $node ],
			 page     => $page,
		         section  => $section }, $class;


    my $link =   bless { type     => 'sequence',
			 letter   => 'L',
			 children => [ $node ],
			 target   => $target }, $class;

    $link
}


sub is_code     { shift->{type} eq 'code'     }
sub is_command  { shift->{type} eq 'command'  }
sub is_for      { shift->{type} eq 'for'      }
sub is_item     { shift->{type} eq 'item'     }
sub is_letter   { shift->{type} eq 'letter'   }
sub is_list     { shift->{type} eq 'list'     }
sub is_ordinary { shift->{type} eq 'ordinary' }
sub is_root     { shift->{type} eq 'root'     }
sub is_sequence { shift->{type} eq 'sequence' }
sub is_text     { shift->{type} eq 'text'     }
sub is_verbatim { shift->{type} eq 'verbatim' }

sub is_link
{
    my $node = shift;
    is_sequence $node and $node->{'letter'} eq 'L'
}

sub is_pod
{
    my $node = shift;
    not is_code $node and not is_c_cut $node and not is_c_pod $node
}

sub is_c_head1
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'head1' 
}

sub is_c_head2
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'head2' 
}

sub is_c_head3
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'head3' 
}

sub is_c_head4
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'head4' 
}

sub is_c_cut
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'cut' 
}

sub is_c_pod
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'pod' 
}

sub is_c_over
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'over' 
}

sub is_c_back 
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'back' 
}

sub is_c_item
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'item' 
}

sub is_c_for
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'for' 
}

sub is_c_begin
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'begin' 
}

sub is_c_end
{ 
    my $node = shift; 
    $node->{type} eq 'command' and $node->{'command'} eq 'end' 
}

sub get_arg       { shift->{ arg       } }
sub get_back      { shift->{ back      } }
sub get_children  { shift->{ children  } }
sub get_command   { shift->{'command'  } }
sub get_domain    { shift->{ domain    } }
sub get_item_type { shift->{ item_type } }
sub get_letter    { shift->{'letter'   } }
sub get_list_type { shift->{ list_type } }
sub get_page      { shift->{ page      } }
sub get_raw       { shift->{ raw       } }
sub get_raw_kids  { shift->{ raw_kids  } }
sub get_section   { shift->{ section   } }
sub get_siblings  { shift->{ siblings  } }
sub get_target    { shift->{'target'   } }
sub get_text      { shift->{'text'     } }
sub get_type      { shift->{'type'     } }
sub get_url       { shift->{'url'      } }

sub get_brackets  
{ 
    my $node     = shift;
    my $brackets = $node->{brackets};
    $brackets
}


sub get_deep_text
{
    my $node = shift;

    for ($node->get_type)
    {
	/text/     and return $node->{'text'};
	/verbatim/ and return $node->{'text'};
    }

    join '', map { $_->get_deep_text } @{$node->{children}}
}


sub force_text
{
    my($node, $text) = @_;
    $node->{ type } = 'text';
    $node->{'text'} = $text;
    undef $node->{children};
}


sub force_for
{
    my $node = shift;
    $node->{type    } = 'for';

    my($bracket) = $node->{raw} =~ /^(=\w+\s+\w+\s+)/;

    $node->{brackets} = [ $bracket ];
}


sub parse_begin
{
    my($node, $nodes) = @_;

    my $foreign;
    my @raw;
    while (@$nodes)
    {
	$foreign = shift @$nodes;
	is_c_end $foreign and last;
	push @raw, $foreign->{'raw'};
    }
    $node->{'text'} = join '', @raw;

    my $interpreter = $foreign->{arg};
    $interpreter and $interpreter ne $node->{arg} and
	$node->_warn("Mismatched =begin/=end tags around\n$node->{'text'}");

    $node->{type    } = 'for';
    $node->{brackets} = [ $node->{raw}, $foreign->{raw} ];
}


sub set_children
{
    my($node, $children) = @_;
    $node->{children} = $children;
}


sub make_sequences
{
    my $node          = shift;
    my $text          = $node->{'text'};
    my @tokens        = split /( [A-Z]<<+\s+ | [A-Z]< | \s+>>+ | > )/x, $text;
    my $sequences     = _parse_text(\@tokens);
    $node->{children} = $sequences;
}


sub _parse_text
{
    my $tokens = shift;
    my(@stack, @width);

    while (@$tokens)
    {
	my $token = shift @$tokens;
	length $token or next;

	$token =~ /^[A-Z]</ and do
	{
	    my $width = $token =~ tr/</</;
	    push @width, $width;
	    my $node = letter Pod::Tree::Node $token;
	    push @stack, $node;
	    next;
	};

	@width and $token =~ />{$width[-1],}$/ and do
	{
	    my $width = pop @width;
	    my($letter, $interior) = _pop_sequence(\@stack, $width);
	    my $node = sequence Pod::Tree::Node $letter, $interior;
	    push @stack, $node;
	    $token =~ s/^\s*>{$width}//;
	    my @tokens = split //, $token;
	    unshift @$tokens, @tokens;
	    next;
	};

	my $node = text Pod::Tree::Node $token;
	push @stack, $node;
    }

    if (@width)
    {
	my @text = map { $_->get_deep_text } @stack;
	Pod::Tree::Node->_warn("Missing '>' delimiter in\n@text");
    }

    \@stack
}


sub _pop_sequence
{
    my($stack, $width) = @_;
    my($node, @interior);

    while (@$stack)
    {
	$node = pop @$stack;
	is_letter $node and $node->{width} == $width and 
	    return ($node, \@interior);
	unshift @interior, $node;
    }

    my @text = map { $_->get_deep_text } @interior;
    $node->_warn("Mismatched sequence delimiters around\n@text");

    $node = letter Pod::Tree::Node  ' ';
    $node, \@interior;
}


sub parse_links
{
    my $node = shift;

    is_link $node and $node->_parse_link;

    my $children = $node->{children};
    for my $child (@$children)
    {
	$child->parse_links;
    }
}


sub _parse_link
{
    my $node = shift;

    $node->{raw_kids} = $node->clone->{children};

    my $children = $node->{children};
    my($text_kids, $target_kids) = SplitBar($children);

    $node->{ children } = $text_kids;
    $node->{'target'  } = target Pod::Tree::Node $target_kids;
}


sub SplitBar
{
    my $children = shift;
    my(@text, @link);

    while (@$children)
    {
	my $child = shift @$children;

	is_text $child or do 
	{
	    push @text, $child;
	    next;
	};
	
	my($text, $link) = split m(\|), $child->{'text'}, 2;
	$link and do
	{
	    push @text,  text Pod::Tree::Node $text if $text;
	    push @link, (text Pod::Tree::Node $link), @$children;
	    return (\@text, \@link)
	};

	push @text, $child;
    }

    (\@text, \@text)
}


sub unescape
{
    my $node = shift;

    my $children = $node->{children};
    for my $child (@$children)
    {
	$child->unescape;
    }

    is_sequence $node and $node->_unescape_sequence;
}


sub _unescape_sequence
{
    my $node = shift;

    for ($node->{'letter'})
    {
	/Z/ and $node->force_text(''), last;
	/E/ and do 
	{
	    my $child = $node->{children}[0];
	    $child or last;
	    my $text = $child->_unescape_text;
	    $text and $node->force_text($text);
	    last;
	};
    }
}


sub _unescape_text
{
    my $node = shift;
    my $text = $node->{'text'};

    defined $text ? Pod::Escapes::e2char($text) : "E<UNDEF?!>";
}



sub consolidate
{
    my $node = shift;
    my $old  = $node->{children};
    $old and @$old or return;

    my $new  = [];

    push @$new, shift @$old;

    while (@$old)
    {
	if (is_text     $new->[-1] and is_text     $old->[ 0] or
	    is_verbatim $new->[-1] and is_verbatim $old->[ 0] or
	    is_code     $new->[-1] and is_code     $old->[ 0] )
	{
	    $new->[-1]{'text'} .= $old->[0]{'text'};
	    shift @$old;
	}
	else
	{
	    push @$new, shift @$old;	    
	}
    }

    $node->{children} = $new;

    for my $child (@$new)
    {
	$child->consolidate;
    }
}


sub make_lists
{
    my $root  = shift;
    my $nodes = $root->{children};

    $root->_make_lists($nodes);
}


sub _make_lists
{
    my($node, $old) = @_;
    my $new = [];
    my $back;

    while (@$old)
    {
	my $child = shift @$old;
	is_c_over $child and $child->_make_lists($old);
	is_c_item $child and $child->_make_item ($old);
	is_c_back $child and $back = $child, last;
	push @$new, $child;
    }

    $node->{children} = $new;

    is_root $node and return;

    $node->{type} = 'list';
    $node->{back} = $back;
    $node->_set_list_type;
}


sub _set_list_type
{
    my $list     = shift;
    my $children = $list->{children};

    $list->{list_type} = '';  # -w

    for my $child (@$children)
    {
	$child->{type} eq 'item' or next;
	$list->{list_type} = $child->{item_type};
	last;
    }
}


sub _make_item
{
    my($item, $old) = @_;
    my $siblings = [];

    while (@$old)
    {
	my $sibling = $old->[0];
	is_c_item $sibling and last;
	is_c_back $sibling and last;

	shift @$old;
	is_c_over $sibling and do
	{
	    $sibling->_make_lists($old);
	};
	push @$siblings, $sibling;
    }

    $item->{type    } = 'item';
    $item->{siblings} = $siblings;
    $item->_set_item_type;
}


sub _set_item_type
{
    my $item = shift;
    my $text = $item->{'text'};

    $text =~ m(^\s* \*  \s*$ )x and $item->{item_type} = 'bullet';
    $text =~ m(^\s* \d+ \s*$ )x and $item->{item_type} = 'number';
    $item->{item_type} or $item->{item_type} = 'text';
}

sub clone
{
    my $node  = shift;
    my $clone = { %$node };

    my $children = $node->{children};
    $clone->{children} = [ map { $_->clone } @$children ];

    bless $clone, ref $node
}


my $Indent;
my $String;

sub dump
{
    my $node = shift;

    $Indent = 0;
    $String = '';
    $node->_dump;
    $String
}


sub _dump
{
    my $node = shift;
    my $type = $node->get_type;

    $String .=  ' ' x $Indent .  uc $type . " ";

    for ($type)
    {
	/command/  and $String .= $node->_dump_command;
	/code/     and $String .= $node->_dump_code;
	/for/      and $String .= $node->_dump_for;
	/item/     and $String .= $node->_dump_item;
	/list/     and $String .= $node->_dump_list;
	/ordinary/ and $String .= "\n";
	/root/     and $String .= "\n";
	/sequence/ and $String .= $node->_dump_sequence;
	/text/     and $String .= $node->_dump_text;
	/verbatim/ and $String .= $node->_dump_verbatim;
    }

    $node->_dump_children;
    $node->_dump_siblings;
}


sub _dump_command
{
    my $node    = shift;
    my $command = $node->get_command;
    my $arg     = $node->get_arg;

    "$command $arg\n"
}


sub _dump_code
{
    my $node  = shift;

    my $text  = _indent($node->get_text, 3);
    my $block = "\n{\n$text}\n";

    _indent($block, $Indent)
}

sub _dump_for
{
    my $node = shift;
    my $arg  = $node->get_arg;
    my $text = _indent($node->get_text, $Indent+3);

    "$arg\n$text\n"
}


sub _dump_item
{
    my $node = shift;
    uc $node->get_item_type . "\n"
}


sub _dump_list
{
    my $node = shift;
    uc $node->get_list_type . "\n"
}


sub _dump_sequence
{
    my $node   = shift;
    my $letter = $node->get_letter;
    my $link   = $node->is_link ? $node->_dump_target : '';

    "$letter$link\n";
}


sub _dump_text
{
    my $node = shift;
    my $text = $node->get_text;

    $text =~ s/([\x80-\xff])/sprintf("\\x%02x", ord($1))/eg;

    my $indent = ' ' x ($Indent+5);
    $text =~ s( (?<=\n) (?=.) )($indent)xg;
    "$text\n"
}


sub _dump_verbatim
{
    my $node = shift;
    "\n" . $node->get_text . "\n"
}


sub _dump_target
{
    my $node    = shift;
    my $target  = $node->get_target;
    my $page    = $target->{page};
    my $section = $target->{section};
    " $page / $section"
}


sub _dump_children
{
    my $node     = shift;
    my $children = $node->get_children;
    $children and DumpList($children, '{', '}');
}

    
sub _dump_siblings
{
    my $node     = shift;
    my $siblings = $node->get_siblings;
    $siblings and DumpList($siblings, '[', ']');
}

    
sub DumpList
{
    my($nodes, $open, $close) = @_;

    $String .= ' ' x $Indent . "$open\n";
    $Indent += 3;

    for my $node (@$nodes)
    {
	$node->_dump;
    }

    $Indent -= 3;
    $String .= ' ' x $Indent . "$close\n";
}


sub _indent
{
    my($text, $spaces) = @_;
    my $indent = ' ' x $spaces;
    $text =~ s( (?<=\n) (?=.) )($indent)xg;
    $indent . $text
}


sub _warn
{
    my($node, $message) = @_;

    my $filename = $node->get_filename;
    my $tag      = $filename ? "in $filename" : $filename;
    warn "$message $tag\n";
}

sub set_filename
{
    my($package, $filename) = @_;
    
    $Pod::Tree::Node::filename = $filename;
}

sub get_filename
{
    $Pod::Tree::Node::filename    
}


1

__END__


=head1 NAME

Pod::Tree::Node - nodes in a Pod::Tree

=head1 SYNOPSIS

  $node = root     Pod::Tree::Node \@paragraphs;
  $node = code     Pod::Tree::Node $paragraph;
  $node = verbatim Pod::Tree::Node $paragraph;
  $node = command  Pod::Tree::Node $paragraph;
  $node = ordinary Pod::Tree::Node $paragraph;
  $node = letter   Pod::Tree::Node $token;
  $node = sequence Pod::Tree::Node $letter, \@children;
  $node = text     Pod::Tree::Node $text;
  $node = target   Pod::Tree::Node $target;
  $node = link     Pod::Tree::Node $node, $page, $section;
  
  is_code     $node and ...
  is_command  $node and ...
  is_for      $node and ...
  is_item     $node and ...
  is_letter   $node and ...
  is_list     $node and ...
  is_ordinary $node and ...
  is_pod      $node and ...
  is_root     $node and ...
  is_sequence $node and ...
  is_text     $node and ...
  is_verbatim $node and ...
  is_link     $node and ...
  
  is_c_head1  $node and ...
  is_c_head2  $node and ...
  is_c_head3  $node and ...
  is_c_head4  $node and ...
  is_c_cut    $node and ...
  is_c_pod    $node and ...
  is_c_over   $node and ...
  is_c_back   $node and ...
  is_c_item   $node and ...
  is_c_for    $node and ...
  is_c_begin  $node and ...
  is_c_end    $node and ...
  
  $arg       = get_arg       $node;
  $brackets  = get_brackets  $node;
  $children  = get_children  $node;
  $command   = get_command   $node;
  $domain    = get_domain    $node;
  $item_type = get_item_type $node;
  $letter    = get_letter    $node;
  $list_type = get_list_type $node;
  $page      = get_page      $node;
  $raw       = get_raw       $node;
  $raw_kids  = get_raw_kids  $node;
  $section   = get_section   $node;
  $siblings  = get_siblings  $node;
  $target    = get_target    $node;
  $text      = get_text      $node;
  $type      = get_type      $node;
  $deep_text = get_deep_text $node;
  
  $node->force_text($text);
  $node->force_for;
  $node->parse_begin (\@nodes);
  $node->set_children(\@children);
  $node->make_sequences;
  $node->parse_links;
  $node->unescape;
  $node->consolidate;
  $node->make_lists;

  $node->clone;
  $node->dump;
  
  Pod::Tree::Node->set_filename($filename);
  $filename = $node->get_filename;


=head1 REQUIRES

Pod::Escapes


=head1 DESCRIPTION

C<Pod::Tree::Node> objects are nodes in a tree that represents a POD.
Applications walk the tree to recover the structure and content of the POD.

Methods are provided for

=over 4

=item *

creating nodes in the tree

=item *

parsing the POD into nodes

=item *

returning information about nodes

=item *

walking the tree

=back


=head1 TREE STRUCTURE


=head2 Root node

The tree descends from a single root node;
C<is_root> returns true on this node and no other.

	$children = $root->get_children

returns a reference to an array of nodes.
These nodes represent the POD.


=head2 Node types

For each node,
call C<get_type> to discover the type of the node

	for $child (@$children)
	{
	    $type = $child->get_type;
	}

I<$type> will be one of these strings:

=over 4

=item 'root'

The node is the root of the tree.

=item 'code'

The node represents a paragraph that is not part of the POD.

=item 'verbatim'

The node represents a verbatim paragraph.

=item 'ordinary'

The node represents an ordinary paragraph.

=item 'command'

The node represents an =command paragraph (but not an =over paragraph).

=item 'sequence'

The node represents an interior sequence.

=item 'target'

The node represents the target of a link (An LE<lt>E<gt> markup).

=item 'text'

The node represents text that contains no interior sequences.

=item 'list'

The node represents an =over list.

=item 'item'

The node represents an item in an =over list.

=item 'for'

The node represents a =for paragraph,
or it represents the paragraphs between =begin/=end commands.

=back

Here are instructions for walking these node types.


=head2 root node

Call

	$children = $node->get_children

to get a list of nodes representing the POD.


=head2 code nodes

A code node contains the text of a paragraph that is not part of the
POD, for example, a paragraph that follows an C<=cut> command. Call

	$text = $node->get_text

to recover the text of the paragraph.


=head2 verbatim nodes

A verbatim node contains the text of a verbatim paragraph.
Call

	$text = $node->get_text

to recover the text of the paragraph.


=head2 ordinary nodes

An ordinary node represents the text of an ordinary paragraph.
The text is parsed into a list of text and sequence nodes;
these nodes are the children of the ordinary node.
Call

	$children = $node->get_children

to get a list of the children.
Iterate over this list to recover the text of the paragraph.


=head2 command nodes

A command node represents an =command paragraph.
Call 

	$command = $node->get_command;

to recover the name of the command. 
The name is returned I<without> the equals sign.

Z<>=over paragraphs are represented by list nodes,
not command nodes; see L<list nodes>, below.

The text of a command paragraph is parsed into 
a list of text and sequence nodes;
these nodes are the children of the command node.
Call

	$children = $node->get_children;

to get a list of the children.
Iterate over this list to recover the text of the paragraph.


=head2 sequence nodes

A sequence node represents a single interior sequence (a <> markup).
Call

	$node->get_letter

to recover the original markup letter.
The contents of the markup are parsed into a list of 
text and sequence nodes; 
these nodes are the children of the sequence node.
Call

	$node->get_children

to recover them.

ZE<lt>E<gt> and EE<lt>E<gt> markups do not generate sequence nodes;
these markups are expanded by C<Pod::Tree> when the tree is built.


=head2 target nodes

If a sequence node represents a link (an C<< LZ<><> >> markup),
then

	is_link $node

returns true and

	$target = $node->get_target

returns a node representing the target of the link. 

C<Pod::Tree::Node> can represent targets in two I<domains>: C<POD> and C<HTTP>.
The C<POD> domain represents the

	L<page/section>

markups that are described in L<perlpod>. 
The C<HTTP> domain represents C<< LZ<><> >> markups that contain a URL, e.g.

	L<http://foo.bar.com/page.html#fragment>

Call

	$domain = $target->get_domain

to discover the domain of the target.
For targets in the POD domain, call

	$page    = $target->get_page;
	$section = $target->get_section;

to recover the man page and section that the link refers to.
For targets in the HTTP domain, call

	$url     = $target->get_page;

to recover the URL for the link.

I<$target> is used only for constructing hyper-links;
the text to be displayed for the link is recovered by 
walking the children of I<$node>, as for any other interior sequence.


=head2 text nodes

A text node represents text that contains no interior sequences.
Call

	$text = $node->get_text

to recover the text.


=head2 list nodes

A list node represents an =over list.
Call

	$list_type = $node->get_list_type;

to discover the type of the list. This will be one of the strings

=over 4

=item 'bullet'

=item 'number'

=item 'text'

=back

The type of a list is the type of the first item in the list.

The children of a list node are item nodes;
each item node represents one item in the list.

You can call

	$node->get_arg;

to recover the indent value following the =over.


=head2 item nodes

An item node represents one item in an =over list.
Call

	$item_type = $node->get_item_type;

to discover the type of the item. 
This will be one of the strings shown above for L<list nodes>.
Typically, all the items in a list have the same type,
but C<Pod::Tree::Node> doesn't assume this.

The children of an item node represent the text of the =item paragraph;
this is usually of interest only for 'text' items.
Call

	$children = $node->get_children

to get a list of the children; 
these will be sequence and text nodes,
as for any other =command paragraph.

Each item node also has a list of nodes representing 
all the paragraphs following it,
up to the next =item command, 
or the end of the list.
These nodes are called I<siblings> of the item node.
Call

	$siblings = $node->get_siblings

to get a list of sibling nodes.


=head2 for nodes

for nodes represent text that is to be passed to an external formatter.
Call

	$formatter = $node->get_arg;

to discover the name of the formatter.
Call

	$text = $node->get_text;

to obtain the text to be passed to the formatter.
This will either be the text of an =for command, 
or all of the text between =begin and =end commands.


=head2 Walking the tree

PODs have a recursive structure;
therefore, any application that walks a Pod::Tree must also be recursive.
See F<skeleton> for an example of the necessary code.


=head1 METHODS

=head2 Constructors

These methods construct C<Pod::Tree::Node> objects.
They are used to build trees.
They aren't necessary to walk trees.

  $node = root     Pod::Tree::Node \@paragraphs;
  $node = code     Pod::Tree::Node $paragraph;
  $node = verbatim Pod::Tree::Node $paragraph;
  $node = command  Pod::Tree::Node $paragraph;
  $node = ordinary Pod::Tree::Node $paragraph;
  $node = letter   Pod::Tree::Node $token;
  $node = sequence Pod::Tree::Node $letter, \@children;
  $node = text     Pod::Tree::Node $text;
  $node = target   Pod::Tree::Node $target;
  $node = link     Pod::Tree::Node $node, $page, $section;

=over 4

=item I<$link> = C<Pod::Tree::Node>->C<link>(I<$node>, I<$page>, I<$section>)

Creates a new sequence node representing an C<< LZ<><> >> markup.
I<$node> becomes the sole child of the new node.
The target of the node is constructed from I<$page> and I<$section>.

This method isn't used to parse PODs.
It is provided for applications that want to create new links in an 
existing C<Pod::Tree> structure.

=back

=head2 Tests

These methods return true iff I<$node> has the type indicated by the
method name.

  is_code     $node and ...
  is_command  $node and ...
  is_for      $node and ...
  is_item     $node and ...
  is_letter   $node and ...
  is_link     $node and ...
  is_list     $node and ...
  is_ordinary $node and ...
  is_pod      $node and ...
  is_root     $node and ...
  is_sequence $node and ...
  is_text     $node and ...
  is_verbatim $node and ...

C<is_pod> returns true for all nodes except code, C<=pod>, 
and C<=cut> nodes. 


These methods return true iff I<$node> is a command node,
and the command is the one indicated by the method name.

  is_c_head1  $node and ...
  is_c_head2  $node and ...
  is_c_head3  $node and ...
  is_c_head4  $node and ...
  is_c_cut    $node and ...
  is_c_pod    $node and ...
  is_c_over   $node and ...
  is_c_back   $node and ...
  is_c_item   $node and ...
  is_c_for    $node and ...
  is_c_begin  $node and ...
  is_c_end    $node and ...


=head2 Accessors

These methods return information about nodes.
Most accessors are only relevant for certain types of nodes.

=over 4


=item I<$arg> = C<get_arg> I<$node>

Returns the argument of I<$node>.
This is the number following an =over command,
or the name of an external translator for =for, =begin, and =end commands.
Only relevant for these four command nodes.


=item I<$brackets> = C<get_brackets> I<$node>

Only relevant for for nodes.

If the node represents an =for command, 
I<@$brackets> is a single-element list.
The list element is the text of the =for command and its argument,
i.e. the name of the external formatter.

If the node represents a =begin/=end construct, 
I<@$brackets> is a two-element list containing
the text of the =begin and =end paragraphs.


=item I<$children> = C<get_children> I<$node>

Returns a reference to the list of nodes that are children of I<$node>.
May be called on any node.
The list may be empty.


=item I<$command> = C<get_command> I<$node>

Returns the name of a command, without the equals sign.
Only relevant for command nodes.


=item I<$domain> = C<get_domain> I<$node>

Only relevant for target nodes.
Returns the domain of the target.
This will be one of the strings

=over 4

=item 'HTTP'

=item 'POD'

=back


=item I<$item_type> = C<get_item_type> I<$node>

Returns the type of an item node. The type will be one of

=over 4

=item 'bullet'

=item 'number'

=item 'text'

=back


=item I<$letter> = C<get_letter> I<$node>

Returns the letter that introduces an interior sequence.
Only relevant for sequence nodes.


=item I<$list_type> = C<get_list_type> I<$node>

Returns the type of a list node.
The type of a list node is the type of the first item node in the list.


=item I<$page> = C<get_page> I<$node>

Only relevant for target nodes.
For targets in the C<POD> domain,
returns the man page that is the target of the link.
For targets in the C<HTTP> domain,
returns the URL that is the target of the link.


=item I<$raw> = C<get_raw> I<$node>

Returns the original text of a paragraph.
Currently provided for command, verbatim, and ordinary paragraphs.


=item I<$raw_kids> = C<get_raw_kids> I<$node>

Only provided for LZ<><> sequence nodes.
Returns a reference to a list of nodes representing the entire text 
of the LZ<><> sequence, including any part following a vertical bar (|).

The original text of the LZ<><> markup can be reconstructed from this list.


=item I<$section> = C<get_section> I<$node>

Only relevant for target nodes in the C<POD> domain.
Returns the section that is the target of a link.


=item I<$siblings> = C<get_siblings> I<$node>

Returns the siblings of a node.
May be called on any node.
Only item nodes have siblings.


=item I<$target> = C<get_target> I<$node>

Returns the target of a node.
Only relevant for sequence nodes that represent links 
(C<LE<lt>E<gt>> markups).
C<is_link> returns true on these nodes.


=item I<$text> = C<get_text> I<$node>

Returns the text of a node.
I<$text> will not contain any interior sequences.
Only relevant for text nodes.


=item I<$type> = C<get_type> I<$node>

Returns the type of I<$node>.
May be called on any node.
See L</TREE STRUCTURE> for descriptions of the node types.


=item I<$deep_text> = C<get_deep_text> I<$node>

Recursively walks the children of a node,
catenates together the text from each node,
and returns all that text as a single string.
All interior sequence markups are discarded.

C<get_deep_text> is provided as a convenience for applications that
want to ignore markups in a POD paragraph.

=back


=head2 Parsing

These methods manipulate the tree while it is being built.
They aren't necessary to walk the tree.

  $node->force_text($text)
  $node->force_for;
  $node->parse_begin (\@nodes);
  $node->set_children(\@children);
  $node->make_sequences;
  $node->parse_links;
  $node->unescape;
  $node->consolidate;
  $node->make_lists;


=head2 Utility

=over 4

=item I<$node>->C<clone>

Returns a deep copy of a node.
Only implemented for C<text> and C<sequence> nodes.


=item I<$node>->C<dump>

Returns a string containing a pretty-printed representation of the node.
Calling C<dump> on the root node of a tree will show the entire POD.


=item C<Pod::Tree::Node>->C<set_filename>(I<$filename>)

Sets the file name to be reported in error messages.


=item I<$filename> = $I<node>->C<getfile_name>

Returns the file name set by C<set_file_name>.


=back


=head1 EXAMPLES

The F<t/> directory in the C<Pod::Tree> distribution contains
examples of PODs, 
together with dumps of the trees that C<Pod::Tree> constructs for them.
The tree for C<t/>F<file>C<.pod> is in C<t/>F<file>C<.p_exp>.

C<Pod::Tree::Node::dump> is a simple example of code that walks a POD tree.

F<skeleton> is a skeleton application that walks a POD tree.


=head1 NOTES

=over 4

=item *

There is no provision in L<perlpod> for C<< LZ<><> >> markups to contain
URLs, but due to popular demand, this is now supported in
C<Pod::Tree::Node>.

=back


=head1 SEE ALSO

perl(1), L<C<Pod::Tree>>


=head1 AUTHOR

Steven McDougall, swmcd@world.std.com


=head1 COPYRIGHT

Copyright (c) 1999-2004 by Steven McDougall. This module is free
software; you can redistribute it and/or modify it under the same
terms as Perl itself.