Skip to content

Commit ba081b2

Browse files
committed
Handle cases where qw blocks are mixed with strings in ' or "
in loadMacros
1 parent 9d0c244 commit ba081b2

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

lib/WeBWorK/PG/ConvertToPGML.pm

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,29 +107,45 @@ sub convertToPGML {
107107
push(@pgml_block, $row);
108108
} elsif ($row =~ /loadMacros\(/) {
109109
# Parse the macros, which may be on multiple rows and may be in a qw block.
110-
my $macros = '';
110+
my $macros = '';
111+
my $num_macro_lines = 0; # store the number of lines in the loadMacro so the output is similar to the input.
111112
while (1) {
112113
# Remove comments within loadMacros block (should we keep them?)
113114
$row =~ s/#.*$//;
114115
$macros .= $row;
115116
last if ($row =~ /(.*)\);/);
117+
++$num_macro_lines;
116118
$row = shift @rows;
117119
}
118-
119120
my @macros = ();
120121
my ($qw_start, $qw_end); # the characters if the loadMacros has a qw block.
121122

122123
# The following can parse loadMacros in the form loadMacros('macro1.pl', 'macro2.pl'); or
123124
# loadMacros(qw{macro1.pl macro2.pl});
124-
if ($macros =~ /loadMacros\((qw(.))?(.*?)(.)?\)/ms) {
125-
($qw_start, $qw_end) = ($2, $4);
125+
if ($macros =~ /loadMacros\((.*?)\);/ms) {
126+
my @macro_str = split(/\s*,\s*/, $1);
127+
128+
for my $str (@macro_str) {
129+
if ($str =~ /^qw(.)/) {
130+
my $qw_matches = { '{' => '}', '(' => ')', '[' => ']', '/' => '/', '|' => '|' };
131+
$qw_start = $1;
132+
$qw_end = $qw_matches->{$qw_start};
133+
134+
if ($str =~ /^qw\Q${qw_start}\E(.*?)\Q${qw_end}\E/) {
135+
push(@macros, split(/\s+/, $1));
136+
}
137+
} else {
138+
push(@macros, $str);
139+
}
140+
}
141+
126142
@macros =
127143
grep {
128144
$_
129145
&& $_ !~
130146
/(PGstandard|PGML|PGauxiliaryFunctions|PGbasicmacros|PGanswermacros|MathObjects|PGcourse|AnswerFormatHelp).pl/
131147
}
132-
map {s/['"]//gr} split(/\s+|\s*,\s*/, $3);
148+
map {s/['"]//gr} @macros;
133149

134150
# Remove any duplicates:
135151
my %seen;
@@ -141,7 +157,13 @@ sub convertToPGML {
141157
@macros = ('PGstandard.pl', 'PGML.pl', @macros, 'PGcourse.pl');
142158

143159
if ($qw_start) {
144-
push(@all_lines, "loadMacros(qw$qw_start\n\t" . join("\n\t", @macros) . "\n$qw_end);");
160+
if ($num_macro_lines > 1) { # put each macro on a separate line
161+
push(@all_lines, "loadMacros(qw$qw_start");
162+
push(@all_lines, "\t$_") for (@macros);
163+
push(@all_lines, "$qw_end);");
164+
} else {
165+
push(@all_lines, "loadMacros(qw$qw_start" . join(' ', @macros) . "$qw_end);");
166+
}
145167
} else {
146168
push(@all_lines, 'loadMacros(' . join(', ', map {"'$_'"} @macros) . ');');
147169
}

0 commit comments

Comments
 (0)