Skip to content

Commit

Permalink
fixed a few bugs in Mojolicious::Plugin::PODRenderer by switching fro…
Browse files Browse the repository at this point in the history
…m Pod::Simple::HTML to Pod::Simple::XHTML
  • Loading branch information
kraih committed Jul 26, 2014
1 parent 66d6615 commit 35791c5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

5.20 2014-07-27
- Fixed a few bugs in Mojolicious::Plugin::PODRenderer by switching from
Pod::Simple::HTML to Pod::Simple::XHTML.

5.19 2014-07-26
- Improved support for Unicode anchors in Mojolicious::Plugin::PODRenderer.
Expand Down
22 changes: 9 additions & 13 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -6,7 +6,7 @@ use Mojo::ByteStream 'b';
use Mojo::DOM;
use Mojo::URL;
use Mojo::Util qw(slurp unindent url_escape);
use Pod::Simple::HTML;
use Pod::Simple::XHTML;
use Pod::Simple::Search;

sub register {
Expand Down Expand Up @@ -42,12 +42,12 @@ sub _html {
my $perldoc = $self->url_for('/perldoc/');
for my $e ($dom->find('a[href]')->each) {
my $attrs = $e->attr;
$attrs->{href} =~ s!%3A%3A!/!gi
if $attrs->{href} =~ s!^http://search\.cpan\.org/perldoc\?!$perldoc!;
$attrs->{href} =~ s!::!/!gi
if $attrs->{href} =~ s!^https://metacpan\.org/pod/!$perldoc!;
}

# Rewrite code blocks for syntax highlighting and correct indentation
for my $e ($dom->find('pre')->each) {
for my $e ($dom->find('pre > code')->each) {
$e->content(my $str = unindent $e->content);
next if $str =~ /^\s*(?:\$|Usage:)\s+/m || $str !~ /[\$\@\%]\w|->\w/m;
my $attrs = $e->attr;
Expand All @@ -58,12 +58,12 @@ sub _html {
# Rewrite headers
my $toc = Mojo::URL->new->fragment('toc');
my (%anchors, @parts);
my $pod = Pod::Simple::HTML->new;
my $pod = Pod::Simple::XHTML->new;
for my $e ($dom->find('h1, h2, h3')->each) {

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

Expand Down Expand Up @@ -103,16 +103,12 @@ sub _perldoc {
sub _pod_to_html {
return '' unless defined(my $pod = ref $_[0] eq 'CODE' ? shift->() : shift);

my $parser = Pod::Simple::HTML->new;
$parser->$_('') for qw(force_title html_header_before_title);
$parser->$_('') for qw(html_header_after_title html_footer);
my $parser = Pod::Simple::XHTML->new;
$parser->perldoc_url_prefix('https://metacpan.org/pod/');
$parser->$_('') for qw(html_header html_footer);
$parser->output_string(\(my $output));
return $@ unless eval { $parser->parse_string_document("$pod"); 1 };

# Filter
$output =~ s!<a name='___top' class='dummyTopAnchor'\s*?></a>\n!!g;
$output =~ s!<a class='u'.*?name=".*?"\s*>(.*?)</a>!$1!sg;

return $output;
}

Expand Down
9 changes: 2 additions & 7 deletions lib/Mojolicious/templates/mojobar.html.ep
Expand Up @@ -68,15 +68,10 @@
</div>
</div>
<script>
function escapeAnchor(hash) {
var anchor = hash.split("#")[1]
anchor = anchor.replace(/[!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~]/g, "\\$&");
return "#" + anchor;
}
var mojobar = $('#mojobar');
var mojobarHeight = mojobar.outerHeight();
function fixOffset() {
var offset = $(escapeAnchor(window.location.hash)).offset();
var offset = $(window.location.hash).offset();
if (offset) {
$('html, body').animate({scrollTop: offset.top - mojobarHeight}, 1);
}
Expand Down Expand Up @@ -117,7 +112,7 @@
e.preventDefault();
e.stopPropagation();
var hash = "#" + this.href.split("#")[1];
var target = $(escapeAnchor(hash));
var target = $(hash);
var old = target.attr('id');
target.attr('id', '');
location.hash = hash;
Expand Down
10 changes: 6 additions & 4 deletions lib/Mojolicious/templates/perldoc.html.ep
Expand Up @@ -16,7 +16,7 @@
line-height: 1.5em;
margin: 0;
}
code {
:not(pre) > code {
background-color: rgba(0, 0, 0, 0.04);
border-radius: 3px;
font: 0.9em Consolas, Menlo, Monaco, Courier, monospace;
Expand All @@ -35,12 +35,14 @@
border: 1px solid #d1d1d1;
border-radius: 3px;
box-shadow: 0 1px #fff, inset -1px 1px 4px rgba(0, 0, 0, 0.1);
color: #4d4d4c;
font: 0.9em Consolas, Menlo, Monaco, Courier, monospace;
line-height: 1.5em;
padding: 1em;
padding-bottom: 1.5em;
padding-top: 1.5em;
}
pre > code {
color: #4d4d4c;
font: 0.9em Consolas, Menlo, Monaco, Courier, monospace;
line-height: 1.5em;
text-align: left;
text-shadow: #eee 0 1px 0;
white-space: pre-wrap;
Expand Down
10 changes: 5 additions & 5 deletions t/mojolicious/pod_renderer_lite_app.t
Expand Up @@ -31,17 +31,17 @@ my $t = Test::Mojo->new;

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

# POD helper
$t->post_ok('/')->status_is(200)
->content_like(qr!test123\s+<h1>A</h1>\s+<h1>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\s+<h2>lalala</h2>\s+<p><code>test</code></p>!)
->content_like(qr/Gray/);
->content_like(qr!test321<h2.*>lalala</h2>!)
->content_like(qr!<p><code>test</code></p>!)->content_like(qr/Gray/);

# Empty
$t->get_ok('/empty')->status_is(200)->content_is('');
Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/twinkle_lite_app.t
Expand Up @@ -87,13 +87,13 @@ $t->get_ok('/advanced')->status_is(200)->header_is('X-Append' => 'bar')
->content_is("&LT;escape me>\n123423");

# Normal "pod" template
$t->get_ok('/docs')->status_is(200)->content_like(qr!<h3>snowman</h3>!);
$t->get_ok('/docs')->status_is(200)->content_like(qr!<h3.*>snowman</h3>!);

# Template in "teapod" format
$t->get_ok('/docs2')->status_is(200)->content_like(qr!<h2>snowman</h2>!);
$t->get_ok('/docs2')->status_is(200)->content_like(qr!<h2.*>snowman</h2>!);

# Empty stash value
$t->get_ok('/docs3')->status_is(200)->content_like(qr!<h3></h3>!);
$t->get_ok('/docs3')->status_is(200)->content_like(qr!<h3.*></h3>!);

# REST request for "foo" format
$t->get_ok('/rest')->status_is(200)->header_is('X-Rest' => 1)
Expand Down

0 comments on commit 35791c5

Please sign in to comment.