| 1 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | package Template::Plugin::Mariachi; | ||||||||||||
| 3 | 1 | ▶ | use URI::Find::Schemeless::Stricter; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Email::Find; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use base qw(Template::Plugin::Filter); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | our $FILTER_NAME = "mariachi"; | |||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | Template::Plugin::Mariachi - gussy up email for the Mariachi mailing list archiver | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||
| 16 | |||||||||||||
| 17 | [% USE Mariachi %] | ||||||||||||
| 18 | |||||||||||||
| 19 | <b>From:</b> [% message.from | mariachi(uris => 0) %]<br /> | ||||||||||||
| 20 | <b>Subject:</b> [% message.subject | html | mariachi %]<br /> | ||||||||||||
| 21 | <b>Date:</b> [% date.format(message.epoch_date) %]<br /> | ||||||||||||
| 22 | |||||||||||||
| 23 | <pre>[% message.body | html | mariachi %]</pre> | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||
| 26 | |||||||||||||
| 27 | Used by the mariachi mailing list archiver to make emails more | ||||||||||||
| 28 | suitable for display as html by hiding email addresses and turning | ||||||||||||
| 29 | bare urls into links. | ||||||||||||
| 30 | |||||||||||||
| 31 | Theoretically this could be done with some other C<Template::Toolkit> | ||||||||||||
| 32 | plugins but this is easier for us. | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 METHODS | ||||||||||||
| 35 | |||||||||||||
| 36 | =head2 [% USE Mariachi %] | ||||||||||||
| 37 | |||||||||||||
| 38 | Initialise the Mariahci filter in your template. Can take options like so: | ||||||||||||
| 39 | |||||||||||||
| 40 | [% USE Mariachi( uris => 0, email => 1) %] | ||||||||||||
| 41 | |||||||||||||
| 42 | which, in this case, turns off uri munging and turns on email munging. | ||||||||||||
| 43 | |||||||||||||
| 44 | Both options are on by default. | ||||||||||||
| 45 | |||||||||||||
| 46 | =cut | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub init { | ||||||||||
|
Subroutine
init: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 49 | 0 | my ($self,@args) = @_; | |||||||||||
| 50 | 0 | ▶ | my $config = (ref $args[-1] eq 'HASH')? pop @args : {}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | |||||||||||||
| 52 | 0 | $self->{_DYNAMIC} = 1; | |||||||||||
| 53 | 0 | $self->{_MYCONFIG} = $config; | |||||||||||
| 54 | |||||||||||||
| 55 | 0 | $self->install_filter($FILTER_NAME); | |||||||||||
| 56 | |||||||||||||
| 57 | 0 | return $self; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | =head2 [% FILTER mariachi %] | ||||||||||||
| 61 | |||||||||||||
| 62 | =head2 [% somevar | mariachi %] | ||||||||||||
| 63 | |||||||||||||
| 64 | Filter some text. Can take options in a similar manner to initialisation. | ||||||||||||
| 65 | |||||||||||||
| 66 | [% FILTER mariachi(email => 0) %] | ||||||||||||
| 67 | |||||||||||||
| 68 | [% somevar | mariachi(uris => 0) %] | ||||||||||||
| 69 | |||||||||||||
| 70 | |||||||||||||
| 71 | =cut | ||||||||||||
| 72 | |||||||||||||
| 73 | # possibly extraneous cargo culting but it works so ... | ||||||||||||
| 74 | 0 | ▶ | sub filter { | ||||||||||
|
Subroutine
filter: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 75 | 0 | my ($self, $text, @args) = @_; | |||||||||||
| 76 | 0 | ▶ | my $config = (ref $args[-1] eq 'HASH')? pop @args : {}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | |||||||||||||
| 78 | 0 | ▶ | if ($self->_should_do('email', $config)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 0 | find_emails($text, \&munge_email); | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | ▶ | if ($self->_should_do('uris', $config)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 0 | URI::Find::Schemeless::Stricter->new(\&munge_uri)->find(\$text); | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 0 | ▶ | if ($self->_should_do('quoting', $config)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | munge_quoting(\$text); | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 0 | return $text; | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | |||||||||||||
| 94 | 0 | ▶ | sub _should_do { | ||||||||||
|
Subroutine
_should_do: not called CC=5 SCAR=34.0
| |||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||
| 96 | 0 | ▶ | my $key = shift || croak("Must pass a key"); | ||||||||||
|
Condition0%0/2
shift() || croak('Must pass a key')
MC/DC0%0/1
shift() || croak('Must pass a key')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 97 | 0 | ▶ | my $config = shift || {}; | ||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 98 | |||||||||||||
| 99 | # if it's defined in the local config then use that value | ||||||||||||
| 100 | 0 | ▶ | return $config->{$key} if defined $config->{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | # otherwise check in the initialised config | ||||||||||||
| 102 | 0 | ▶ | return $self->{_MY_CONFIG}->{$key} if defined $self->{_MY_CONFIG}->{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | |||||||||||||
| 104 | # otherwise we're on by default | ||||||||||||
| 105 | 0 | return 1; | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | |||||||||||||
| 109 | =head2 munge_quoting <text_ref> | ||||||||||||
| 110 | |||||||||||||
| 111 | Takes a reference to some text and returns it munged for quoting | ||||||||||||
| 112 | |||||||||||||
| 113 | =cut | ||||||||||||
| 114 | |||||||||||||
| 115 | |||||||||||||
| 116 | 0 | ▶ | sub munge_quoting { | ||||||||||
|
Subroutine
munge_quoting: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 117 | 0 | my $textref = shift; | |||||||||||
| 118 | |||||||||||||
| 119 | 0 | $$textref =~ s!^(\s*>.+)$!<i>$1</i>!gm; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | =head2 munge_email <email> <orig_email> | ||||||||||||
| 123 | |||||||||||||
| 124 | Takes exactly the same options as callbacks to | ||||||||||||
| 125 | C<Email::Find>. Currently turns all non period characters in the | ||||||||||||
| 126 | domain part of an email address and turns them into 'x's such that : | ||||||||||||
| 127 | |||||||||||||
| 128 | simon@thegestalt.org | ||||||||||||
| 129 | |||||||||||||
| 130 | becomes | ||||||||||||
| 131 | |||||||||||||
| 132 | simon@xxxxxxxxxx.xxx | ||||||||||||
| 133 | |||||||||||||
| 134 | Should be overridden if you want different behaviour. | ||||||||||||
| 135 | |||||||||||||
| 136 | =cut | ||||||||||||
| 137 | |||||||||||||
| 138 | 0 | ▶ | sub munge_email { | ||||||||||
|
Subroutine
munge_email: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 139 | 0 | my ($email, $orig_email) = @_; | |||||||||||
| 140 | |||||||||||||
| 141 | 0 | $orig_email =~ s{ | |||||||||||
| 142 | \@(.+)$ # everything after the '@' | ||||||||||||
| 143 | }{ | ||||||||||||
| 144 | "@". | ||||||||||||
| 145 | join '.', # join together with dots | ||||||||||||
| 146 | 0 | map { "x" x length($_) } # make each part into 'x's | |||||||||||
| 147 | split /\./, $1 # split stuff apart on dots | ||||||||||||
| 148 | }ex; | ||||||||||||
| 149 | |||||||||||||
| 150 | 0 | return $orig_email; | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | =head2 munge_uri <uri> <orig_uri> | ||||||||||||
| 154 | |||||||||||||
| 155 | Takes exactly the same options as callbacks to C<URI::Find> although | ||||||||||||
| 156 | it actually uses C<URI::Find::Schemeless::Stricter>. | ||||||||||||
| 157 | |||||||||||||
| 158 | As such you should be wary if overriding that the uri may not have a | ||||||||||||
| 159 | scheme. This | ||||||||||||
| 160 | |||||||||||||
| 161 | $uri->scheme('http') unless defined $uri->scheme; | ||||||||||||
| 162 | |||||||||||||
| 163 | solves that particular problem (for various values of solve) | ||||||||||||
| 164 | |||||||||||||
| 165 | Currently just turns uris into simple clickable links | ||||||||||||
| 166 | |||||||||||||
| 167 | www.foo.com | ||||||||||||
| 168 | |||||||||||||
| 169 | becomes | ||||||||||||
| 170 | |||||||||||||
| 171 | <a href="http://www.foo.com">www.foo.com</a> | ||||||||||||
| 172 | |||||||||||||
| 173 | |||||||||||||
| 174 | Should be overridden if you want different behaviour. | ||||||||||||
| 175 | |||||||||||||
| 176 | =cut | ||||||||||||
| 177 | |||||||||||||
| 178 | |||||||||||||
| 179 | 0 | ▶ | sub munge_uri { | ||||||||||
|
Subroutine
munge_uri: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 180 | 0 | my ($uri,$orig_uri) = @_; | |||||||||||
| 181 | 0 | ▶ | $uri->scheme('http') unless defined $uri->scheme(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | |||||||||||||
| 183 | 0 | return "<a href='$uri'>$orig_uri</a>"; | |||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | 1 | 1; | |||||||||||
| 187 | |||||||||||||
| 188 | __END__ | ||||||||||||