Skip to content

Commit ba47ea5

Browse files
committed
Restructure find-definition to allow for multiple definitions
1 parent c473a1b commit ba47ea5

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

htmlify.p6

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -295,42 +295,45 @@ sub find-definitions (:$pod, :$origin, :$dr, :$min-level = -1) {
295295
my int $i = 0;
296296
my int $len = +@c;
297297
while $i < $len {
298+
NEXT {$i = $i + 1}
298299
my $c := @c[$i];
299-
if $c ~~ Pod::Heading {
300-
return $i if $c.level <= $min-level;
301-
302-
# Is this new header a definition?
303-
# If so, begin processing it.
304-
# If not, skip to the next heading.
305-
my @header := $c.contents[0].contents;
306-
my @words;
307-
given @header {
308-
when :("", Pod::FormattingCode $, "") {
309-
proceed unless .[1].type eq "X";
310-
@words = .[1].meta[0][0,1]; # XXX Multiple definitions
311-
}
312-
when :(Str $ where /^The \s \S+ \s \w+$/) {
313-
# The Foo Infix
314-
@words = .[0].words[2,1];
315-
}
316-
when :(Str $ where {m/^(\w+) \s (\S+)$/}) {
317-
# Infix Foo
318-
@words = .[0].words[0,1];
319-
}
320-
when :("The ", Pod::FormattingCode $, Str $ where /^\s (\w+)$/) {
321-
# The C<Foo> infix
322-
@words = .[2].words[0], .[1].contents[0];
323-
}
324-
when :(Str $ where /^(\w+) \s$/, Pod::FormattingCode $, "") {
325-
# infix C<Foo>
326-
@words = .[0].words[0], .[1].contents[0];
327-
}
328-
default { $i = $i + 1; next }
300+
next unless $c ~~ Pod::Heading;
301+
return $i if $c.level <= $min-level;
302+
303+
# Is this new header a definition?
304+
# If so, begin processing it.
305+
# If not, skip to the next heading.
306+
my @header := $c.contents[0].contents;
307+
my @definitions; # [subkind, name]
308+
given @header {
309+
when :("", Pod::FormattingCode $, "") {
310+
proceed unless .[1].type eq "X";
311+
@definitions = .[1].meta[];
312+
}
313+
when :(Str $ where /^The \s \S+ \s \w+$/) {
314+
# The Foo Infix
315+
@definitions = [.[0].words[2,1]];
316+
}
317+
when :(Str $ where {m/^(\w+) \s (\S+)$/}) {
318+
# Infix Foo
319+
@definitions = [.[0].words[0,1]];
320+
}
321+
when :("The ", Pod::FormattingCode $, Str $ where /^\s (\w+)$/) {
322+
# The C<Foo> infix
323+
@definitions = [.[2].words[0], .[1].contents[0]];
329324
}
325+
when :(Str $ where /^(\w+) \s$/, Pod::FormattingCode $, "") {
326+
# infix C<Foo>
327+
@definitions = [.[0].words[0], .[1].contents[0]];
328+
}
329+
default { next }
330+
}
330331

331-
my ($subkinds, $name) = @words;
332+
my int $new-i = $i;
333+
for @definitions -> [$sk, $name] {
334+
my $subkinds = $sk.lc;
332335
my %attr;
333-
given $subkinds.lc {
336+
given $subkinds {
334337
when / ^ [in | pre | post | circum | postcircum ] fix | listop / {
335338
%attr = :kind<routine>,
336339
:categories<operator>,
@@ -349,9 +352,10 @@ sub find-definitions (:$pod, :$origin, :$dr, :$min-level = -1) {
349352
:categories($subkinds),
350353
}
351354
default {
352-
$i = $i + 1 and next
355+
last
353356
}
354357
}
358+
355359
# We made it this far, so it's a valid definition
356360
my $created = $dr.add-new(
357361
:$origin,
@@ -364,14 +368,19 @@ sub find-definitions (:$pod, :$origin, :$dr, :$min-level = -1) {
364368

365369
# Preform sub-parse, checking for definitions elsewhere in the pod
366370
# And updating $i to be after the places we've already searched
367-
my int $new-i = $i + find-definitions :pod(@c[$i+1..*]), :origin($created), :$dr, :min-level(@c[$i].level);
368-
369-
@c[$i].contents[0] = pod-link "$subkinds $name",
370-
$created.url ~ "#$origin.human-kind() $origin.name()".subst(:g, /\s+/, '_');
371-
372-
my $chunk = $created.pod.push: pod-lower-headings(@c[$i..$new-i], :to(%attr<kind> eq 'type' ?? 0 !! 2));
371+
once {
372+
$new-i = $i + find-definitions
373+
:pod(@c[$i+1..*]), :origin($created), :$dr, :min-level(@c[$i].level);
374+
}
373375

374-
$i = $new-i;
376+
my $new-head = Pod::Heading.new(
377+
:level(@c[$i].level),
378+
:contents[pod-link "$subkinds $name",
379+
$created.url ~ "#$origin.human-kind() $origin.name()".subst(:g, /\s+/, '_')
380+
]
381+
);
382+
my @orig-chunk = $new-head, @c[$i ^.. $new-i];
383+
my $chunk = $created.pod.push: pod-lower-headings(@orig-chunk, :to(%attr<kind> eq 'type' ?? 0 !! 2));
375384

376385
if $subkinds eq 'routine' {
377386
# Determine proper subkinds
@@ -394,7 +403,7 @@ sub find-definitions (:$pod, :$origin, :$dr, :$min-level = -1) {
394403
);
395404
}
396405
}
397-
$i = $i + 1;
406+
$i = $new-i + 1;
398407
}
399408
return $i;
400409
}

0 commit comments

Comments
 (0)