| 1 | # Generate page components from a spin .sitemap file. | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # The top of a web site source tree for use with spin may contain a .sitemap | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # file. This lists all the pages of the site and their relative structure, | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # which is used to generate navigation links in both the HTML header and | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # surrounding the page. This module parses that file and generates the data | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # that it controls. | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # SPDX-License-Identifier: MIT | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # Modules and declarations | ||||||||||||||||||||||||||||||||||||||||||
| 13 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | package App::DocKnot::Spin::Sitemap v8.0.1; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use 5.024; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use autodie; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use List::SomeUtils qw(pairwise); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # File parsing | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # Read the sitemap data and populate the internal data structures. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # The format of the sitemap file is one line per web page, with indentation | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # showing the tree structure, and with each line formatted as a partial URL, a | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # colon, and a page description. If two pages at the same level aren't | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # related, a line with three dashes should be put between them at the same | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # indentation level. | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # $path - Path to the .sitemap file | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # Raises: autodie exception on file read errors | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # Text exception on file parsing errors | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 8 | ▶ | sub _read_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_data: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 8 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 8 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # @indents holds a stack of indentation levels to detect indentation | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # changes that translate into page structure. Each element in the stack | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # is an anonymous array of three elements: the indentation level, the | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # parent URL for that level of indentation, and the previous page at that | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # level of indentation. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | my @indents; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | # Parse the file. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 8 | my $fh = $path->openr_utf8(); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | while (defined(my $line = <$fh>)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 229 | ▶ | next if $line =~ m{ \A \s* \# }xms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 228 | chomp($line); | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Break a prev/next chain at --- lines. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 228 | ▶ | if ($line =~ m{ \A ([ ]*) --- \z }xms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 27 | my $indent = length($1); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 27 | ▶ | while (@indents && $indents[-1]->[0] > $indent) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@indents and $indents[-1][0] > $indent
MC/DC50%1/2
@indents and $indents[-1][0] > $indent
@indents
$indents[-1][0] > $indent
Truth table66%2/3
A and B
A= @indentsB= $indents[-1][0] > $indent
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | pop(@indents); | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 27 | ▶ | if (@indents) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 27 | $indents[-1]->[2] = undef; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 27 | next; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | # Regular line. Parse it. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 201 | my ($spaces, $url, $desc) | |||||||||||||||||||||||||||||||||||||||||
| 71 | = $line =~ m{ \A ([ ]*) /([^\s:]*): \s+ (.+) \z}xms; | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 201 | ▶ | if (!defined($desc)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | die "invalid line $. in $path\n"; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # Error on duplicate lines. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 200 | ▶ | if ($seen{$url}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | die "duplicate entry for /$url in $path (line $.)\n"; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 199 | $seen{$url} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # Open or close indentation levels. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 199 | my $indent = length($spaces); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 199 | ▶ | if (!@indents || $indent > $indents[-1]->[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @indents or $indent > $indents[-1][0]
MC/DC100%2/2
not @indents or $indent > $indents[-1][0]
not @indents
$indent > $indents[-1][0]
Truth table100%3/3
not(A) or B
A= not @indentsB= $indent > $indents[-1][0]
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 46 | ▶ | my $prev = @indents ? $indents[-1]->[2] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 46 | push(@indents, [$indent, $prev, undef]); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 153 | ▶ | while ($indents[-1]->[0] > $indent) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 27 | pop(@indents); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | # Store this page information in the object. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 199 | $self->{pagedesc}{$url} = $desc; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 199 | push($self->{sitemap}->@*, [$indent, $url, $desc]); | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | # Create the links. Gather all of the parent links to create the | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # links for this page, set this as the next URL of the previous URL if | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # any, and indicate that this page should be the previous page for the | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # next page on the same level. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 199 | my @parents = map { $_->[1] } @indents; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 199 | shift(@parents); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 199 | $self->{links}{$url} = [$indents[-1]->[2], undef, reverse(@parents)]; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 199 | ▶ | if (defined($indents[-1]->[2])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 126 | $self->{links}{ $indents[-1]->[2] }[1] = $url; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 199 | $indents[-1]->[2] = $url; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | close($fh); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # Utility methods | ||||||||||||||||||||||||||||||||||||||||||
| 115 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # Escape a page description so that it can be put in HTML output. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # $desc - The string to escape | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # $is_attr - If true, escape for putting in an HTML attribute | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # Returns: $desc escaped so that it's safe to interpolate into an attribute | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 41 | ▶ | sub _escape { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 41 | my ($desc, $is_attr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 41 | $desc =~ s{ & }{&}xmsg; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 41 | $desc =~ s{ < }{<}xmsg; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 41 | $desc =~ s{ > }{>}xmsg; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 41 | ▶ | if ($is_attr) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 22 | $desc =~ s{ \" }{"}xmsg; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 41 | return $desc; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # Given the partial URL (relative to the top of the site) to the current page | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # and the partial URL to another page, generate a URL to the second page | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # relative to the first. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # $origin - The current page | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # $dest - A partial URL of another page | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # Returns: A relative link from $origin to $dest | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 102 | ▶ | sub _relative { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_relative: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 102 | my ($origin, $dest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 102 | my @origin = split(qr{ / }xms, $origin, -1); | |||||||||||||||||||||||||||||||||||||||||
| 145 | 102 | my @dest = split(qr{ / }xms, $dest, -1); | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # Remove the common prefix. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 102 | ▶ | while (@origin && @dest && $origin[0] eq $dest[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@origin and @dest and $origin[0] eq $dest[0]
@origin and @dest
MC/DC66%2/3
@origin and @dest and $origin[0] eq $dest[0]
@origin
@dest
$origin[0] eq $dest[0]
Truth table75%3/4
A and B and C
A= @originB= @destC= $origin[0] eq $dest[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 83 | shift(@origin); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 83 | shift(@dest); | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | # If there are the same number of components in both links, the link | ||||||||||||||||||||||||||||||||||||||||||
| 154 | # should be relative to the current directory. Otherwise, ascend to the | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # common prefix and then descend to the dest link. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 102 | ▶ | if (@origin == 1 && @dest <= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@origin == 1 and @dest <= 1
MC/DC50%1/2
@origin == 1 and @dest <= 1
@origin == 1
@dest <= 1
Truth table66%2/3
A and B
A= @origin == 1B= @dest <= 1
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 19 | ▶ | return (@dest && length($dest[0])) > 0 ? $dest[0] : q{./}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@dest && length $dest[0]
MC/DC50%1/2
@dest && length $dest[0]
@dest
length $dest[0]
Truth table66%2/3
A && B
A= @destB= length $dest[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 83 | return ('../' x $#origin) . join(q{/}, @dest); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | # Return the link data for a given page. | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # $path - Path to the output, relative to the top of the web site | ||||||||||||||||||||||||||||||||||||||||||
| 166 | # | ||||||||||||||||||||||||||||||||||||||||||
| 167 | # Returns: List of links, each of which is a tuple of the relative URL and | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # the description (escaped for safe interpolation as an attribute). | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # The relative URL and description may be undef if missing. | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 47 | ▶ | sub _page_links { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_page_links: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 47 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 47 | my $key; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 47 | ▶ | if ($path->basename() eq 'index.html') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 18 | $key = $path->parent() . q{/}; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 29 | $key = "$path"; | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | # If the page is not present in the sitemap, return nothing. There are | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # also no meaningful links to generate for the top page. | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 47 | ▶ | return () if ($key eq q{/} || !$self->{links}{$key}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key eq "/" or not $self->{"links"}{$key}
MC/DC50%1/2
$key eq "/" or not $self->{"links"}{$key}
$key eq "/"
not $self->{"links"}{$key}
Truth table66%2/3
A or not(B)
A= $key eq "/"B= not $self->{"links"}{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | # Convert all the links to relative and add the page descriptions. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | return | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 115 | ▶ | map { defined ? [_relative($path, $_), $self->{pagedesc}{$_}] : undef } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 27 | $self->{links}{$key}->@*; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # Public interface | ||||||||||||||||||||||||||||||||||||||||||
| 191 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # Parse a .versions file into a new App::DocKnot::Spin::Sitemap object. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # $path - Path to the .sitemap file | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # Returns: Newly created object | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # Throws: Text exception on failure to parse the file | ||||||||||||||||||||||||||||||||||||||||||
| 199 | # autodie exception on failure to read the file | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 8 | my ($class, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | # Create an empty object. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # sitemap is an array of anonymous arrays holding the complete site map. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | # Each element represents a page. The element will contain three | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # elements: the numeric indent level, the partial output URL, and the | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # description. | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # pagedesc maps partial URLs to page descriptions used for links to that | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # page. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # links maps partial URLs to a list of other partial URLs (previous, next, | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # and then the full upwards hierarchy to the top of the site) used for | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # interpage links. | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 8 | my $self = { | |||||||||||||||||||||||||||||||||||||||||
| 217 | links => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 218 | pagedesc => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 219 | sitemap => [], | ||||||||||||||||||||||||||||||||||||||||||
| 220 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 8 | bless($self, $class); | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | # Parse the file into the newly-created object. | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 8 | $self->_read_data(path($path)); | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | # Return the populated object. | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # Return the <link> tags for a given output file, suitable for its <head> | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # section. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # $path - Path to the output file relative to the top of the output tree | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # Returns: List of lines to add to the <head> section | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 17 | ▶ | sub links { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
links: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 17 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 17 | my @links = $self->_page_links(path($path)); | |||||||||||||||||||||||||||||||||||||||||
| 239 | 17 | ▶ | return () if !@links; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | # We only care about the first parent, not the rest of the chain to the | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # top of the site. Add the names of the link types. | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 10 | my @types = qw(previous next up); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 10 | @links = @links[0 .. 2]; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 10 | ▶ | @links = pairwise { defined($b) ? [$a, $b->@*] : undef } @types, @links; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # Generate the HTML for those links. | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 10 | my @output; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 10 | for my $link (@links) { | |||||||||||||||||||||||||||||||||||||||||
| 250 | 30 | ▶ | next unless defined($link); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 22 | my ($type, $url, $desc) = $link->@*; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 22 | $desc = _escape($desc, 1); | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # Break the line if it would be longer than 79 characters. | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 22 | my $line = qq{ <link rel="$type" href="$url"}; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 22 | ▶ | if (length($line) + length($desc) + 12 > 79) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 4 | push(@output, $line . "\n"); | |||||||||||||||||||||||||||||||||||||||||
| 258 | 4 | $line = (q{ } x 8) . qq{title="$desc"}; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 18 | $line .= qq{ title="$desc"}; | |||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 22 | push(@output, $line . " />\n"); | |||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | # Add the link to the top-level page. | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 10 | my $url = _relative($path, q{}); | |||||||||||||||||||||||||||||||||||||||||
| 267 | 10 | push(@output, qq{ <link rel="top" href="$url" />\n}); | |||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | # Return the results. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 10 | return @output; | |||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | # Return the navigation bar for a given output file. | ||||||||||||||||||||||||||||||||||||||||||
| 274 | # | ||||||||||||||||||||||||||||||||||||||||||
| 275 | # $path - Path to the output file relative to the top of the output tree | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # Returns: List of lines that create the navbar | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 30 | ▶ | sub navbar { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
navbar: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 30 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 30 | my ($prev, $next, @parents) = $self->_page_links(path($path)); | |||||||||||||||||||||||||||||||||||||||||
| 281 | 30 | ▶ | return () if !@parents; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | # Construct the left and right links (previous and next). | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 17 | my $prev_link = q{ <td class="navleft">}; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 17 | ▶ | if (defined($prev)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 6 | my ($url, $desc) = $prev->@*; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 6 | $desc = _escape($desc); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 6 | $prev_link .= qq{< <a href="$url">$desc</a>}; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 17 | $prev_link .= "</td>\n"; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 17 | my $next_link = q{ <td class="navright">}; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 17 | ▶ | if (defined($next)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 13 | my ($url, $desc) = $next->@*; | |||||||||||||||||||||||||||||||||||||||||
| 294 | 13 | $desc = _escape($desc); | |||||||||||||||||||||||||||||||||||||||||
| 295 | 13 | $next_link .= qq{<a href="$url">$desc</a> >}; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 17 | $next_link .= "</td>\n"; | |||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | # Construct the bread crumbs for the page hierarchy. | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 17 | my @breadcrumbs = (" <td>\n"); | |||||||||||||||||||||||||||||||||||||||||
| 301 | 17 | my $first = 1; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 17 | for my $parent (reverse(@parents)) { | |||||||||||||||||||||||||||||||||||||||||
| 303 | 38 | my ($url, $desc) = $parent->@*; | |||||||||||||||||||||||||||||||||||||||||
| 304 | 38 | my $prefix = q{ } x 4; | |||||||||||||||||||||||||||||||||||||||||
| 305 | 38 | ▶ | if ($first) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 17 | $first = 0; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 21 | $prefix .= '> '; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 38 | push(@breadcrumbs, $prefix . qq{<a href="$url">$desc</a>\n}); | |||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 17 | push(@breadcrumbs, " </td>\n"); | |||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | # Generate the HTML for the navbar. | ||||||||||||||||||||||||||||||||||||||||||
| 315 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 17 | qq{<table class="navbar"><tr>\n}, | |||||||||||||||||||||||||||||||||||||||||
| 317 | $prev_link, | ||||||||||||||||||||||||||||||||||||||||||
| 318 | @breadcrumbs, | ||||||||||||||||||||||||||||||||||||||||||
| 319 | $next_link, | ||||||||||||||||||||||||||||||||||||||||||
| 320 | "</tr></table>\n", | ||||||||||||||||||||||||||||||||||||||||||
| 321 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | # Return the sitemap formatted as HTML. The resulting HTML will only be valid | ||||||||||||||||||||||||||||||||||||||||||
| 325 | # from a page at the top of the output tree due to the relative links. | ||||||||||||||||||||||||||||||||||||||||||
| 326 | # | ||||||||||||||||||||||||||||||||||||||||||
| 327 | # Returns: List of lines presenting the sitemap in HTML | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 2 | ▶ | sub sitemap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sitemap: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 330 | 2 | my @output; | |||||||||||||||||||||||||||||||||||||||||
| 331 | 2 | my @indents = (0); | |||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | # Build the sitemap as nested unordered lists. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 2 | for my $page ($self->{sitemap}->@*) { | |||||||||||||||||||||||||||||||||||||||||
| 335 | 142 | my ($indent, $url, $desc) = $page->@*; | |||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | # Skip the top page. | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 142 | ▶ | next if $indent == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | # Open or close <ul> elements as needed by the indentation. | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 140 | ▶ | if ($indent > $indents[-1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 23 | push(@output, (q{ } x $indent) . "<ul>\n"); | |||||||||||||||||||||||||||||||||||||||||
| 343 | 23 | push(@indents, $indent); | |||||||||||||||||||||||||||||||||||||||||
| 344 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 117 | ▶ | while ($indent < $indents[-1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 21 | push(@output, (q{ } x $indents[-1]) . "</ul>\n"); | |||||||||||||||||||||||||||||||||||||||||
| 347 | 21 | pop(@indents); | |||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | # Add the <li> for this page. | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 140 | my $spaces = q{ } x $indent; | |||||||||||||||||||||||||||||||||||||||||
| 353 | 140 | push(@output, $spaces . qq(<li><a href="$url">$desc</a></li>\n)); | |||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | # Close the remaining open <ul> tags. | ||||||||||||||||||||||||||||||||||||||||||
| 357 | 2 | for my $indent (reverse(@indents)) { | |||||||||||||||||||||||||||||||||||||||||
| 358 | 4 | ▶ | last if $indent == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 2 | push(@output, (q{ } x $indent) . "</ul>\n"); | |||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | # Return the results. | ||||||||||||||||||||||||||||||||||||||||||
| 363 | 2 | return @output; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 367 | # Module return value and documentation | ||||||||||||||||||||||||||||||||||||||||||
| 368 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 371 | __END__ | ||||||||||||||||||||||||||||||||||||||||||