Navigation Menu

Skip to content

Commit

Permalink
idify can generate unique ids
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 26, 2014
1 parent bc03be9 commit 5fca5d3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
7 changes: 3 additions & 4 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -58,17 +58,16 @@ sub _html {
# Rewrite headers
my $toc = Mojo::URL->new->fragment('toc');
my (%anchors, @parts);
my $pod = Pod::Simple::XHTML->new;
for my $e ($dom->find('h1, h2, h3')->each) {

# Anchor
my $anchor = my $name = $e->{id};
my $i = 1;
$anchor = $name . $i++ while $anchors{$anchor}++;
my $text = $e->all_text;
my $anchor = $pod->idify($e->{id} // $text);

# Rewrite
push @parts, [] if $e->type eq 'h1' || !@parts;
my $link = Mojo::URL->new->fragment($anchor);
my $text = $e->all_text;
push @{$parts[-1]}, $text, $link;
my $permalink = $self->link_to('#' => $link, class => 'permalink');
$e->content($permalink . $self->link_to($text => $toc, id => $anchor));
Expand Down
9 changes: 4 additions & 5 deletions t/mojolicious/pod_renderer_lite_app.t
Expand Up @@ -30,18 +30,17 @@ get '/empty' => {inline => '', handler => 'pod'};
my $t = Test::Mojo->new;

# Simple POD template
$t->get_ok('/')->status_is(200)
->content_like(qr!<h1 id="Test123">Test123</h1>!)
$t->get_ok('/')->status_is(200)->content_like(qr!<h1.*>Test123</h1>!)
->content_like(qr|<p>It <code>works</code>!</p>|);

# POD helper
$t->post_ok('/')->status_is(200)->content_like(qr!test123<h1 id="A">A</h1>!)
->content_like(qr!<h1 id="B">B</h1>!)
$t->post_ok('/')->status_is(200)->content_like(qr!test123<h1.*>A</h1>!)
->content_like(qr!<h1.*>B</h1>!)
->content_like(qr!\s+<p><code>test</code></p>!)->content_like(qr/Gray/);

# POD filter
$t->post_ok('/block')->status_is(200)
->content_like(qr!test321<h2 id="lalala">lalala</h2>!)
->content_like(qr!test321<h2.*>lalala</h2>!)
->content_like(qr!<p><code>test</code></p>!)->content_like(qr/Gray/);

# Empty
Expand Down

0 comments on commit 5fca5d3

Please sign in to comment.