qcm-insere.pl

Script perl - Alexis Bienvenüe, 07/12/2022 02:29 pm

Download (1.9 kB)

 
1
#! /usr/bin/perl
2
3
use Getopt::Long;
4
5
my $qf='';
6
7
GetOptions("questions=s"=>\$qf,
8
	   );
9
10
my %q=();
11
my %global=();
12
my $k='';
13
my %boucle=();
14
15
open(QUEST,$qf) or die "Error opening $qf : $!";
16
while(<QUEST>) {
17
    chomp;
18
    $ko=$k;
19
    if(/^\%([\+=])\s*$/) {
20
	$k=$1;
21
	die "Cles imbriquees : $k dans $ko" if($ko && $ko !~ /^[\+=]$/);
22
    }
23
    if(/^\s*\\begin\{(question|questionmultx?)\}\{([^}]+)\}/) {
24
	$k=$2;
25
	die "Cles imbriquees : $k dans $ko" if($ko && $ko !~ /^[\+=]$/);
26
	die "Cle double : $k" if($q{$k});
27
	$global{$k}=$q{'+'};
28
	$q{'+'}=[];
29
	$boucle{$k}=$q{'='};
30
	$q{'='}=[];
31
    }
32
    if(/^\%<([^\s]+)/) {
33
	die "Cles imbriquees : $1 dans $k" if($k);
34
	$k=$1;
35
    }
36
    push @{$q{$k}},$_ if($k);
37
    if(/^\s*\\end\{(question|questionmultx?)\}/) {
38
	die "Cle non ouverte" if(! $k);
39
	$k='';
40
    }
41
    if(/^\%>([^\s]+)/) {
42
	die "Cle mal ouverte : $1 / $k" if($k ne $1);
43
	$k='';
44
    }
45
}
46
close(QUEST);
47
48
sub inclusion {
49
    my ($k,$gr)=@_;
50
    die "Question introuvable : $k" if(!$q{$k});
51
    print join("\n",@{$global{$k}})."\n\n" if(@{$global{$k}});
52
    push @{$q{'BOUCLE'}},@{$boucle{$k}};
53
    print "\\element{$gr}{\n" if($gr);
54
    print join("\n",@{$q{$k}})."\n";
55
    print "}\n\n" if($gr);
56
}
57
58
%groupes=();
59
60
$groupe_courant = 'grq';
61
62
while (<>) {
63
    chomp;
64
65
    if (/^\s*\\exemplaire\{/) {
66
        print("\n");
67
        for my $g ( keys %groupes ) {
68
            print("\\setgroupmode{$g}{cyclic}\n");
69
            print("\\element{$groupes{$g}->{in}}{\n\n\\insertgroup[1]{$g}\n\n}\n");
70
            print("\n");
71
        }
72
    }
73
74
    print "$_\n";
75
    if (/^\%[Q]\[(.+)\]\{(.+)\}/) {
76
        my $q = $1;
77
        my $g = $2;
78
        $groupes{$g}->{in}=$groupe_courant;
79
        $groupes{$g}->{n}++;
80
        inclusion( $q, $g );
81
    } elsif (/^\%[Q]\[(.+)\]\[(.+)\]/) {
82
        inclusion( $1, $2 );
83
    } elsif (/^\%([QT])\[(.+)\]/) {
84
        inclusion( $2, ( $1 eq 'T' ? '' : $groupe_courant ) );
85
    } elsif (/^\%G=([^\s]+)/) {
86
        $groupe_courant=$1;
87
    }
88
}