Skip to content

Commit

Permalink
accommodate includePGproblem and log assets
Browse files Browse the repository at this point in the history
  • Loading branch information
drdrew42 committed Jan 2, 2021
1 parent cf2443f commit e143b4d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
1 change: 1 addition & 0 deletions Contrib
1 change: 1 addition & 0 deletions Library
70 changes: 34 additions & 36 deletions lib/RenderApp/Controller/RenderProblem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use Getopt::Long qw[:config no_ignore_case bundling];
use File::Find;
use FileHandle;
use File::Path;
use File::Basename;

#use File::Temp qw/tempdir/;
use String::ShellQuote;
Expand Down Expand Up @@ -130,6 +131,7 @@ sub process_pg_file {
problem_state => $pg_obj->{problem_state},
flags => $pg_obj->{flags},
form_data => $inputHash,
assets => $pg_obj->{assets},
};

# havoc caused by problemRandomize.pl inserting CODE ref into pg->{flags}
Expand Down Expand Up @@ -181,24 +183,31 @@ sub process_problem {
#$inputs_ref->{pathToProblemFile} = $adj_file_path;
}

# TODO: rework using app_root level symlinks instead
while ($source =~ m/^# This file is just a pointer/
&& $source =~ m/includePGproblem\("(.*)"\);/ )
my $assets = [];
while ($source =~ m/includePG(?:problem|file)\(["'](.*)["']\);/g )
{
warn "problem-level redirect found!\nMatch 1: "
. $1
. "\nMatch 2: "
. $2 . "\n"
if $UNIT_TESTS_ON;
my $redirect = $1;
$redirect =~
s/^Library/webwork-open-problem-library\/OpenProblemLibrary/;
warn "REDIRECTING TO: " . $redirect . "\n" if $UNIT_TESTS_ON;
( $adj_file_path, $source ) = get_source($redirect);
warn "PG asset reference found!\n" . $1 . "\n" if $UNIT_TESTS_ON;
my $pgAsset = $1;
$pgAsset = dirname($file_path) . "/" . $pgAsset if ($pgAsset =~ /^[^\/]*\.pg$/);
warn "Recording PG asset as: $pgAsset\n" if $UNIT_TESTS_ON;
# warn "REDIRECTING TO: " . $redirect . "\n" if $UNIT_TESTS_ON;
# ( $adj_file_path, $source ) = get_source($redirect);
push @$assets, $pgAsset;
}

$inputs_ref->{pathToProblemFile} = $adj_file_path
if ( defined $adj_file_path );
# this does not capture _all_ image asset references, unfortunately...
# asset filenames may be stored as variables before image() is called
while ($source =~ m/image\(\s*("[^\$]+?"|'[^\$]+?')\s*[,\)]/g) {
warn "Image asset reference found!\n" . $1 . "\n";
my $image = $1;
$image =~ s/['"]//g;
$image = dirname($file_path) . '/' . $image if ($image =~ /^[^\/]*\.(?:gif|jpg|jpeg|png)$/i);
warn "Recording image asset as: $image\n";
push @$assets, $image;
}

# $inputs_ref->{pathToProblemFile} = $adj_file_path
# if ( defined $adj_file_path );

##################################################
# Process the pg file
Expand All @@ -220,6 +229,9 @@ sub process_problem {
# can include @args as fourth input below
$return_object = standaloneRenderer( $ce, \$source, $inputs_ref );

# stash assets list in $return_object
$return_object->{assets} = $assets;

#######################################################################
# Handle errors
#######################################################################
Expand Down Expand Up @@ -511,14 +523,13 @@ sub get_source {
local $/ = undef;
if ( $file_path eq '-' ) {
$source = <STDIN>;
}
else {
# To support proper behavior with UTF-8 files, we need to open them with "<:encoding(UTF-8)"
# as otherwise, the first HTML file will render properly, but when "Preview" "Submit answer"
# or "Show correct answer" is used it will make problems, as in process_problem() the
# encodeSource() method is called on a data which is still UTF-8 encoded, and leads to double
# encoding and gibberish.
# NEW:
} else {
# To support proper behavior with UTF-8 files, we need to open them with "<:encoding(UTF-8)"
# as otherwise, the first HTML file will render properly, but when "Preview" "Submit answer"
# or "Show correct answer" is used it will make problems, as in process_problem() the
# encodeSource() method is called on a data which is still UTF-8 encoded, and leads to double
# encoding and gibberish.
# NEW:
open( FH, "<:encoding(UTF-8)", $file_path )
or die "Couldn't open file $file_path: $!";

Expand All @@ -529,19 +540,6 @@ sub get_source {
}
};
die "Something is wrong with the contents of $file_path\n" if $@;

# TODO: rework using app_root level symlinks instead
while ($source =~ m/^# This file is just a pointer/
&& $source =~ m/includePGproblem\("(.*)"\);/ )
{
warn "problem-level redirect found! " . $1 . "\n" if $UNIT_TESTS_ON;
my $redirect = $1;
$redirect =~
s/^Library/webwork-open-problem-library\/OpenProblemLibrary/;
warn "REDIRECTING TO: " . $redirect . "\n" if $UNIT_TESTS_ON;
( $file_path, $source ) = get_source($redirect);
}

return $file_path, $source;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/WeBWorK/conf/site.conf
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ $webwork_htdocs_dir = "$webwork_dir/htdocs";

# URL and path to courses directory.
$webwork_courses_url = "/webwork2_course_files";
$webwork_courses_dir = "/opt/webwork/courses"; # a typical place to put course directories
$webwork_courses_dir = "$ENV{RENDER_ROOT}"; # standalone renderer has no courses


################################################################################
Expand Down
1 change: 1 addition & 0 deletions lib/WeBWorK/lib/WeBWorK/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use DateTime;
use DateTime::TimeZone;
use Date::Format;
use Encode qw(encode_utf8 decode_utf8);
use File::Spec::Functions qw(canonpath);

use constant DATE_FORMAT => "%m/%d/%Y at %I:%M%P %Z";
use constant MKDIR_ATTEMPTS => 10;
Expand Down

0 comments on commit e143b4d

Please sign in to comment.