Debian Codegraph @

texmf-dist/doc/support/cluttex/src/recovery.sml

raw
1 structure Recovery:
2 sig
3 val createMissingDirectories:
4 {execlog: string, auxfile: string, outdir: string}
5 -> bool
6 val runEpstopdf:
7 { options: AppOptions.options
8 , execlog: string
9 , originalWorkingDirectory: string
10 }
11 -> bool
12 val tryRecovery:
13 { options: AppOptions.options
14 , execlog: string
15 , auxfile: string
16 , originalWorkingDirectory: string
17 }
18 -> bool
19 end =
20 struct
21 fun createMissingDirectories {execlog, auxfile, outdir} =
22 if String.isSubstring "I can't write on file" execlog then
23 (* There is a possibility that there are some subfiles under subdirectories.
24 * Directories for sub-auxfiles are not created automatically, so we need to provide them. *)
25 let
26 val (madeNewDirectory, _) =
27 AuxFile.createMissingDirectories
28 {auxfile = auxfile, outdir = outdir, seen = StringSet.empty}
29 in
30 if madeNewDirectory andalso Message.getVerbosity () >= 1 then
31 Message.info "Created missing directories."
32 else
33 ();
34 madeNewDirectory
35 end
36 else
37 false
38
39 fun runEpstopdf
40 { options: AppOptions.options
41 , execlog: string
42 , originalWorkingDirectory: string
43 } =
44 if #shell_escape options <> SOME ShellEscape.FORBIDDEN then (* (possibly restricted) \write18 enabled *)
45 let
46 val lines =
47 Substring.tokens (fn c => c = #"\n") (Substring.full execlog)
48 fun doLine (line, run) =
49 case List.map Substring.string (Substring.tokens Char.isSpace line) of
50 ["(epstopdf)", "Command:", command, outfile', infile'] =>
51 if
52 command = "<epstopdf"
53 orelse
54 command = "<repstopdf"
55 andalso String.isPrefix "--outfile=" outfile'
56 andalso String.isSuffix ">" infile'
57 then
58 let
59 val outfile = String.extract (outfile', 10, NONE)
60 val infile = String.substring
61 (infile', 0, String.size infile' - 1)
62 val infileAbs =
63 PathUtil.abspath
64 {path = infile, cwd = SOME originalWorkingDirectory}
65 in
66 if FSUtil.isFile infileAbs then (* input file exists *)
67 let
68 val outfileAbs = PathUtil.abspath
69 {path = outfile, cwd = SOME (#output_directory options)}
70 val () =
71 if Message.getVerbosity () >= 1 then
72 Message.info ("Running epstopdf on " ^ infile ^ ".")
73 else
74 ()
75 val outdir = PathUtil.dirname outfileAbs
76 val () =
77 if not (FSUtil.isDirectory outdir) then
78 FSUtil.mkDirRec outdir
79 else
80 ()
81 val command =
82 "epstopdf --outfile=" ^ ShellUtil.escape outfileAbs
83 ^ " " ^ ShellUtil.escape infileAbs
84 val () = Message.exec command
85 val success =
86 OS.Process.isSuccess (OS.Process.system command)
87 in
88 run orelse success
89 end
90 else
91 run
92 end
93 else
94 run
95 | _ => run
96 in
97 List.foldl doLine false lines
98 end
99 else
100 false
101
102 (* The next time we will able to set \PassOptionsToPackage{outputdir=}{minted} *)
103 fun checkMinted {execlog} =
104 String.isSubstring
105 "Package minted Error: Missing Pygments output; \\inputminted was" execlog
106
107 fun tryRecovery {options, execlog, auxfile, originalWorkingDirectory} =
108 let
109 val recovered =
110 createMissingDirectories
111 { execlog = execlog
112 , auxfile = auxfile
113 , outdir = #output_directory options
114 }
115 val recovered =
116 runEpstopdf
117 { options = options
118 , execlog = execlog
119 , originalWorkingDirectory = originalWorkingDirectory
120 } orelse recovered
121 val recovered = recovered orelse checkMinted {execlog = execlog}
122 in
123 recovered
124 end
125 end;