| 1 | package Muster::Hook::Directives; | ||||||||||||||||||||||||||
| 2 | 11 | $Muster::Hook::Directives::VERSION = '0.96'; | |||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||
| 5 | Muster::Hook::Directives - Muster base class for preprocessor directives | ||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||
| 7 | =head1 VERSION | ||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||
| 9 | version 0.96 | ||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||
| 13 | # CamelCase plugin name | ||||||||||||||||||||||||||
| 14 | package Muster::Hook::Directives; | ||||||||||||||||||||||||||
| 15 | use Mojo::Base 'Muster::Hook'; | ||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||
| 19 | L<Muster::Hook::Directives> processes for preprocessor directives. | ||||||||||||||||||||||||||
| 20 | This has sub-classes for all the directives. | ||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||
| 22 | As with IkiWiki, directives are prefixed with "[[!I<name>" | ||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||
| 26 | 11 | ▶ | use Mojo::Base 'Muster::Hook'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 27 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 28 | 11 | ▶ | use Muster::LeafFile; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 29 | 11 | ▶ | use Muster::Hooks; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 30 | 11 | ▶ | use YAML::Any; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||
| 33 | =head1 METHODS | ||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||
| 35 | =head2 register | ||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||
| 37 | Initialize and register. This must be defined in a subclass. | ||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub register { | ||||||||||||||||||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||
| 41 | 0 | croak __PACKAGE__, " register must be defined in a subclass"; | |||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||
| 44 | =head2 do_directives | ||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||
| 46 | Extracts and processes directives from the content of the leaf. | ||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub do_directives { | ||||||||||||||||||||||||
|
Subroutine
do_directives: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||||||||
| 52 | 0 | my %args = @_; | |||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||
| 54 | 0 | my $leaf = $args{leaf}; | |||||||||||||||||||||||||
| 55 | 0 | my $phase = $args{phase}; | |||||||||||||||||||||||||
| 56 | 0 | my $no_scan = $args{no_scan}; | |||||||||||||||||||||||||
| 57 | 0 | my $directive = $args{directive}; | |||||||||||||||||||||||||
| 58 | 0 | my $call = $args{call}; | |||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||
| 60 | 0 | my $page = $leaf->pagename; | |||||||||||||||||||||||||
| 61 | 0 | my $content = $leaf->cooked; | |||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||
| 63 | # adapted fom IkiWiki code | ||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $handle=sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||
| 65 | 0 | my $escape=shift; | |||||||||||||||||||||||||
| 66 | 0 | my $prefix=shift; | |||||||||||||||||||||||||
| 67 | 0 | my $command=shift; | |||||||||||||||||||||||||
| 68 | 0 | my $params=shift; | |||||||||||||||||||||||||
| 69 | 0 | ▶ | $params="" if ! defined $params; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (length $escape) | ||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||
| 73 | 0 | return "[[${prefix}${command} ${params}]]"; | |||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||
| 75 | ▶ | elsif ($phase eq $Muster::Hooks::PHASE_SCAN and $no_scan) | |||||||||||||||||||||||||
|
Condition0%0/3
$phase eq $Muster::Hooks::PHASE_SCAN and $no_scan
MC/DC0%0/2
$phase eq $Muster::Hooks::PHASE_SCAN and $no_scan
$phase eq $Muster::Hooks::PHASE_SCAN
$no_scan
Truth table0%0/3
A and B
A= $phase eq $Muster::Hooks::PHASE_SCANB= $no_scan
| |||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||
| 77 | # save time and return immediately | ||||||||||||||||||||||||||
| 78 | 0 | return ""; | |||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||
| 80 | else # this already matches our directive | ||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||
| 82 | # Note: preserve order of params, some plugins may | ||||||||||||||||||||||||||
| 83 | # consider it significant. | ||||||||||||||||||||||||||
| 84 | 0 | my @params; | |||||||||||||||||||||||||
| 85 | 0 | ▶ | while ($params =~ m{ | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 86 | (?:([-.\w]+)=)? # 1: named parameter key? | ||||||||||||||||||||||||||
| 87 | (?: | ||||||||||||||||||||||||||
| 88 | """(.*?)""" # 2: triple-quoted value | ||||||||||||||||||||||||||
| 89 | | | ||||||||||||||||||||||||||
| 90 | "([^"]*?)" # 3: single-quoted value | ||||||||||||||||||||||||||
| 91 | | | ||||||||||||||||||||||||||
| 92 | '''(.*?)''' # 4: triple-single-quote | ||||||||||||||||||||||||||
| 93 | | | ||||||||||||||||||||||||||
| 94 | <<([a-zA-Z]+)\n # 5: heredoc start | ||||||||||||||||||||||||||
| 95 | (.*?)\n\5 # 6: heredoc value | ||||||||||||||||||||||||||
| 96 | | | ||||||||||||||||||||||||||
| 97 | (\S+) # 7: unquoted value | ||||||||||||||||||||||||||
| 98 | ) | ||||||||||||||||||||||||||
| 99 | (?:\s+|$) # delimiter to next param | ||||||||||||||||||||||||||
| 100 | }msgx) | ||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||
| 102 | 0 | my $key=$1; | |||||||||||||||||||||||||
| 103 | 0 | my $val; | |||||||||||||||||||||||||
| 104 | 0 | ▶ | if (defined $2) | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||
| 106 | 0 | $val=$2; | |||||||||||||||||||||||||
| 107 | 0 | $val=~s/\r\n/\n/mg; | |||||||||||||||||||||||||
| 108 | 0 | $val=~s/^\n+//g; # remove starting newlines | |||||||||||||||||||||||||
| 109 | 0 | $val=~s/\n+$//g; # remove trailing newlines | |||||||||||||||||||||||||
| 110 | # to compensate for the newline removal, enable substitution of \n as a newline | ||||||||||||||||||||||||||
| 111 | 0 | $val=~s/\\n/\n/g; | |||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||
| 113 | elsif (defined $3) | ||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||
| 115 | 0 | $val=$3; | |||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||
| 117 | elsif (defined $4) | ||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||
| 119 | 0 | $val=$4; | |||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||
| 121 | elsif (defined $7) | ||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||
| 123 | 0 | $val=$7; | |||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||
| 125 | elsif (defined $6) | ||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||
| 127 | 0 | $val=$6; | |||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | if (defined $key) | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||
| 132 | 0 | push @params, $key, $val; | |||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||
| 134 | else | ||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||
| 136 | 0 | push @params, $val, ''; | |||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ($self->{preprocessing}->{$page}++ > 8) | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||
| 141 | # Avoid loops of preprocessed pages preprocessing | ||||||||||||||||||||||||||
| 142 | # other pages that preprocess them, etc. | ||||||||||||||||||||||||||
| 143 | return "[[!$command <span class=\"error\">". | ||||||||||||||||||||||||||
| 144 | sprintf("preprocessing loop detected on %s at depth %i", | ||||||||||||||||||||||||||
| 145 | 0 | $page, $self->{preprocessing}->{$page}). | |||||||||||||||||||||||||
| 146 | "</span>]]"; | ||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||
| 148 | 0 | my $ret; | |||||||||||||||||||||||||
| 149 | 0 | ▶ | if ($phase eq $Muster::Hooks::PHASE_BUILD) # not scanning | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||
| 151 | 0 | $ret=eval { | |||||||||||||||||||||||||
| 152 | 0 | $call->(%args, params=>\@params); | |||||||||||||||||||||||||
| 153 | }; | ||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($@) | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||
| 156 | 0 | my $error=$@; | |||||||||||||||||||||||||
| 157 | 0 | chomp $error; | |||||||||||||||||||||||||
| 158 | 0 | eval q{use HTML::Entities}; | |||||||||||||||||||||||||
| 159 | 0 | $error = encode_entities($error); | |||||||||||||||||||||||||
| 160 | 0 | $ret="[[!$command <span class=\"error\">". | |||||||||||||||||||||||||
| 161 | "Error".": $error"."</span>]]"; | ||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||
| 164 | else # scanning | ||||||||||||||||||||||||||
| 165 | { | ||||||||||||||||||||||||||
| 166 | 0 | eval { | |||||||||||||||||||||||||
| 167 | 0 | $call->(%args, params=>\@params); | |||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||
| 169 | 0 | $ret=""; | |||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||
| 171 | 0 | $self->{preprocessing}->{$page}--; | |||||||||||||||||||||||||
| 172 | 0 | return $ret; | |||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||
| 174 | 0 | }; | |||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||
| 176 | # NOTE: unlike with the IkiWiki version of directives | ||||||||||||||||||||||||||
| 177 | # we already know WHICH directive to search for, as it is | ||||||||||||||||||||||||||
| 178 | # one of the arguments passed in to do_directive. | ||||||||||||||||||||||||||
| 179 | # So we aren't going to have any false positives about whether | ||||||||||||||||||||||||||
| 180 | # this is really the command we are looking for. | ||||||||||||||||||||||||||
| 181 | 0 | my $regex = qr{ | |||||||||||||||||||||||||
| 182 | (\\?) # 1: escape? | ||||||||||||||||||||||||||
| 183 | \[\[(!) # directive open; 2: prefix | ||||||||||||||||||||||||||
| 184 | ($directive) # 3: command | ||||||||||||||||||||||||||
| 185 | ( # 4: the parameters.. | ||||||||||||||||||||||||||
| 186 | \s+ # Must have space if parameters present | ||||||||||||||||||||||||||
| 187 | (?: | ||||||||||||||||||||||||||
| 188 | (?:[-.\w]+=)? # named parameter key? | ||||||||||||||||||||||||||
| 189 | (?: | ||||||||||||||||||||||||||
| 190 | """.*?""" # triple-quoted value | ||||||||||||||||||||||||||
| 191 | | | ||||||||||||||||||||||||||
| 192 | "[^"]*?" # single-quoted value | ||||||||||||||||||||||||||
| 193 | | | ||||||||||||||||||||||||||
| 194 | '''.*?''' # triple-single-quote | ||||||||||||||||||||||||||
| 195 | | | ||||||||||||||||||||||||||
| 196 | <<([a-zA-Z]+)\n # 5: heredoc start | ||||||||||||||||||||||||||
| 197 | (?:.*?)\n\5 # heredoc value | ||||||||||||||||||||||||||
| 198 | | | ||||||||||||||||||||||||||
| 199 | [^"\s\]]+ # unquoted value | ||||||||||||||||||||||||||
| 200 | ) | ||||||||||||||||||||||||||
| 201 | \s* # whitespace or end | ||||||||||||||||||||||||||
| 202 | # of directive | ||||||||||||||||||||||||||
| 203 | ) | ||||||||||||||||||||||||||
| 204 | *)? # 0 or more parameters | ||||||||||||||||||||||||||
| 205 | \]\] # directive closed | ||||||||||||||||||||||||||
| 206 | }sx; | ||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||
| 208 | 0 | $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg; | |||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||
| 210 | 0 | $leaf->{cooked} = $content; | |||||||||||||||||||||||||
| 211 | 0 | return $leaf; | |||||||||||||||||||||||||
| 212 | } # do_directives | ||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||
| 214 | 11 | 1; | |||||||||||||||||||||||||