]> git.lyx.org Git - lyx.git/blob - src/ImportNoweb.C
9657530c86d2de658db82d37341eed591603cb2b
[lyx.git] / src / ImportNoweb.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor         
5  *           Copyright 1995 Matthias Ettrich
6  *           Copyright 1995-1999 The LyX Team.
7  *
8  *           This file is Copyright 1999
9  *           Kayvan A. Sylvan
10  *
11  * ====================================================== 
12  */
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include <fstream>
21 using std::ifstream;
22
23 #include "ImportNoweb.h"
24 #include "lyxrc.h"
25 #include "support/syscall.h"
26 #include "bufferlist.h"
27
28 extern LyXRC * lyxrc;
29 extern BufferList bufferlist;
30
31 /*
32  * Implementation the ImportNoweb methods.
33  */
34
35 Buffer * ImportNoweb::run()
36 {
37         // run reLyX -n
38         string tmp = lyxrc->relyx_command + " -n -c " +
39                                         documentclass() + " -f " + file;
40         Systemcalls one;
41         Buffer * buf = 0;
42         int result= one.startscript(Systemcalls::System, tmp);
43         if (result == 0) {
44                 string filename = file + ".lyx";
45                 // File was generated without problems. Load it.
46                 buf = bufferlist.loadLyXFile(filename);
47         }
48         return buf;
49 }
50
51
52 // Provide the literate documentclass by parsing the file.
53 string ImportNoweb::documentclass()
54 {
55         string result = "literate-article"; // Default
56
57 #warning If you use literate programming you should verify that this works
58         // This method has been rewritten to use ifstream, but since I
59         // don't use literate programming myself I am unable to check
60         // this correclty. (Lgb)
61         ifstream ifs(file.c_str());
62
63         if (!ifs) return "nofile"; // Should not happen!
64         string line;
65         while (getline(ifs, line)) {
66                 int p = line.find("\\documentclass");
67                 if (p != string::npos) {
68                         p = line.find('{', p);
69                         int q = line.find('}', p);
70                         result = "literate-" + line.substr(p, q - p);
71                         break;
72                 }
73         }
74         return result;
75 }