Skip to content

Commit

Permalink
Merge pull request #1133 from drgrice1/pg-tidy-enddocument-handling
Browse files Browse the repository at this point in the history
Update WeBWorK::PG::Tidy to handle content after ENDDOCUMENT.
  • Loading branch information
pstaabp authored Oct 29, 2024
2 parents 1c7291f + b1ef049 commit b6f45ee
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/WeBWorK/PG/Tidy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ use warnings;
use Perl::Tidy;
use Mojo::File qw(curfile);

our @EXPORT = qw(pgtidy);
our @EXPORT_OK = qw(pgtidy);

my $perltidy_pg_rc = curfile->dirname->dirname->dirname->dirname . '/bin/perltidy-pg.rc';

my $contentAfterEnd;

# Apply the same preprocessing as the PG Translator, except for the removal of everything after ENDDOCUMENT.
my $prefilter = sub {
my $evalString = shift // '';

# Remove text after ENDDOCUMENT, but save it to add back later. Note that unlike the translator,
# this is done first so that its contents are not modified by the substitutions below.
$contentAfterEnd = $evalString =~ /ENDDOCUMENT(?:\h*\([^)]*\))?(?:\h*;)?(.*)/ms ? $1 : undef;
$evalString =~ s/ENDDOCUMENT.*/ENDDOCUMENT();/s;

$evalString =~ s/\n\h*END_TEXT[\h;]*\n/\nEND_TEXT\n/g;
$evalString =~ s/\n\h*END_PGML[\h;]*\n/\nEND_PGML\n/g;
$evalString =~ s/\n\h*END_PGML_SOLUTION[\h;]*\n/\nEND_PGML_SOLUTION\n/g;
Expand Down Expand Up @@ -88,11 +95,17 @@ my $postfilter = sub {
$evalString =~ s/(.*)->tex\(<<END_LATEX_IMAGE\);/$1->BEGIN_LATEX_IMAGE/g;

# Care is needed to reverse the preprocessing here.
# First in all occurences of an odd number of backslashes the first backslash is replaced with two tildes.
$evalString =~ s/(?<!\\) \\ ((?:\\{2})*) (?!\\)/~~$1/gx;
# First in all occurences of an odd number of backslashes the last backslash is replaced with two tildes.
$evalString =~ s/(?<!\\) \\ ((?:\\{2})*) (?!\\)/$1~~/gx;
# Then all pairs of backslashes are replaced with a single backslash.
$evalString =~ s/\\\\/\\/g;

# Add back any content that was after ENDDOCUMENT.
if ($contentAfterEnd) {
chomp($evalString);
$evalString .= $contentAfterEnd;
}

return $evalString;
};

Expand Down

0 comments on commit b6f45ee

Please sign in to comment.