add macro transform sub, convert upgrade to use it
This commit is contained in:
parent
d087b77131
commit
e7891e9191
2 changed files with 106 additions and 52 deletions
|
|
@ -43,7 +43,7 @@ These functions are available from this package:
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
our $macro_re = qr{
|
my $macro_re = qr{
|
||||||
( # capture #1 - entire macro call
|
( # capture #1 - entire macro call
|
||||||
\^ # start with carat
|
\^ # start with carat
|
||||||
([-a-zA-Z0-9_@#/*]{1,64}) # capture #2 - macro name
|
([-a-zA-Z0-9_@#/*]{1,64}) # capture #2 - macro name
|
||||||
|
|
@ -62,6 +62,35 @@ our $macro_re = qr{
|
||||||
)
|
)
|
||||||
}msx;
|
}msx;
|
||||||
|
|
||||||
|
my $quote_re = qr{
|
||||||
|
(?<!\z) # don't try to match if we are at the end of the string
|
||||||
|
(?: # either
|
||||||
|
\s* " # white space followed by a double quote
|
||||||
|
( (?: # capture inside
|
||||||
|
[^"\\] # something other than backslash or double quote
|
||||||
|
| # or
|
||||||
|
\\. # a backslash followed by any character
|
||||||
|
) * ) # as many times as needed
|
||||||
|
" \s* # end quote and any white space
|
||||||
|
| # or
|
||||||
|
\s* ' # same as above, but with single quotes
|
||||||
|
( (?:
|
||||||
|
[^'\\]
|
||||||
|
|
|
||||||
|
\\.
|
||||||
|
) * )
|
||||||
|
' \s*
|
||||||
|
| # or
|
||||||
|
([^,]*) # anything but a comma
|
||||||
|
)
|
||||||
|
(?: # followed by
|
||||||
|
\z # end of the string
|
||||||
|
| # or
|
||||||
|
, # a comma
|
||||||
|
)
|
||||||
|
}msx;
|
||||||
|
|
||||||
|
|
||||||
=head2 filter ( html )
|
=head2 filter ( html )
|
||||||
|
|
||||||
Removes all the macros from the HTML segment.
|
Removes all the macros from the HTML segment.
|
||||||
|
|
@ -141,7 +170,7 @@ sub process {
|
||||||
sub _processMacro {
|
sub _processMacro {
|
||||||
my $session = shift;
|
my $session = shift;
|
||||||
my $macroname = shift;
|
my $macroname = shift;
|
||||||
my $parameters = shift;
|
my $parameterString = shift;
|
||||||
if ($macroname =~ /^[-0-9]$/) { # ^0; ^1; ^2; and ^-; have special uses, don't replace
|
if ($macroname =~ /^[-0-9]$/) { # ^0; ^1; ^2; and ^-; have special uses, don't replace
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -160,48 +189,16 @@ sub _processMacro {
|
||||||
$session->log->error("Macro has no process sub: $macropackage.");
|
$session->log->error("Macro has no process sub: $macropackage.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$parameters =~ s/^\(//;
|
|
||||||
$parameters =~ s/\)$//;
|
|
||||||
|
|
||||||
my @params;
|
my $params = _processParameters($parameterString);
|
||||||
while ($parameters =~ m{
|
|
||||||
(?<!\z) # don't try to match if we are at the end of the string
|
|
||||||
(?: # either
|
|
||||||
\s* " # white space followed by a double quote
|
|
||||||
( (?: # capture inside
|
|
||||||
[^"\\] # something other than backslash or double quote
|
|
||||||
| # or
|
|
||||||
\\. # a backslash followed by any character
|
|
||||||
) * ) # as many times as needed
|
|
||||||
" \s* # end quote and any white space
|
|
||||||
| # or
|
|
||||||
\s* ' # same as above, but with single quotes
|
|
||||||
( (?:
|
|
||||||
[^'\\]
|
|
||||||
|
|
|
||||||
\\.
|
|
||||||
) * )
|
|
||||||
' \s*
|
|
||||||
| # or
|
|
||||||
([^,]*) # anything but a comma
|
|
||||||
)
|
|
||||||
(?: # followed by
|
|
||||||
\z # end of the string
|
|
||||||
| # or
|
|
||||||
, # a comma
|
|
||||||
)
|
|
||||||
}xg) {
|
|
||||||
# three matches, only one will exist per run
|
|
||||||
push @params, $+;
|
|
||||||
}
|
|
||||||
|
|
||||||
for my $param (@params) {
|
for my $param (@$params) {
|
||||||
$param =~ s/\\(.)/$1/xmsg; # deal with backslash escapes
|
|
||||||
process($session, \$param)
|
process($session, \$param)
|
||||||
if ($param); # process any macros
|
if ($param); # process any macros
|
||||||
}
|
}
|
||||||
|
|
||||||
my $output;
|
my $output;
|
||||||
unless ( eval { $output = $process->($session, @params); 1 } ) { # call process sub with parameters
|
unless ( eval { $output = $process->($session, @$params); 1 } ) { # call process sub with parameters
|
||||||
$session->log->error("Unable to process macro '$macroname': $@");
|
$session->log->error("Unable to process macro '$macroname': $@");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -211,5 +208,60 @@ sub _processMacro {
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _processParameters {
|
||||||
|
my $parameters = shift;
|
||||||
|
|
||||||
|
$parameters =~ s/^\(//;
|
||||||
|
$parameters =~ s/\)$//;
|
||||||
|
|
||||||
|
my @params;
|
||||||
|
while ($parameters =~ m{$quote_re}msxg) {
|
||||||
|
# three matches, only one will exist per run
|
||||||
|
my $param = $+;
|
||||||
|
$param =~ s/\\(.)/$1/xmsg; # deal with backslash escapes
|
||||||
|
push @params, $param;
|
||||||
|
}
|
||||||
|
|
||||||
|
return \@params;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub transform {
|
||||||
|
my $session = shift;
|
||||||
|
my $content = shift;
|
||||||
|
my $sub = shift;
|
||||||
|
|
||||||
|
${ $content } =~ s{$macro_re}{
|
||||||
|
my $initialText = $1;
|
||||||
|
my $replaceText = _transformMacro($session, $sub, $initialText, $2, $3);
|
||||||
|
# _processMacro returns undef on failure, use original text
|
||||||
|
defined $replaceText ? $replaceText : $initialText;
|
||||||
|
}ge;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub _transformMacro {
|
||||||
|
my $session = shift;
|
||||||
|
my $sub = shift;
|
||||||
|
my $original = shift;
|
||||||
|
my $macro = shift;
|
||||||
|
my $paramString = shift;
|
||||||
|
|
||||||
|
my $macroPackage = "WebGUI::Macro::" . $session->config->get("macros")->{$macro};
|
||||||
|
my $params = _processParameters($paramString);
|
||||||
|
return $sub->({
|
||||||
|
session => $session,
|
||||||
|
macro => $macro,
|
||||||
|
macroPackage => $macroPackage,
|
||||||
|
originalText => $original,
|
||||||
|
parameters => $params,
|
||||||
|
parameterString => $paramString,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
sub quote {
|
||||||
|
my $text = shift;
|
||||||
|
$text =~ s/([\\'])/\\$1/g;
|
||||||
|
return "'$text'";
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,19 @@
|
||||||
|
|
||||||
use WebGUI::Upgrade::Script;
|
use WebGUI::Upgrade::Script;
|
||||||
|
|
||||||
report "\tRemoving Admin Bar... ";
|
start_step "Editing templates to remove AdminBar macro calls";
|
||||||
|
|
||||||
session->config->delete( 'macros/AdminBar' );
|
|
||||||
|
|
||||||
report "\tEditing templates to remove AdminBar macro calls...";
|
|
||||||
|
|
||||||
use WebGUI::Macro;
|
use WebGUI::Macro;
|
||||||
use WebGUI::Asset::Template;
|
use WebGUI::Asset::Template;
|
||||||
|
|
||||||
|
my $removeAdminBar = sub {
|
||||||
|
my $macro = shift;
|
||||||
|
if ($macro->{macroPackage} eq 'WebGUI::Macro::AdminBar') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
my $iter = WebGUI::Asset::Template->getIsa( session );
|
my $iter = WebGUI::Asset::Template->getIsa( session );
|
||||||
ASSET: while (1) {
|
ASSET: while (1) {
|
||||||
my $template = eval { $iter->() };
|
my $template = eval { $iter->() };
|
||||||
|
|
@ -20,17 +24,15 @@ ASSET: while (1) {
|
||||||
last ASSET unless $template;
|
last ASSET unless $template;
|
||||||
|
|
||||||
my $content = $template->template;
|
my $content = $template->template;
|
||||||
while ( $content =~ m/$WebGUI::Macro::macro_re/g ) {
|
WebGUI::Macro::transform( session, \$content, $removeAdminBar );
|
||||||
my $macroCall = $1;
|
|
||||||
my $macroName = $2;
|
|
||||||
if ( $macroName eq 'AdminBar' ) {
|
|
||||||
$content =~ s/\Q$macroCall//g;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$template->template( $content );
|
$template->template( $content );
|
||||||
$template->write;
|
$template->write;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
done;
|
||||||
|
|
||||||
|
start_step "Removing Admin Bar";
|
||||||
|
|
||||||
|
session->config->delete( 'macros/AdminBar' );
|
||||||
|
|
||||||
done;
|
done;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue