Skip to content

Commit

Permalink
Add a text post processing phase to the translator.
Browse files Browse the repository at this point in the history
Macros or problems can add a hook by calling
`add_content_post_processor` with a subroutine that will be called after
the translator has processed answers.  For all display modes except TeX,
the subroutine will be passed the problem text parsed into a Mojo::DOM
object and a reference to the header text.  For the TeX display mode a
reference to the problem text is passed.  The subroutine can then modify
the problem DOM or text, and header text.  The resulting Mojo::DOM
object will be converted back to a string and the problem text reference
returned by the translator set to point to that string.

Note that the `$PG_PROBLEM_TEXT_ARRAY_REF` has been removed from the
translator code.  It is never used and is redundant with the
`$PG_PROBLEM_TEXT_REF` which is all that is used.  It was becoming
annoying to maintain both in a compatible manner.

The hidden MathQuill inputs for the latex string is added in this post
processing stage (see the ENDDOCUMENT method in PG.pl).
  • Loading branch information
drgrice1 committed Sep 16, 2023
1 parent fc2bb90 commit 1ec1f6a
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 231 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
libhtml-parser-perl \
libjson-perl \
libjson-xs-perl \
libmojolicious-perl \
libtest2-suite-perl \
libtie-ixhash-perl \
libuuid-tiny-perl \
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ on runtime => sub {
requires 'JSON';
requires 'JSON::XS';
requires 'Locale::Maketext';
requires 'Mojolicious';
requires 'Tie::IxHash';
requires 'Types::Serialiser';
requires 'UUID::Tiny';
Expand Down
1 change: 1 addition & 0 deletions docker/pg.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN apt-get update \
libhtml-parser-perl \
libjson-perl \
libjson-xs-perl \
libmojolicious-perl \
libtest2-suite-perl \
libtie-ixhash-perl \
libuuid-tiny-perl \
Expand Down
2 changes: 1 addition & 1 deletion htdocs/js/InputColor/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
answerInput.focus();
});
} else {
answerLink.href = '';
answerLink.removeAttribute('href');
}
};

Expand Down
7 changes: 7 additions & 0 deletions lib/PGcore.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ sub new {
PG_alias => undef,
PG_problem_grader => undef,
displayMode => undef,
content_post_processors => [],
envir => $envir,
WARNING_messages => [],
DEBUG_messages => [],
Expand Down Expand Up @@ -563,6 +564,12 @@ sub get_persistent_data {
return $self->{PERSISTENCE_HASH}{$label};
}

sub add_content_post_processor {
my ($self, $handler) = @_;
push(@{ $self->{content_post_processors} }, $handler) if ref($handler) eq 'CODE';
return;
}

sub check_answer_hash {
my $self = shift;
foreach my $key (keys %{ $self->{PG_ANSWERS_HASH} }) {
Expand Down
4 changes: 3 additions & 1 deletion lib/WeBWorK/PG.pm
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ sub new_helper ($invocant, %options) {
}
}

$translator->translate();
$translator->translate;

# IMPORTANT: The translator environment should not be trusted after the problem code runs.

Expand Down Expand Up @@ -172,6 +172,8 @@ sub new_helper ($invocant, %options) {
);
}

$translator->post_process_content;

# HTML_dpng uses an ImageGenerator. We have to render the queued equations.
if ($image_generator) {
my $sourceFile = "$options{templateDirectory}$options{sourceFilePath}";
Expand Down
Loading

0 comments on commit 1ec1f6a

Please sign in to comment.