1# Parse an L<> formatting code in POD text. 2# 3# This module implements parsing of the text of an L<> formatting code as 4# defined in perlpodspec. It should be suitable for any POD formatter. It 5# exports only one function, parselink(), which returns the five-item parse 6# defined in perlpodspec. 7# 8# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl 9 10############################################################################## 11# Modules and declarations 12############################################################################## 13 14package Pod::ParseLink; 15 16use 5.010; 17use strict; 18use warnings; 19 20use Exporter; 21 22our @ISA = qw(Exporter); 23our @EXPORT = qw(parselink); 24our $VERSION = '5.01_02'; 25$VERSION =~ tr/_//d; 26 27############################################################################## 28# Implementation 29############################################################################## 30 31# Parse the name and section portion of a link into a name and section. 32sub _parse_section { 33 my ($link) = @_; 34 $link =~ s/^\s+//; 35 $link =~ s/\s+$//; 36 37 # If the whole link is enclosed in quotes, interpret it all as a section 38 # even if it contains a slash. 39 return (undef, $1) if ($link =~ /^"\s*(.*?)\s*"$/); 40 41 # Split into page and section on slash, and then clean up quoting in the 42 # section. If there is no section and the name contains spaces, also 43 # guess that it's an old section link. 44 my ($page, $section) = split (/\s*\/\s*/, $link, 2); 45 $section =~ s/^"\s*(.*?)\s*"$/$1/ if $section; 46 if ($page && $page =~ / / && !defined ($section)) { 47 $section = $page; 48 $page = undef; 49 } else { 50 $page = undef unless $page; 51 $section = undef unless $section; 52 } 53 return ($page, $section); 54} 55 56# Infer link text from the page and section. 57sub _infer_text { 58 my ($page, $section) = @_; 59 my $inferred; 60 if ($page && !$section) { 61 $inferred = $page; 62 } elsif (!$page && $section) { 63 $inferred = '"' . $section . '"'; 64 } elsif ($page && $section) { 65 $inferred = '"' . $section . '" in ' . $page; 66 } 67 return $inferred; 68} 69 70# Given the contents of an L<> formatting code, parse it into the link text, 71# the possibly inferred link text, the name or URL, the section, and the type 72# of link (pod, man, or url). 73sub parselink { 74 my ($link) = @_; 75 $link =~ s/\s+/ /g; 76 my $text; 77 if ($link =~ /\|/) { 78 ($text, $link) = split (/\|/, $link, 2); 79 } 80 if ($link =~ /\A\w+:[^:\s]\S*\Z/) { 81 my $inferred; 82 if (defined ($text) && length ($text) > 0) { 83 return ($text, $text, $link, undef, 'url'); 84 } else { 85 return ($text, $link, $link, undef, 'url'); 86 } 87 } else { 88 my ($name, $section) = _parse_section ($link); 89 my $inferred; 90 if (defined ($text) && length ($text) > 0) { 91 $inferred = $text; 92 } else { 93 $inferred = _infer_text ($name, $section); 94 } 95 my $type = ($name && $name =~ /\(\S*\)/) ? 'man' : 'pod'; 96 return ($text, $inferred, $name, $section, $type); 97 } 98} 99 100############################################################################## 101# Module return value and documentation 102############################################################################## 103 104# Ensure we evaluate to true. 1051; 106__END__ 107 108=for stopwords 109markup Allbery URL 110 111=head1 NAME 112 113Pod::ParseLink - Parse an LE<lt>E<gt> formatting code in POD text 114 115=head1 SYNOPSIS 116 117 use Pod::ParseLink; 118 my $link = get_link(); 119 my ($text, $inferred, $name, $section, $type) = parselink($link); 120 121=head1 DESCRIPTION 122 123This module only provides a single function, parselink(), which takes the 124text of an LE<lt>E<gt> formatting code and parses it. It returns the 125anchor text for the link (if any was given), the anchor text possibly 126inferred from the name and section, the name or URL, the section if any, 127and the type of link. The type will be one of C<url>, C<pod>, or C<man>, 128indicating a URL, a link to a POD page, or a link to a Unix manual page. 129 130Parsing is implemented per L<perlpodspec>. For backward compatibility, 131links where there is no section and name contains spaces, or links where the 132entirety of the link (except for the anchor text if given) is enclosed in 133double-quotes are interpreted as links to a section (LE<lt>/sectionE<gt>). 134 135The inferred anchor text is implemented per L<perlpodspec>: 136 137 L<name> => L<name|name> 138 L</section> => L<"section"|/section> 139 L<name/section> => L<"section" in name|name/section> 140 141The name may contain embedded EE<lt>E<gt> and ZE<lt>E<gt> formatting codes, 142and the section, anchor text, and inferred anchor text may contain any 143formatting codes. Any double quotes around the section are removed as part 144of the parsing, as is any leading or trailing whitespace. 145 146If the text of the LE<lt>E<gt> escape is entirely enclosed in double 147quotes, it's interpreted as a link to a section for backward 148compatibility. 149 150No attempt is made to resolve formatting codes. This must be done after 151calling parselink() (since EE<lt>E<gt> formatting codes can be used to 152escape characters that would otherwise be significant to the parser and 153resolving them before parsing would result in an incorrect parse of a 154formatting code like: 155 156 L<verticalE<verbar>barE<sol>slash> 157 158which should be interpreted as a link to the C<vertical|bar/slash> POD page 159and not as a link to the C<slash> section of the C<bar> POD page with an 160anchor text of C<vertical>. Note that not only the anchor text will need to 161have formatting codes expanded, but so will the target of the link (to deal 162with EE<lt>E<gt> and ZE<lt>E<gt> formatting codes), and special handling of 163the section may be necessary depending on whether the translator wants to 164consider markup in sections to be significant when resolving links. See 165L<perlpodspec> for more information. 166 167=head1 AUTHOR 168 169Russ Allbery <rra@cpan.org> 170 171=head1 COPYRIGHT AND LICENSE 172 173Copyright 2001, 2008, 2009, 2014, 2018-2019, 2022 Russ Allbery <rra@cpan.org> 174 175This program is free software; you may redistribute it and/or modify it 176under the same terms as Perl itself. 177 178=head1 SEE ALSO 179 180L<Pod::Parser> 181 182The current version of this module is always available from its web site at 183L<https://www.eyrie.org/~eagle/software/podlators/>. 184 185=cut 186 187# Local Variables: 188# copyright-at-end-flag: t 189# End: 190