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