blib/lib/Game/HexDescribe/Utils.pm

filter: Statementstmt 49.5% 338 / 682 Branchbran 33.9% 116 / 342 Conditioncond 32.9% 54 / 164 MC/DCmcdc 18.7% 18 / 96 Subroutinesub 68.0% 32 / 47 Podpod 97.3% 37 / 38 totaltotal 43.4% 595 / 1369 CC 266 SCAR 69.3
CC
266
Cov
43%
describe
2461.1
parse_table
226.0
same_direction
156.0
CRAP 1026.0
↑ Summary blib/lib/Game/HexDescribe.pm »
1 #!/usr/bin/env perl
2 # Copyright (C) 2018–2022 Alex Schroeder <alex@gnu.org>
3 #
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
7 # later version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU Affero General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Game::HexDescribe::Utils - utilities to use the Hex Describe data
22  
23 =head1 DESCRIPTION
24  
25 L<Hex::Describe> is a web application which uses recursive random tables to
26 create the description of a map. This package contains the functions used to
27 access the information outside the web application framework.
28  
29 =cut
30  
31 package Game::HexDescribe::Utils;
32 6 require Exporter;
33 6 our @ISA = qw(Exporter);
34 6 our @EXPORT_OK = qw(init markdown describe_text list_tables parse_table load_table
35 describe_map parse_map load_map);
36 6 ▶use Text::Autoformat;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 6 ▶use Game::HexDescribe::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use List::Util qw(shuffle sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use Array::Utils qw(intersect);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 6 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 6 my $log = Game::HexDescribe::Log->get;
47  
48 6 our $face_generator_url;
49 6 our $text_mapper_url;
50  
51 =item list_tables($dir)
52  
53 This function returns the table names in $dir. These are based on the following
54 filename convention: "$dir/hex-describe-$name-table.txt".
55  
56 =cut
57  
58 sub list_tables {
59 0 ▶ my $dir = shift;
Subroutine list_tables: not called CC=2 SCAR=17.9
Pod documented
60 0 $log->debug("Looking for maps in the contrib directory: $dir");
61 0 my @names = map { $_->basename('.txt') } Mojo::File->new($dir)->list->each;
62 0 ▶ return grep { $_ } map { $1 if /^hex-describe-(.*)-table$/ } @names;
Branch: 1 branch
BranchTrueFalseTotal
if /^hex-describe-(.*)-table$/u 0 0 0
63 }
64  
65 =item load_table($name, $dir)
66  
67 This function returns the unparsed table from the filename
68 "$dir/hex-describe-$name-table.txt".
69  
70 =cut
71  
72 sub load_table {
73 7 ▶ my ($name, $dir) = @_;
Subroutine load_table: called CC=2 SCAR=7.4
Pod documented
74 7 $log->debug("Looking for table '$name' in the contrib directory '$dir'");
75 7 my $file = Mojo::File->new("$dir/hex-describe-$name-table.txt");
76 7 ▶ return decode_utf8($file->slurp) if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 7 0 7
77 0 return '';
78 }
79  
80 =item load_map($name, $dir)
81  
82 This function returns the unparsed map from the filename
83 "$dir/hex-describe-$name-map.txt".
84  
85 =cut
86  
87 sub load_map {
88 1 ▶ my ($name, $dir) = @_;
Subroutine load_map: called CC=2 SCAR=7.0
Pod documented
89 1 $log->debug("Looking for map in the contrib directory: $dir");
90 1 my $file = Mojo::File->new("$dir/hex-describe-$name-map.txt");
91 1 ▶ return decode_utf8($file->slurp) if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 1 0 1
92 }
93  
94 =item parse_table
95  
96 This parses the random tables. This is also where *bold* gets translated to
97 HTML. We also do some very basic checking of references. If we refer to another
98 table in square brackets we check whether we've seen such a table.
99  
100 Table data is a reference to a hash of hashes. The key to the first hash is the
101 name of the table; the key to the second hash is "total" for the number of
102 options and "lines" for a reference to a list of hashes with two keys, "count"
103 (the weight of this lines) and "text" (the text of this line).
104  
105 A table like the following:
106  
107 ;tab
108 1,a
109 2,b
110  
111 Would be:
112  
113 $table_data->{tab}->{total} == 3
114 $table_data->{tab}->{lines}->[0]->{count} == 1
115 $table_data->{tab}->{lines}->[0]->{text} eq "a"
116 $table_data->{tab}->{lines}->[1]->{count} == 2
117 $table_data->{tab}->{lines}->[1]->{text} eq "b"
118  
119 =cut
120  
121 6 my $dice_re = qr/^(save )?(?:(\d+)d(\d+)(?:x(\d+))?(?:([+-]\d+))?(?:>=(-?\d+))?(?:<=(-?\d+))?|(\d+))(?: as (.+))?$/;
122 6 my $math_re = qr/^(save )?([-+*\/%<>=()0-9]+)(?: as (.+))?$/;
123  
124 sub parse_table {
125 7 ▶ my $text = shift;
Subroutine parse_table: called CC=31 SCAR=54.2
Pod documented
126 7 $log->debug("parse_table: parsing " . length($text) . " characters");
127 7 my $data = {};
128 7 my $words = "[^\[\]\n]*";
129 7 my (%aliases, $key, $c, $t);
130 7 for my $line (split(/\r?\n/, $text)) {
131 285047 ▶ if ($line =~ /^;([^#\r\n]+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^;([^#\r\n]+)/u) { } 14553 270494 285047
elsif ($key and () = $line =~ /^(\d+),(.*)/u) { } 253316 17178 270494
elsif ($line ne '' and not $line =~ /^\s*#/u) { } 0 32053 32053
132 14553 $key = $1;
133 14553 ▶ $log->warn("parse_table: reset '$key'") if exists $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$key} 0 14553 14553
134 14553 $data->{$key} = {}; # reset, don't merge
135 ▶ } elsif ($key and ($c, $t) = $line =~ /^(\d+),(.*)/) {
Condition100%3/3
$key and () = $line =~ /^(\d+),(.*)/u
!ll&&!rl&&r
2117157253316
MC/DC100%2/2
$key and () = $line =~ /^(\d+),(.*)/u
$key () = $line =~ /^(\d+),(.*)/u
Truth table100%3/3
A and B
A= $keyB= () = $line =~ /^(\d+),(.*)/u
ABresult
0X0
100
111
136 253316 $t =~ s/\*\*(.*?)\*\*/<strong>$1<\/strong>/g;
137 253316 $t =~ s/\*(.*?)\*/<em>$1<\/em>/g;
138 253316 my %h = (text => $t);
139 253316 ▶ if ($c == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 0) 42 253274 253316
140 42 $h{unique} = 1;
141 42 $c = 1;
142 }
143 253316 $h{count} = $c;
144 253316 $data->{$key}->{total} += $c;
145 253316 push(@{$data->{$key}->{lines}}, \%h);
146 # [foo as bar]
147 253316 for my $alias ($h{text} =~ /\[$words as ($words)\]/g) {
148 0 $aliases{$alias} = 1;
149 }
150 # [foo [baz] quux as bar] (one level of indirection allowed
151 253316 for my $alias ($h{text} =~ /\[$words\[$words\]$words as ($words)\]/g) {
152 0 $aliases{$alias} = 1;
153 }
154 ▶ } elsif ($line ne '' and $line !~ /^\s*#/) {
Condition66%2/3
$line ne "" and not $line =~ /^\s*#/u
!ll&&!rl&&r
1487523030
MC/DC0%0/2
$line ne "" and not $line =~ /^\s*#/u
$line ne "" not $line =~ /^\s*#/u
Truth table66%2/3
A and not(B)
A= $line ne ""B= not $line =~ /^\s*#/u
ABresult
0X0
100
111
155 0 $log->warn("unknown line type: '$line'");
156 }
157 }
158 # check tables
159 7 for my $table (keys %$data) {
160 14553 for my $line (@{$data->{$table}->{lines}}) {
161 253316 for my $subtable ($line->{text} =~ /\[($words)\]/g) {
162 0 ▶ next if index($subtable, '|') != -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($subtable, "|") != -1 0 0 0
163 0 ▶ next if $subtable =~ /$dice_re/;
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /$dice_re/u 0 0 0
164 0 ▶ next if $subtable =~ /$math_re/;
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /$math_re/u 0 0 0
165 0 ▶ next if $subtable =~ /^redirect https?:/;
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^redirect https?:/u 0 0 0
166 0 ▶ next if $subtable =~ /^names for (.*)/ and $data->{"name for $1"};
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^names for (.*)/u and $$data{"name for $1"} 0 0 0
Condition0%0/3
$subtable =~ /^names for (.*)/u and $$data{"name for $1"}
!ll&&!rl&&r
000
MC/DC0%0/2
$subtable =~ /^names for (.*)/u and $$data{"name for $1"}
$subtable =~ /^names for (.*)/u $$data{"name for $1"}
Truth table0%0/3
A and B
A= $subtable =~ /^names for (.*)/uB= $$data{"name for $1"}
ABresult
0X0
100
111
167 0 ▶ next if $subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/ and $data->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/u and $data->{$1} 0 0 0
Condition0%0/3
$subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/u and $data->{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/u and $data->{$1}
$subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/u $data->{$1}
Truth table0%0/3
A and B
A= $subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/uB= $data->{$1}
ABresult
0X0
100
111
168 0 ▶ next if $subtable =~ /^adjacent hex$/; # experimental
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^adjacent hex$/u 0 0 0
169 0 ▶ next if $subtable =~ /^same (.*)/ and ($data->{$1} or $aliases{$1} or $1 eq 'adjacent hex');
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^same (.*)/u and $data->{$1} || $aliases{$1} || $1 eq "adjacent hex" 0 0 0
Condition0%0/9
$subtable =~ /^same (.*)/u and $data->{$1} || $aliases{$1} || $1 eq "adjacent hex"
!ll&&!rl&&r
000
$data->{$1} or $aliases{$1} or $1 eq "adjacent hex"
l!l&&r!l&&!r
000
$data->{$1} or $aliases{$1}
l!l&&r!l&&!r
000
MC/DC0%0/4
$subtable =~ /^same (.*)/u and $data->{$1} || $aliases{$1} || $1 eq "adjacent hex"
$subtable =~ /^same (.*)/u $data->{$1} $aliases{$1} $1 eq "adjacent hex"
Truth table0%0/5
A and B or C or D
A= $subtable =~ /^same (.*)/uB= $data->{$1}C= $aliases{$1}D= $1 eq "adjacent hex"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
170 0 ▶ next if $subtable =~ /^(?:here|nearby|closest|other|append|later|with|and|save|store) (.+?)( as (.+))?$/ and $data->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^(?:here|nearby|closest|other|append|later|with|and|save|store) (.+?)( as (.+))?$/u and $data->{$1} 0 0 0
Condition0%0/3
$subtable =~ /^(?:here|nearby|closest|other|append|later|with|and|save|store) (.+?)( as (.+))?$/u and $data->{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$subtable =~ /^(?:here|nearby|closest|other|append|later|with|and|save|store) (.+?)( as (.+))?$/u and $data->{$1}
$subtable =~ /^(?:here|nearby|closest|other|append|later|with|and|save|store) (.+?)( as (.+))?$/u $data->{$1}
Truth table0%0/3
A and B
A= $subtable =~ /^(?:here|nearby|closest|other|append|later|with|and|save|store) (.+?)( as (.+))?$/uB= $data->{$1}
ABresult
0X0
100
111
171 0 ▶ $subtable = $1 if $subtable =~ /^(.+) as (.+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^(.+) as (.+)/u 0 0 0
172 $log->error("Error in table $table: subtable $subtable is missing")
173 0 ▶ unless $data->{$subtable};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$subtable} 0 0 0
174 }
175 }
176 }
177 7 return $data;
178 }
179  
180 =item init
181  
182 When starting a description, we need to initialize our data. There are two
183 global data structures beyond the map.
184  
185 B<$extra> is a reference to a hash of lists of hashes used to keep common data
186 per line. In this context, lines are linear structures like rivers or trails on
187 the map. The first hash uses the hex coordinates as a key. This gets you the
188 list of hashes, one per line going through this hex. Each of these hashes uses
189 the key "type" to indicate the type of line, "line" for the raw data (for
190 debugging), and later "name" will be used to name these lines.
191  
192 $extra->{"0101"}->[0]->{"type"} eq "river"
193  
194 B<%names> is just a hash of names. It is used for all sorts of things. When
195 using the reference C<name for a bugbear band1>, then "name for a bugbear band1"
196 will be a key in this hash. When using the reference C<name for forest foo>,
197 then "name for forest foo: 0101" and will be set for every hex sharing that
198 name.
199  
200 $names{"name for a bugbear band1"} eq "Long Fangs"
201 $names{"name for forest foo: 0101"} eq "Dark Wood"
202  
203 Note that for C</describe/text>, C<init> is called for every paragraph.
204  
205 B<%locals> is a hash of all the "normal" table lookups encountered so far. It is
206 reset for every paragraph. To refer to a previous result, start a reference
207 with the word "same". This doesn't work for references to adjacent hexes, dice
208 rolls, or names. Here's an example:
209  
210 ;village boss
211 1,[man] is the village boss. They call him Big [same man].
212 1,[woman] is the village boss. They call her Big [same woman].
213  
214 Thus:
215  
216 $locals{man} eq "Alex"
217  
218 B<%globals> is a hash of hashes of all the table lookups beginning with the word
219 "here" per hex. In a second phase, all the references starting with the word
220 "nearby" will be resolved using these. Here's an example:
221  
222 ;ingredient
223 1,fey moss
224 1,blue worms
225 ;forest
226 3,There is nothing here but trees.
227 1,You find [here ingredient].
228 ;village
229 1,The alchemist needs [nearby ingredient].
230  
231 Some of the forest hexes will have one of the two possible ingredients and the
232 village alchemist will want one of the nearby ingredients. Currently, there is a
233 limitation in place: we can only resolve the references starting with the word
234 "nearby" when everything else is done. This means that at that point, references
235 starting with the word "same" will no longer work since C<%locals> will no
236 longer be set.
237  
238 Thus:
239  
240 $globals->{ingredient}->{"0101"} eq "fey moss"
241  
242 =cut
243  
244 6 my $extra;
245 my %names;
246 6 my %locals;
247 6 my $globals;
248  
249 sub init {
250 6 ▶ %names = ();
Subroutine init: called CC=1 SCAR=0.0
Pod documented
251 6 %locals = ();
252 6 $globals = undef;
253 6 $extra = undef;
254 }
255  
256 =item parse_map_data
257  
258 This does basic parsing of hexes on the map as produced by Text Mapper, for
259 example:
260  
261 0101 dark-green trees village
262  
263 =cut
264  
265 sub parse_map_data {
266 1 ▶ my $map = shift;
Subroutine parse_map_data: called CC=8 SCAR=22.4
Pod documented
267 1 my $map_data;
268 1 ▶ if ($map and $map->isa('Mojo::Upload')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map and $map->isa("Mojo::Upload")) 0 1 1
Condition33%1/3
$map and $map->isa("Mojo::Upload")
!ll&&!rl&&r
010
MC/DC0%0/2
$map and $map->isa("Mojo::Upload")
$map $map->isa("Mojo::Upload")
Truth table33%1/3
A and B
A= $mapB= $map->isa("Mojo::Upload")
ABresult
0X0
100
111
269 0 $map = $map->slurp();
270 };
271 1 for my $hex (split(/\r?\n/, $map)) {
272 1 ▶ if (my ($x, $y) = $hex =~ /^(\d\d)(\d\d)\s*empty$/cg) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = $hex =~ /^(\d\d)(\d\d)\s*empty$/cgu) { } 0 1 1
elsif (() = $hex =~ /^(\d\d)(\d\d)\s+/cgu) { } 1 0 1
273 # skip
274 } elsif (($x, $y) = $hex =~ /^(\d\d)(\d\d)\s+/cg) {
275 1 my @types = ("system"); # Traveller
276 1 ▶ while($hex =~ /\G([a-z]="[^"]+")\s*/cg or $hex =~ /(\S+)/cg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hex =~ /\G([a-z]="[^"]+")\s*/cgu or $hex =~ /(\S+)/cgu) 1 1 2
Condition66%2/3
$hex =~ /\G([a-z]="[^"]+")\s*/cgu or $hex =~ /(\S+)/cgu
l!l&&r!l&&!r
011
MC/DC50%1/2
$hex =~ /\G([a-z]="[^"]+")\s*/cgu or $hex =~ /(\S+)/cgu
$hex =~ /\G([a-z]="[^"]+")\s*/cgu $hex =~ /(\S+)/cgu
Truth table66%2/3
A or B
A= $hex =~ /\G([a-z]="[^"]+")\s*/cguB= $hex =~ /(\S+)/cgu
ABresult
1X1
011
000
277 1 push(@types, $1);
278 }
279 1 $map_data->{"$x$y"} = \@types;
280 }
281 }
282 1 return $map_data;
283 }
284  
285 =item parse_map_lines
286  
287 This does basic parsing of linear structures on the map as produced by Text
288 Mapper, for example:
289  
290 0302-0101 trail
291  
292 We use C<compute_missing_points> to find all the missing points on the line.
293  
294 =cut
295  
296 6 my $line_re = qr/^(\d\d\d\d(?:-\d\d\d\d)+)\s+(\S+)/m;
297  
298 sub parse_map_lines {
299 1 ▶ my $map = shift;
Subroutine parse_map_lines: called CC=2 SCAR=8.5
Pod documented
300 1 my @lines;
301 1 ▶ while ($map =~ /$line_re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map =~ /$line_re/gu) 0 1 1
302 0 my ($line, $type) = ($1, $2);
303 0 my @points = compute_missing_points(split(/-/, $line));
304 0 push(@lines, [$type, @points]);
305 }
306 1 return \@lines;
307 }
308  
309 =item process_map_merge_lines
310  
311 As we process lines, we want to do two things: if a hex is part of a linear
312 structure, we want to add the B<type> to the terrain features. Thus, given the
313 following hex and river, we want to add "river" to the terrain features of 0101:
314  
315 0801-0802-0703-0602-0503-0402-0302-0201-0101-0100 river
316  
317 The (virtual) result:
318  
319 0101 dark-green trees village river
320  
321 Furthermore, given another river like the following, we want to merge these
322 where they meet (at 0302):
323  
324 0701-0601-0501-0401-0302-0201-0101-0100 river
325  
326 Again, the (virtual) result:
327  
328 0302 dark-green trees town river river-merge
329  
330 If you look at the default map, here are some interesting situations:
331  
332 A river starts at 0906 but it immediately merges with the river starting at 1005
333 thus it should be dropped entirely.
334  
335 A trail starts at 0206 and passes through 0305 on the way to 0404 but it
336 shouldn't end at 0305 just because there's also a trail starting at 0305 going
337 north to 0302.
338  
339 =cut
340  
341 sub process_map_merge_lines {
342 1 ▶ my $map_data = shift;
Subroutine process_map_merge_lines: called CC=11 SCAR=47.1
Pod documented
343 1 my $lines = shift;
344 1 for my $line (@$lines) {
345 0 my $type = $line->[0];
346 0 my %data = (type => $type, line => $line);
347 # $log->debug("New $type...");
348 0 my $start = 1;
349 COORD:
350 0 for my $i (1 .. $#$line) {
351 0 my $coord = $line->[$i];
352 # don't add data for hexes outside the map
353 0 ▶ last unless $map_data->{$coord};
Branch: 1 branch
BranchTrueFalseTotal
unless $map_data->{$coord} 0 0 0
354 # don't start a line going in the same direction as an existing line in
355 # the same hex (e.g. 0906) but also don't stop a line if it runs into a
356 # merge and continues (e.g. 0305)
357 0 my $same_dir = 0;
358 0 for my $line2 (grep { $_->{type} eq $type } @{$extra->{$coord}}) {
359 0 ▶ if (same_direction($coord, $line, $line2->{line})) {
Branch: 1 branch
BranchTrueFalseTotal
if (same_direction($coord, $line, $line2->{"line"})) 0 0 0
360 # $log->debug("... at $coord, @$line and @{$line2->{line}} go in the same direction");
361 0 $same_dir = 1;
362 0 last;
363 }
364 }
365 0 ▶ if ($start and $same_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start and $same_dir) 0 0 0
Condition0%0/3
$start and $same_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$start and $same_dir
$start $same_dir
Truth table0%0/3
A and B
A= $startB= $same_dir
ABresult
0X0
100
111
366 # $log->debug("... skipping");
367 0 last COORD;
368 }
369 # add type to the hex description, add "$type-merge" when
370 # running into an existing one
371 0 my $merged;
372 0 ▶ if (not grep { $_ eq $type } @{$map_data->{$coord}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not grep({$_ eq $type;} @{$$map_data{$coord};})) { } 0 0 0
elsif (not grep({$_ eq "$type-merge";} @{$$map_data{$coord};})) { } 0 0 0
373 # $log->debug("...$type leading into $coord");
374 0 push(@{$map_data->{$coord}}, $type);
375 0 } elsif (not grep { $_ eq "$type-merge" } @{$map_data->{$coord}}) {
376 0 $merged = $same_dir; # skip the rest of the line, if same dir
377 # $log->debug("...noted merge into existing $type at $coord");
378 0 push(@{$map_data->{$coord}}, "$type-merge");
379 } else {
380 0 $merged = $same_dir; # skip the rest of the line, if same dir
381 # $log->debug("...leads into existing $type merge at $coord");
382 }
383 0 $start = 0;
384 # all hexes along a line share this hash
385 0 push(@{$extra->{$coord}}, \%data);
386 # if a river merges into another, don't add any hexes downriver
387 0 ▶ last if $merged;
Branch: 1 branch
BranchTrueFalseTotal
if $merged 0 0 0
388 }
389 }
390 }
391  
392 =item process_map_start_lines
393  
394 As we process lines, we also want to note the start of lines: sources of rivers,
395 the beginning of trails. Thus, given the following hex and river, we want to add
396 "river-start" to the terrain features of 0801:
397  
398 0801-0802-0703-0602-0503-0402-0302-0201-0101-0100 river
399  
400 Adds a river to the hex:
401  
402 0801 light-grey mountain river river-start
403  
404 But note that we don't want to do this where linear structures have merged. If a
405 trail ends at a town and merges with other trails there, it doesn't "start"
406 there. It can only be said to start somewhere if no other linear structure
407 starts there.
408  
409 In case we're not talking about trails and rivers but things like routes from A
410 to B, it might be important to note the fact. Therefore, both ends of the line
411 get a "river-end" (if a river).
412  
413 =cut
414  
415 sub process_map_start_lines {
416 1 ▶ my $map_data = shift;
Subroutine process_map_start_lines: called CC=6 SCAR=33.6
Pod documented
417 1 my $lines = shift;
418 # add "$type-start" to the first and last hex of a line, unless it is a merge
419 1 for my $line (@$lines) {
420 0 my $type = $line->[0];
421 0 for my $coord ($line->[1], $line->[$#$line]) {
422 # ends get marked either way
423 0 ▶ push(@{$map_data->{$coord}}, "$type-end") unless grep { $_ eq "$type-end" } @{$map_data->{$coord}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "$type-end";} @{$$map_data{$coord};} 0 0 0
424 # skip hexes outside the map
425 0 ▶ last unless $map_data->{$coord};
Branch: 1 branch
BranchTrueFalseTotal
unless $map_data->{$coord} 0 0 0
426 # skip merges
427 0 ▶ last if grep { $_ eq "$type-merge" } @{$map_data->{$coord}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "$type-merge";} @{$$map_data{$coord};} 0 0 0
428 # add start
429 0 push(@{$map_data->{$coord}}, "$type-start");
430 }
431 }
432 }
433  
434 =item parse_map
435  
436 This calls all the map parsing and processing functions we just talked about.
437  
438 =cut
439  
440 sub parse_map {
441 1 ▶ my $map = shift;
Subroutine parse_map: called CC=1 SCAR=0.0
Pod documented
442 1 my $map_data = parse_map_data($map);
443 1 my $lines = parse_map_lines($map);
444 # longest rivers first
445 1 @$lines = sort { @$b <=> @$a } @$lines;
446 # for my $line (@$lines) {
447 # $log->debug("@$line");
448 # }
449 1 process_map_merge_lines($map_data, $lines);
450 1 process_map_start_lines($map_data, $lines);
451 # for my $coord (sort keys %$map_data) {
452 # $log->debug(join(" ", $coord, @{$map_data->{$coord}}));
453 # }
454 1 return $map_data;
455 }
456  
457 =item pick_description
458  
459 Pick a description from a given table. In the example above, pick a random
460 number between 1 and 3 and then go through the list, addin up counts until you
461 hit that number.
462  
463 If the result picked is unique, remove it from the list. That is, set it's count
464 to 0 such that it won't ever get picked again.
465  
466 =cut
467  
468 sub pick_description {
469 363 ▶ my $h = shift;
Subroutine pick_description: called CC=4 SCAR=14.7
Pod documented
470 363 my $total = $h->{total};
471 363 my $lines = $h->{lines};
472 363 my $roll = int(rand($total)) + 1;
473 363 my $i = 0;
474 363 for my $line (@$lines) {
475 12660 $i += $line->{count};
476 12660 ▶ if ($i >= $roll) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $roll) 363 12297 12660
477 363 ▶ if ($line->{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"unique"}) 0 363 363
478 0 $h->{total} -= $line->{count};
479 0 $line->{count} = 0;
480 }
481 363 return $line->{text};
482 }
483 }
484 0 $log->error("picked nothing");
485 0 return '';
486 }
487  
488 =item resolve_redirect
489  
490 This handles the special redirect syntax: request an URL and if the response
491 code is a 301 or 302, take the location header in the response and return it.
492  
493 If the environment variable C<HEX_DESCRIBE_OFFLINE> is set, these do not get
494 resolved and the empty string is returned.
495  
496 =cut
497  
498 sub resolve_redirect {
499 # If you install this tool on a server using HTTPS, then some browsers will
500 # make sure that including resources from other servers will not work.
501 10 ▶ my $url = shift;
Subroutine resolve_redirect: called CC=8 SCAR=38.4
Pod documented
502 10 my $redirects = shift;
503 10 ▶ return '' unless $redirects and not $ENV{HEX_DESCRIBE_OFFLINE};
Branch: 1 branch
BranchTrueFalseTotal
unless $redirects and not $ENV{"HEX_DESCRIBE_OFFLINE"} 10 0 10
Condition33%1/3
$redirects and not $ENV{"HEX_DESCRIBE_OFFLINE"}
!ll&&!rl&&r
0100
MC/DC0%0/2
$redirects and not $ENV{"HEX_DESCRIBE_OFFLINE"}
$redirects not $ENV{"HEX_DESCRIBE_OFFLINE"}
Truth table33%1/3
A and not(B)
A= $redirectsB= not $ENV{"HEX_DESCRIBE_OFFLINE"}
ABresult
0X0
100
111
504 # Special case because table writers probably used the default face generator URL
505 0 ▶ $url =~ s!^https://campaignwiki\.org/face!$face_generator_url! if $face_generator_url;
Branch: 1 branch
BranchTrueFalseTotal
if $Game::HexDescribe::Utils::face_generator_url 0 0 0
506 0 ▶ $url =~ s!^https://campaignwiki\.org/text-mapper!$text_mapper_url! if $text_mapper_url;
Branch: 1 branch
BranchTrueFalseTotal
if $Game::HexDescribe::Utils::text_mapper_url 0 0 0
507 0 my $ua = Mojo::UserAgent->new;
508 0 my $res = eval { $ua->get($url)->result };
509 0 ▶ if (not $res) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $res) { } 0 0 0
elsif ($res->code == 301 or $res->code == 302) { } 0 0 0
510 0 my $warning = $@;
511 0 chomp($warning);
512 0 $log->warn("connecting to $url: $warning");
513 0 return "";
514 ▶ } elsif ($res->code == 301 or $res->code == 302) {
Condition0%0/3
$res->code == 301 or $res->code == 302
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code == 301 or $res->code == 302
$res->code == 301 $res->code == 302
Truth table0%0/3
A or B
A= $res->code == 301B= $res->code == 302
ABresult
1X1
011
000
515 0 return Mojo::URL->new($res->headers->location)
516 ->base(Mojo::URL->new($url))
517 ->to_abs;
518 }
519 0 $log->info("resolving redirect for $url did not result in a redirection");
520 0 return $url;
521 }
522  
523 =item pick
524  
525 This function picks the appropriate table given a particular word (usually a map
526 feature such as "forest" or "river").
527  
528 This is where I<context> is implemented. Let's start with this hex:
529  
530 0101 dark-green trees village river trail
531  
532 Remember that parsing the map added more terrain than was noted on the map
533 itself. Our function will get called for each of these words, Let's assume it
534 will get called for "dark-green". Before checking whether a table called
535 "dark-green" exists, we want to check whether any of the other words provide
536 enough context to pick a more specific table. Thus, we will check "trees
537 dark-green", "village dark-green", "river dark-green" and "trail dark-green"
538 before checking for "dark-green".
539  
540 If such a table exists in C<$table_data>, we call C<pick_description> to pick a
541 text from the table and then we go through the text and call C<describe> to
542 resolve any table references in square brackets.
543  
544 Remember that rules for the remaining words are still being called. Thus, if you
545 write a table for "trees dark-green" (which is going to be picked in preference
546 to "dark-green"), then there should be no table for "trees" because that's the
547 next word that's going to be processed!
548  
549 =cut
550  
551 sub pick {
552 364 ▶ my $map_data = shift;
Subroutine pick: called CC=6 SCAR=18.0
Pod documented
553 364 my $table_data = shift;
554 364 my $level = shift;
555 364 my $coordinates = shift;
556 364 my $words = shift;
557 364 my $word = shift;
558 364 my $redirects = shift;
559 364 my $text;
560 # Make sure we're testing all the context combinations first. Thus, if $words
561 # is [ "mountains" white" "chaos"] and $word is "mountains", we want to test
562 # "white mountains", "cold mountains" and "mountains", in this order.
563 364 for my $context (grep( { $_ ne $word } @$words), $word) {
564 377 ▶ my $key = ($context eq $word ? $word : "$context $word");
Branch: 1 branch
BranchTrueFalseTotal
$context eq $word ? : 364 13 377
565 # $log->debug("$coordinates: looking for a $key table") if $coordinates eq "0109";
566 377 ▶ if ($table_data->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_data->{$key}) 363 14 377
567 363 $text = pick_description($table_data->{$key});
568 # $log->debug("$coordinates → $key → $text");
569 363 my $seed = int(rand(~0)); # maxint
570 363 $text =~ s/\[\[redirect (https:.*?)\]\]/my $url = $1; $url =~ s!\$seed!$seed!; resolve_redirect($url, $redirects)/ge;
571 # this makes sure we recursively resolve all references, in order, because
572 # we keep rescanning from the beginning
573 363 my $last = $text;
574 363 ▶ while ($text =~ s/\[([^][]*)\]/describe($map_data,$table_data,$level+1,$coordinates,[$1], $redirects)/e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/\[([^][]*)\]/describe($map_data, $table_data, $level + 1, $coordinates, [$1], $redirects);/eu) 510 363 873
575 510 ▶ if ($last eq $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last eq $text) 0 510 510
576 0 $log->error("Infinite loop: $text");
577 0 last;
578 }
579 510 $last = $text;
580 };
581 363 last;
582 }
583 }
584 # $log->debug("$word → $text ") if $text;
585 364 return $text;
586 }
587  
588 =item describe
589  
590 This is where all the references get resolved. We handle references to dice
591 rolls, the normal recursive table lookup, and all the special rules for names
592 that get saved once they have been determined both globally or per terrain
593 features. Please refer to the tutorial on the help page for the various
594 features.
595  
596 =cut
597  
598 sub describe {
599 529 ▶ my $map_data = shift;
Subroutine describe: called CC=125 SCAR=78.1
Pod documented
600 529 my $table_data = shift;
601 529 my $level = shift;
602 529 my $coordinates = shift;
603 529 my $words = shift;
604 529 my $redirects = shift;
605 529 ▶ $log->error("Recursion level $level exceeds 20 in $coordinates (@$words)!") if $level > 20;
Branch: 1 branch
BranchTrueFalseTotal
if $level > 20 0 529 529
606 529 ▶ return '' if $level > 20;
Branch: 1 branch
BranchTrueFalseTotal
if $level > 20 0 529 529
607 529 ▶ if ($level == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 1) 3 526 529
608 3 %locals = (hex => $coordinates); # reset once per paragraph
609 3 for my $word (@$words) {
610 4 ▶ if ($word =~ /^([a-z]+)="(.*)"/ or
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /^([a-z]+)="(.*)"/u or $word =~ /(.*)-(\d+)$/u) { } 0 4 4
Condition33%1/3
$word =~ /^([a-z]+)="(.*)"/u or $word =~ /(.*)-(\d+)$/u
l!l&&r!l&&!r
004
MC/DC0%0/2
$word =~ /^([a-z]+)="(.*)"/u or $word =~ /(.*)-(\d+)$/u
$word =~ /^([a-z]+)="(.*)"/u $word =~ /(.*)-(\d+)$/u
Truth table33%1/3
A or B
A= $word =~ /^([a-z]+)="(.*)"/uB= $word =~ /(.*)-(\d+)$/u
ABresult
1X1
011
000
611 $word =~ /(.*)-(\d+)$/) {
612 # assigments in the form uwp=“777777” assign “777777” to “uwp”
613 # words in the form law-5 assign “5” to “law”
614 0 $locals{$1} = $2;
615 } else {
616 4 $locals{$word} = 1;
617 }
618 }
619 }
620 529 my @descriptions;
621 529 for my $word (@$words) {
622 # valid dice rolls: 1d6, 1d6+1, 1d6x10, 1d6x10+1
623 530 ▶ if (my ($just_save, $n, $d, $m, $p, $min, $max, $c, $save_as) = $word =~ /$dice_re/) {
Branch: 22 branches
BranchTrueFalseTotal
if (() = $word =~ /$dice_re/u) { } 40 490 530
elsif (() = $word =~ /$math_re/u) { } 0 490 490
elsif ($word =~ /^(\S+)\?\|\|(.*)/u) { } 0 490 490
elsif ($word =~ /^!(\S+)\|\|(.*)/u) { } 0 490 490
elsif (index($word, '||') != -1) { } 0 490 490
elsif (index($word, '|') != -1) { } 0 490 490
elsif ($word =~ /^name for an? /u) { } 5 485 490
elsif ($word =~ /^names for (\S+)/u) { } 0 485 485
elsif ($word =~ /^name for (\S+)/u) { } 0 485 485
elsif ($word eq 'adjacent hex') { } 0 485 485
elsif ($word =~ /^(?:nearby|closest|other|later) ./u) { } 1 484 485
elsif ($word =~ /^append (.*)/u) { } 0 484 484
elsif ($word =~ /^same (.+)/u) { } 93 391 484
elsif ($word =~ /^(?:(here|global) )?with (.+?)(?: as (.+))?$/u) { } 2 389 391
elsif ($word =~ /^(?:(here|global) )?and (.+?)(?: as (.+))?$/u) { } 2 387 389
elsif ($word =~ /^capitalize (.+)/u) { } 0 387 387
elsif ($word =~ /^titlecase (.+)/u) { } 0 387 387
elsif ($word =~ /^highlightcase (.+)/u) { } 0 387 387
elsif ($word =~ /^normalize-elvish (.+)/u) { } 3 384 387
elsif ($word =~ /^(?:(here|global) )?(?:(save|store|quote) )?(.+?)(?: as (.+))?$/u) { } 384 0 384
elsif ($level > 1 and not exists $table_data->{$word} and not $locals{$word}) { } 0 0 0
elsif ($level > 1 and not $table_data->{$word} and not $locals{$word}) { } 0 0 0
624 40 my $r = 0;
625 40 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 0 40 40
626 0 $r = $c;
627 } else {
628 40 ▶ for(my $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 53 40 93
629 53 $r += int(rand($d)) + 1;
630 }
631 40 ▶ $r *= $m||1;
Condition100%2/2
$m || 1
l!l
1030
MC/DC100%1/1
$m || 1
$m
Truth table100%2/2
A
A= $m
Aresult
11
00
632 40 ▶ $r += $p||0;
Condition100%2/2
$p || 0
l!l
1030
MC/DC100%1/1
$p || 0
$p
Truth table100%2/2
A
A= $p
Aresult
11
00
633 40 ▶ $r = $min if defined $min and $r < $min;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and $r < $min 0 40 40
Condition33%1/3
defined $min and $r < $min
!ll&&!rl&&r
4000
MC/DC0%0/2
defined $min and $r < $min
defined $min $r < $min
Truth table33%1/3
A and B
A= defined $minB= $r < $min
ABresult
0X0
100
111
634 40 ▶ $r = $max if defined $max and $r > $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and $r > $max 0 40 40
Condition33%1/3
defined $max and $r > $max
!ll&&!rl&&r
4000
MC/DC0%0/2
defined $max and $r > $max
defined $max $r > $max
Truth table33%1/3
A and B
A= defined $maxB= $r > $max
ABresult
0X0
100
111
635 }
636 # $log->debug("rolling dice: $word = $r");
637 40 ▶ $locals{$save_as} = $r if $save_as;
Branch: 1 branch
BranchTrueFalseTotal
if $save_as 10 30 40
638 40 ▶ push(@descriptions, $r) unless $just_save;
Branch: 1 branch
BranchTrueFalseTotal
unless $just_save 40 0 40
639 } elsif (my ($save, $expression, $as) = $word =~ /$math_re/) {
640 0 my $r = eval($expression);
641 0 ▶ $locals{$as} = $r if $as;
Branch: 1 branch
BranchTrueFalseTotal
if $as 0 0 0
642 0 ▶ push(@descriptions, $r) unless $save;
Branch: 1 branch
BranchTrueFalseTotal
unless $save 0 0 0
643 } elsif ($word =~ /^(\S+)\?\|\|(.*)/) {
644 # [a?||b] return b if a is defined, or nothing
645 0 ▶ push(@descriptions, $2) if $locals{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $locals{$1} 0 0 0
646 } elsif ($word =~ /^!(\S+)\|\|(.*)/) {
647 # [!a||b] return b if a is undefined
648 0 ▶ push(@descriptions, $2) if not $locals{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless $locals{$1} 0 0 0
649 } elsif (index($word, "||") != -1) {
650 # [a||b] returns a if defined, otherwise b
651 0 for my $html (split(/\|\|/, $word)) {
652 0 my $copy = $html;
653 0 $copy =~ s/<.*?>|…//g; # strip tags, e.g. span elements, and ellipsis
654 0 ▶ if ($copy =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copy =~ /\S/u) 0 0 0
655 0 push(@descriptions, $html);
656 0 last;
657 }
658 }
659 } elsif (index($word, "|") != -1) {
660 # [a|b] returns one of a or b
661 0 push(@descriptions, one(split(/\|/, $word)));
662 } elsif ($word =~ /^name for an? /) {
663 # for global things like factions, dukes
664 5 my $name = $names{$word};
665 # $log->debug("memoized: $word is $name") if $name;
666 5 ▶ return $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 1 4 5
667 4 $name = pick($map_data, $table_data, $level, $coordinates, $words, $word, $redirects);
668 4 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
669 4 $names{$word} = $name;
670 # $log->debug("$word is $name");
671 4 push(@descriptions, $name);
672 } elsif ($word =~ /^names for (\S+)/) {
673 0 my $key = $1; # "river"
674 # $log->debug("Looking at $key for $coordinates...");
675 0 ▶ if (my @lines = grep { $_->{type} eq $key } @{$extra->{$coordinates}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@lines) = grep({$_->{"type"} eq $key;} @{$$extra{$coordinates};})) 0 0 0
676 # $log->debug("...@lines");
677 # make sure all the lines (rivers, trails) are named
678 0 my @names = ();
679 0 for my $line (@lines) {
680 0 my $name = $line->{name};
681 0 ▶ if (not $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
682 0 ▶ $name ||= pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects);
Condition0%0/3
$name ||= pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects)
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects)
$name pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects)
Truth table0%0/3
A ||= B
A= $nameB= pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects)
ABresult
1X1
011
000
683 0 $line->{name} = $name;
684 }
685 0 push(@names, $name);
686 }
687 0 my $list;
688 0 ▶ if (@names > 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@names > 2) { } 0 0 0
elsif (@names == 2) { } 0 0 0
689 0 $list = join(", ", @names[0 .. $#names-1], "and " . $names[-1]);
690 } elsif (@names == 2) {
691 0 $list = join(" and ", @names);
692 } else {
693 0 $log->error("$coordinates has merge but just one line (@lines)");
694 0 $list = shift(@names);
695 }
696 0 ▶ $log->error("$coordinates uses merging rule without names") unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
697 0 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
698 0 push(@descriptions, $list);
699 }
700 } elsif ($word =~ /^name for (\S+)/) {
701 0 my $key = $1; # "white" or "river"
702 # $log->debug("Looking at $key for $coordinates...");
703 0 ▶ if (my @lines = grep { $_->{type} eq $key } @{$extra->{$coordinates}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@lines) = grep({$_->{'type'} eq $key;} @{$$extra{$coordinates};})) { } 0 0 0
704 # for rivers and the like: "name for river"
705 0 for my $line (@lines) {
706 # $log->debug("Looking at $word for $coordinates...");
707 0 my $name = $line->{name};
708 # $log->debug("... we already have a name: $name") if $name;
709 # if a type appears twice for a hex, this returns the same name for all of them
710 0 ▶ return $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
711 0 $name = pick($map_data, $table_data, $level, $coordinates, $words, $word, $redirects);
712 # $log->debug("... we picked a new name: $name") if $name;
713 0 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
714 0 push(@descriptions, $name);
715 0 $line->{name} = $name;
716 0 $globals->{$key}->{$_} = $name for @{$line->{line}}[1..$#{$line->{line}}];
717 # name the first one without a name, don't keep adding names
718 0 last;
719 }
720 } else {
721 # regular features: "name for white big mountain"
722 0 my $name = $names{"$word: $coordinates"}; # "name for white big mountain: 0101"
723 # $log->debug("$word for $coordinates is $name") if $name;
724 0 ▶ return $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
725 0 $name = pick($map_data, $table_data, $level, $coordinates, $words, $word, $redirects);
726 # $log->debug("new $word for $coordinates is $name") if $name;
727 0 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
728 0 $names{"$word: $coordinates"} = $name;
729 0 push(@descriptions, $name);
730 0 ▶ spread_name($map_data, $coordinates, $word, $key, $name) if %$map_data;
Branch: 1 branch
BranchTrueFalseTotal
if %$map_data 0 0 0
731 }
732 } elsif ($word eq 'adjacent hex') {
733 # experimental
734 0 ▶ my $location = $coordinates eq 'no map' ? 'somewhere' : one(neighbours($map_data, $coordinates));
Branch: 1 branch
BranchTrueFalseTotal
$coordinates eq 'no map' ? : 0 0 0
735 0 $locals{$word} = $location;
736 0 return $location;
737 } elsif ($word =~ /^(?:nearby|closest|other|later) ./) {
738 # skip on the first pass
739 1 return "「$word」";
740 } elsif ($word =~ /^append (.*)/) {
741 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $1, $redirects);
742 # remember it's legitimate to have no result for a table
743 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
744 0 $locals{$word} = $text;
745 0 push(@descriptions, "「append $text」");
746 } elsif ($word =~ /^same (.+)/) {
747 93 my $key = $1;
748 93 ▶ return $locals{$key}->[0] if exists $locals{$key} and ref($locals{$key}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if exists $locals{$key} and ref $locals{$key} eq "ARRAY" 0 93 93
Condition33%1/3
exists $locals{$key} and ref $locals{$key} eq "ARRAY"
!ll&&!rl&&r
0930
MC/DC0%0/2
exists $locals{$key} and ref $locals{$key} eq "ARRAY"
exists $locals{$key} ref $locals{$key} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $locals{$key}B= ref $locals{$key} eq "ARRAY"
ABresult
0X0
100
111
749 93 ▶ return $locals{$key} if exists $locals{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $locals{$key} 93 0 93
750 0 ▶ return $globals->{$key}->{global} if $globals->{$key} and $globals->{$key}->{global};
Branch: 1 branch
BranchTrueFalseTotal
if $globals->{$key} and $globals->{$key}{"global"} 0 0 0
Condition0%0/3
$globals->{$key} and $globals->{$key}{"global"}
!ll&&!rl&&r
000
MC/DC0%0/2
$globals->{$key} and $globals->{$key}{"global"}
$globals->{$key} $globals->{$key}{"global"}
Truth table0%0/3
A and B
A= $globals->{$key}B= $globals->{$key}{"global"}
ABresult
0X0
100
111
751 0 $log->warn("[same $key] is undefined for $coordinates");
752 0 push(@descriptions, "…");
753 } elsif ($word =~ /^(?:(here|global) )?with (.+?)(?: as (.+))?$/) {
754 2 my ($where, $key, $alias) = ($1, $2, $3);
755 2 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
756 2 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 2 2
757 2 $locals{$key} = [$text]; # start a new list
758 2 ▶ $locals{$alias} = $text if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 2 2
759 2 ▶ $globals->{$key}->{$coordinates} = $text if $where and $where eq 'here';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" 0 2 2
Condition33%1/3
$where and $where eq "here"
!ll&&!rl&&r
200
MC/DC0%0/2
$where and $where eq "here"
$where $where eq "here"
Truth table33%1/3
A and B
A= $whereB= $where eq "here"
ABresult
0X0
100
111
760 2 ▶ $globals->{$alias}->{$coordinates} = $text if $where and $where eq 'here' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" and $alias 0 2 2
Condition33%2/6
$where and $where eq "here" and $alias
!ll&&!rl&&r
200
$where and $where eq "here"
!ll&&!rl&&r
200
MC/DC0%0/3
$where and $where eq "here" and $alias
$where $where eq "here" $alias
Truth table25%1/4
A and B and C
A= $whereB= $where eq "here"C= $alias
ABCresult
0XX0
10X0
1100
1111
761 2 ▶ $globals->{$key}->{global} = $text if $where and $where eq 'global';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" 0 2 2
Condition33%1/3
$where and $where eq "global"
!ll&&!rl&&r
200
MC/DC0%0/2
$where and $where eq "global"
$where $where eq "global"
Truth table33%1/3
A and B
A= $whereB= $where eq "global"
ABresult
0X0
100
111
762 2 ▶ $globals->{$alias}->{global} = $text if $where and $where eq 'global' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" and $alias 0 2 2
Condition33%2/6
$where and $where eq "global" and $alias
!ll&&!rl&&r
200
$where and $where eq "global"
!ll&&!rl&&r
200
MC/DC0%0/3
$where and $where eq "global" and $alias
$where $where eq "global" $alias
Truth table25%1/4
A and B and C
A= $whereB= $where eq "global"C= $alias
ABCresult
0XX0
10X0
1100
1111
763 2 push(@descriptions, $text);
764 } elsif ($word =~ /^(?:(here|global) )?and (.+?)(?: as (.+))?$/) {
765 2 my ($where, $key, $alias) = ($1, $2, $3);
766 2 my $found = 0;
767 # limited attempts to find a unique entry for an existing list (instead of
768 # modifying the data structures)
769 2 for (1 .. 20) {
770 3 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
771 3 ▶ $log->warn("[and $key] is used before [with $key] is done in $coordinates") if ref $locals{$key} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $locals{$key} ne "ARRAY" 0 3 3
772 3 ▶ $locals{$key} = [$text] if ref $locals{$key} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $locals{$key} ne "ARRAY" 0 3 3
773 3 ▶ next if not $text or grep { $text eq $_ } @{$locals{$key}};
Branch: 1 branch
BranchTrueFalseTotal
if not $text or grep {$text eq $_;} @{$locals{$key};} 1 2 3
Condition66%2/3
not $text or grep {$text eq $_;} @{$locals{$key};}
l!l&&r!l&&!r
012
MC/DC50%1/2
not $text or grep {$text eq $_;} @{$locals{$key};}
not $text grep {$text eq $_;} @{$locals{$key};}
Truth table66%2/3
not(A) or B
A= not $textB= grep {$text eq $_;} @{$locals{$key};}
ABresult
1X1
011
000
774 2 push(@{$locals{$key}}, $text);
775 2 push(@descriptions, $text);
776 2 ▶ $locals{$alias} = $text if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 2 2
777 2 ▶ $globals->{$key}->{$coordinates} = $text if $where and $where eq 'here';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" 0 2 2
Condition33%1/3
$where and $where eq "here"
!ll&&!rl&&r
200
MC/DC0%0/2
$where and $where eq "here"
$where $where eq "here"
Truth table33%1/3
A and B
A= $whereB= $where eq "here"
ABresult
0X0
100
111
778 2 ▶ $globals->{$alias}->{$coordinates} = $text if $where and $where eq 'here' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" and $alias 0 2 2
Condition33%2/6
$where and $where eq "here" and $alias
!ll&&!rl&&r
200
$where and $where eq "here"
!ll&&!rl&&r
200
MC/DC0%0/3
$where and $where eq "here" and $alias
$where $where eq "here" $alias
Truth table25%1/4
A and B and C
A= $whereB= $where eq "here"C= $alias
ABCresult
0XX0
10X0
1100
1111
779 2 ▶ $globals->{$key}->{global} = $text if $where and $where eq 'global';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" 0 2 2
Condition33%1/3
$where and $where eq "global"
!ll&&!rl&&r
200
MC/DC0%0/2
$where and $where eq "global"
$where $where eq "global"
Truth table33%1/3
A and B
A= $whereB= $where eq "global"
ABresult
0X0
100
111
780 2 ▶ $globals->{$alias}->{global} = $text if $where and $where eq 'global' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" and $alias 0 2 2
Condition33%2/6
$where and $where eq "global" and $alias
!ll&&!rl&&r
200
$where and $where eq "global"
!ll&&!rl&&r
200
MC/DC0%0/3
$where and $where eq "global" and $alias
$where $where eq "global" $alias
Truth table25%1/4
A and B and C
A= $whereB= $where eq "global"C= $alias
ABCresult
0XX0
10X0
1100
1111
781 2 $found = 1;
782 2 last;
783 }
784 2 ▶ if (not $found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 2 2
785 0 $log->warn("[and $key] not unique in $coordinates");
786 0 push(@descriptions, "…");
787 }
788 } elsif ($word =~ /^capitalize (.+)/) {
789 0 my $key = $1;
790 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
791 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
792 0 $locals{$key} = $text;
793 0 push(@descriptions, ucfirst $text);
794 } elsif ($word =~ /^titlecase (.+)/) {
795 0 my $key = $1;
796 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
797 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
798 0 $locals{$key} = $text;
799 0 push(@descriptions, autoformat($text, { case => 'titlecase' }));
800 } elsif ($word =~ /^highlightcase (.+)/) {
801 0 my $key = $1;
802 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
803 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
804 0 $locals{$key} = $text;
805 0 push(@descriptions, autoformat($text, { case => 'highlight' }));
806 } elsif ($word =~ /^normalize-elvish (.+)/) {
807 3 my $key = $1;
808 3 my $text = normalize_elvish($key);
809 3 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 3 3
810 3 $locals{$key} = $text;
811 3 push(@descriptions, $text);
812 } elsif ($word =~ /^(?:(here|global) )?(?:(save|store|quote) )?(.+?)(?: as (.+))?$/) {
813 384 my ($where, $action, $key, $alias) = ($1, $2, $3, $4);
814 384 my $text;
815 384 ▶ if (not $action or $action eq "save") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $action or $action eq 'save') { } 355 29 384
Condition100%3/3
not $action or $action eq "save"
l!l&&r!l&&!r
352329
MC/DC100%2/2
not $action or $action eq "save"
not $action $action eq "save"
Truth table100%3/3
not(A) or B
A= not $actionB= $action eq "save"
ABresult
1X1
011
000
816 # no action and save are with lookup
817 355 $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
818 } else {
819 # quote and store are without lookup
820 29 $text = $key;
821 }
822 384 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 82 302 384
823 302 $locals{$key} = $text;
824 302 ▶ $locals{$alias} = $text if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 35 267 302
825 302 ▶ $globals->{$key}->{$coordinates} = $text if $where and $where eq 'here';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" 4 298 302
Condition100%3/3
$where and $where eq "here"
!ll&&!rl&&r
29264
MC/DC100%2/2
$where and $where eq "here"
$where $where eq "here"
Truth table100%3/3
A and B
A= $whereB= $where eq "here"
ABresult
0X0
100
111
826 302 ▶ $globals->{$alias}->{$coordinates} = $text if $where and $where eq 'here' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" and $alias 4 298 302
Condition83%5/6
$where and $where eq "here" and $alias
!ll&&!rl&&r
29804
$where and $where eq "here"
!ll&&!rl&&r
29264
MC/DC66%2/3
$where and $where eq "here" and $alias
$where $where eq "here" $alias
Truth table75%3/4
A and B and C
A= $whereB= $where eq "here"C= $alias
ABCresult
0XX0
10X0
1100
1111
827 302 ▶ $globals->{$key}->{global} = $text if $where and $where eq 'global';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" 6 296 302
Condition100%3/3
$where and $where eq "global"
!ll&&!rl&&r
29246
MC/DC100%2/2
$where and $where eq "global"
$where $where eq "global"
Truth table100%3/3
A and B
A= $whereB= $where eq "global"
ABresult
0X0
100
111
828 302 ▶ $globals->{$alias}->{global} = $text if $where and $where eq 'global' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" and $alias 6 296 302
Condition83%5/6
$where and $where eq "global" and $alias
!ll&&!rl&&r
29606
$where and $where eq "global"
!ll&&!rl&&r
29246
MC/DC66%2/3
$where and $where eq "global" and $alias
$where $where eq "global" $alias
Truth table75%3/4
A and B and C
A= $whereB= $where eq "global"C= $alias
ABCresult
0XX0
10X0
1100
1111
829 302 ▶ push(@descriptions, $text) if not $action or $action eq "quote";
Branch: 1 branch
BranchTrueFalseTotal
if not $action or $action eq "quote" 270 32 302
Condition66%2/3
not $action or $action eq "quote"
l!l&&r!l&&!r
270032
MC/DC50%1/2
not $action or $action eq "quote"
not $action $action eq "quote"
Truth table66%2/3
not(A) or B
A= not $actionB= $action eq "quote"
ABresult
1X1
011
000
830 ▶ } elsif ($level > 1 and not exists $table_data->{$word} and not $locals{$word}) {
Condition0%0/6
$level > 1 and not exists $table_data->{$word} and not $locals{$word}
!ll&&!rl&&r
000
$level > 1 and not exists $table_data->{$word}
!ll&&!rl&&r
000
MC/DC0%0/3
$level > 1 and not exists $table_data->{$word} and not $locals{$word}
$level > 1 not exists $table_data->{$word} not $locals{$word}
Truth table0%0/4
A and not(B) and not(C)
A= $level > 1B= not exists $table_data->{$word}C= not $locals{$word}
ABCresult
0XX0
10X0
1100
1111
831 # on level one, many terrain types do not exist (e.g. river-start)
832 0 $log->error("unknown table for $coordinates/$level: $word");
833 ▶ } elsif ($level > 1 and not $table_data->{$word} and not $locals{$word}) {
Condition0%0/6
$level > 1 and not $table_data->{$word} and not $locals{$word}
!ll&&!rl&&r
000
$level > 1 and not $table_data->{$word}
!ll&&!rl&&r
000
MC/DC0%0/3
$level > 1 and not $table_data->{$word} and not $locals{$word}
$level > 1 not $table_data->{$word} not $locals{$word}
Truth table0%0/4
A and not(B) and not(C)
A= $level > 1B= not $table_data->{$word}C= not $locals{$word}
ABCresult
0XX0
10X0
1100
1111
834 # on level one, many terrain types do not exist (e.g. river-start)
835 0 $log->error("empty table for $coordinates/$level: $word");
836 } else {
837 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $word, $redirects);
838 # remember it's legitimate to have no result for a table, and remember we
839 # cannot use a local with the same name that's defined because sometimes
840 # locals are simply defined as "1" since they start out as "words" and I
841 # don't want to make "1" a special case to ignore, here
842 0 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
843 0 $locals{$word} = $text;
844 0 push(@descriptions, $text);
845 }
846 }
847 434 return join(' ', @descriptions);
848 }
849  
850 =item describe_text
851  
852 This function does what C<describe> does, but for simple text without hex
853 coordinates.
854  
855 =cut
856  
857 sub describe_text {
858 5 ▶ my $input = shift;
Subroutine describe_text: called CC=2 SCAR=6.9
Pod documented
859 5 my $table_data = shift;
860 5 my $redirects = shift;
861 5 my @descriptions;
862 5 init();
863 5 for my $text (split(/\r?\n/, $input)) {
864 # recusion level 2 makes sure we don't reset %locals
865 16 $text =~ s/\[(.*?)\]/describe({},$table_data,2,"no map",[$1],$redirects)/ge;
866 16 push(@descriptions, process($text, $redirects));
867 16 %locals = (); # reset once per paragraph
868 }
869 5 return \@descriptions;
870 }
871  
872 =item normalize_elvish
873  
874 We do some post-processing of words, inspired by these two web pages, but using
875 our own replacements.
876 http://sindarinlessons.weebly.com/37---how-to-make-names-1.html
877 http://sindarinlessons.weebly.com/38---how-to-make-names-2.html
878  
879 =cut
880  
881 sub normalize_elvish {
882 3 ▶ my $original = shift;
Subroutine normalize_elvish: called CC=1 SCAR=0.0
Pod documented
883 3 my $name = $original;
884  
885 3 $name =~ s/(.) \1/$1/g;
886 3 $name =~ s/d t/d/g;
887 3 $name =~ s/a ui/au/g;
888 3 $name =~ s/nd m/dhm/g;
889 3 $name =~ s/n?d w/dhw/g;
890 3 $name =~ s/r gw/rw/g;
891 3 $name =~ s/^nd/d/;
892 3 $name =~ s/^ng/g/;
893 3 $name =~ s/th n?d/d/g;
894 3 $name =~ s/dh dr/dhr/g;
895 3 $name =~ s/ //g;
896  
897 3 $name =~ tr/âêîôûŷ/aeioúi/;
898 3 $name =~ s/ll$/l/;
899 3 $name =~ s/ben$/wen/g;
900 3 $name =~ s/bwi$/wi/;
901 3 $name =~ s/[^aeiouúi]ndil$/dil/g;
902 3 $name =~ s/ae/aë/g;
903 3 $name =~ s/ea/ëa/g;
904  
905 3 $name = ucfirst($name);
906  
907 # $log->debug("Elvish normalize: $original → $name");
908 3 return $name;
909 }
910  
911 =item process
912  
913 We do some post-processing after the description has been assembled: we move all
914 the IMG tags in a SPAN element with class "images". This makes it easier to lay
915 out the result using CSS.
916  
917 =cut
918  
919 sub process {
920 19 ▶ my $text = shift;
Subroutine process: called CC=2 SCAR=6.9
Pod documented
921 19 my $images = shift;
922 19 ▶ if ($images) {
Branch: 1 branch
BranchTrueFalseTotal
if ($images) { } 10 9 19
923 10 $text =~ s/(<img[^>]+?>)/<span class="images">$1<\/span>/g;
924 } else {
925 9 $text =~ s/(<img[^>]+?>)//g;
926 }
927 # fix whilespace at the end of spans
928 19 $text =~ s/\s+<\/span>/<\/span> /g;
929 # strip empty paragraphs
930 19 $text =~ s/<p>\s*<\/p>//g;
931 19 $text =~ s/<p>\s*<p>/<p>/g;
932 # strip other empty elements
933 19 $text =~ s/<em><\/em>//g;
934 19 return $text;
935 }
936  
937 =item resolve_appends
938  
939 This removes text marked for appending and adds it at the end of a hex
940 description. This modifies the third parameter, C<$descriptions>.
941  
942 =cut
943  
944 sub resolve_appends {
945 1 ▶ my $map_data = shift;
Subroutine resolve_appends: called CC=3 SCAR=12.0
Pod documented
946 1 my $table_data = shift;
947 1 my $descriptions = shift;
948 1 my $redirects = shift;
949 1 my $text;
950 1 for my $coord (keys %$descriptions) {
951 3 ▶ while ($descriptions->{$coord} =~ s/「append ([^][」]*)」/$text = $1; ""/e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descriptions->{$coord} =~ s/\x{ff62}append ([^][\x{ff63}]*)\x{ff63}/$text = $1; "";/eu) 0 3 3
952 0 $descriptions->{$coord} .= ' ' . $text;
953 }
954 }
955 }
956  
957 =item resolve_nearby
958  
959 We have nearly everything resolved except for references starting with the words
960 "closest" and "nearby" because these require all of the other data to be
961 present. This modifies the third parameter, C<$descriptions>.
962  
963 =cut
964  
965 sub resolve_nearby {
966 1 ▶ my $map_data = shift;
Subroutine resolve_nearby: called CC=4 SCAR=14.9
Pod documented
967 1 my $table_data = shift;
968 1 my $descriptions = shift;
969 1 my $redirects = shift;
970 1 for my $coord (keys %$descriptions) {
971 $descriptions->{$coord} =~
972 0 ▶ s/「(nearby|closest) ([^][」]*)」/closest($map_data,$table_data,$coord,$2,$1,$redirects) or '…'/ge
Branch: 1 branch
BranchTrueFalseTotal
closest($map_data, $table_data, $coord, $2, $1, $redirects) or "\x{2026}" 0 0 0
973 3 for 1 .. 2; # two levels deep of 「nearby ...」
974 3 $descriptions->{$coord} =~ s!( \(<a href="#desc\d+">\d+</a>\))</em>!</em>$1!g; # fixup
975 }
976 }
977  
978 =item closest
979  
980 This picks the closest instance of whatever we're looking for. If the type
981 argument is "nearby", the same coordinates are excluded.
982  
983 =cut
984  
985 sub closest {
986 0 ▶ my $map_data = shift;
Subroutine closest: not called CC=4 SCAR=30.0
Pod documented
987 0 my $table_data = shift;
988 0 my $coordinates = shift;
989 0 my $key = shift;
990 0 my $type = shift;
991 0 my $redirects = shift;
992 0 my @coordinates = grep { $_ ne 'global' } keys %{$globals->{$key}};
993 0 ▶ @coordinates = grep { $_ ne $coordinates } @coordinates if $type eq "nearby";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "nearby" 0 0 0
994 0 ▶ if (not @coordinates) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@coordinates) 0 0 0
995 0 $log->info("Did not find any hex with $key ($coordinates)");
996 0 return "…";
997 }
998 0 ▶ if ($coordinates !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $coordinates =~ /^\d+$/u) { } 0 0 0
999 # if $coordinates is "TOP" or "END" or something like that, we cannot get
1000 # the closest one and we need to return a random one
1001 0 my $random = one(@coordinates);
1002 0 return $globals->{$key}->{$random}
1003 . qq{ (<a href="#desc$random">$random</a>)}; # see resolve_later!
1004 } else {
1005 0 @coordinates = sort { distance($coordinates, $a) <=> distance($coordinates, $b) } @coordinates;
1006 # the first one is the closest
1007 0 return $globals->{$key}->{$coordinates[0]}
1008 . qq{ (<a href="#desc$coordinates[0]">$coordinates[0]</a>)}; # see resolve_later!
1009 }
1010 }
1011  
1012 =item distance
1013  
1014 Returns the distance between two hexes. Either provide two coordinates (strings
1015 in the form "0101", "0102") or four numbers (1, 1, 1, 2).
1016  
1017 =cut
1018  
1019 sub distance {
1020 0 ▶ my ($x1, $y1, $x2, $y2) = @_;
Subroutine distance: not called CC=5 SCAR=34.0
Pod documented
1021 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
1022 0 ($x1, $y1, $x2, $y2) = map { xy($_) } @_;
1023 }
1024 # transform the coordinate system into a decent system with one axis tilted by
1025 # 60°
1026 0 $y1 = $y1 - POSIX::ceil($x1/2);
1027 0 $y2 = $y2 - POSIX::ceil($x2/2);
1028 0 ▶ if ($x1 > $x2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 0 0
1029 # only consider moves from left to right and transpose start and
1030 # end point to make it so
1031 0 my ($t1, $t2) = ($x1, $y1);
1032 0 ($x1, $y1) = ($x2, $y2);
1033 0 ($x2, $y2) = ($t1, $t2);
1034 }
1035 0 ▶ if ($y2>=$y1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 >= $y1) { } 0 0 0
1036 # if it the move has a downwards component add Δx and Δy
1037 0 return $x2-$x1 + $y2-$y1;
1038 } else {
1039 # else just take the larger of Δx and Δy
1040 0 ▶ return $x2-$x1 > $y1-$y2 ? $x2-$x1 : $y1-$y2;
Branch: 1 branch
BranchTrueFalseTotal
$x2 - $x1 > $y1 - $y2 ? : 0 0 0
1041 }
1042 }
1043  
1044 =item resolve_other
1045  
1046 This is a second phase. We have nearly everything resolved except for references
1047 starting with the word "other" because these require all of the other data to
1048 be present. This modifies the third parameter, C<$descriptions>.
1049  
1050 =cut
1051  
1052 sub resolve_other {
1053 1 ▶ my $map_data = shift;
Subroutine resolve_other: called CC=3 SCAR=11.8
Pod documented
1054 1 my $table_data = shift;
1055 1 my $descriptions = shift;
1056 1 my $redirects = shift;
1057 1 for my $coord (keys %$descriptions) {
1058 3 $descriptions->{$coord} =~
1059 0 ▶ s/「other ([^][」]*)」/some_other($map_data,$table_data,$coord,$1, $redirects) or '…'/ge;
Branch: 1 branch
BranchTrueFalseTotal
some_other($map_data, $table_data, $coord, $1, $redirects) or "\x{2026}" 0 0 0
1060 3 $descriptions->{$coord} =~ s!( \(<a href="#desc\d+">\d+</a>\))</em>!</em>$1!g; # fixup
1061 }
1062 }
1063  
1064 =item some_other
1065  
1066 This picks some other instance of whatever we're looking for, irrespective of distance.
1067  
1068 =cut
1069  
1070 sub some_other {
1071 0 ▶ my $map_data = shift;
Subroutine some_other: not called CC=2 SCAR=17.9
Pod documented
1072 0 my $table_data = shift;
1073 0 my $coordinates = shift;
1074 0 my $key = shift;
1075 0 my $redirects = shift;
1076 # make sure we don't pick the same location!
1077 0 my @coordinates = grep !/$coordinates/, keys %{$globals->{$key}};
1078 0 ▶ if (not @coordinates) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@coordinates) 0 0 0
1079 0 $log->info("Did not find any other hex with $key");
1080 0 return "…";
1081 }
1082 # just pick a random one
1083 0 my $other = one(@coordinates);
1084 0 return $globals->{$key}->{$other}
1085 . qq{ (<a href="#desc$other">$other</a>)}; # see resolve_later!
1086 }
1087  
1088  
1089 =item resolve_later
1090  
1091 This is a second phase. We have nearly everything resolved except for references
1092 starting with the word "later" because these require all of the other data to be
1093 present. This modifies the third parameter, C<$descriptions>. Use this for
1094 recursive lookup involving "nearby" and "other".
1095  
1096 This also takes care of hex references introduced by "nearby" and "other". This
1097 is also why we need to take extra care to call C<quotemeta> on various strings
1098 we want to search and replace: these hex references contain parenthesis!
1099  
1100 =cut
1101  
1102 sub resolve_later {
1103 1 ▶ my $map_data = shift;
Subroutine resolve_later: called CC=7 SCAR=33.2
Pod documented
1104 1 my $table_data = shift;
1105 1 my $descriptions = shift;
1106 1 my $redirects = shift;
1107 1 for my $coord (keys %$descriptions) {
1108 3 ▶ while ($descriptions->{$coord} =~ /「later ([^][」]*)」/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descriptions->{$coord} =~ /\x{ff62}later ([^][\x{ff63}]*)\x{ff63}/u) 0 3 3
1109 0 my $words = $1;
1110 0 my ($ref) = $words =~ m!( \(<a href=".*">.*</a>\))!;
1111 0 ▶ $ref //= ''; # but why should it ever be empty?
Condition0%0/2
$ref //= ""
l!l
00
MC/DC0%0/1
$ref //= ""
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
1112 0 my $key = $words;
1113 0 my $re = quotemeta($ref);
1114 0 ▶ $key =~ s/$re// if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 0 0 0
1115 0 $re = quotemeta($words);
1116 0 my $result = $descriptions->{$coord} =~
1117 0 ▶ s/「later $re」/describe($map_data,$table_data,1,$coord,[$key], $redirects) . $ref or '…'/ge;
Branch: 1 branch
BranchTrueFalseTotal
describe($map_data, $table_data, 1, $coord, [$key], $redirects) . $ref or "\x{2026}" 0 0 0
1118 0 ▶ if (not $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
1119 0 $log->error("Could not resolve later reference in '$words'");
1120 0 last; # avoid infinite loops!
1121 }
1122 }
1123 }
1124 }
1125  
1126 =item describe_map
1127  
1128 This is one of the top entry points: it simply calls C<describe> for every hex
1129 in C<$map_data> and calls C<process> on the result. All the texts are collected
1130 into a new hash where the hex coordinates are the key and the generated
1131 description is the value.
1132  
1133 =cut
1134  
1135 sub describe_map {
1136 1 ▶ my $map_data = shift;
Subroutine describe_map: called CC=5 SCAR=16.2
Pod documented
1137 1 my $table_data = shift;
1138 1 my $redirects = shift;
1139 1 my %descriptions;
1140 # first, add special rule for TOP and END keys which the description template knows
1141 1 for my $coords (qw(TOP END)) {
1142 # with redirects means we keep images
1143 2 my $description =
1144 process(describe($map_data, $table_data, 1,
1145 $coords, [$coords], $redirects), $redirects);
1146 # only set the TOP and END key if there is a description
1147 2 ▶ $descriptions{$coords} = $description if $description;
Branch: 1 branch
BranchTrueFalseTotal
if $description 2 0 2
1148 }
1149 # shuffle sort the coordinates so that it is reproducibly random
1150 1 for my $coord (shuffle sort keys %$map_data) {
1151 # with redirects means we keep images
1152 my $description =
1153 process(describe($map_data, $table_data, 1,
1154 1 $coord, $map_data->{$coord}, $redirects), $redirects);
1155 # only set the description if there is one (empty hexes are not listed)
1156 1 ▶ $descriptions{$coord} = $description if $description;
Branch: 1 branch
BranchTrueFalseTotal
if $description 1 0 1
1157 }
1158 1 resolve_nearby($map_data, $table_data, \%descriptions, $redirects);
1159 1 resolve_other($map_data, $table_data, \%descriptions, $redirects);
1160 1 resolve_later($map_data, $table_data, \%descriptions, $redirects);
1161 # as append might include the items above, it must come last
1162 1 resolve_appends($map_data, $table_data, \%descriptions, $redirects);
1163 1 return \%descriptions;
1164 }
1165  
1166 =item add_labels
1167  
1168 This function is used after generating the descriptions to add the new names of
1169 rivers and trails to the existing map.
1170  
1171 =cut
1172  
1173 sub add_labels {
1174 0 ▶ my $map = shift;
Subroutine add_labels: not called CC=1 SCAR=6.9
Pod documented
1175 0 $map =~ s/$line_re/get_label($1,$2)/ge;
1176 0 return $map;
1177 }
1178  
1179 =item get_label
1180  
1181 This function returns the name of a line.
1182  
1183 =cut
1184  
1185 sub get_label {
1186 0 ▶ my $map_line = shift;
Subroutine get_label: not called CC=5 SCAR=34.0
Pod documented
1187 0 my $type = shift;
1188 0 my @hexes = split(/-/, $map_line);
1189 LINE:
1190 0 for my $line (@{$extra->{$hexes[0]}}) {
1191 0 ▶ next unless $line->{type} eq $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $line->{"type"} eq $type 0 0 0
1192 0 for my $hex (@hexes) {
1193 0 my @line = @{$line->{line}};
1194 0 ▶ next LINE unless grep(/$hex/, @line);
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$hex/u, @line 0 0 0
1195 }
1196 0 my $label = $line->{name};
1197 0 return qq{$map_line $type "$label"};
1198 }
1199 0 return qq{$map_line $type};
1200 }
1201  
1202 =item xy
1203  
1204 This is a helper function to turn "0101" into ("01", "01") which is equivalent
1205 to (1, 1).
1206  
1207 =cut
1208  
1209 sub xy {
1210 0 ▶ my $coordinates = shift;
Subroutine xy: not called CC=1 SCAR=6.9
Pod documented
1211 0 return (substr($coordinates, 0, 2), substr($coordinates, 2));
1212 }
1213  
1214 =item coordinates
1215  
1216 This is a helper function to turn (1, 1) back into "0101".
1217  
1218 =cut
1219  
1220 sub coordinates {
1221 0 ▶ my ($x, $y) = @_;
Subroutine coordinates: not called CC=1 SCAR=6.9
Pod documented
1222 0 return sprintf("%02d%02d", $x, $y);
1223 }
1224  
1225 =item neighbour
1226  
1227 This is a helper function that takes the coordinates of a hex, a reference like
1228 [1,1] or regular coordinates like "0101", and a direction from 0 to 5, and
1229 returns the coordinates of the neighbouring hex in that direction.
1230  
1231 =cut
1232  
1233 6 my $delta = [[[-1, 0], [ 0, -1], [+1, 0], [+1, +1], [ 0, +1], [-1, +1]], # x is even
1234 [[-1, -1], [ 0, -1], [+1, -1], [+1, 0], [ 0, +1], [-1, 0]]]; # x is odd
1235  
1236 sub neighbour {
1237 # $hex is [x,y] or "0101" and $i is a number 0 .. 5
1238 0 ▶ my ($hex, $i) = @_;
Subroutine neighbour: not called CC=2 SCAR=17.9
Pod documented
1239 0 ▶ $hex = [xy($hex)] unless ref $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hex 0 0 0
1240 # return is a string like "0102"
1241 0 return coordinates(
1242 $hex->[0] + $delta->[$hex->[0] % 2]->[$i]->[0],
1243 $hex->[1] + $delta->[$hex->[0] % 2]->[$i]->[1]);
1244 }
1245  
1246 =item neighbours
1247  
1248 This is a helper function that takes map_data and the coordinates of a hex, a
1249 reference like [1,1] or regular coordinates like "0101", and returns a list of
1250 existing neighbours, or the string "[…]". This makes a difference at the edge of
1251 the map.
1252  
1253 =cut
1254  
1255 sub neighbours {
1256 0 ▶ my $map_data = shift;
Subroutine neighbours: not called CC=5 SCAR=34.0
Pod documented
1257 0 my $hex = shift;
1258 0 my @neighbours;
1259 0 ▶ $hex = [xy($hex)] unless ref $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hex 0 0 0
1260 0 for my $i (0 .. 5) {
1261 0 my $neighbour = neighbour($hex, $i);
1262 # $log->debug($neighbour);
1263 0 ▶ push(@neighbours, $neighbour) if $map_data->{$neighbour};
Branch: 1 branch
BranchTrueFalseTotal
if $map_data->{$neighbour} 0 0 0
1264 }
1265 0 ▶ return "..." unless @neighbours;
Branch: 1 branch
BranchTrueFalseTotal
unless @neighbours 0 0 0
1266 0 return @neighbours;
1267 }
1268  
1269 =item one
1270  
1271 This is a helper function that picks a random element from a list. This works
1272 both for actual lists and for references to lists.
1273  
1274 =cut
1275  
1276 sub one {
1277 0 ▶ my @arr = @_;
Subroutine one: not called CC=3 SCAR=24.8
Pod documented
1278 0 ▶ @arr = @{$arr[0]} if @arr == 1 and ref $arr[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if @arr == 1 and ref $arr[0] eq "ARRAY" 0 0 0
Condition0%0/3
@arr == 1 and ref $arr[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@arr == 1 and ref $arr[0] eq "ARRAY"
@arr == 1 ref $arr[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @arr == 1B= ref $arr[0] eq "ARRAY"
ABresult
0X0
100
111
1279 0 return $arr[int(rand(scalar @arr))];
1280 }
1281  
1282 =item one_step_to
1283  
1284 Given a hex to start from, check all directions and figure out which neighbour
1285 is closer to your destination. Return the coordinates of this neighbour.
1286  
1287 =cut
1288  
1289 sub one_step_to {
1290 0 ▶ my $from = shift;
Subroutine one_step_to: not called CC=4 SCAR=30.0
Pod documented
1291 0 my $to = shift;
1292 0 my ($min, $best);
1293 0 for my $i (0 .. 5) {
1294 # make a new guess
1295 0 my ($x, $y) = ($from->[0] + $delta->[$from->[0] % 2]->[$i]->[0],
1296 $from->[1] + $delta->[$from->[0] % 2]->[$i]->[1]);
1297 0 my $d = ($to->[0] - $x) * ($to->[0] - $x)
1298 + ($to->[1] - $y) * ($to->[1] - $y);
1299 0 ▶ if (!defined($min) || $d < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $min or $d < $min) 0 0 0
Condition0%0/3
not defined $min or $d < $min
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $min or $d < $min
not defined $min $d < $min
Truth table0%0/3
not(A) or B
A= not defined $minB= $d < $min
ABresult
1X1
011
000
1300 0 $min = $d;
1301 0 $best = [$x, $y];
1302 }
1303 }
1304 0 return $best;
1305 }
1306  
1307 =item compute_missing_points
1308  
1309 Return a list of coordinates in string form. Thus, given a list like ("0302",
1310 "0101") it will return ("0302", "0201", "0101").
1311  
1312 =cut
1313  
1314 sub compute_missing_points {
1315 0 ▶ my @result = ($_[0]); # "0101" not [01,02]
Subroutine compute_missing_points: not called CC=4 SCAR=30.0
Pod documented
1316 0 my @points = map { [xy($_)] } @_;
1317 # $log->debug("Line: " . join(", ", map { coordinates(@$_) } @points));
1318 0 my $from = shift(@points);
1319 0 ▶ while (@points) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points) 0 0 0
1320 # $log->debug("Going from " . coordinates(@$from) . " to " . coordinates(@{$points[0]}));
1321 0 $from = one_step_to($from, $points[0]);
1322 0 ▶ shift(@points) if $from->[0] == $points[0]->[0] and $from->[1] == $points[0]->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $from->[0] == $points[0][0] and $from->[1] == $points[0][1] 0 0 0
Condition0%0/3
$from->[0] == $points[0][0] and $from->[1] == $points[0][1]
!ll&&!rl&&r
000
MC/DC0%0/2
$from->[0] == $points[0][0] and $from->[1] == $points[0][1]
$from->[0] == $points[0][0] $from->[1] == $points[0][1]
Truth table0%0/3
A and B
A= $from->[0] == $points[0][0]B= $from->[1] == $points[0][1]
ABresult
0X0
100
111
1323 0 push(@result, coordinates(@$from));
1324 }
1325 0 return @result;
1326 }
1327  
1328 =item same_direction
1329  
1330 Given two linear structures and a point of contact, return 1 if the these
1331 objects go in the same direction on way or the other.
1332  
1333 =cut
1334  
1335 sub same_direction {
1336 0 ▶ my $coord = shift;
Subroutine same_direction: not called CC=12 SCAR=50.5
Pod documented
1337 0 my $line1 = shift;
1338 0 my $line2 = shift;
1339 # $log->debug("same_direction: $coord, @$line1 and @$line2");
1340 # this code assumes that a line starts with $type at index 0
1341 0 my $j;
1342 0 for my $i (1 .. $#$line1) {
1343 0 ▶ if ($line1->[$i] eq $coord) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line1->[$i] eq $coord) 0 0 0
1344 0 $j = $i;
1345 0 last;
1346 }
1347 }
1348 # $log->debug("same_direction: $coord has index $j in @$line1");
1349 0 for my $i1 ($j - 1, $j + 1) {
1350 0 ▶ next if $i1 == 0 or $i1 > $#$line1;
Branch: 1 branch
BranchTrueFalseTotal
if $i1 == 0 or $i1 > $line1->$#* 0 0 0
Condition0%0/3
$i1 == 0 or $i1 > $line1->$#*
l!l&&r!l&&!r
000
MC/DC0%0/2
$i1 == 0 or $i1 > $line1->$#*
$i1 == 0 $i1 > $line1->$#*
Truth table0%0/3
A or B
A= $i1 == 0B= $i1 > $line1->$#*
ABresult
1X1
011
000
1351 0 my $next = $line1->[$i1];
1352 0 for my $i2 (1 .. $#$line2) {
1353 0 ▶ if ($line2->[$i2] eq $coord) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line2->[$i2] eq $coord) 0 0 0
1354 0 ▶ if ($line2->[$i2-1] and $next eq $line2->[$i2-1]
Branch: 1 branch
BranchTrueFalseTotal
if ($$line2[$i2 - 1] and $next eq $$line2[$i2 - 1] or $$line2[$i2 + 1] and $next eq $$line2[$i2 + 1]) 0 0 0
Condition0%0/9
$$line2[$i2 - 1] and $next eq $$line2[$i2 - 1] or $$line2[$i2 + 1] and $next eq $$line2[$i2 + 1]
l!l&&r!l&&!r
000
$$line2[$i2 - 1] and $next eq $$line2[$i2 - 1]
!ll&&!rl&&r
000
$$line2[$i2 + 1] and $next eq $$line2[$i2 + 1]
!ll&&!rl&&r
000
MC/DC0%0/4
$$line2[$i2 - 1] and $next eq $$line2[$i2 - 1] or $$line2[$i2 + 1] and $next eq $$line2[$i2 + 1]
$$line2[$i2 - 1] $next eq $$line2[$i2 - 1] $$line2[$i2 + 1] $next eq $$line2[$i2 + 1]
Truth table0%0/7
A and B or C and D
A= $$line2[$i2 - 1]B= $next eq $$line2[$i2 - 1]C= $$line2[$i2 + 1]D= $next eq $$line2[$i2 + 1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1355 or $line2->[$i2+1] and $next eq $line2->[$i2+1]) {
1356 # $log->debug("same direction at $coord: @$line1 and @$line2");
1357 0 return 1;
1358 }
1359 }
1360 }
1361 }
1362 0 return 0;
1363 }
1364  
1365 =item spread_name
1366  
1367 This function is used to spread a name along terrain features.
1368  
1369 =cut
1370  
1371 sub spread_name {
1372 0 ▶ my $map_data = shift;
Subroutine spread_name: not called CC=7 SCAR=40.3
Pod documented
1373 0 my $coordinates = shift;
1374 0 my $word = shift; # "name for white big mountain"
1375 0 my $key = shift; # "white"
1376 0 my @keys = split(/\//, $key); # ("white")
1377 0 my $name = shift; # "Vesuv"
1378 0 my %seen = ($coordinates => 1);
1379 0 $globals->{$key}->{$coordinates} = $name;
1380 # $log->debug("$word: $coordinates = $name");
1381 0 my @queue = map { neighbour($coordinates, $_) } 0..5;
1382 0 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 0 0 0
1383 # $log->debug("Working on the first item of @queue");
1384 0 my $coord = shift(@queue);
1385 0 ▶ next if $seen{$coord} or not $map_data->{$coord};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$coord} or not $map_data->{$coord} 0 0 0
Condition0%0/3
$seen{$coord} or not $map_data->{$coord}
l!l&&r!l&&!r
000
MC/DC0%0/2
$seen{$coord} or not $map_data->{$coord}
$seen{$coord} not $map_data->{$coord}
Truth table0%0/3
A or not(B)
A= $seen{$coord}B= not $map_data->{$coord}
ABresult
1X1
011
000
1386 0 $seen{$coord} = 1;
1387 0 ▶ if (intersect(@keys, @{$map_data->{$coord}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&intersect(\@keys, \@{$$map_data{$coord};})) 0 0 0
1388 $log->error("$word for $coord is already something else")
1389 0 ▶ if $names{"$word for $coord"};
Branch: 1 branch
BranchTrueFalseTotal
if $names{"$word for $coord"} 0 0 0
1390 0 $names{"$word: $coord"} = $name; # "name for white big mountain: 0102"
1391 # $log->debug("$coord: $name for @keys");
1392 0 $globals->{$_}->{$coord} = $name for @keys;
1393 # $log->debug("$word: $coord = $name");
1394 0 push(@queue, map { neighbour($coord, $_) } 0..5);
1395 }
1396 }
1397 }
1398  
1399 =item markdown
1400  
1401 This allows us to generate Markdown output.
1402  
1403 =cut
1404  
1405 sub markdown {
1406 5 ▶ my $descriptions = shift;
Subroutine markdown: called CC=2 SCAR=7.0
Pod documented
1407 5 ▶ my $separator = shift || "\n\n---\n\n";
Condition100%2/2
shift() || "\n\n---\n\n"
l!l
14
MC/DC100%1/1
shift() || "\n\n---\n\n"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1408 my @paragraphs = map {
1409 # remove inline images
1410 5 s!<img[^>]*>!!g;
1411 # empty spans left after img has been removed
1412 9 s!<span[^>]*>\s*</span>!!g;
1413 # remaining spans result in Japanese brackets around their text
1414 9 s!<span[^>]*>\s*!「!g;
1415 9 s!\s*</span>!」!g;
1416 # emphasis
1417 9 s!</?(strong|b)>!**!g;
1418 9 s!</?(em|i)>!*!g;
1419 9 s!</?u>!_!g;
1420 # remove links but leave their text
1421 9 s!</?a\b[^>]*>!!g;
1422 # closing paragraph tags are optional
1423 9 s!</p>!!g;
1424 # paragraph breaks
1425 9 s!<p.*?>!\n\n!g;
1426 # blockquotes
1427 9 s!<blockquote>(.*?)</blockquote>!local $_ = $1; s/^/\n> /g; $_!gem;
1428 # unreplaced references (nearby, other, later)
1429 9 s!(.*?)!$1!g;
1430 # return what's left
1431 9 markdown_lists($_);
1432 } @$descriptions;
1433 5 return join($separator, @paragraphs);
1434 }
1435  
1436 sub markdown_lists {
1437 9 ▶ $_ = shift;
Subroutine markdown_lists: called CC=9 SCAR=33.0
Pod undocumented
1438 9 my ($str, @list);
1439 9 for (split(/(<.*?>)/)) {
1440 128 ▶ if (/^<ol.*>$/) { unshift(@list, '1.'); $str .= "\n" }
Branch: 5 branches
BranchTrueFalseTotal
if (/^<ol.*>$/u) { } 0 128 128
elsif (/^<ul.*>$/u) { } 0 128 128
elsif (/^<li>$/u) { } 0 128 128
elsif (m[^</(ol|ul)>$]u) { } 0 128 128
elsif (m[^</li>$]u) { } 0 128 128
1441 0 elsif (/^<ul.*>$/) { unshift(@list, '*'); $str .= "\n" }
1442 elsif (/^<li>$/) {
1443 $str .= (@list > 1
1444 # all the list markers except for the current one
1445 0 ▶ ? (" " x (sum map { length($_) + 1 } @list[1..$#list]))
Branch: 1 branch
BranchTrueFalseTotal
@list > 1 ? : 0 0 0
1446 : "")
1447 . $list[0] . " " }
1448 0 elsif (/^<\/(ol|ul)>$/) { shift(@list) }
1449 0 ▶ elsif (/^<\/li>$/) { $str .= "\n" unless $str =~ /\n$/ }
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\n$/u 0 0 0
1450 128 else { $str .= $_ }
1451 }
1452 9 return $str;
1453 }
1454  
1455 6 1;
↑ Summary blib/lib/Game/HexDescribe.pm »