| 1 | 4 | ▶ | use strictures 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | package Mojito::Filter::Shortcuts; | ||||||||||
| 3 | 4 | $Mojito::Filter::Shortcuts::VERSION = '0.25'; | |||||||||
| 4 | 4 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use Mojito::Model::MetaCPAN; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use Data::Dumper::Concise; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 4 | with('Mojito::Role::Config'); | |||||||||
| 11 | |||||||||||
| 12 | 4 | has shortcuts => ( | |||||||||
| 13 | is => 'ro', | ||||||||||
| 14 | isa => ArrayRef, | ||||||||||
| 15 | lazy => 1, | ||||||||||
| 16 | builder => '_build_shortcuts', | ||||||||||
| 17 | ); | ||||||||||
| 18 | 1 | ▶ | sub _build_shortcuts { | ||||||||
|
Subroutine
_build_shortcuts: called CC=2 SCAR=7.0
| |||||||||||
| 19 | 1 | my $self = shift; | |||||||||
| 20 | 1 | my @shortcuts = qw( | |||||||||
| 21 | cpan_URL | ||||||||||
| 22 | metacpan_module_URL | ||||||||||
| 23 | metacpan_author_URL | ||||||||||
| 24 | internal_URL | ||||||||||
| 25 | gist_URL | ||||||||||
| 26 | cpan_recent_synopses | ||||||||||
| 27 | cpan_synopsis); | ||||||||||
| 28 | 1 | ▶ | push @shortcuts, 'fonality_ticket' if ($self->config->{fonality_ticket_url}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 1 | return \@shortcuts; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | =head1 Methods | ||||||||||
| 33 | |||||||||||
| 34 | =head2 expand_shortcuts | ||||||||||
| 35 | |||||||||||
| 36 | Expand the available shortcuts into the content. | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | sub expand_shortcuts { | ||||||||
|
Subroutine
expand_shortcuts: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 41 | 1 | my ($self, $content) = @_; | |||||||||
| 42 | 1 | foreach my $shortcut ( @{$self->shortcuts} ) { | |||||||||
| 43 | 7 | $content = $self->${shortcut}(${content}); | |||||||||
| 44 | } | ||||||||||
| 45 | 1 | return $content; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | =head2 cpan_URL | ||||||||||
| 49 | |||||||||||
| 50 | Expand the cpan abbreviated shortcut. | ||||||||||
| 51 | |||||||||||
| 52 | =cut | ||||||||||
| 53 | |||||||||||
| 54 | 1 | ▶ | sub cpan_URL { | ||||||||
|
Subroutine
cpan_URL: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 55 | 1 | my ($self, $content) = @_; | |||||||||
| 56 | 1 | ▶ | return if !$content; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 1 | $content =~ s/\{\{cpan\s+([^}]*)}}/<a href="http:\/\/search.cpan.org\/perldoc?$1">$1<\/a>/sig; | |||||||||
| 58 | 1 | return $content; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | =head2 gist_URL | ||||||||||
| 62 | |||||||||||
| 63 | Expand the gist.github.com abbreviated shortcut. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 1 | ▶ | sub gist_URL { | ||||||||
|
Subroutine
gist_URL: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 68 | 1 | my ($self, $content) = @_; | |||||||||
| 69 | 1 | ▶ | return if !$content; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 1 | $content =~ s/\{\{gist\s+([^}]*)}}/<script src="https:\/\/gist.github.com\/$1.js"><\/script>/sig; | |||||||||
| 71 | 1 | return $content; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | has metacpan => ( | ||||||||||
| 75 | is => 'ro', | ||||||||||
| 76 | lazy => 1, | ||||||||||
| 77 | 0 | ▶ | default => sub { Mojito::Model::MetaCPAN->new }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 78 | 4 | ); | |||||||||
| 79 | |||||||||||
| 80 | =head2 cpan_synopsis | ||||||||||
| 81 | |||||||||||
| 82 | Show the CPAN SYNOPSIS for a Perl Module | ||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||
| 85 | |||||||||||
| 86 | 1 | ▶ | sub cpan_synopsis { | ||||||||
|
Subroutine
cpan_synopsis: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 87 | 1 | my ($self, $content) = @_; | |||||||||
| 88 | 1 | ▶ | return if !$content; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | $content =~ s/\{\{cpan.synopsis\s+([^}]*)}}/$self->metacpan->get_synopsis_formatted($1, 'presentation')/esig; | |||||||||
| 90 | 1 | return $content; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | =head2 cpan_recent_synopses | ||||||||||
| 94 | |||||||||||
| 95 | Show the synopses of the CPAN recent releases | ||||||||||
| 96 | |||||||||||
| 97 | =cut | ||||||||||
| 98 | |||||||||||
| 99 | 1 | ▶ | sub cpan_recent_synopses { | ||||||||
|
Subroutine
cpan_recent_synopses: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 100 | 1 | my ($self, $content) = @_; | |||||||||
| 101 | 1 | ▶ | return if !$content; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | $content =~ s/\{\{cpan.synopses.recent\s*(\d+)}}/$self->metacpan->get_recent_synopses($1)/esig; | |||||||||
| 103 | 1 | return $content; | |||||||||
| 104 | } | ||||||||||
| 105 | =head2 metacpan_module_URL | ||||||||||
| 106 | |||||||||||
| 107 | Expand the cpan abbreviated shortcut. | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | 1 | ▶ | sub metacpan_module_URL { | ||||||||
|
Subroutine
metacpan_module_URL: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 112 | 1 | my ($self, $content) = @_; | |||||||||
| 113 | 1 | ▶ | return if !$content; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 1 | $content =~ s|\{\{metacpan\s+([^}]*)}}|<a href="http://metacpan.org/module/$1">$1</a>|sig; | |||||||||
| 115 | 1 | return $content; | |||||||||
| 116 | } | ||||||||||
| 117 | =head2 metacpan_module_URL | ||||||||||
| 118 | |||||||||||
| 119 | Expand the cpan abbreviated shortcut. | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 1 | ▶ | sub metacpan_author_URL { | ||||||||
|
Subroutine
metacpan_author_URL: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 124 | 1 | my ($self, $content) = @_; | |||||||||
| 125 | 1 | ▶ | return if !$content; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 1 | $content =~ s|\{\{metacpan.author\s+([^}]*)}}|<a href="http://metacpan.org/author/$1">$1</a>|sig; | |||||||||
| 127 | 1 | return $content; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | =head2 internal_URL | ||||||||||
| 132 | |||||||||||
| 133 | Expand an internal URL | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||
| 136 | |||||||||||
| 137 | 1 | ▶ | sub internal_URL { | ||||||||
|
Subroutine
internal_URL: called CC=2 SCAR=11.6
Pod
documented
| |||||||||||
| 138 | 1 | my ($self, $content) = @_; | |||||||||
| 139 | 1 | ▶ | return if !$content; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 1 | my ($base_url, $path_info, $http_referer, $http_host) = @{$self->config}{qw/base_url PATH_INFO HTTP_REFERER HTTP_HOST/}; | |||||||||
| 141 | 0 | ▶ | my $add_link = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 142 | 0 | my ($link, $title) = @_; | |||||||||
| 143 | # Strip ending slash as we only append the base_url to link starting with a slash | ||||||||||
| 144 | 0 | $base_url =~ s|/$||; | |||||||||
| 145 | 0 | ▶ | if ($link !~ m|^/|) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | # If we have a path_info of /preview then we want to use the | ||||||||||
| 147 | # referred instead as the path_info. | ||||||||||
| 148 | 0 | ▶ | if ($path_info eq '/preview') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | # create path info from where the post came | ||||||||||
| 150 | 0 | ($path_info) = $http_referer =~ m|${http_host}${base_url}(.*)/edit$|; | |||||||||
| 151 | } | ||||||||||
| 152 | 0 | ▶ | $path_info = ($path_info =~ m|/$|) ? $path_info : $path_info . '/'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 0 | $base_url .= $path_info; | |||||||||
| 154 | } | ||||||||||
| 155 | 0 | return "<a href='${base_url}${link}'>${title}</a>"; | |||||||||
| 156 | 1 | }; | |||||||||
| 157 | 0 | $content =~ s/\[\[([^\|]*)\|([^\]]*)\]\]/$add_link->($1,$2)/esig; | |||||||||
| 158 | 1 | return $content; | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | 4 | 1 | |||||||||