summaryrefslogtreecommitdiffstats
path: root/utilities/divideqqz.pl
blob: 90d8084bb28a33554531e07e3b6ad3fd96c1186c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env perl
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Divide qqz.tex for each chapter
#
# Usage:
#
# $ utilities/divideqqz.pl
#
# Note:
#
# Input file is fixed to qqz.tex.
#
# Name of each output file is taken from the 3rd argument of the
# \QuickQAC macro, which originates from the \QuickQuizChapter macro
# in source .tex files.
#
# Copyright (C) Akira Yokosawa, 2020
#
# Authors: Akira Yokosawa <akiyks@gmail.com>

use strict;
use warnings;

my $src_file = "qqz.tex";
my $chp_name;
my $QAC_ptn = "\\\\QuickQAC\\{[^}]*}\\{[^}]*}\\{([^}]*)}" ;
my $QA_ptn  = "\\\\QuickA\\{" ;
my $QQ_ptn  = "\\\\QuickQ\\{" ;
my $out_h;
my $out_file;
my $skip = 0;

open(my $in_h, "<", $src_file) or die "cannot open $src_file: $!" ;
while (my $line = <$in_h>) {
    if ($line =~ /$QAC_ptn/) {
	if ($out_h) {
	    print $out_h "\\QuickQE{}\n";
	}
	$out_file = $1 . ".tex" ;
	open ($out_h, ">", $out_file) or die "cannot open $out_file: $!" ;
	print $out_h "% mainfile: perfbook.tex\n" ;
	print $out_h "% Do not edit! Generated by divideqqz.pl\n" ;
	$line =~ s/QuickQAC/QuickQQC/;
	$skip = 0;
    }
    if ($out_h) {
	if ($line =~ /$QA_ptn/) {
	    $skip = 1;
	}
	if ($skip == 1 && $line =~ /$QQ_ptn/) {
	    print $out_h "\\QuickQE{}\n";
	    $skip = 0;
	}
	if ($skip == 0) {
	    if ($line =~ /$QQ_ptn/) {
		print $out_h "\\QuickQQ{}\n";
	    } else {
		print $out_h $line ;
	    }
	}
    }
}
# need to print file line
print $out_h "\\QuickQE{}\n";