File: latexCompiler.ml

package info (click to toggle)
coq 8.20.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,116 kB
  • sloc: ml: 234,160; sh: 4,301; python: 3,270; ansic: 2,644; makefile: 882; lisp: 172; javascript: 63; xml: 24; sed: 2
file content (89 lines) | stat: -rw-r--r-- 3,140 bytes parent folder | download | duplicates (2)
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
(************************************************************************)
(*         *   The Coq Proof Assistant / The Coq Development Team       *)
(*  v      *         Copyright INRIA, CNRS and contributors             *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)

open Printf
open Common

let locally dir f x =
  let cwd = Sys.getcwd () in
  try
    Sys.chdir dir;
    let y = f x in
    Sys.chdir cwd; y
  with e -> Sys.chdir cwd; raise e

let clean_temp_files basefile =
  let remove f = try Sys.remove f with _ -> () in
  remove (basefile ^ ".tex");
  remove (basefile ^ ".log");
  remove (basefile ^ ".aux");
  remove (basefile ^ ".toc");
  remove (basefile ^ ".dvi");
  remove (basefile ^ ".ps");
  remove (basefile ^ ".pdf");
  remove (basefile ^ ".haux");
  remove (basefile ^ ".html")

let clean_and_exit file res = clean_temp_files file; exit res

let cat file =
  let c = open_in file in
  try
    while true do
      print_char (input_char c)
    done
  with End_of_file -> close_in c

let compile ~otypes ~produce_document fl =
  let texfile = Filename.temp_file "coqdoc" ".tex" in
  let basefile = Filename.chop_suffix texfile ".tex" in
  let final_out_to = !prefs.out_to in
  prefs := { !prefs with out_to = File texfile };
  prefs := { !prefs with output_dir = Filename.dirname texfile };  produce_document fl;
  let latexexe = if List.mem Pdf otypes then "pdflatex" else "latex" in
  let latexcmd =
    let file = Filename.basename texfile in
    let file =
      if !prefs.quiet then sprintf "'\\nonstopmode\\input{%s}'" file else file
    in
    sprintf "%s %s && %s %s 1>&2 %s" latexexe file latexexe file
      (if !prefs.quiet then "> /dev/null" else "")
  in
  let res = locally (Filename.dirname texfile) Sys.command latexcmd in
  if res <> 0 then begin
    eprintf "Couldn't run LaTeX successfully\n";
    clean_and_exit basefile res
  end;
  let dvifile = basefile ^ ".dvi" in
  ( if List.mem Dvi otypes then
    match final_out_to with
    | MultFiles | StdOut -> cat dvifile
    | File f -> FileUtil.copy dvifile f );
  let pdffile = basefile ^ ".pdf" in
  ( if List.mem Pdf otypes then
    match final_out_to with
    | MultFiles | StdOut -> cat pdffile
    | File f -> FileUtil.copy pdffile f );
  if List.mem Ps otypes then begin
    let psfile = basefile ^ ".ps" in
    let command =
      sprintf "dvips %s -o %s %s" dvifile psfile
        (if !prefs.quiet then "> /dev/null 2>&1" else "")
    in
    let res = Sys.command command in
    if res <> 0 then begin
      eprintf "Couldn't run dvips successfully\n";
      clean_and_exit basefile res
    end;
    match final_out_to with
    | MultFiles | StdOut -> cat psfile
    | File f -> FileUtil.copy psfile f
  end;
  clean_temp_files basefile