Skip to content

Commit e44622a

Browse files
authored
Merge pull request openwebwork#1198 from drgrice1/sample-problem-get-code
Add a `getSampleProblemCode` method to `SampleProblemParser.pm`.
2 parents e31eb63 + 06145e9 commit e44622a

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

Diff for: lib/SampleProblemParser.pm

+40-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use File::Basename qw(dirname basename);
2525
use File::Find qw(find);
2626
use Pandoc;
2727

28-
our @EXPORT_OK = qw(parseSampleProblem generateMetadata);
28+
our @EXPORT_OK = qw(parseSampleProblem generateMetadata getSampleProblemCode);
2929

3030
=head1 NAME
3131
@@ -150,7 +150,7 @@ sub generateMetadata ($problem_dir, %options) {
150150
say "Reading file: $File::Find::name" if $options{verbose};
151151

152152
if ($File::Find::name =~ /\.pg$/) {
153-
my $metadata = parseMetadata($File::Find::name, $problem_dir, $options{macro_locations});
153+
my $metadata = parseMetadata($File::Find::name, $problem_dir);
154154
unless (@{ $metadata->{types} }) {
155155
warn "The type of sample problem is missing for $File::Find::name.";
156156
return;
@@ -175,7 +175,7 @@ my @macros_to_skip = qw(
175175
PGstandard.pl
176176
);
177177

178-
sub parseMetadata ($path, $problem_dir, $macro_locations = {}) {
178+
sub parseMetadata ($path, $problem_dir) {
179179
open(my $FH, '<:encoding(UTF-8)', $path) or do {
180180
warn qq{Could not open file "$path": $!};
181181
return {};
@@ -228,4 +228,41 @@ sub parseMetadata ($path, $problem_dir, $macro_locations = {}) {
228228
return $metadata;
229229
}
230230

231+
=head2 C<getSampleProblemCode>
232+
233+
Parse a PG file with extra documentation comments and strip that all out
234+
returning the clean problem code. This returns the same code that the
235+
C<parseSampleProblem> returns, except at much less expense as it does not parse
236+
the documentation, it does not require that the metadata be parsed first, and it
237+
does not need macro POD information.
238+
239+
=cut
240+
241+
sub getSampleProblemCode ($file) {
242+
my $filename = basename($file);
243+
open(my $FH, '<:encoding(UTF-8)', $file) or do {
244+
warn qq{Could not open file "$file": $!};
245+
return '';
246+
};
247+
my @file_contents = <$FH>;
248+
close $FH;
249+
250+
my (@code_rows, $inCode);
251+
252+
while (my $row = shift @file_contents) {
253+
chomp($row);
254+
$row =~ s/\t/ /g;
255+
if ($row =~ /^#:(.*)?/) {
256+
# This is documentation so skip it.
257+
} elsif ($row =~ /^\s*(END)?DOCUMENT.*$/) {
258+
$inCode = $1 ? 0 : 1;
259+
push(@code_rows, $row);
260+
} elsif ($inCode) {
261+
push(@code_rows, $row);
262+
}
263+
}
264+
265+
return join("\n", @code_rows);
266+
}
267+
231268
1;

0 commit comments

Comments
 (0)