summaryrefslogtreecommitdiffstats
path: root/misc/quiz-extract-automata.pl
blob: 8327ce3afef20b1c5ca80d5df1071f80be5d04da (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
#! /usr/local/bin/perl -w

use strict;
use warnings;

my $inputfile = $ARGV[0];
die "please pass input file as argument" unless defined($inputfile);
my $outputdir = "/tmp/automata";

my $f;

open $f, "<", $inputfile or die "can't open $inputfile: $!";
my %automata;
my $thisname;
while (<$f>) {
    $thisname = undef if defined($thisname) && /^\\end\{center\}/;
    $automata{$thisname} .= $_ if defined($thisname);
    $thisname = $1 if /^\\begin\{center\}\s*\%+\s*NAME:\s*(\S+)/;
}
close $f;

my $prologue = <<'__EOF__';
\documentclass[crop,tikz]{standalone}
%
%
%
\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}
%
\tikzstyle{automaton}=[>=stealth',initial text={},thick,every loop/.style={min distance=7mm,looseness=5}]
\tikzstyle{state}=[]
\tikzstyle{final}=[accepting by arrow]
%
%
%
__EOF__

mkdir $outputdir;

foreach my $name (keys(%automata)) {
    chdir $outputdir;
    print "Processing $name\n";
    open $f, ">", "${outputdir}/${name}.tex";
    print $f $prologue;
    print $f "\\begin{document}\n\n";
    print $f $automata{$name};
    print $f "\n\\end{document}\n";
    close $f;
    system "pdflatex", "${name}.tex";
    system "pdf2svg", "${name}.pdf", "${name}.raw.svg";
    system "scour", "-i", "${name}.raw.svg", "-o", "${name}.svg";
    system "rsvg-convert", "${name}.svg", "-d", "300", "-p", "300", "-b", "white", "-o", "${name}.png";
}