| 1 | package Muster::Hook::Shortcut; | ||||||||||||||||||
| 2 | 1 | $Muster::Hook::Shortcut::VERSION = '0.96'; | |||||||||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Muster::Hook::Directives'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use Muster::LeafFile; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 1 | ▶ | use Muster::Hooks; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use YAML::Any; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | Muster::Hook::Shortcut - Muster shortcut directive | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | version 0.96 | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | L<Muster::Hook::Shortcut> creates shortcuts. | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | =head1 METHODS | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | L<Muster::Hook::Shortcut> inherits all methods from L<Muster::Hook::Directives>. | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | =head2 register | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | Do some intialization. | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | =cut | ||||||||||||||||||
| 31 | 0 | ▶ | sub register { | ||||||||||||||||
|
Subroutine
register: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 32 | 0 | my $self = shift; | |||||||||||||||||
| 33 | 0 | my $hookmaster = shift; | |||||||||||||||||
| 34 | 0 | my $config = shift; | |||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | # The shortcuts are defined in the config | ||||||||||||||||||
| 37 | # Each shortcut needs a callback to expand that particular shortcut | ||||||||||||||||||
| 38 | # and that callback is passed to the do_directives method, | ||||||||||||||||||
| 39 | # which in turn is called inside the callback added as a hook to the hookmaster. | ||||||||||||||||||
| 40 | 0 | foreach my $sh (keys %{$config->{hook_conf}->{'Muster::Hook::Shortcut'}}) | |||||||||||||||||
| 41 | { | ||||||||||||||||||
| 42 | 0 | ▶ | my $callback = sub { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 43 | 0 | my %args = @_; | |||||||||||||||||
| 44 | 0 | my $leaf = $args{leaf}; | |||||||||||||||||
| 45 | 0 | my $phase = $args{phase}; | |||||||||||||||||
| 46 | 0 | my @params = @{$args{params}}; | |||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | return $self->shortcut_expand( | ||||||||||||||||||
| 49 | $config->{hook_conf}->{'Muster::Hook::Shortcut'}->{$sh}->{url}, | ||||||||||||||||||
| 50 | $config->{hook_conf}->{'Muster::Hook::Shortcut'}->{$sh}->{desc}, | ||||||||||||||||||
| 51 | 0 | phase=>$phase, | |||||||||||||||||
| 52 | leaf=>$leaf, | ||||||||||||||||||
| 53 | @params); | ||||||||||||||||||
| 54 | 0 | }; | |||||||||||||||||
| 55 | 0 | ▶ | $hookmaster->add_hook($sh => sub { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 56 | 0 | my %args = @_; | |||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | 0 | return $self->do_directives( | |||||||||||||||||
| 59 | directive=>$sh, | ||||||||||||||||||
| 60 | no_scan=>1, | ||||||||||||||||||
| 61 | call=>$callback, | ||||||||||||||||||
| 62 | %args); | ||||||||||||||||||
| 63 | }, | ||||||||||||||||||
| 64 | 0 | ); | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | 0 | return $self; | |||||||||||||||||
| 67 | } # register | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | =head2 shortcut_expand | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | Expand the placeholders in the given shortcut. | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | =cut | ||||||||||||||||||
| 74 | 0 | ▶ | sub shortcut_expand ($$@) { | ||||||||||||||||
|
Subroutine
shortcut_expand: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||
| 76 | 0 | my $url=shift; | |||||||||||||||||
| 77 | 0 | my $desc=shift; | |||||||||||||||||
| 78 | 0 | my %params=@_; | |||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | # code from IkiWiki | ||||||||||||||||||
| 81 | # Get params in original order. | ||||||||||||||||||
| 82 | 0 | my @params; | |||||||||||||||||
| 83 | 0 | ▶ | while (@_) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | 0 | my $key=shift; | |||||||||||||||||
| 85 | 0 | my $value=shift; | |||||||||||||||||
| 86 | 0 | ▶ | push @params, $key if ! length $value; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 0 | my $text=join(" ", @params); | |||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 0 | $url=~s{\%([sSW])}{ | |||||||||||||||||
| 92 | 0 | ▶ | if ($1 eq 's') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 93 | 0 | my $t=$text; | |||||||||||||||||
| 94 | 0 | $t=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; | |||||||||||||||||
| 95 | 0 | $t; | |||||||||||||||||
| 96 | } | ||||||||||||||||||
| 97 | elsif ($1 eq 'S') { | ||||||||||||||||||
| 98 | 0 | $text; | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | elsif ($1 eq 'W') { | ||||||||||||||||||
| 101 | 0 | my $t=Encode::encode_utf8($text); | |||||||||||||||||
| 102 | 0 | $t=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; | |||||||||||||||||
| 103 | 0 | $t; | |||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | }eg; | ||||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | 0 | $text=~s/_/ /g; | |||||||||||||||||
| 108 | 0 | ▶ | if (defined $params{desc}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 109 | 0 | $desc=$params{desc}; | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | 0 | ▶ | if (defined $desc) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 112 | 0 | $desc=~s/\%s/$text/g; | |||||||||||||||||
| 113 | } | ||||||||||||||||||
| 114 | else { | ||||||||||||||||||
| 115 | 0 | $desc=$text; | |||||||||||||||||
| 116 | } | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 0 | return "<a href=\"$url\">$desc</a>"; | |||||||||||||||||
| 119 | } | ||||||||||||||||||
| 120 | 1 | 1; | |||||||||||||||||