Skip to content

Commit 775e68a

Browse files
committed
Update the renderer to use getAssetsUrl and the current develop branch
(or PG-2.17 branch) of PG.
1 parent b6d3c90 commit 775e68a

32 files changed

+1071
-1251
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ render_app.conf
33
lib/WeBWorK/htdocs/tmp/renderer/gif/*
44
lib/WeBWorK/htdocs/tmp/renderer/images/*
55
lib/WeBWorK/htdocs/DATA/*.json
6-
lib/WeBWorK/htdocs/js/apps/Problem/problem.css
76
lib/WeBWorK/bin/*
87
webwork-open-problem-library/
98
private/
10-
tmp/
9+
tmp/*
1110
logs/*.log
1211
node_modules
1312
node_modules/*

.gitmodules

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[submodule "lib/PG"]
22
path = lib/PG
3-
url = https://github.com/drgrice1/pg.git
4-
branch = ww3-rendering-work-new
3+
url = https://github.com/openwebwork/pg.git
4+
branch = main

lib/PG

Submodule PG updated 162 files

lib/RenderApp.pm

+13-35
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ BEGIN {
55
use Mojo::File;
66
$main::dirname = Mojo::File::curfile->dirname;
77

8-
#RENDER_ROOT is required for initializing conf files
8+
# RENDER_ROOT is required for initializing conf files.
99
$ENV{RENDER_ROOT} = $main::dirname->dirname
1010
unless ( defined( $ENV{RENDER_ROOT} ) );
1111

12-
#WEBWORK_ROOT is required for PG/lib/WeBWorK/IO
13-
$ENV{WEBWORK_ROOT} = $main::dirname . '/WeBWorK'
14-
unless ( defined( $ENV{WEBWORK_ROOT} ) );
12+
# WEBWORK_ROOT is required for PG/lib/WeBWorK/IO.
13+
# PG_ROOT is required for PG/lib/PGEnvironment.pm.
14+
# These are hardcoded to avoid conflict with environment variables for webwork2.
15+
# There is no need for these to be configurable.
16+
$ENV{WEBWORK_ROOT} = $main::dirname . '/WeBWorK';
17+
$ENV{PG_ROOT} = $main::dirname . '/PG';
1518

16-
#used for reconstructing library paths from sym-links
19+
# Used for reconstructing library paths from sym-links.
1720
$ENV{OPL_DIRECTORY} = "webwork-open-problem-library";
1821
$WeBWorK::Constants::WEBWORK_DIRECTORY = $main::dirname . "/WeBWorK";
1922
$WeBWorK::Constants::PG_DIRECTORY = $main::dirname . "/PG";
@@ -60,7 +63,7 @@ sub startup {
6063

6164
# Handle optional CORS settings
6265
if (my $CORS_ORIGIN = $self->config('CORS_ORIGIN')) {
63-
die "CORS_ORIGIN ($CORS_ORIGIN) must be an absolute URL or '*'"
66+
die "CORS_ORIGIN ($CORS_ORIGIN) must be an absolute URL or '*'"
6467
unless ($CORS_ORIGIN eq '*' || $CORS_ORIGIN =~ /^https?:\/\//);
6568

6669
$self->hook(before_dispatch => sub {
@@ -108,35 +111,10 @@ sub startup {
108111
$r->post('/render-api/tags')->to('IO#setTags');
109112
}
110113

111-
# Route requests for webwork2_files/CAPA_Graphics to render root Contrib/CAPA/CAPA_Graphics
112-
$r->any(
113-
'/webwork2_files/CAPA_Graphics/*static' => sub {
114-
my $c = shift;
115-
$c->reply->file("$ENV{RENDER_ROOT}/Contrib/CAPA/CAPA_Graphics/" . $c->stash('static'));
116-
}
117-
);
118-
119-
# Route requests for webwork2_files/tmp to the render root tmp
120-
# This should really be configurable.
121-
$r->any(
122-
'/webwork2_files/tmp/*static' => sub {
123-
my $c = shift;
124-
$c->reply->file("$ENV{RENDER_ROOT}/tmp/" . $c->stash('static'));
125-
}
126-
);
127-
128-
# Route requests to webwork2_files to lib/WeBWorK/htdocs if the requested file exists there,
129-
# otherwise route them to lib/PG/htdocs.
130-
$r->any(
131-
'/webwork2_files/*static' => sub {
132-
my $c = shift;
133-
if (-e "$WeBWorK::Constants::WEBWORK_DIRECTORY/htdocs/" . $c->stash('static')) {
134-
$c->reply->file("$WeBWorK::Constants::WEBWORK_DIRECTORY/htdocs/" . $c->stash('static'));
135-
} else {
136-
$c->reply->file("$WeBWorK::Constants::PG_DIRECTORY/htdocs/" . $c->stash('static'));
137-
}
138-
}
139-
);
114+
# Static file routes
115+
$r->any('/webwork2_files/CAPA_Graphics/*static')->to('StaticFiles#CAPA_graphics_file');
116+
$r->any('/webwork2_files/tmp/*static')->to('StaticFiles#temp_file');
117+
$r->any('/pg_files/*static')->to('StaticFiles#pg_file');
140118

141119
# any other requests fall through
142120
$r->any('/*fail' => sub {

lib/RenderApp/Controller/FormatRenderedProblem.pm

+20-29
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ FormatRenderedProblem.pm
2424

2525
package RenderApp::Controller::FormatRenderedProblem;
2626

27+
use warnings;
28+
use strict;
29+
2730
use lib "$WeBWorK::Constants::WEBWORK_DIRECTORY/lib";
2831
use lib "$WeBWorK::Constants::PG_DIRECTORY/lib";
2932
use MIME::Base64 qw( encode_base64 decode_base64);
3033
use WeBWorK::Utils::AttemptsTable; #import from ww2
3134
use WeBWorK::PG::ImageGenerator; # import from ww2
3235
use WeBWorK::Utils::LanguageAndDirection;
33-
use WeBWorK::Utils qw( wwRound); # required for score summary
36+
use WeBWorK::Utils qw(wwRound getAssetURL); # required for score summary
3437
use WeBWorK::Localize ; # for maketext
3538
our $UNIT_TESTS_ON = 0;
3639

@@ -46,7 +49,7 @@ sub format_hash_ref {
4649
sub new {
4750
my $invocant = shift;
4851
my $class = ref $invocant || $invocant;
49-
$self = { # Is this function redundant given the declarations within sub formatRenderedProblem?
52+
my $self = { # Is this function redundant given the declarations within sub formatRenderedProblem?
5053
return_object => {},
5154
encoded_source => {},
5255
sourceFilePath => '',
@@ -199,7 +202,7 @@ sub formatRenderedProblem {
199202
answersSubmitted => $self->{inputs_ref}{answersSubmitted}//0,
200203
answerOrder => $answerOrder//[],
201204
displayMode => $self->{inputs_ref}{displayMode},
202-
imgGen => $imgGen,
205+
imgGen => undef, # $imgGen,
203206
ce => '', #used only to build the imgGen
204207
showAnswerNumbers => 0,
205208
showAttemptAnswers => 0,
@@ -242,39 +245,33 @@ sub formatRenderedProblem {
242245

243246
# Add JS files requested by problems via ADD_JS_FILE() in the PG file.
244247
my $extra_js_files = '';
245-
if (ref($rh_result->{flags}{extra_js_files}) eq "ARRAY") {
248+
if (ref($rh_result->{flags}{extra_js_files}) eq 'ARRAY') {
246249
$rh_result->{js} = [];
247250
my %jsFiles;
248251
for (@{ $rh_result->{flags}{extra_js_files} }) {
249252
next if $jsFiles{ $_->{file} };
250253
$jsFiles{ $_->{file} } = 1;
251-
my $attributes = ref($_->{attributes}) eq "HASH" ? $_->{attributes} : {};
254+
my %attributes = ref($_->{attributes}) eq 'HASH' ? %{ $_->{attributes} } : ();
252255
if ($_->{external}) {
253256
push @{ $rh_result->{js} }, $_->{file};
254-
$extra_js_files .= CGI::script({ src => $_->{file}, %$attributes }, '');
255-
} elsif (
256-
!$_->{external}
257-
&& (-f "$WeBWorK::Constants::WEBWORK_DIRECTORY/htdocs/$_->{file}"
258-
|| -f "$WeBWorK::Constants::PG_DIRECTORY/htdocs/$_->{file}")
259-
)
260-
{
261-
push @{ $rh_result->{js} }, "$webwork_htdocs_url/$_->{file}";
262-
$extra_js_files .= CGI::script({ src => "$webwork_htdocs_url/$_->{file}", %$attributes }, '');
257+
$extra_js_files .= CGI::script({ src => $_->{file}, %attributes }, '');
263258
} else {
264-
$extra_js_files .= "<!-- $_->{file} is not available in htdocs/ on this server -->";
259+
my $url = getAssetURL($self->{inputs_ref}{language} // 'en', $_->{file});
260+
push @{ $rh_result->{js} }, "$SITE_URL/$url";
261+
$extra_js_files .= CGI::script({ src => $url, %attributes }, '');
265262
}
266263
}
267264
}
268265

269266
# Add CSS files requested by problems via ADD_CSS_FILE() in the PG file
270-
# or via a setting of $ce->{pg}{specialPGEnvironmentVars}{extra_css_files}
267+
# or via a setting of $self->{ce}{pg}{specialPGEnvironmentVars}{extra_css_files}
271268
# (the value should be an anonomous array).
272269
my $extra_css_files = '';
273270
my @cssFiles;
274-
if (ref($ce->{pg}{specialPGEnvironmentVars}{extra_css_files}) eq "ARRAY") {
275-
push(@cssFiles, { file => $_, external => 0 }) for @{ $ce->{pg}{specialPGEnvironmentVars}{extra_css_files} };
271+
if (ref($self->{ce}{pg}{specialPGEnvironmentVars}{extra_css_files}) eq 'ARRAY') {
272+
push(@cssFiles, { file => $_, external => 0 }) for @{ $self->{ce}{pg}{specialPGEnvironmentVars}{extra_css_files} };
276273
}
277-
if (ref($rh_result->{flags}{extra_css_files}) eq "ARRAY") {
274+
if (ref($rh_result->{flags}{extra_css_files}) eq 'ARRAY') {
278275
push @cssFiles, @{ $rh_result->{flags}{extra_css_files} };
279276
}
280277
my %cssFilesAdded; # Used to avoid duplicates
@@ -284,17 +281,11 @@ sub formatRenderedProblem {
284281
$cssFilesAdded{ $_->{file} } = 1;
285282
if ($_->{external}) {
286283
push @{ $rh_result->{css} }, $_->{file};
287-
$extra_css_files .= CGI::Link({ rel => 'stylesheet', href => "$_->{file}" });
288-
} elsif (
289-
!$_->{external}
290-
&& (-f "$WeBWorK::Constants::WEBWORK_DIRECTORY/htdocs/$_->{file}"
291-
|| -f "$WeBWorK::Constants::PG_DIRECTORY/htdocs/$_->{file}")
292-
)
293-
{
294-
push @{ $rh_result->{css} }, "$webwork_htdocs_url/$_->{file}";
295-
$extra_css_files .= CGI::Link({ rel => 'stylesheet', href => "$webwork_htdocs_url/$_->{file}" });
284+
$extra_css_files .= CGI::Link({ rel => 'stylesheet', href => $_->{file} });
296285
} else {
297-
$extra_css_files .= qq{<!-- $_->{file} is not available in htdocs/ on this server -->};
286+
my $url = getAssetURL($self->{inputs_ref}{language} // 'en', $_->{file});
287+
push @{ $rh_result->{css} }, "$SITE_URL/$url";
288+
$extra_css_files .= CGI::Link({ href => $url, rel => 'stylesheet' });
298289
}
299290
}
300291

lib/RenderApp/Controller/RenderProblem.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ sub standaloneRenderer {
368368
refreshMath2img => 1,
369369
processAnswers => $processAnswers,
370370
QUIZ_PREFIX => $inputs_ref->{answerPrefix} // '',
371-
useMathQuill => 1,
371+
useMathQuill => !defined $inputs_ref->{entryAssist} || $inputs_ref->{entryAssist} eq 'MathQuill' ? 1 : 0,
372372

373373
#use_site_prefix => 'http://localhost:3000',
374374
use_opaque_prefix => 0,
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package RenderApp::Controller::StaticFiles;
2+
use Mojo::Base 'Mojolicious::Controller', -signatures;
3+
4+
use Mojo::File qw(path);
5+
6+
sub reply_with_file_if_readable ($c, $file) {
7+
if (-r $file) {
8+
return $c->reply->file($file);
9+
} else {
10+
return $c->render(data => 'File not found', status => 404);
11+
}
12+
}
13+
14+
# Route requests for webwork2_files/CAPA_Graphics to render root Contrib/CAPA/CAPA_Graphics
15+
sub CAPA_graphics_file ($c) {
16+
return $c->reply_with_file_if_readable($c->app->home->child('Contrib/CAPA/CAPA_Graphics', $c->stash('static')));
17+
}
18+
19+
# Route requests for webwork2_files to the render root tmp. The
20+
# only requests should be for files in the temporary directory.
21+
# FIXME: Perhaps this directory should be configurable.
22+
sub temp_file ($c) {
23+
$c->reply_with_file_if_readable($c->app->home->child('tmp', $c->stash('static')));
24+
}
25+
26+
# Route request to pg_files to lib/PG/htdocs.
27+
sub pg_file ($c) {
28+
$c->reply_with_file_if_readable(path($WeBWorK::Constants::PG_DIRECTORY, 'htdocs', $c->stash('static')));
29+
}
30+
31+
1;

lib/WeBWorK/VERSION

-4
This file was deleted.

0 commit comments

Comments
 (0)