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 Oct 23, 2023
1 parent 7ef3d3f commit 9fea99e
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 235 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
libjson-perl \
libjson-xs-perl \
liblocale-maketext-lexicon-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 @@ -18,6 +18,7 @@ on runtime => sub {
requires 'JSON::XS';
requires 'Locale::Maketext';
requires 'Locale::Maketext::Lexicon';
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 @@ -21,6 +21,7 @@ RUN apt-get update \
libjson-perl \
libjson-xs-perl \
liblocale-maketext-lexicon-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 @@ -561,6 +562,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
6 changes: 5 additions & 1 deletion lib/WeBWorK/PG.pm
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,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 @@ -174,6 +174,8 @@ sub new_helper ($invocant, %options) {
}

# HTML_dpng uses an ImageGenerator. We have to render the queued equations.
# This must be done before the post processing, since the image tags output by the image generator initially
# include markers which are invalid html. Mojo::DOM will change these markers into attributes and this will fail.
if ($image_generator) {
my $sourceFile = "$options{templateDirectory}$options{sourceFilePath}";
$image_generator->render(
Expand All @@ -182,6 +184,8 @@ sub new_helper ($invocant, %options) {
);
}

$translator->post_process_content if ref($translator->{rh_pgcore}) eq 'PGcore';

return bless {
translator => $translator,
head_text => ${ $translator->r_header },
Expand Down
2 changes: 1 addition & 1 deletion lib/WeBWorK/PG/ImageGenerator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ sub fix_markers ($self) {
my %depths = %{ $self->{depths} };
for my $depthkey (keys %depths) {
if ($depths{$depthkey} eq 'none') {
${ $self->{body_text} } =~ s/MaRkEr$depthkey/style="vertical-align:"$self->{dvipng_align}"/g;
${ $self->{body_text} } =~ s/MaRkEr$depthkey/style="vertical-align:$self->{dvipng_align}"/g;
} else {
my $ndepth = 0 - $depths{$depthkey};
${ $self->{body_text} } =~ s/MaRkEr$depthkey/style="vertical-align:${ndepth}px"/g;
Expand Down
Loading

0 comments on commit 9fea99e

Please sign in to comment.