]> git.lyx.org Git - lyx.git/blob - src/Literate.C
7443d01cfd1ad6d78464dcc0ede39a3c7c32d462
[lyx.git] / src / Literate.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor         
5  *           Copyright 1995 Matthias Ettrich
6  *           Copyright 1995-2000 The LyX Team.
7  *
8  * ====================================================== 
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16 #include <fstream>
17
18 #include "support/filetools.h"
19 #include "LaTeX.h"
20 #include "Literate.h"
21 #include "support/FileInfo.h"
22 #include "debug.h"
23 #include "support/lyxlib.h"
24 #include "support/syscall.h"
25 #include "support/syscontr.h"
26 #include "support/path.h"
27 #include "bufferlist.h"
28 #include "minibuffer.h"
29 #include "gettext.h"
30
31 using std::ifstream;
32 using std::getline;
33 using std::endl;
34
35 extern BufferList bufferlist;
36
37 Literate::Literate(string const & latex, string const & f, string const & p, 
38                    string const & l, 
39                    string const & literate, string const & literate_f, 
40                    string const & build, string const & build_f)
41                    : LaTeX(latex, f, p),
42                      litfile(l),
43                      literate_cmd(literate), literate_filter(literate_f), 
44                      build_cmd(build), build_filter(build_f)
45 {}
46
47
48 int Literate::weave(TeXErrors & terr, MiniBuffer * minib)
49 {
50         int scanres = Literate::NO_ERRORS;
51         string tmp1, tmp2;
52         int ret1, ret2;
53         Systemcalls one, two;
54
55         // The class LaTeX does not know the temp path.
56         bufferlist.updateIncludedTeXfiles(GetCWD());
57         
58         lyxerr[Debug::LATEX] << "Weaving document" << endl;
59         minib->Set(string(_("Weaving document")));
60         minib->Store();
61
62         // Run the literate program to convert \literate_extension file to .tex file
63         //
64         tmp1 = literate_cmd + " < " + litfile + " > " + file + " 2> " + litfile + ".out";
65         tmp2 = literate_filter + " < " + litfile + ".out" + " > " + litfile + ".log";
66         ret1 = one.startscript(Systemcalls::System, tmp1);
67         ret2 = two.startscript(Systemcalls::System, tmp2);
68         lyxerr.debug() << "LITERATE {" << tmp1 << "} {" << tmp2 << "}" << endl;
69         scanres = scanLiterateLogFile();
70         if (scanres & Literate::ERRORS) return scanres; // return on literate error
71         return run(terr, minib);
72 }
73
74
75 int Literate::build(TeXErrors & /*terr*/, MiniBuffer * minib)
76         // We know that this function will only be run if the lyx buffer
77         // has been changed. 
78 {
79         int scanres = Literate::NO_ERRORS;
80         num_errors = 0; // just to make sure.
81         string tmp1, tmp2;
82         int ret1, ret2;
83         Systemcalls one, two;
84         
85         // The class LaTeX does not know the temp path.
86         bufferlist.updateIncludedTeXfiles(GetCWD());
87         
88         lyxerr[Debug::LATEX] << "Building program" << endl;
89         minib->Set(string(_("Building program")));
90         minib->Store();
91
92         // Run the build program
93         //
94         tmp1 = build_cmd + ' ' + litfile + " > " + litfile + ".out 2>&1";
95         tmp2 = build_filter + " < " + litfile + ".out" + " > " + litfile + ".log";
96         ret1 = one.startscript(Systemcalls::System, tmp1);
97         ret2 = two.startscript(Systemcalls::System, tmp2);
98         scanres = scanBuildLogFile();
99         lyxerr[Debug::LATEX] << "Done." << endl;
100
101         return scanres;
102 }
103
104
105 int Literate::scanLiterateLogFile()
106 {
107         string token;
108         int retval = NO_ERRORS;
109         
110         string tmp = litfile + ".log";
111         
112         ifstream ifs(tmp.c_str());
113         while (getline(ifs, token)) {
114                 lyxerr[Debug::LATEX] << token << endl;
115                 
116                 if (prefixIs(token, "Build Warning:")) {
117                         // Here shall we handle different
118                         // types of warnings
119                         retval |= LATEX_WARNING;
120                         lyxerr[Debug::LATEX] << "Build Warning." << endl;
121                 } else if (prefixIs(token, "! Build Error:")) {
122                         // Here shall we handle different
123                         // types of errors
124                         retval |= LATEX_ERROR;
125                         lyxerr[Debug::LATEX] << "Build Error." << endl;
126                         // this is not correct yet
127                         ++num_errors;
128                 }
129         }       
130         return retval;
131 }
132
133
134 int Literate::scanBuildLogFile()
135 {
136         string token;
137         int retval = NO_ERRORS;
138  
139         string tmp = litfile + ".log";
140         
141         ifstream ifs(tmp.c_str());
142         while (getline(ifs, token)) {
143                 lyxerr[Debug::LATEX] << token << endl;
144                 
145                 if (prefixIs(token, "Build Warning:")) {
146                         // Here shall we handle different
147                         // types of warnings
148                         retval |= LATEX_WARNING;
149                         lyxerr[Debug::LATEX] << "Build Warning." << endl;
150                 } else if (prefixIs(token, "! Build Error:")) {
151                         // Here shall we handle different
152                         // types of errors
153                         retval |= LATEX_ERROR;
154                         lyxerr[Debug::LATEX] << "Build Error." << endl;
155                         // this is not correct yet
156                         ++num_errors;
157                 }
158         }       
159         return retval;
160 }