forked from openwebwork/webwork2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile the achievement notification email templates in a safe compar…
…tment. To do so the WeBWorK::SafeTemplate module must be used. This module derives from the Mojo::Template module and overrides the _trap method. The Mojo::Template _wrap method adds "use Mojo::Base -strict" and "no warnings 'ambiguous'" to the code generated in this method. When that is called later it causes an error in the safe compartment because "require" is trapped. So instead the WeBWorK::SafeTemplate override method imports strict, warnings, and utf8 which is equivalent to calling "use Mojo::Base -strict". Calling "no warnings 'ambiguous'" prevents warnings from a lack of parentheses on a scalar call in the generated code. So if this package is used, those warnings need to be prevented in another way. That is done when the module is used in Mojolicious::WeBWorK::Tasks::AchievementNotification using a $SIG{__WARN__} handler that doesn't do anything if the ambiguous warning is encountered.
- Loading branch information
Showing
3 changed files
with
65 additions
and
17 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
courses.dist/modelCourse/templates/achievements/notifications/default.html.ep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package WeBWorK::SafeTemplate; | ||
use Mojo::Base 'Mojo::Template'; | ||
|
||
# The Mojo::Template _wrap method adds "use Mojo::Base -strict" and "no warnings 'ambiguous'" to the code generated in | ||
# this method. When that is called later it causes an error in the safe compartment because "require" is trapped. So | ||
# instead this imports strict, warnings, and utf8 which is equivalent to calling "use Mojo::Base -strict". Calling "no | ||
# warnings 'ambiguous'" prevents warnings from a lack of parentheses on a scalar call in the generated code. So if this | ||
# package is used, those warnings need to be prevented in another way. | ||
sub _wrap { | ||
my ($self, $body, $vars) = @_; | ||
|
||
# Variables | ||
my $args = ''; | ||
if ($self->vars && (my @vars = grep {/^\w+$/} keys %$vars)) { | ||
$args = 'my (' . join(',', map {"\$$_"} @vars) . ')'; | ||
$args .= '= @{shift()}{qw(' . join(' ', @vars) . ')};'; | ||
} | ||
|
||
# Wrap lines | ||
my $num = () = $body =~ /\n/g; | ||
my $code = $self->_line(1) . "\npackage @{[$self->namespace]};"; | ||
$code .= 'BEGIN { strict->import; warnings->import; utf8->import; }'; | ||
$code .= "sub { my \$_O = ''; @{[$self->prepend]};{ $args { $body\n"; | ||
$code .= $self->_line($num + 1) . "\n;}@{[$self->append]}; } \$_O };"; | ||
|
||
return $code; | ||
} | ||
|
||
1; |