Browse files

we must never speak of this parse work.

  • Loading branch information...
1 parent 795a037 commit 954ac35b7b53ac1cc11d32102d4bae755b2ed268 @schelcj committed Oct 1, 2012
Showing with 111 additions and 3 deletions.
  1. +59 −3 create.pl
  2. +52 −0 parse.pl
View
62 create.pl
@@ -4,6 +4,9 @@
use WWW::Mechanize;
use Net::Netrc;
use Readonly;
+use File::Slurp;
+use File::Temp;
+use IO::Scalar;
use Data::Dumper;
Readonly::Scalar my $EMPTY => q{};
@@ -15,13 +18,66 @@
Readonly::Scalar my $DIRECTIONS => q{Hello World};
Readonly::Scalar my $SUMMARY => q{Goodbye World};
+my $latex = $ARGV[0]; # TODO - use getopts
my $lesson_name = q{2013}; # TODO - get from command line arg
my $title = q{SPH Algebra Assesment for 2013}; # TODO - get from command line arg
my $agent = get_login_agent();
+my $question_ref = parse_latex($latex);
-create_lesson($lesson_name, $title);
-my $resource_id = create_resource($lesson_name);
-create_question($resource_id, $lesson_name, $question, @answers);
+
+print Dumper $question_ref;
+exit;
+
+#create_lesson($lesson_name, $title);
+#my $resource_id = create_resource($lesson_name);
+#create_question($resource_id, $lesson_name, $question, @answers);
+
+sub parse_latex {
+ my ($test) = @_;
+ my @questions = ();
+ my $question_ref = [];
+ my $contents = read_file($test);
+ my $temp_fh = File::Temp->new();
+
+ $contents =~ s/^(?:(.*)?\\begin{document})|(?:\\end{document})$//gs;
+
+ write_file($temp_fh->filename, $contents);
+
+ {
+ local $/ = q{%QUESTION };
+ foreach my $line (read_file($temp_fh)) {
+ push @questions, $line;
+ }
+ }
+
+ foreach my $question (@questions) {
+ next if $question !~ /^(?<number>\d+)/;
+ my $number = $+{number};
+ $question_ref->[$number]->{number} = $number;
+
+ {
+ local $/ = q{%};
+ my $question_fh = IO::Scalar->new(\$question);
+ foreach my $line ($question_fh->getlines) {
+
+ given ($line) {
+ when ($line =~ /^$number\n/) {
+ my @parts = grep {/^\\/} split(/\n/, $line);
+ map {$_ =~ s/^(.*) \\\\$/$1/g} @parts;
+ $question_ref->[$number]->{question} = join(qq{\n}, @parts);
+ }
+ when ($line =~ /^$number(?<answer>([A-D]))\n/) {
+ my $answer = $+{answer};
+ $line =~ s/^${number}${answer}\n(.*)(?:(?:\s+[\\]+\s+[\n%]+)|\n+$)/$1/g;
+ $question_ref->[$number]->{answers}->{$answer} = $line;
+ }
+ }
+ }
+ }
+ }
+
+ return $question_ref;
+}
sub get_login_agent {
my $mach = Net::Netrc->lookup('cosign.umich.edu');
View
52 parse.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/env perl
+
+use Modern::Perl;
+use File::Slurp;
+use File::Temp;
+use IO::Scalar;
+use Data::Dumper;
+
+my $test = $ARGV[0];
+my @questions = ();
+my $question_ref = [];
+my $contents = read_file($test);
+my $temp_fh = File::Temp->new();
+
+$contents =~ s/^(?:(.*)?\\begin{document})|(?:\\end{document})$//gs;
+
+write_file($temp_fh->filename, $contents);
+
+{
+ local $/ = q{%QUESTION };
+ foreach my $line (read_file($temp_fh)) {
+ push @questions, $line;
+ }
+}
+
+foreach my $question (@questions) {
+ next if $question !~ /^(?<number>\d+)/;
+ my $number = $+{number};
+ $question_ref->[$number]->{number} = $number;
+
+ {
+ local $/ = q{%};
+ my $question_fh = IO::Scalar->new(\$question);
+ foreach my $line ($question_fh->getlines) {
+
+ given ($line) {
+ when ($line =~ /^$number\n/) {
+ my @parts = grep {/^\\/} split(/\n/, $line);
+ map {$_ =~ s/^(.*) \\\\$/$1/g} @parts;
+ $question_ref->[$number]->{question} = join(qq{\n}, @parts);
+ }
+ when ($line =~ /^$number(?<answer>([A-D]))\n/) {
+ my $answer = $+{answer};
+ $line =~ s/^${number}${answer}\n(.*)(?:(?:\s+[\\]+\s+[\n%]+)|\n+$)/$1/g;
+ $question_ref->[$number]->{answers}->{$answer} = $line;
+ }
+ }
+ }
+ }
+}
+
+print Dumper $question_ref;

0 comments on commit 954ac35

Please sign in to comment.