summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/gridslides/compile.pl
blob: fa9ee6288979d66807f1d9158acfbb924f0f9b0e (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/perl
# Package:     gridslides
# Description: LaTeX package to create free form slides with blocks placed on a grid
# File:        compile.pl
# Author:      Daniel Mendler <mail@daniel-mendler.de>
# Version:     0.1
# Date:        2017/11/11
# License:     GPL2 or LPPL1.3 at your option
# Homepage:    https://github.com/minad/gridslides

use strict;
use Digest::MD5 qw(md5_hex);
use File::Slurp qw(read_file write_file);

sub load_file {
    my ($file) = @_;
    my $code = read_file($file);
    $code =~ /\A(.*?\\begin\{document\})/s;
    return ($1, $');
}

sub compile_slide {
    my ($format, $slide, $body) = @_;

    my $code = "%&$format\n\\begin{document}\\setcounter{slide}{$slide}\n$body\n\\end{document}\n";
    my $hash = md5_hex($code);
    my $pdf = "$hash.pdf";
    unless (-e $pdf) {
        my $tex = "$hash.tex";
        write_file($tex, $code);
        print "Compiling slide $slide - $tex...\n";
        `pdflatex -interaction=batchmode $tex; biber $hash; pdflatex -interaction=batchmode $tex; pdflatex -interaction=batchmode $tex`;
    }

    return $pdf;
}

my $once = 0;
my $file;
if ($ARGV[0] eq "--once") {
    $file = $ARGV[1];
    $once = 1;
} else {
    $file = $ARGV[0];
}

my $result = $file;
$result =~ s/\.tex\Z//g;
$result .= '.pdf';

my $out = 'output';
mkdir($out);
chdir($out);
$ENV{TEXINPUTS}='..:.:';
$ENV{BIBINPUTS}='..';

my ($header, $body) = load_file "../$file";
my $format = "header$file";
write_file("$format.tex", "$header\n\\end{document}");
`pdftex -interaction=batchmode -ini -jobname="$format" '&pdflatex' mylatexformat.ltx "$format.tex"`;

do {
    my @slides = ();
    my $slide = 1;
    while ($body =~ /(\\maketitle|\\begin\{.*?slide\}.*?\\end\{.*?slide\})/sg) {
        my $pdf = compile_slide "$format", $slide, $1;
        push @slides, $pdf if (-e $pdf);
        ++$slide;
    }

    print "Joining $result...\n";
    `pdfjoin -q -o $result @slides`;
    rename $result, "../$result";

    unless ($once) {
        `inotifywait -e modify ../$file`;
        (my $unused, $body) = load_file "../$file";
    }
} until ($once);