Skip to content

Commit

Permalink
Addresses problems with TOC generation
Browse files Browse the repository at this point in the history
Not the best way, probably. At the end of the day, TOC and anchors
should be generated using the same code, but this module is kind of
convoluted, so I'm doing this manually. This should close #41 but also
help with Raku/doc#2245
  • Loading branch information
JJ committed Aug 10, 2018
1 parent 66d4e34 commit 1200691
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
41 changes: 24 additions & 17 deletions lib/Pod/To/HTML.pm
Expand Up @@ -61,7 +61,10 @@ sub unescape_html(Str $str) returns Str {
}

sub escape_id ($id) {
$id.trim.subst(/\s+/, '_', :g).subst('"', '"', :g);
$id.trim.subst(/\s+/, '_', :g)
.subst('"', '"', :g)
.subst(' ', '_', :g)
.subst(''', "'", :g);
}

multi visit(Nil, |a) {
Expand Down Expand Up @@ -209,36 +212,40 @@ sub do-metadata returns Str {
#| Turns accumulated headings into a nested-C«<ol>» table of contents
sub do-toc($pod) returns Str {
my @levels is default(0) = 0;
my proto sub find-headings($node, :$inside-heading){*}
multi sub find-headings(Str $s is raw, :$inside-heading){
$inside-heading ?? $s.trim.&escape_html !! ''
}
multi sub find-headings(Pod::FormattingCode $node is raw where *.type eq 'C', :$inside-heading){
my $html = $node.contents.map(*.&find-headings(:$inside-heading));
$inside-heading ?? qq[<code class="pod-code-inline">{$html}</code>] !! ''
}
multi sub find-headings(Pod::Heading $node is raw, :$inside-heading){
@levels.splice($node.level) if $node.level < +@levels;
@levels[$node.level-1]++;
my @levels is default(0) = 0;
my proto sub find-headings($node, :$inside-heading){*}
multi sub find-headings(Str $s is raw, :$inside-heading){
$inside-heading ?? $s.trim.&escape_html !! ''
}
multi sub find-headings(Pod::FormattingCode $node is raw where *.type eq 'C', :$inside-heading){
my $html = $node.contents.map(*.&find-headings(:$inside-heading));
$inside-heading ?? qq[<code class="pod-code-inline">{$html}</code>] !! ''
}
multi sub find-headings(Pod::Heading $node is raw, :$inside-heading) {
@levels.splice($node.level) if $node.level < +@levels;
@levels[$node.level-1]++;
my $level-hierarchy = @levels.join('.'); # e.g. §4.2.12
my $text = $node.contents.map(*.&find-headings(inside-heading => True));
my $link = escape_id(node2text($node.contents));
qq[<tr class="toc-level-{$node.level}"><td class="toc-number">{$level-hierarchy}</td><td class="toc-text"><a href="#$link">{$text}</a></td></tr>\n];
}
multi sub find-headings(Positional \list, :$inside-heading){
list.map(*.&find-headings(:$inside-heading))
}
multi sub find-headings(Pod::Block $node is raw, :$inside-heading){
$node.contents.map(*.&find-headings(:$inside-heading))
}
multi sub find-headings(Pod::Config $node, :$inside-heading){
''
}
multi sub find-headings(Pod::Raw $node is raw, :$inside-heading){
$node.contents.map(*.&find-headings(:$inside-heading))
}
Expand Down
18 changes: 18 additions & 0 deletions t/11-issue-41.t
@@ -0,0 +1,18 @@
use Test;
use Pod::To::HTML;
plan 2;

=begin pod
=head2 Rendering Perl 6 with no-break space
Nothing to see here
=head2 What's wrong with this rendering?
Nothing to see here either
=end pod

my $r = pod2html $=pod;

ok $r ~~ m/\#What\'s_wrong/;
ok $r ~~ m/\#Rendering_Perl_6/;

0 comments on commit 1200691

Please sign in to comment.