summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/support/latexmk/example_rcfiles/dot2tex-latexmkrc
blob: 64693cab33c3ae9c3265fe0f85e391b7f688f3d2 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# ================ Customization for dot2tex: ================================

push @generated_exts, '%R-dot2tex-fig*.tex', '%R-dot2tex-fig*.dot',
                      '%R-dot2tex-fig*.dot2texopts';
add_cus_dep( 'dot', 'tex', 0, 'dot2tex' );

# Fancy: prefix string for each *latex by "internal" mylatex and basename of tex file
#foreach my $name ( 'latex', 'pdflatex', 'lualatex', 'xelatex' ) {
#   ${$name} = "internal mylatex %B ". ${$name};
#}

$latex    = 'internal mylatex %B latex %O %S';
$lualatex = 'internal mylatex %B lualatex %O %S';
$pdflatex = 'internal mylatex %B pdflatex %O %S';
$xelatex  = 'internal mylatex %B xelatex %O %S';


#---------------------------------

sub dot2tex {
    # Context for a dot2tex custom dependency assumed.

    # Default options:
    my $opts = '--figonly -fpgf -tmath';

    # File that contains the option string. (Written after analysis of the
    # log file for the previous *latex run.)  
    my $opt_file = "$_[0].dot2texopts";

    # Ensure that it is known as a source file of this rule, since some
    # changes in the main .tex could affect the options only, but not the
    # contents of the .dot file
    rdb_ensure_file( $rule, $opt_file );

    if ( -e $opt_file && open( my $fh, '<', $opt_file ) ) {
        $opts = <$fh>;
        close $opt_file;
    }
    else { warn "dot2tex: Cannot open '$opt_file'\n"; }
    
    my @cmd = ( 'dot2tex', split(/\s+/, $opts), '-o', $$Pdest, $$Psource );
    print "dot2tex command line to execute:\n ", join( ' ', @cmd), "\n";
    return system @cmd;
}

#---------------------------------

sub mylatex {
    # Context for a *latex rule assumed.
    # Run *latex as specified in my arguments, and then process the log file
    # to deal with dot2tex conversions needed by the dot2texi package.

    my ($base, @cmd) = @_;
    my $log_name = "$aux_dir1$base.log";

    my $return = system @cmd;
    &examine_log_for_dot2tex( $log_name );
    return $return;
}

#---------------------------------

sub examine_log_for_dot2tex {
   # Context for a *latex rule assumed.
   # From the log file given in the argument to this subroutine,
   # find places where dot2tex would be invoked if *latex were used with
   # the -shell-escape option.  This invocation is on .dot files created
   # by the dot2texi package.
   # Find the basename of the .dot and .tex files (all assumed to be
   # relative to the aux dir).  (Basename includes a possible path
   # component.) 
   # Ensure that for each .dot file the .tex file is in the source file of
   # the current rule, so that latexmk will know to make a corresponding
   # custom dependency.
   # Put the option string in a file where the custom dependency for the
   # dot-to-tex conversion can find it.

   my $log_name = $_[0];

   # Map of basenames_with_relative_path of dot/tex file to option string
   my %found = ();

   open( my $log_fh, '<', $log_name )
      or ( warn( "examine_log_for_dot2tex: Can't read '$log_name'\n" ),
           return
         );
LINE:
   while (my $line = <$log_fh> ) {
       # Aim: find lines of form runsystem\(dot2tex followed by options
       #      followed by "-o file.tex file.dot)", for some value of file.
       #      Allow for continuation lines.
       # Often use /.../x with x option to regex, to get space in pattern
       # ignored, for readability

       # Ignore lines with wrong start:
       if ( $line !~ /^runsystem\(dot2tex \s+ (.*)$/x ) { next; }

       # Rest of command line (after dot2tex) is in $1.
       my $args = $1;

       # Only keep going if arguments begin with options and have -o (for
       # output file):
       if ( $args !~ /(-.*) \s+ -o \s+ (.*) $/x ) {
          next LINE;
       }

       # Putative options for command, then source and dest file:
       my ($opts, $file_part) = ($1, $2);
       # Remove superfluous space:
       $opts =~ s/\s+/ /g;
       $opts =~ s/\s*$//;
       if ( $opts !~ /^(-\S+\s*)*$/x ) {
          warn "Putative options for dot2tex in '$opts' aren't options\n";
          next LINE;
       }
       my $attempts = 0;

CONT_LINE:
       while ($attempts < 2) {
           $attempts++;
           if ($file_part =~ /^(.+) \.tex \s+ \1 \.dot\)/x ) {
               ($found{$1} = $opts) =~ s/\s+/ /g;
               last CONT_LINE;
           }
           if (length($line) >= 80) {
              if (eof($log_fh)) { last LINE; }
              $file_part .= <$log_fh>;
              # Remove trailing new line characters:
              $file_part =~ s/[\n\r]$//;
           }
           else { last CONT_LINE; }
       }
   }
   close $log_fh;

   my @missing_files = ();
   while (my ($base, $opts) = each %found) {
       my $dot = "$aux_dir1$base.dot";
       my $tex = "$aux_dir1$base.tex";
       # File to save options for dot2tex command, so cusdep can read them:
       my $opt_file = "$aux_dir1$base.dot2texopts";

       write_to_file( $opt_file, "$opts\n" );
       if (! -e $tex) { push @missing_files, $tex; }
   }
   if (@missing_files) {
       # No-file lines for missing .tex files will tell latexmk to try
       # to find a cusdep to make them:
       append_to_file( $log_name, map("No file $_.\n", @missing_files) );
   }
}

#---------------------------------

sub write_to_file {
  # Usage: write_to_file( name, items to write )
  my $file = shift;
  open( my $fh, ">", $file )
    or (warn "Cannot write to '$file'\n", return 0 );
  print $fh @_;
  close( $fh );
}

#---------------------------------

sub append_to_file {
  # Usage: append_to_file( name, items to write )
  my $file = shift;
  open( my $fh, ">>", $file )
    or (warn "Cannot append to '$file'\n", return 0 );
  print $fh @_;
  close( $fh );
}

#---------------------------------