File: perli11ndoc

package info (click to toggle)
libglib-object-introspection-perl 0.051-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 584 kB
  • sloc: ansic: 3,543; perl: 2,809; makefile: 9; sh: 3
file content (1939 lines) | stat: -rwxr-xr-x 59,560 bytes parent folder | download
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
#!perl
use strict;
use warnings;
use v5.10; # for '//'
use open qw/:utf8 :std/;
use utf8;
use Config qw//;
use File::Find qw//;
use File::Spec qw//;
use XML::LibXML qw//;

{
  my $have_display;
  BEGIN {
    if (!@ARGV) {
      local $@;
      $have_display = eval {use Gtk3; Gtk3::init_check ()};
    }
  }

  my $parser = GirParser->new;

  if (!@ARGV && $have_display) {
    my @girs = find_girs ();
    my $gui = GirGUI->new ($parser, @girs);
    $gui->run;
    exit;
  }

  if (!@ARGV) {
    die 'Usage: perli11ndoc <library name>[::<element name>[::<element name>]]';
  }

  my $pattern = $ARGV[0];
  my ($lib_pattern, @element_patterns) = split /::/, $pattern;

  my $gir = find_gir ($lib_pattern);
  $parser->open ($gir);

  if (!@element_patterns) {
    print $parser->format_namespace;
  } else {
    print $parser->format_search_results (@element_patterns);
  }
}

# ------------------------------------------------------------------------------

sub find_gir {
  my ($lib_pattern) = @_;

  if ($lib_pattern !~ /^([^\d\-]+)-?(\d(?:\.\d)?)?$/) {
    die "Cannot recognize the library name\n";
  }
  my $name_wanted = $1;
  my $version_wanted = $2;
  if (defined $version_wanted && $version_wanted !~ /\./) {
    $version_wanted .= '.0';
  }

  my $match_func = sub {
    if (defined $version_wanted) {
      return $_ eq "$name_wanted-$version_wanted.gir";
    } else {
      return $_ =~ /^\Q$name_wanted\E-\d+\.\d+\.gir$/;
    }
  };
  my @girs = find_girs ($match_func);
  if (@girs == 0) {
    die "Could not find any matching GIR file\n";
  }
  if (@girs > 1) {
    my $girs_string = join (', ', map { $_->{path} } @girs);
    die "Found multiple matching GIR files: $girs_string; please be more specific\n";
  }

  return $girs[0]->{path};
}

