@@ -25,7 +25,7 @@ use File::Basename qw(dirname basename);
25
25
use File::Find qw( find) ;
26
26
use Pandoc;
27
27
28
- our @EXPORT_OK = qw( parseSampleProblem generateMetadata) ;
28
+ our @EXPORT_OK = qw( parseSampleProblem generateMetadata getSampleProblemCode ) ;
29
29
30
30
=head1 NAME
31
31
@@ -150,7 +150,7 @@ sub generateMetadata ($problem_dir, %options) {
150
150
say " Reading file: $File::Find::name " if $options {verbose };
151
151
152
152
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 );
154
154
unless (@{ $metadata -> {types } }) {
155
155
warn " The type of sample problem is missing for $File::Find::name ." ;
156
156
return ;
@@ -175,7 +175,7 @@ my @macros_to_skip = qw(
175
175
PGstandard.pl
176
176
) ;
177
177
178
- sub parseMetadata ($path , $problem_dir , $macro_locations = {} ) {
178
+ sub parseMetadata ($path , $problem_dir ) {
179
179
open (my $FH , ' <:encoding(UTF-8)' , $path ) or do {
180
180
warn qq{ Could not open file "$path ": $! } ;
181
181
return {};
@@ -228,4 +228,41 @@ sub parseMetadata ($path, $problem_dir, $macro_locations = {}) {
228
228
return $metadata ;
229
229
}
230
230
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
+
231
268
1;
0 commit comments