sub find_girs {
  my ($match_func) = @_;
  $match_func //= sub { 1 };

  my @prefixes = ('/usr');
  my @env_vars = (
    {name => 'LD_LIBRARY_PATH', extra_depth => 1}, # /<prefix>/lib => /<prefix>
    {name => 'GI_TYPELIB_PATH', extra_depth => 2}, # /<prefix>/lib/girepository-1.0 => /<prefix>
  );
  foreach my $env_var (@env_vars) {
    next unless exists $ENV{$env_var->{name}};
    my @dirs = split /$Config::Config{path_sep}/, $ENV{$env_var->{name}};
    foreach my $dir (@dirs) {
      my @dir_parts = File::Spec->splitdir ($dir);
      my $prefix = File::Spec->catdir (
        @dir_parts[0 .. ($#dir_parts-$env_var->{extra_depth})]);
      if (-d $prefix) {
        push @prefixes, Cwd::abs_path ($prefix);
      }
    }
  }
  my %seen;
  my @search_dirs = grep { !$seen{$_}++ && -d $_ }
                    map { $_ . '/share/gir-1.0' }
                    @prefixes;

  my @girs;
  File::Find::find (sub {
                      if ($_ =~ m/\.gir$/ && $match_func->($_)) {
                        push @girs, {path => $File::Find::name,
                                     dir => $File::Find::dir,
                                     file => $_};
                      }
                    }, @search_dirs);

  return @girs;
}

# ------------------------------------------------------------------------------
# --- GirParser ----------------------------------------------------------------
# ------------------------------------------------------------------------------

package GirParser;

use strict;
use warnings;

sub new {
  my ($class) = @_;
  return bless {}, $class
}

sub open {
  my ($self, $gir) = @_;

  $self->{gir} = $gir;
  $self->{parser} = XML::LibXML->new;
  $self->{dom} = $self->{parser}->load_xml (location => $gir);

  $self->{xpc} = XML::LibXML::XPathContext->new;
  $self->{xpc}->registerNs ('core', 'http://www.gtk.org/introspection/core/1.0');

  $self->{repository} = $self->{dom}->documentElement;

  my $namespace_list = $self->{xpc}->find ('core:namespace', $self->{repository});
  if ($namespace_list->size != 1) {
    die 'Can only handle a single namespace';
  }
  $self->{namespace} = $namespace_list->pop;

  $self->{basename} = $self->construct_basename;
}

sub construct_basename {
  my ($self) = @_;
  my $name = $self->find_attribute ($self->{namespace}, 'name');
  my $version = $self->find_attribute ($self->{namespace}, 'version');
  $version =~ s/.0$//;
  $version = '' if $version eq '1';
  return $name . $version;
}

# ------------------------------------------------------------------------------

sub find_attribute {
  my ($self, $element, $attribute) = @_;
  my $attribute_list = $element->find ("\@$attribute");
  return if $attribute_list->size != 1;
  return $attribute_list->pop->value;
}

sub find_full_element_name {
  my ($self, $element) = @_;
  my $name = $self->find_attribute ($element, 'name');
  return () unless defined $name;

  if ($name =~ /\./) {
    die "Unexpected fully qualified name '$name' encountered; aborting\n";
  }

  my $package = '';
  my $current_element = $element;
  while (1) {
    my $parent = $current_element->parentNode;
    last unless defined $parent;
    if ($parent->nodeName eq 'namespace') {
      $package = $self->{basename} . '::' . $package;
      last;
    }
    $package = $self->find_attribute ($parent, 'name') . '::' . $package;
    $current_element = $parent;
  }

  my $full_name = $package . $name;
  $package =~ s/::$//;
  return ($package, $name, $full_name);
}

sub find_node_by_path {
  my ($self, $path) = @_;

  my $match_list = $self->{xpc}->find ($path, $self->{namespace});
  if ($match_list->size < 1) {
    die "Cannot find a matching element for the path $path\n";
  }
  if ($match_list->size > 1) {
    die "Found more than one matching element for the path $path\n";
  }

  return $match_list->pop;
}

sub find_parameters_and_return_value {
  my ($self, $element) = @_;

  my (@in, @out);
  my $parameter_list = $self->{xpc}->find ('core:parameters/core:parameter', $element);
  foreach my $parameter ($parameter_list->get_nodelist) {
    my $direction = $self->find_attribute ($parameter, 'direction') // 'in';
    if ($direction eq 'inout' || $direction eq 'out') {
      push @out, $parameter;
    }
    if ($direction eq 'inout' || $direction eq 'in') {
      push @in, $parameter;
    }
  }

  my $retval = undef;
  my $retval_list = $self->{xpc}->find ('core:return-value', $element);
  if ($retval_list->size == 1) {
    $retval = $retval_list->[0];
    if (defined $retval) {
      if ($self->find_type_name ($retval) eq 'none') {
        $retval = undef;
      }
    }
  }

  return (\@in, $retval, \@out);
}

sub find_type_name {
  my ($self, $element) = @_;

  # arrays
  my $array_list = $self->{xpc}->find ('core:array', $element);
  if ($array_list->size == 1) {
    my $array = $array_list->pop;
    my $prefix = 'reference to array of ';
    my $child_type_name = $self->find_type_name ($array);
    return $prefix . $child_type_name;
  }

  # callbacks
  my $callback_list = $self->{xpc}->find ('core:callback', $element);
  if ($callback_list->size == 1) {
    my $callback = $callback_list->pop;
    my ($in, $retval, $out) = $self->find_parameters_and_return_value ($callback);
    unshift @$out, $retval if defined $retval;
    my $in_list = join ', ', map { $self->find_type_name ($_) } @$in;
    my $out_list = join ', ', map { $self->find_type_name ($_) } @$out;
    my $in_text = $in_list ne '' ? "in: $in_list" : '';
    my $out_text = $out_list ne '' ? "; out: $out_list" : '';
    return "callback ($in_text$out_text)";
  }

  # bare types
  my $type_list = $self->{xpc}->find ('core:type', $element);
  return '[unknown type]' unless $type_list->size == 1;
  my $type = $type_list->pop;
  return $self->find_attribute ($type, 'name');
}

# ------------------------------------------------------------------------------

sub enumerate_namespace {
  my ($self, $descend) = @_;
  $descend //= 0;

  my @class_and_interface_sub_categories = (
    [Constructors => 'core:constructor'],
    [Methods => 'core:method'],
    [Functions => 'core:function'],
    [Signals => 'glib:signal'],
    [Properties => 'core:property'],
    [Fields => 'core:field'],
    ['Virtual methods' => 'core:virtual-method'],
  );

  my @record_sub_categories = (
    [Constructors => 'core:constructor'],
    [Methods => 'core:method'],
    [Functions => 'core:function'],
    [Fields => 'core:field'],
  );

  my @enum_and_bitfield_sub_categories = (
    [Functions => 'core:function'],
  );

  my @categories = (
    [Classes => 'core:class', \@class_and_interface_sub_categories, sub { shift =~ /Accessible$/ }],
    [Interfaces => 'core:interface', \@class_and_interface_sub_categories],
    [Records => 'core:record', \@record_sub_categories, sub { shift =~ /(?:Class|Iface|Interface|Private)$/ }],
    [Enumerations => 'core:enumeration', \@enum_and_bitfield_sub_categories],
    [Bitfields => 'core:bitfield', \@enum_and_bitfield_sub_categories],
    [Functions => 'core:function'],
    [Callbacks => 'core:callback'],
    [Constants => 'core:constant'],
    [Aliases => 'core:alias', undef, sub { shift =~ /_autoptr$/ }],
    ['Classes for accessibility' => 'core:class',
     \@class_and_interface_sub_categories,
     sub { shift !~ /Accessible$/ }],
    ['Records for object classes' => 'core:record',
     \@record_sub_categories,
     sub { shift !~ /Class$/ }],
    ['Records for interfaces' => 'core:record',
     \@record_sub_categories,
     sub { shift !~ /(?:Iface|Interface)$/ }],
  );

  my @results;
  foreach my $category (@categories) {
    my $heading = $category->[0];
    my $path = $category->[1];
    my $sub_categories = $category->[2] // undef;
    my $skip = $category->[3] // sub { 0 }; # accept all by default

    my $list = $self->{xpc}->find ($path, $self->{namespace});
    next if $list->size == 0;
    my @entries;
    foreach my $node ($list->get_nodelist) {
      my $node_path = $node->nodePath;
      my $name = $self->find_attribute ($node, 'name');
      next if $skip->($name);

      my @sub_results;
      if ($descend && defined $sub_categories) {
        foreach my $sub_category (@$sub_categories) {
          my $sub_heading = $sub_category->[0];
          my $sub_path = $sub_category->[1];
          my $sub_list = $self->{xpc}->find ($sub_path, $node);

          next if $sub_list->size == 0;
          my @sub_entries;
          foreach my $sub_node ($sub_list->get_nodelist) {
            my $sub_path = $sub_node->nodePath;
            my $sub_name = $self->find_attribute ($sub_node, 'name');
            push @sub_entries, {path => $sub_path,
                                name => $sub_name};
          }

          push @sub_results, [$sub_heading => \@sub_entries];
        }
      }

      push @entries, {path => $node_path,
                      name => $name,
                      sub_results => \@sub_results};
    }

    next unless @entries;
    push @results, [$heading => \@entries];
  }

  return \@results;
}

sub format_namespace {
  my ($self) = @_;

  my $text = '';

  my $name = $self->find_attribute ($self->{namespace}, 'name');
  my $version = $self->find_attribute ($self->{namespace}, 'version');
  $text .= "NAMESPACE\n\n  $name $version => " . $self->{basename} . "\n\n";

  my $results = $self->enumerate_namespace;
  foreach my $results (@$results) {
    my $heading = uc $results->[0];
    my $entries = $results->[1];
    next unless @$entries;
    $text .= "$heading\n\n";
    foreach my $entry (@$entries) {
      $text .= sprintf "  [%s](%s)\n", $entry->{name}, $entry->{path};
    }
    $text .= "\n";
  }

  $text =~ s/\n\n\Z/\n/;

  return $text;
}

# ------------------------------------------------------------------------------

sub format_search_results {
  my ($self, @search_terms) = @_;
  die 'Can only handle up to two search terms' if @search_terms > 2;

  my $query = @search_terms == 1 ?
    "*[\@name='$search_terms[0]']" :
    "*[\@name='$search_terms[0]']/*[\@name='$search_terms[1]']";

  my $match_list = $self->{xpc}->find ($query, $self->{namespace});
  if ($match_list->size == 0) {
    die "Cannot find a matching element for the search terms @search_terms\n";
  }
  my @matches = $match_list->get_nodelist;
  if (@matches > 1) {
    my $matches_string =
      join (', ', map { $self->format_full_element_name ($_) }
            @matches);
    die "Found two many matches: $matches_string; please be more specific\n";
  }

  my $match = $matches[0];
  return $self->format_node ($match);
}

sub format_node_by_path {
  my ($self, $path) = @_;
  my $node = $self->find_node_by_path ($path);
  return $self->format_node ($node);
}

sub format_node_name_by_path {
  my ($self, $path) = @_;
  my $node = $self->find_node_by_path ($path);
  return $self->format_full_element_name ($node);
}

sub format_node {
  my ($self, $node) = @_;

  my %categories = (
    alias => 'format_alias',
    bitfield => 'format_bitfield',
    callback => 'format_callback',
    class => 'format_class',
    constant => 'format_constant',
    constructor => 'format_constructor',
    enumeration => 'format_enumeration',
    field => 'format_field',
    function => 'format_function',
    method => 'format_method',
    property => 'format_property',
    interface => 'format_interface',
    record => 'format_record',
    'glib:signal' => 'format_signal',
    'virtual-method' => 'format_virtual_method',
  );

  my $type = $node->nodeName;
  my $handler = $categories{$type};
  if (!defined $handler) {
    die "Unknown node type '$type' encountered; aborting\n";
  }
  return $self->$handler ($node);
}

# ------------------------------------------------------------------------------

sub format_alias {
  my ($self, $element) = @_;
  my $text = '';
  my $full_name = $self->format_full_element_name ($element);
  my $type_name = $self->find_type_name ($element);
  my $full_type_name = $self->format_full_type_name ($type_name);
  $text .= "ALIAS\n\n  $full_name = $full_type_name\n";
  $text .= $self->format_description ($element);
  return $text;
}

# ------------------------------------------------------------------------------

sub format_bitfield {
  my ($self, $element) = @_;
  return $self->format_bitfield_and_enumeration ($element, 'BITFIELD');
}

sub format_enumeration {
  my ($self, $element) = @_;
  return $self->format_bitfield_and_enumeration ($element, 'ENUMERATION');
}

sub format_bitfield_and_enumeration {
  my ($self, $element, $heading) = @_;
  my $text = '';
  my $full_name = $self->format_full_element_name ($element);
  $text .= "$heading\n\n  $full_name\n";
  $text .= $self->format_description ($element);
  $text .= $self->format_sub_members ($element);
  $text .= $self->format_sub_functions ($element, 'FUNCTIONS');
  return $text;
}

# ------------------------------------------------------------------------------

sub format_callable {
  my ($self, $element, $heading, $synopsis_format, $flags_formatter) = @_;
  $flags_formatter //= 'format_callable_flags';

  my $text = '';

  my ($package, $name, $full_name) = $self->find_full_element_name ($element);
  my $flags = $self->$flags_formatter ($element);
  $text .= "$heading\n\n  $full_name$flags\n";

  my ($in, $retval, $out) = $self->find_parameters_and_return_value ($element);

  # --- synopsis ---
  my @in_names = map { '$' . $self->find_attribute ($_, 'name') } @$in;
  my @out_names = map { '$' . $self->find_attribute ($_, 'name') } @$out;
  if (defined $retval) {
    unshift @out_names, '$retval';
  }

  my $in_list = join ', ', @in_names;
  my $in_list_pre_comma = @in_names > 0 ? ", $in_list" : '';
  my $in_list_post_comma = @in_names > 0 ? "$in_list, " : '';
  my $out_list = join ', ', @out_names;
  my $out_list_parens = @out_names > 1 ? "($out_list)" : $out_list;
  my $out_list_assign = @out_names > 0 ? "$out_list_parens = " : '';

  my $synopsis = $synopsis_format;
  $synopsis =~ s/\[\[PACKAGE\]\]/$package/g;
  $synopsis =~ s/\[\[NAME\]\]/$name/g;
  $synopsis =~ s/\[\[NAME_UC\]\]/uc $name/ge;
  $synopsis =~ s/\[\[FULL_NAME\]\]/$full_name/g;
  $synopsis =~ s/\[\[IN_LIST\]\]/$in_list/g;
  $synopsis =~ s/\[\[IN_LIST_PRE_COMMA\]\]/$in_list_pre_comma/g;
  $synopsis =~ s/\[\[IN_LIST_POST_COMMA\]\]/$in_list_post_comma/g;
  $synopsis =~ s/\[\[OUT_LIST\]\]/$out_list/g;
  $synopsis =~ s/\[\[OUT_LIST_PARENS\]\]/$out_list_parens/g;
  $synopsis =~ s/\[\[OUT_LIST_ASSIGN\]\]/$out_list_assign/g;

  $text .= "\nSYNOPSIS\n\n  $synopsis\n";

  # --- description ---
  $text .= $self->format_description ($element);

  # --- in ---
  if (@$in) {
    $text .= "\nPARAMETERS\n\n";
    foreach my $parameter (@$in) {
      my $name = $self->find_attribute ($parameter, 'name');
      my $type_name = $self->find_type_name ($parameter);
      my $full_type_name = $self->format_full_type_name ($type_name);
      $text .= "  • $name: $full_type_name\n";
      my $doc = $self->format_docs ($parameter, '    ');
      if (defined $doc) {
        $text .= "$doc\n";
      }
      $text .= "\n";
    }
    $text =~ s/\n\n\Z/\n/;
  }

  # --- retval & out ---
  my $retval_type_name = 'none';
  if (defined $retval) {
    $retval_type_name = $self->find_type_name ($retval);
  }
  if ($retval_type_name ne 'none' || @$out) {
    $text .= "\nRETURN VALUES\n\n";
    if ($retval_type_name ne 'none') {
      my $full_retval_type_name =
        $self->format_full_type_name ($retval_type_name);
      $text .= "  • $full_retval_type_name\n";
      my $doc = $self->format_docs ($retval, '    ');
      if (defined $doc) {
        $text .= "$doc\n\n";
      }
    }
    if (@$out) {
      foreach my $parameter (@$out) {
        my $name = $self->find_attribute ($parameter, 'name');
        my $type_name = $self->find_type_name ($parameter);
        my $full_type_name = $self->format_full_type_name ($type_name);
        $text .= "  • $name: $full_type_name\n";
        my $doc = $self->format_docs ($parameter, '    ');
        if (defined $doc) {
          $text .= "$doc\n\n";
        }
      }
    }
    $text =~ s/\n\n\Z/\n/;
  }

  return $text;
}

sub format_callback {
  my ($self, $element) = @_;
  my $synopsis_format = <<'__EOS__';
sub {
    my ([[IN_LIST]]) = @_;
    ...
    return [[OUT_LIST_PARENS]];
  }
__EOS__
  return $self->format_callable ($element, 'CALLBACK', $synopsis_format);
}

sub format_constructor {
  my ($self, $element) = @_;
  my $synopsis_format = '$object = [[PACKAGE]]->[[NAME]] ([[IN_LIST]])';
  return $self->format_callable ($element, 'CONSTRUCTOR', $synopsis_format);
}

sub format_function {
  my ($self, $element) = @_;
  my $synopsis_format = '[[OUT_LIST_ASSIGN]][[FULL_NAME]] ([[IN_LIST]])';
  return $self->format_callable ($element, 'FUNCTION', $synopsis_format);
}

sub format_method {
  my ($self, $element) = @_;
  my $synopsis_format = '[[OUT_LIST_ASSIGN]]$object->[[NAME]] ([[IN_LIST]])';

  # Treat methods of class structs as functions.
  {
    my $parent = $element->parentNode;
    if ($parent->nodeName eq 'record' &&
        defined $self->find_attribute ($parent, 'glib:is-gtype-struct-for'))
    {
      $synopsis_format = '[[OUT_LIST_ASSIGN]][[FULL_NAME]] ($package[[IN_LIST_PRE_COMMA]])';
    }
  }

  return $self->format_callable ($element, 'METHOD', $synopsis_format);
}

sub format_signal {
  my ($self, $element) = @_;
  my $synopsis_format = <<'__EOS__';
$object->signal_connect ('[[NAME]]' => sub {
    my ($object, [[IN_LIST_POST_COMMA]]$data) = @_;
    ...
    return [[OUT_LIST_PARENS]];
  }, $data);
__EOS__
  return $self->format_callable ($element,
                                 'SIGNAL',
                                 $synopsis_format,
                                 'format_signal_flags');
}

sub format_virtual_method {
  my ($self, $element) = @_;
  my $synopsis_format = <<'__EOS__';
sub [[NAME_UC]] {
    my ($object[[IN_LIST_PRE_COMMA]]) = @_;
    ...
    return [[OUT_LIST_PARENS]];
  }
__EOS__
  return $self->format_callable ($element,
                                 'VIRTUAL METHOD',
                                 $synopsis_format,
                                 'format_virtual_method_flags');
}

# ------------------------------------------------------------------------------

sub format_class {
  my ($self, $element) = @_;

  my $format_hierarchy_and_interfaces = sub {
    my @parents;
    my $current_element = $element;
    while (1) {
      my $parent_name = $self->find_attribute ($current_element, 'parent');
      last unless defined $parent_name;
      unshift @parents, $self->format_full_type_name ($parent_name);

      # Stop if the parent is fully qualified, i.e., if it points elsewhere.
      last if $parent_name =~ /\./;

      my $parent_list =
        $self->{xpc}->find ("core:class[\@name='$parent_name']",
                            $self->{namespace});
      if ($parent_list->size != 1) {
        die "Found no or too many classes with name '$parent_name'\n";
      }
      $current_element = $parent_list->pop;
    }

    my @children;
    my $name = $self->find_attribute ($element, 'name');
    my $children_list =
      $self->{xpc}->find ("core:class[\@parent='$name']",
                          $self->{namespace});
    foreach my $child ($children_list->get_nodelist) {
      push @children, $self->format_full_element_name ($child);
    }

    my $hierarchy_text = '';
    if (@parents || @children) {
      push @parents, $self->format_full_element_name ($element);
      $hierarchy_text = "\nHIERARCHY\n\n";
      my $hook = '╰── '; # thanks, devhelp
      my $spacer = ' ' x length $hook;
      for (my $i = 0; $i < @parents; $i++) {
        $hierarchy_text .=
          '  ' .
          ($i > 0 ? (($spacer x ($i-1)) . $hook) : '') .
          $parents[$i] . "\n";
      }
      foreach my $child (@children) {
        $hierarchy_text .=
          '  ' .
          $spacer x $#parents . $hook .
          $child . "\n";
      }
    }

    my $impl_list = $self->{xpc}->find ('core:implements', $element);
    my $impl_text =
      $self->format_full_type_names ($impl_list, 'IMPLEMENTED INTERFACES');

    return $hierarchy_text . $impl_text;
  };

  return $self->format_class_and_interface ($element, 'CLASS',
                                            $format_hierarchy_and_interfaces);
}

sub format_interface {
  my ($self, $element) = @_;

  my $format_prerequisites_and_implementations = sub {
    my $prereq_list = $self->{xpc}->find ('core:prerequisite', $element);
    my $prereq_text =
      $self->format_full_type_names ($prereq_list, 'PREREQUISITES');

    my $name = $self->find_attribute ($element, 'name');
    my $impl_list =
      $self->{xpc}->find ("core:class[./core:implements[\@name='$name']]",
                          $self->{namespace});
    my $impl_text =
      $self->format_full_type_names ($impl_list, 'KNOWN IMPLEMENTATIONS');

    return $prereq_text . $impl_text;
  };

  return $self->format_class_and_interface ($element, 'INTERFACE',
                                            $format_prerequisites_and_implementations);
}

sub format_class_and_interface {
  my ($self, $element, $heading, $intro) = @_;
  my $text = '';
  my $full_name = $self->format_full_element_name ($element);
  $text .= "$heading\n\n  $full_name\n";
  $text .= $intro->();
  $text .= $self->format_description ($element);
  $text .= $self->format_sub_constructors ($element);
  $text .= $self->format_sub_methods ($element);
  $text .= $self->format_sub_functions ($element, 'CLASS FUNCTIONS');
  $text .= $self->format_sub_signals ($element);
  $text .= $self->format_sub_properties ($element);
  $text .= $self->format_sub_fields ($element);
  $text .= $self->format_sub_virtual_methods ($element);
  return $text;
}

# ------------------------------------------------------------------------------

sub format_constant {
  my ($self, $element) = @_;
  my $text = '';
  my $full_name = $self->format_full_element_name ($element);
  my $value = $self->find_attribute ($element, 'value');
  my $type_name = $self->find_type_name ($element);
  my $full_type_name = $self->format_full_type_name ($type_name);
  $text .= "CONSTANT\n\n  $full_name = $value ($full_type_name)\n";
  $text .= $self->format_description ($element);
  return $text;
}

# ------------------------------------------------------------------------------

sub format_field {
  my ($self, $element) = @_;
  my $text = '';
  my $full_name = $self->format_full_element_name ($element);
  my $type_name = $self->find_type_name ($element);
  my $full_type_name = $self->format_full_type_name ($type_name);
  my $flags = $self->format_field_flags ($element);
  $text .= "FIELD\n\n  $full_name: $full_type_name$flags\n";
  $text .= $self->format_description ($element);
  return $text;
}

# ------------------------------------------------------------------------------

sub format_property {
  my ($self, $element) = @_;
  my $text = '';
  my $full_name = $self->format_full_element_name ($element);
  my $type_name = $self->find_type_name ($element);
  my $full_type_name = $self->format_full_type_name ($type_name);
  my $flags = $self->format_property_flags ($element);
  $text .= "PROPERTY\n\n  $full_name: $full_type_name$flags\n";
  $text .= $self->format_description ($element);
  return $text;
}

# ------------------------------------------------------------------------------

sub format_record {
  my ($self, $element) = @_;
  my $text = '';
  my $full_name = $self->format_full_element_name ($element);
  $text .= "RECORD\n\n  $full_name\n";
  $text .= $self->format_description ($element);
  $text .= $self->format_sub_fields ($element);
  $text .= $self->format_sub_constructors ($element);
  $text .= $self->format_sub_methods ($element);
  $text .= $self->format_sub_functions ($element, 'FUNCTIONS');
  return $text;
}

# ------------------------------------------------------------------------------

sub format_sub_constructors {
  my ($self, $element) = @_;
  my $text = '';
  my $ctor_list = $self->{xpc}->find ('core:constructor', $element);
  if ($ctor_list->size > 0) {
    $text .= "\nCONSTRUCTORS\n\n";
    foreach my $ctor ($ctor_list->get_nodelist) {
      my $name = $self->find_attribute ($ctor, 'name');
      my $path = $ctor->nodePath;
      my $flags = $self->format_callable_flags ($ctor,
                                                qw/introspectable version/);
      $text .= "  • [$name]($path)$flags\n";
    }
  }
  return $text;
}

sub format_sub_fields {
  my ($self, $element) = @_;
  my $text = '';
  my $field_list = $self->{xpc}->find ('core:field', $element);
  if ($field_list->size > 0) {
    $text .= "\nFIELDS\n\n";
    foreach my $field ($field_list->get_nodelist) {
      my $name = $self->find_attribute ($field, 'name');
      my $path = $field->nodePath;
      my $type_name = $self->find_type_name ($field);
      my $full_type_name = $self->format_full_type_name ($type_name);
      my $flags = $self->format_field_flags ($field, qw/introspectable/);
      $text .= "  • [$name]($path): $full_type_name$flags\n";
    }
  }
  return $text;
}

sub format_sub_functions {
  my ($self, $element, $heading) = @_;
  my $text = '';
  my $function_list = $self->{xpc}->find ('core:function', $element);
  if ($function_list->size > 0) {
    $text .= "\n$heading\n\n";
    foreach my $function ($function_list->get_nodelist) {
      my $name = $self->find_attribute ($function, 'name');
      my $path = $function->nodePath;
      my $flags = $self->format_callable_flags ($function,
                                                qw/introspectable version/);
      $text .= "  • [$name]($path)$flags\n";
    }
  }
  return $text;
}

sub format_sub_members {
  my ($self, $element) = @_;
  my $text = '';
  my $member_list = $self->{xpc}->find ('core:member', $element);
  if ($member_list->size > 0) {
    $text .= "\nMEMBERS\n";
    foreach my $member ($member_list->get_nodelist) {
      my $name = $self->find_attribute ($member, 'name');
      my $value = $self->find_attribute ($member, 'value');
      $text .= "\n  • $name = $value\n";
      my $doc = $self->format_docs ($member, '    ');
      if (defined $doc) {
        $text .= "$doc\n";
      }
    }
  }
  return $text;
}

sub format_sub_methods {
  my ($self, $element) = @_;
  my $text = '';
  my $method_list = $self->{xpc}->find ('core:method', $element);
  if ($method_list->size > 0) {
    $text .= "\nMETHODS\n\n";
    foreach my $method ($method_list->get_nodelist) {
      my $name = $self->find_attribute ($method, 'name');
      my $path = $method->nodePath;
      my $flags = $self->format_callable_flags ($method,
                                                qw/introspectable version/);
      $text .= "  • [$name]($path)$flags\n";
    }
  }
  return $text;
}

sub format_sub_properties {
  my ($self, $element) = @_;
  my $text = '';
  my $property_list = $self->{xpc}->find ('core:property', $element);
  if ($property_list->size > 0) {
    $text .= "\nPROPERTIES\n\n";
    foreach my $property ($property_list->get_nodelist) {
      my $name = $self->find_attribute ($property, 'name');
      my $path = $property->nodePath;
      my $type_name = $self->find_type_name ($property);
      my $full_type_name = $self->format_full_type_name ($type_name);
      my $flags = $self->format_property_flags ($property, qw/version/);
      $text .= "  • [$name]($path): $full_type_name$flags\n";
    }
  }
  return $text;
}

sub format_sub_signals {
  my ($self, $element) = @_;
  my $text = '';
  my $signal_list = $self->{xpc}->find ('glib:signal', $element);
  if ($signal_list->size > 0) {
    $text .= "\nSIGNALS\n\n";
    foreach my $signal ($signal_list->get_nodelist) {
      my $name = $self->find_attribute ($signal, 'name');
      my $path = $signal->nodePath;
      my $flags = $self->format_signal_flags ($signal, qw/version/);
      $text .= "  • [$name]($path)$flags\n";
    }
  }
  return $text;
}

sub format_sub_virtual_methods {
  my ($self, $element) = @_;
  my $text = '';
  my $vfunc_list = $self->{xpc}->find ('core:virtual-method', $element);
  if ($vfunc_list->size > 0) {
    $text .= "\nVIRTUAL METHODS\n\n";
    foreach my $vfunc ($vfunc_list->get_nodelist) {
      my $name = $self->find_attribute ($vfunc, 'name');
      my $path = $vfunc->nodePath;
      my $flags = $self->format_virtual_method_flags ($vfunc);
      $text .= "  • [$name]($path)$flags\n";
    }
  }
  return $text;
}

# ------------------------------------------------------------------------------

sub format_deprecation_docs {
  my ($self, $element) = @_;
  my $deprecated = $self->find_attribute ($element, 'deprecated') // 0;
  return unless $deprecated;

  my $text = '';

  my $version = $self->find_attribute ($element, 'deprecated-version');
  if (defined $version) {
    $text .= "Deprecated since: $version.";
  }

  my $doc_dep_list = $self->{xpc}->find ('core:doc-deprecated', $element);
  if ($doc_dep_list->size == 1) {
    $text .= '  ' . $doc_dep_list->pop->textContent;
  }

  return if $text eq '';
  return $text;
}

sub format_description {
  my ($self, $element) = @_;
  my $docs = $self->format_docs ($element);
  return defined $docs ? "\nDESCRIPTION\n\n$docs\n" : '';
}

sub format_docs {
  my ($self, $element, $indent) = @_;
  $indent //= '  ';

  my $text = '';

  # The normal docs.
  my $docs_list = $self->{xpc}->find ('core:doc', $element);
  if ($docs_list->size == 1) {
    $text .= $docs_list->pop->textContent;
  }

  # The version constraint.
  my $ver = $self->format_version_constraint ($element);
  $text .= "\n\n$ver\n" if defined $ver;

  # The deprecation docs.
  my $dep = $self->format_deprecation_docs ($element);
  $text .= "\n\n$dep\n" if defined $dep;

  return if $text eq '';

  # Extract code blocks so that they are not wrapped.
  my $code_block_pattern = qr/\|\[\n?(.*?)\n?\]\|/s;
  my $empty_code_block = '|[]|';
  my $empty_code_block_pattern = qr/\|\[\]\|/;
  my @code_blocks = $text =~ m/$code_block_pattern/g;
  $text =~ s/$code_block_pattern/$empty_code_block/g;

  # Remove leading white space as fill() otherwise takes it for starting a new
  # paragraph.  Do this after the code block extraction to preserve their
  # indentation.
  $text =~ s/^[ \t]+//mg;

  require Text::Wrap;
  my $formatted_text = Text::Wrap::fill ($indent, $indent, $text);

  while ($formatted_text =~ m/$empty_code_block_pattern/g) {
    my $code_block = shift @code_blocks;
    $code_block =~ s/^/$indent/mg;
    my $divider = '-' x (76-length($indent));
    my $formatted_code_block =
      "\n$indent$divider\n$code_block\n$indent$divider";
    $formatted_text =~
      s/(?:\n)?(?:$indent)?$empty_code_block_pattern/$formatted_code_block/;
  }

  return $formatted_text;
}

sub format_full_element_name {
  my ($self, $element) = @_;
  my (undef, undef, $full_name) = $self->find_full_element_name ($element);
  return $full_name;
}

sub format_full_type_name {
  my ($self, $name) = @_;
  if ($name =~ /\./) { # fully qualified
    $name =~ s/\./::/g;
    return $name;
  }
  if ($name =~ /^[A-Z]/) { # local
    return $self->{basename} . '::' . $name;
  }
  return $name; # global
}

sub format_full_type_names {
  my ($self, $list, $heading) = @_;
  my $text = '';
  if ($list->size > 0) {
    $text .= "\n$heading\n\n";
    foreach my $node ($list->get_nodelist) {
      my $type_name = $self->find_attribute ($node, 'name');
      my $full_type_name = $self->format_full_type_name ($type_name);
      $text .= "  • $full_type_name\n";
    }
  }
  return $text;
}

sub format_version_constraint {
  my ($self, $element) = @_;
  my $version = $self->find_attribute ($element, 'version');
  return if !defined $version;
  return "Since: $version.";
}

# ------------------------------------------------------------------------------

sub format_flags {
  my ($self, $element, $available, $wanted) = @_;
  $wanted //= [];

  my @texts;
  foreach my $flag (@$available) {
    my $name = $flag->[0];
    my $default = $flag->[1];
    my $formatter = $flag->[2];

    if (@$wanted) {
      next unless grep { $_ eq $name } @$wanted;
    }

    my $value = $self->find_attribute ($element, $name) // $default;
    my $text = $formatter->($value);
    push @texts, $text if defined $text;
  }

  return '' unless @texts;
  return ' [' . join (', ', @texts) . ']';
}

sub format_callable_flags {
  my ($self, $element, @wanted) = @_;

  # name, default, formatter
  my @available = (
    ['introspectable', 1,     sub { !$_[0] ? 'NOT INTROSPECTABLE' : undef }],
    ['deprecated',     0,     sub { $_[0] ? "deprecated" : undef }],
    ['moved-to',       undef, sub { defined $_[0] ? "moved to $_[0]" : undef }],
    ['shadowed-by',    undef, sub { defined $_[0] ? "shadowed by $_[0]" : undef }], # FIXME: Format $_[0] properly.
    ['throws',         0,     sub { $_[0] ? "throws" : undef }],
    ['version',        undef, sub { defined $_[0] ? "available since $_[0]" : undef }],
    ['shadows',        undef, sub { defined $_[0] ? "shadows $_[0]" : undef }],     # FIXME: Format $_[0] properly.
  );

  return $self->format_flags ($element, \@available, \@wanted);
}

sub format_field_flags {
  my ($self, $element, @wanted) = @_;

  # name, default, formatter
  my @available = (
    ['introspectable', 1, sub { !$_[0] ? 'NOT INTROSPECTABLE' : undef }],
    ['readable',       1, sub { $_[0] ? 'readable' : undef }],
    ['writable',       1, sub { $_[0] ? 'writable' : undef }],
  );

  return $self->format_flags ($element, \@available, \@wanted);
}

sub format_property_flags {
  my ($self, $element, @wanted) = @_;
  my @available = (
    ['deprecated', 0,     sub { $_[0] ? "deprecated" : undef }],
    ['version',    undef, sub { defined $_[0] ? "available since $_[0]" : undef }],
    ['readable',   1,     sub { $_[0] ? 'readable' : undef }],
    ['writable',   0,     sub { $_[0] ? 'writable' : undef }],
  );
  return $self->format_flags ($element, \@available, \@wanted);
}

sub format_signal_flags {
  my ($self, $element, @wanted) = @_;

  # name, default, formatter
  my @available = (
    ['deprecated', 0,     sub { $_[0] ? "deprecated" : undef }],
    ['version',    undef, sub { defined $_[0] ? "available since $_[0]" : undef }],
    ['when',       undef, sub { defined $_[0] ? "$_[0]" : undef }],
    ['no-recurse', 0,     sub { $_[0] ? "no recurse" : undef }],
    ['detailed',   0,     sub { $_[0] ? "detailed" : undef }],
  );

  return $self->format_flags ($element, \@available, \@wanted);
}

sub format_virtual_method_flags {
  my ($self, $element, @wanted) = @_;
  my $name = $self->find_attribute ($element, 'name');
  my @available = (
    ['introspectable', 1,     sub { !$_[0] ? 'NOT INTROSPECTABLE' : undef }],
    ['invoker',        undef, sub { defined $_[0] && $_[0] ne $name ? "invoked by $_[0]" : undef }],
    ['version',        undef, sub { defined $_[0] ? "available since $_[0]" : undef }],
  );
  return $self->format_flags ($element, \@available, \@wanted);
}

# ------------------------------------------------------------------------------
# --- GirGUI ----------------------------------------------------------------
# ------------------------------------------------------------------------------

package GirGUI;

use strict;
use warnings;
use File::Basename qw//;

sub TRUE () {1}
sub FALSE () {0}

sub FILE_MENU_COL_TEXT () { 0 }
sub FILE_MENU_COL_FILE () { 1 }
sub FILE_MENU_COL_DIR () { 2 }
sub FILE_MENU_COL_PATH () { 3 }
sub FILE_MENU_COL_IS_SENSITIVE () { 4 }

sub GIR_VIEW_COL_TEXT () { 0 }
sub GIR_VIEW_COL_PATH () { 1 }
sub GIR_VIEW_COL_IS_CATEGORY () { 2 }
sub GIR_VIEW_COL_IS_VISIBLE () { 3 }

sub new {
  my ($class, $parser, @girs) = @_;

  if (!Gtk3::CHECK_VERSION (3, 10, 0)) {
    die "Need gtk+ >= 3.10 for the GUI\n";
  }

  my $self = bless {
    parser => $parser,
  }, $class;

  my $window = Gtk3::Window->new;

  $self->setup_file_menu (@girs);
  $self->setup_gir_view;
  $self->setup_search_entry;
  $self->setup_path_bar;
  $self->setup_result_view;

  my $gir_view_window = Gtk3::ScrolledWindow->new;
  $gir_view_window->add ($self->{gir_view});

  my $result_view_window = Gtk3::ScrolledWindow->new;
  $result_view_window->add ($self->{result_view});

  my $side_box = Gtk3::Box->new ('vertical', 2);
  $side_box->pack_start ($self->{file_menu}, FALSE, FALSE, 0);
  $side_box->pack_start ($gir_view_window, TRUE, TRUE, 0);
  $side_box->pack_start ($self->{search_entry}, FALSE, FALSE, 0);
  $side_box->set (margin => 2);

  my $result_box = Gtk3::Box->new ('vertical', 0);
  $result_box->pack_start ($self->{path_bar}, FALSE, FALSE, 0);
  $result_box->pack_start ($result_view_window, TRUE, TRUE, 0);

  my $paned = Gtk3::Paned->new ('horizontal');
  $paned->pack1 ($side_box, TRUE, TRUE);
  $paned->pack2 ($result_box, TRUE, TRUE);
  $paned->set_position (300);

  $window->add ($paned);
  $window->signal_connect (delete_event => sub { $self->quit; });
  $window->set_default_geometry (900, 800);

  my $accel_group = Gtk3::AccelGroup->new;
  $accel_group->connect (Gtk3::Gdk::KEY_q (), qw/control-mask/, [], sub {
    $self->quit;
    return Gtk3::EVENT_STOP ();
  });
  $accel_group->connect (Gtk3::Gdk::KEY_k (), qw/control-mask/, [], sub {
    $self->{search_entry}->grab_focus;
    return Gtk3::EVENT_STOP ();
  });
  $window->add_accel_group ($accel_group);

  $self->{window} = $window;

  return $self;
}

sub filter_gir_view {
  my ($self, $criterion) = @_;
  my $view = $self->{gir_view};
  my $model = $self->{gir_model};
  my $filter_model = $self->{gir_filter_model};

  if (!defined $criterion || $criterion eq '') {
    # Make everything visible.
    $model->foreach (sub {
      my (undef, undef, $iter) = @_;
      $model->set ($iter, GIR_VIEW_COL_IS_VISIBLE, TRUE);
      return FALSE; # continue
    });

    # Scroll to selected element.
    my $selection = $view->get_selection;
    my ($selected_model, $selected_iter) = $selection->get_selected;
    if (defined $selected_iter) {
      my $selected_path = $selected_model->get_path ($selected_iter);
      $view->scroll_to_cell ($selected_path, undef, FALSE, 0.5, 0.5);
    }
  } else {
    my $re;
    if ($criterion =~ m|\A/.+/\z|) {
      $criterion =~ s|\A/(.+)/\z|$1|;
      $re = qr/$criterion/;
    } else {
      $re = qr/\Q$criterion\E/i;
    }

    my $check_tree;
    $check_tree = sub {
      my ($iter) = @_;
      my @children = map { $model->iter_nth_child ($iter, $_) }
                         0..$model->iter_n_children ($iter);
      foreach my $child (@children) {
        my ($text, $is_cat) = $model->get ($child,
                                           GIR_VIEW_COL_TEXT,
                                           GIR_VIEW_COL_IS_CATEGORY);
        if ($is_cat || $text !~ $re) { # no match
          $model->set ($child, GIR_VIEW_COL_IS_VISIBLE, FALSE);
          $check_tree->($child); # descend
        } else { # match
          # Make the element and all its parents visible.
          my $cur = $child;
          do {
            $model->set ($cur, GIR_VIEW_COL_IS_VISIBLE, TRUE);
          } while (defined ($cur = $model->iter_parent ($cur)));

          # Expand the matching element and all its parents.
          $view->expand_to_path (
            $filter_model->convert_child_path_to_path (
              $model->get_path ($child)));

          # No need to descend as we want all children of matching elements to
          # be visible.  (All elements are visible by default.)
        }
      }
    };
    $check_tree->(undef); # start with the virtual root node
  }
}

sub display_results {
  my ($self, $results) = @_;

  my $b = $self->{result_buffer};
  $b->delete ($b->get_start_iter (), $b->get_end_iter ());

  my $iter = $b->get_start_iter ();
  my $insert_part = sub {
    my ($start, $end) = @_;
    $b->insert ($iter, substr ($results, $start, $end - $start));
  };

  my ($prev_match_start, $prev_match_end) = (0, 0);
  while ($results =~ m/\[([^\n\]]+)\]\(([^\n\)]+)\)/g) {
    my ($link_text, $link_target) = ($1, $2);
    my ($match_start, $match_end) = ($-[0], $+[0]);

    if ($match_start != $prev_match_end) {
      $insert_part->($prev_match_end, $match_start);
    }

    my $tag = $b->create_tag (undef, foreground => 'blue');
    $tag->{__target} = $link_target;
    $b->insert_with_tags ($iter, $link_text, $tag);

    ($prev_match_start, $prev_match_end) = ($match_start, $match_end);
  }
  my $end_offset = length ($results);
  if ($prev_match_end != $end_offset) {
    $insert_part->($prev_match_end, $end_offset);
  }
}

sub run {
  my ($self) = @_;
  $self->{window}->show_all;
  Gtk3::main ();
}

sub setup_file_menu {
  my ($self, @girs) = @_;

  my $file_model = Gtk3::TreeStore->new (qw/Glib::String
                                            Glib::String
                                            Glib::String
                                            Glib::String
                                            Glib::Boolean/);
  my $file_menu = Gtk3::ComboBox->new_with_model ($file_model);
  my $renderer = Gtk3::CellRendererText->new;
  $file_menu->pack_start ($renderer, TRUE);
  $file_menu->set_attributes ($renderer,
                              text => FILE_MENU_COL_TEXT,
                              sensitive => FILE_MENU_COL_IS_SENSITIVE);
  $file_menu->set_id_column (FILE_MENU_COL_PATH);

  my $prompt = '<Select GIR>';
  $file_model->set ($file_model->append,
                    FILE_MENU_COL_TEXT, $prompt,
                    FILE_MENU_COL_IS_SENSITIVE, FALSE);
  $file_menu->set_active (0);

  my %dirs;
  $dirs{$_->{dir}}++ for @girs;
  my $n_dirs = scalar keys %dirs;
  foreach my $gir (sort { $a->{file} cmp $b->{file} } @girs) {
    my $text = File::Basename::fileparse ($gir->{file}, qr/\.gir$/);
    if ($n_dirs > 1) {
      my $dir = $gir->{dir};
      $dir =~ s|/share/gir-1\.0$||;
      $text .= ' (' . $dir . ')';
    }
    $file_model->set ($file_model->append,
                      FILE_MENU_COL_TEXT, $text,
                      FILE_MENU_COL_FILE, $gir->{file},
                      FILE_MENU_COL_DIR, $gir->{dir},
                      FILE_MENU_COL_PATH, $gir->{path},
                      FILE_MENU_COL_IS_SENSITIVE, TRUE);
  }

  $file_menu->signal_connect (changed => sub {
    my (undef, $iter) = $file_menu->get_active_iter;
    $self->{parser}->open ($file_model->get ($iter, FILE_MENU_COL_PATH));
    $self->update_gir_view;
  });

  $self->{file_menu} = $file_menu;
}

sub setup_gir_view {
  my ($self) = @_;

  my $gir_model = Gtk3::TreeStore->new (qw/Glib::String
                                           Glib::String
                                           Glib::Boolean
                                           Glib::Boolean/);
  my $gir_filter_model = Gtk3::TreeModelFilter->new ($gir_model);
  $gir_filter_model->set_visible_column (GIR_VIEW_COL_IS_VISIBLE);

  my $gir_view = Gtk3::TreeView->new_with_model ($gir_filter_model);
  $gir_view->insert_column_with_attributes (
    GIR_VIEW_COL_TEXT, 'Element',
    Gtk3::CellRendererText->new,
    text => GIR_VIEW_COL_TEXT);
  $gir_view->set_headers_visible (FALSE);

  $gir_view->signal_connect (key_press_event => sub {
    my (undef, $event) = @_;
    if ($event->keyval == Gtk3::Gdk::KEY_Left () ||
        $event->keyval == Gtk3::Gdk::KEY_Right ()) {
      my $selection = $gir_view->get_selection;
      my ($model, $iter) = $selection->get_selected;
      if (defined $iter) {
        my $path = $model->get_path ($iter);
        if ($event->keyval == Gtk3::Gdk::KEY_Left ()) {
          $gir_view->collapse_row ($path);
        } else {
          $gir_view->expand_row ($path, FALSE);
        }
      }
      return Gtk3::EVENT_STOP ();
    }
    return Gtk3::EVENT_PROPAGATE ();
  });

  $gir_view->get_selection->signal_connect (changed => sub {
    $self->go_to_selection
      unless $self->{suppress_gir_view_selection_changes};
  });

  $self->{gir_model} = $gir_model;
  $self->{gir_filter_model} = $gir_filter_model;
  $self->{gir_view} = $gir_view;
}

sub setup_path_bar {
  my ($self) = @_;
  my $path_bar = PathBar->new (orientation => 'horizontal', spacing => 2);
  $path_bar->set_update_func (sub {
    my ($name, $path) = @_;
    $self->update_results ($path);
  });
  $self->{path_bar} = $path_bar;
}

sub setup_search_entry {
  my ($self) = @_;

  my $wait_time_ms = 500;
  my $search_entry = Gtk3::SearchEntry->new;
  $search_entry->signal_connect (search_changed => sub {
    # Use a timeout which is reset when the search text changes so that we do
    # not filter the view too often.
    if (defined $search_entry->{__timer_id}) {
      Glib::Source->remove ($search_entry->{__timer_id});
    }
    $search_entry->{__timer_id} = Glib::Timeout->add ($wait_time_ms, sub {
      $self->filter_gir_view ($search_entry->get_text);
      $search_entry->{__timer_id} = undef;
      return Glib::SOURCE_REMOVE ();
    });
  });

  $self->{search_entry} = $search_entry;
}

sub setup_result_view {
  my ($self) = @_;

  my $result_buffer = Gtk3::TextBuffer->new (undef);

  my $result_view = Gtk3::TextView->new_with_buffer ($result_buffer);
  $result_view->set (editable => FALSE, margin => 2);

  my $display = $result_view->get_display ();
  $result_view->{__hand_cursor} = Gtk3::Gdk::Cursor->new_from_name ($display, 'pointer');
  $result_view->{__regular_cursor} = Gtk3::Gdk::Cursor->new_from_name ($display, 'text');

  my $hovering_over_link = sub {
    my ($event) = @_;
    my ($x, $y) = $result_view->window_to_buffer_coords ('widget', $event->x, $event->y);
    my $iter = $result_view->get_iter_at_location ($x, $y);
    if (!$iter) {
      return;
    }
    my $tags = $iter->get_tags ();
    foreach my $tag (@$tags) {
      if (defined $tag->{__target}) {
        return $tag;
      }
    }
    return;
  };

  $result_view->{__hovering} = FALSE;
  $result_view->signal_connect (motion_notify_event => sub {
    my ($result_view, $event) = @_;
    my $hovering = defined $hovering_over_link->($event);
    if ($result_view->{__hovering} != $hovering) {
      $result_view->{__hovering} = $hovering;
      $result_view->get_window ('text')->set_cursor (
        $hovering ? $result_view->{__hand_cursor} : $result_view->{__regular_cursor});
    }
    return Gtk3::EVENT_PROPAGATE ();
  });

  my $handle_button = sub {
    my ($event, $cb) = @_;
    if ($event->button == Gtk3::Gdk::BUTTON_PRIMARY ()) {
      my $tag = $hovering_over_link->($event);
      if (defined $tag) {
        if (defined $cb) {
          $cb->($tag);
        }
        return Gtk3::EVENT_STOP ();
      }
    }
    return Gtk3::EVENT_PROPAGATE ();
  };
  $result_view->signal_connect (button_press_event => sub {
    my ($result_view, $event) = @_;
    return $handle_button->($event);
  });
  $result_view->signal_connect (button_release_event => sub {
    my ($result_view, $event) = @_;
    return $handle_button->($event, sub {
      $self->go_to_path ($_[0]->{__target});
    });
  });

  $self->{result_buffer} = $result_buffer;
  $self->{result_view} = $result_view;
}

sub update_gir_view {
  my ($self) = @_;

  $self->{suppress_gir_view_selection_changes} = TRUE;

  $self->{gir_model}->clear;
  $self->{search_entry}->set_text ('');
  $self->{path_bar}->clear;

  my $inserter = sub {
    my ($iter, $text, $path, $is_cat, $is_vis) = @_;
    $self->{gir_model}->set ($iter,
                             GIR_VIEW_COL_TEXT, $text,
                             GIR_VIEW_COL_PATH, $path,
                             GIR_VIEW_COL_IS_CATEGORY, $is_cat,
                             GIR_VIEW_COL_IS_VISIBLE, $is_vis);
  };

  my $results = $self->{parser}->enumerate_namespace (TRUE);
  foreach my $result (@$results) {
    my $heading = $result->[0];
    my $entries = $result->[1];

    my $heading_iter = $self->{gir_model}->append;
    $inserter->($heading_iter, $heading, undef, TRUE, TRUE);

    next unless defined $entries;
    foreach my $entry (@$entries) {
      my $iter = $self->{gir_model}->append ($heading_iter);
      $inserter->($iter, $entry->{name}, $entry->{path}, FALSE, TRUE);

      next unless defined $entry->{sub_results};
      foreach my $sub_result (@{$entry->{sub_results}}) {
        my $sub_heading = $sub_result->[0];
        my $sub_entries = $sub_result->[1];

        my $sub_heading_iter = $self->{gir_model}->append ($iter);
        $inserter->($sub_heading_iter, $sub_heading, undef, TRUE, TRUE);

        next unless defined $sub_entries;
        foreach my $sub_entry (@$sub_entries) {
          my $sub_iter = $self->{gir_model}->append ($sub_heading_iter);
          $inserter->($sub_iter, $sub_entry->{name}, $sub_entry->{path}, FALSE, TRUE);
        }
      }
    }
  }

  $self->{suppress_gir_view_selection_changes} = FALSE;

  $self->display_results ($self->{parser}->format_namespace);
}

sub go_to_selection {
  my ($self) = @_;
  my $selection = $self->{gir_view}->get_selection;
  my ($model, $iter) = $selection->get_selected;
  if (!defined $iter) {
    $self->display_results ($self->{parser}->format_namespace);
  } elsif (!$model->get ($iter, GIR_VIEW_COL_IS_CATEGORY)) {
    my $path = $model->get ($iter, GIR_VIEW_COL_PATH);
    $self->go_to_path ($path);
  }
}

sub go_to_path {
  my ($self, $path) = @_;
  my $name = $self->{parser}->format_node_name_by_path ($path);
  $self->{path_bar}->append ($name, $path); # indirectly calls update_results
}

sub update_results {
  my ($self, $path) = @_;
  $self->display_results ($self->{parser}->format_node_by_path ($path));

  # Show and select the correponding tree entry.
  $self->{gir_model}->foreach (sub {
    my ($model, $tree_path, $iter) = @_;
    my $this_path = $model->get ($iter, GIR_VIEW_COL_PATH);
    if (defined $this_path && $this_path eq $path) {
      $self->{gir_view}->expand_to_path ($tree_path);
      $self->{gir_view}->scroll_to_cell ($tree_path, undef, FALSE, 0.0, 0.0);
      $self->{suppress_gir_view_selection_changes} = TRUE;
      {
        $self->{gir_view}->get_selection ()->select_path ($tree_path);
      }
      $self->{suppress_gir_view_selection_changes} = FALSE;
      return TRUE; # stop
    }
    return FALSE; # continue
  });
}

sub quit {
  my ($self) = @_;
  Gtk3::main_quit ();
}

package PathBar;

# The BEGIN { eval } dance is to support not loading Gtk3 in text mode.
BEGIN { eval {use Glib::Object::Subclass qw/Gtk3::Box/;} }

sub TRUE () {1}
sub FALSE () {0}

sub INIT_INSTANCE {
  my ($self) = @_;

  my $back_button = Gtk3::Button->new;
  $back_button->set_image (
    Gtk3::Image->new_from_icon_name ('go-previous-symbolic', 'button'));
  $back_button->set_sensitive (FALSE);
  $back_button->signal_connect (clicked => sub { $self->{path_label}->go_back });

  my $forward_button = Gtk3::Button->new;
  $forward_button->set_image (
    Gtk3::Image->new_from_icon_name ('go-next-symbolic', 'button'));
  $forward_button->set_sensitive (FALSE);
  $forward_button->signal_connect (clicked => sub { $self->{path_label}->go_forward });

  my $nav_box = Gtk3::Box->new ('horizontal', 2);
  $nav_box->pack_start ($back_button, FALSE, FALSE, 0);
  $nav_box->pack_start ($forward_button, FALSE, FALSE, 0);
  $nav_box->get_style_context->add_class ('linked');

  my $path_label = PathLabel->new;
  $path_label->set_update_func (sub {
    my ($name, $path) = @_;
    $self->update_buttons;
    if (defined $self->{update_func}) {
      $self->{update_func}->($name, $path);
    }
  });

  $self->pack_start ($nav_box, FALSE, FALSE, 0);
  $self->pack_start (Gtk3::VSeparator->new, FALSE, FALSE, 0);
  $self->pack_start ($path_label, TRUE, TRUE, 0);
  $self->set (margin => 2);

  $self->{back_button} = $back_button;
  $self->{forward_button} = $forward_button;
  $self->{path_label} = $path_label;

  return $self;
}

sub clear {
  my ($self) = @_;
  $self->{path_label}->clear ();
  $self->update_buttons ();
}

sub append {
  my ($self, $name, $path) = @_;
  $self->{path_label}->append ($name, $path);
}

sub set_update_func {
  my ($self, $func) = @_;
  $self->{update_func} = $func;
}

sub update_buttons {
  my ($self) = @_;
  $self->{back_button}->set_sensitive ($self->{path_label}->can_go_back);
  $self->{forward_button}->set_sensitive ($self->{path_label}->can_go_forward);
}

package PathLabel;

# The BEGIN { eval } dance is to support not loading Gtk3 in text mode.
BEGIN { eval {use Glib::Object::Subclass qw/Gtk3::Label/;} }

sub TRUE () {1}
sub FALSE () {0}

sub INIT_INSTANCE {
  my ($self) = @_;

  $self->signal_connect (activate_link => sub {
    my (undef, $index) = @_;
    $self->{current_child} = $index;
    $self->update;
    return Gtk3::EVENT_STOP ();
  });
  $self->set_track_visited_links (FALSE);

  $self->clear ();
}

sub clear {
  my ($self) = @_;
  $self->{children} = [];
  $self->{current_child} = undef;
  $self->{natural_width} = 0;
  $self->update ();
}

sub append {
  my ($self, $name, $path) = @_;
  my $cur = $self->{current_child};
  # If the new entry is equal to the current entry, do nothing.
  if (defined $cur) {
    my $child = $self->{children}->[$cur];
    if ($child->{name} eq $name && $child->{path} eq $path) {
      return;
    }
  }
  # If the current entry is not the last entry, remove all entries after the
  # current one before appending the new entry.
  if (defined $cur && $cur < $#{$self->{children}}) {
    splice @{$self->{children}}, $cur+1;
  }
  push @{$self->{children}}, {name => $name, path => $path};
  $self->{current_child} = $#{$self->{children}};
  $self->update;
}

sub can_go_back {
  my ($self) = @_;
  return defined $self->{current_child} && $self->{current_child} > 0;
}

sub can_go_forward {
  my ($self) = @_;
  return defined $self->{current_child} && $self->{current_child} < $#{$self->{children}};
}

sub go_back {
  my ($self) = @_;
  return unless $self->{current_child} > 0;
  $self->{current_child}--;
  $self->update;
}

sub go_forward {
  my ($self) = @_;
  return unless $self->{current_child} < $#{$self->{children}};
  $self->{current_child}++;
  $self->update;
}

sub set_update_func {
  my ($self, $func) = @_;
  $self->{update_func} = $func;
}

sub update {
  my ($self) = @_;
  $self->set_markup ($self->_format_children);
  if (defined $self->{current_child} && defined $self->{update_func}) {
    my $child = $self->{children}->[$self->{current_child}];
    $self->{update_func}->($child->{name}, $child->{path});
  }
}

sub GET_PREFERRED_WIDTH {
  #say 'GET_PREFERRED_WIDTH';
  my ($self) = @_;
  (undef, $self->{natural_width}) = $self->SUPER::GET_PREFERRED_WIDTH;
  return (0, 0);
}

sub SIZE_ALLOCATE {
  #say 'SIZE_ALLOCATE';
  my ($self, $allocation) = @_;
  if ($self->{natural_width} > $allocation->{width}) {
    my @selected = ($self->{current_child});
    while (1) {
      my @candidates = @selected;
      if ($selected[0] > 0) {
        unshift @candidates, $selected[0]-1;
      }
      if ($selected[-1] < $#{$self->{children}}) {
        push @candidates, $selected[-1]+1;
      }
      $self->set_markup ($self->_format_children (@candidates));
      my ($ink_rect, $logical_rect) = $self->get_layout->get_extents;
      my $text_width = $logical_rect->{width}/Pango::SCALE ();
      if ($text_width > $allocation->{width}) {
        last;
      } else {
        @selected = @candidates;
      }
    }
    $self->set_markup ($self->_format_children (@selected));
  }
  $self->SUPER::SIZE_ALLOCATE ($allocation);
}

 # Use undef as an indicator for left-out children.
sub _add_omission_markers {
  my ($self, @indices) = @_;
  if (!@indices) {
    return @indices;
  }
  if ($indices[0] > 0) {
    unshift @indices, undef;
  }
  if ($indices[-1] < $#{$self->{children}}) {
    push @indices, undef;
  }
  return @indices;
}

sub _format_child {
  my ($self, $index) = @_;
  return '…' unless defined $index;
  my $name = $self->{children}->[$index]->{name};
  my $markup = $index == $self->{current_child}
    ? "<b>$name</b>"
    : "<a href='$index'>$name</a>";
  return $markup;
}

sub _format_children {
  my ($self, @indices) = @_;
  if (!@indices) {
    @indices = 0..$#{$self->{children}};
  }
  @indices = $self->_add_omission_markers (@indices);
  return join ' ▸ ', map { $self->_format_child ($_) } @indices;
}

__END__

=encoding utf8
 
=head1 NAME
 
perli11ndoc - view documentation of installed libraries bound using
Glib::Object::Introspection
 
=head1 ABSTRACT
 
perli11ndoc allows you to view Perl documentation for Glib libraries bound to
Perl using Glib::Object::Introspection. Glib::Object::Introspection uses the
gobject-introspection and libffi projects to dynamically create Perl bindings
for a wide variety of libraries. Examples include gtk+, webkit, libsoup and
many more. All libraries currently installed will be shown with Perl-specific
documentation and examples. Where Glib::Object::Introspection differs from the
C library, differences are pointed out and the Perl-specific interface is
defined.
 
=head1 DEPENDENCIES

perli11ndoc requires that XML::LibXML be installed to run. It also requires
that the corresponding C library headers be installed on the system to provide
documentation. For instance, on a Debian-based system you would need
libgtk-3-dev to see the Perl introspected documentation for Gtk3.

=head1 SEE ALSO
 
=over
 
=item perl-Glib: L<Glib>
 
=item gobject-introspection: L<http://live.gnome.org/GObjectIntrospection>
 
=item libffi: L<http://sourceware.org/libffi/>

=item Glib::Object::Introspection: L<https://metacpan.org/pod/Glib::Object::Introspection>
 
=back
 
=head1 AUTHORS
 
=over
 
=item Emmanuele Bassi <ebassi at linux intel com>
 
=item muppet <scott asofyet org>
 
=item Torsten Schönfeld <kaffeetisch at gmx de>
 
=back
 
=head1 LICENSE
 
This utility is free software; you can redistribute it and/or modify it under
the terms of the Lesser General Public License (LGPL).  For more information,
see http://www.fsf.org/licenses/lgpl.txt
 
=cut