]> git.lyx.org Git - lyx.git/blob - src/LayoutFile.cpp
Pure HTML output for math macros.
[lyx.git] / src / LayoutFile.cpp
1 /**
2  * \file LayoutFileList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "LayoutFile.h"
15 #include "Counters.h"
16 #include "Floating.h"
17 #include "FloatList.h"
18 #include "Lexer.h"
19 #include "TextClass.h"
20
21 #include "frontends/alert.h"
22
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
28 #include "support/lstrings.h"
29
30 #include <boost/bind.hpp>
31 #include <boost/regex.hpp>
32
33 #include <fstream>
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39
40 using boost::bind;
41 using boost::regex;
42 using boost::smatch;
43
44 LayoutFile::LayoutFile(string const & fn, string const & cln,
45                            string const & desc, string const & prereq,
46                                  bool texclassavail) 
47 {
48         name_ = fn;
49         latexname_ = cln;
50         description_ = desc;
51         prerequisites_ = prereq;
52         tex_class_avail_ = texclassavail;
53 }
54
55
56 LayoutFileList::~LayoutFileList()
57 {
58         ClassMap::const_iterator it = classmap_.begin();
59         ClassMap::const_iterator en = classmap_.end();
60         for (; it != en; ++it) {
61                 delete it->second;
62         }
63 }
64
65
66 LayoutFileList & LayoutFileList::get() 
67 {
68         static LayoutFileList baseclasslist;
69         return baseclasslist;
70 }
71
72
73 bool LayoutFileList::haveClass(string const & classname) const
74 {
75         ClassMap::const_iterator it = classmap_.begin();
76         ClassMap::const_iterator en = classmap_.end();
77         for (; it != en; ++it) {
78                 if (it->first == classname)
79                         return true;
80         }
81         return false;
82 }
83
84
85 LayoutFile const & LayoutFileList::operator[](string const & classname) const
86 {
87         LASSERT(haveClass(classname), /**/);
88         return *classmap_[classname];
89 }
90
91
92 LayoutFile & LayoutFileList::operator[](string const & classname)
93 {
94         LASSERT(haveClass(classname), /**/);
95         return *classmap_[classname];
96 }
97
98
99 // Reads LyX textclass definitions according to textclass config file
100 bool LayoutFileList::read()
101 {
102         Lexer lex;
103         FileName const real_file = libFileSearch("", "textclass.lst");
104         LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
105
106         if (real_file.empty()) {
107                 lyxerr << "LayoutFileList::Read: unable to find "
108                           "textclass file  `"
109                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
110                        << "'. Exiting." << endl;
111                 return false;
112                 // This causes LyX to end... Not a desirable behaviour. Lgb
113                 // What do you propose? That the user gets a file dialog
114                 // and is allowed to hunt for the file? (Asger)
115                 // more that we have a layout for minimal.cls statically
116                 // compiled in... (Lgb)
117         }
118
119         if (!lex.setFile(real_file)) {
120                 lyxerr << "LayoutFileList::Read: "
121                         "lyxlex was not able to set file: "
122                        << real_file << endl;
123         }
124
125         if (!lex.isOK()) {
126                 lyxerr << "LayoutFileList::Read: unable to open "
127                           "textclass file  `"
128                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
129                        << "'\nCheck your installation. LyX can't continue."
130                        << endl;
131                 return false;
132         }
133
134         bool finished = false;
135         // Parse config-file
136         LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
137         while (lex.isOK() && !finished) {
138                 LYXERR(Debug::TCLASS, "\tline by line");
139                 switch (lex.lex()) {
140                 case Lexer::LEX_FEOF:
141                         finished = true;
142                         break;
143                 default:
144                         string const fname = lex.getString();
145                         LYXERR(Debug::TCLASS, "Fname: " << fname);
146                         if (!lex.next()) 
147                                 break;
148                         string const clname = lex.getString();
149                         LYXERR(Debug::TCLASS, "Clname: " << clname);
150                         if (!lex.next()) 
151                                 break;
152                         string const desc = lex.getString();
153                         LYXERR(Debug::TCLASS, "Desc: " << desc);
154                         if (!lex.next()) 
155                                 break;
156                         bool avail = lex.getBool();
157                         LYXERR(Debug::TCLASS, "Avail: " << avail);
158                         if (!lex.next()) 
159                                 break;
160                         string const prereq = lex.getString();
161                         LYXERR(Debug::TCLASS, "Prereq: " << prereq);
162                         // This code is run when we have
163                         // fname, clname, desc, prereq, and avail
164                         LayoutFile * tmpl = new LayoutFile(fname, clname, desc, prereq, avail);
165                         if (lyxerr.debugging(Debug::TCLASS)) {
166                                 // only system layout files are loaded here so no
167                                 // buffer path is needed.
168                                 tmpl->load();
169                         }
170                         classmap_[fname] = tmpl;
171                 }
172         }
173         LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
174
175         // lyx will start with an empty classmap_, but only reconfigure is allowed
176         // in this case. This gives users a second chance to configure lyx if
177         // initial configuration fails. (c.f. bug 2829)
178         if (classmap_.empty())
179                 lyxerr << "LayoutFileList::Read: no textclasses found!"
180                        << endl;
181         return true;
182 }
183
184
185 std::vector<LayoutFileIndex> LayoutFileList::classList() const
186 {
187         std::vector<LayoutFileIndex> cl;
188         ClassMap::const_iterator it = classmap_.begin();
189         ClassMap::const_iterator en = classmap_.end();
190         for (; it != en; ++it)
191                 cl.push_back(it->first);
192         return cl;
193 }
194
195
196 void LayoutFileList::reset(LayoutFileIndex const & classname) {
197         LASSERT(haveClass(classname), /**/);
198         LayoutFile * tc = classmap_[classname];
199         LayoutFile * tmpl = 
200                 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
201                                tc->prerequisites(), tc->isTeXClassAvailable());
202         classmap_[classname] = tmpl;
203         delete tc;
204 }
205
206
207 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
208 {
209         if (haveClass(textclass))
210                 return textclass;
211
212         FileName const tempLayout = FileName::tempName();
213         ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
214         ofs << "# This layout is automatically generated\n"
215                 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
216                 "Format         7\n"
217                 "Input stdclass.inc\n\n"
218                 "Columns                1\n"
219                 "Sides                  1\n"
220                 "SecNumDepth    2\n"
221                 "TocDepth               2\n"
222                 "DefaultStyle   Standard\n\n"
223                 "Style Standard\n"
224                 "       Category              MainText\n"
225                 "       Margin                Static\n"
226                 "       LatexType             Paragraph\n"
227                 "       LatexName             dummy\n"
228                 "       ParIndent             MM\n"
229                 "       ParSkip               0.4\n"
230                 "       Align                 Block\n"
231                 "       AlignPossible         Block, Left, Right, Center\n"
232                 "       LabelType             No_Label\n"
233                 "End\n";
234         ofs.close();
235
236         // We do not know if a LaTeX class is available for this document, but setting
237         // the last parameter to true will suppress a warning message about missing
238         // tex class.
239         LayoutFile * tc = new LayoutFile(textclass, textclass, 
240                         "Unknown text class " + textclass, textclass + ".cls", true);
241         if (!tc->load(tempLayout.absFilename())) {
242                 // The only way this happens is because the hardcoded layout file above
243                 // is wrong.
244                 LASSERT(false, /**/);
245         }
246         classmap_[textclass] = tc;
247         return textclass;
248 }
249
250
251 LayoutFileIndex 
252         LayoutFileList::addLocalLayout(string const & textclass, string const & path)
253 {
254         // FIXME  There is a bug here: 4593
255         //
256         // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
257         // NOTE: latex class name is defined in textclass.layout, which can be 
258         // different from textclass
259         string fullName = addName(path, textclass + ".layout");
260         
261         FileName const layout_file(fullName);
262         if (layout_file.exists()) {
263                 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
264                 // Read .layout file and get description, real latex classname etc
265                 //
266                 // This is a C++ version of function processLayoutFile in configure.py,
267                 // which uses the following regex
268                 //     \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
269                 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
270                 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
271                         "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
272                 string line;
273                 while (getline(ifs, line)) {
274                         // look for the \DeclareXXXClass line
275                         smatch sub;
276                         if (regex_match(line, sub, reg)) {
277                                 // returns: whole string, classtype (not used here), class name, description
278                                 LASSERT(sub.size() == 4, /**/);
279                                 // now, create a TextClass with description containing path information
280                                 string class_name(sub.str(2) == "" ? textclass : sub.str(2));
281                                 string class_prereq(class_name + ".cls");
282                                 LayoutFile * tmpl = 
283                                         new LayoutFile(textclass, class_name, textclass, class_prereq, true);
284                                 //FIXME: The prerequisites are available from the layout file and
285                                 //       can be extracted from the above regex, but for now this
286                                 //       field is simply set to class_name + ".cls"
287                                 // This textclass is added on request so it will definitely be
288                                 // used. Load it now because other load() calls may fail if they
289                                 // are called in a context without buffer path information.
290                                 tmpl->load(path);
291                                 // There will be only one textclass with this name, even if different
292                                 // layout files are loaded from different directories.
293                                 if (haveClass(textclass)) {
294                                         LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
295                                         delete classmap_[textclass];
296                                 }
297                                 classmap_[textclass] = tmpl;
298                                 return textclass;
299                         }
300                 }
301         }
302         // If .layout is not in local directory, or an invalid layout is found, return null
303         return string();
304 }
305
306
307 bool LayoutFileList::load(string const & name, string const & buf_path)
308 {
309         if (!haveClass(name)) {
310                 LYXERR0("Document class \"" << name << "\" does not exist.");
311                 return false;
312         }
313
314         LayoutFile * tc = classmap_[name];
315         if (!tc->load(buf_path)) {
316                 docstring s = bformat(_("The document class %1$s "
317                                    "could not be loaded."), from_utf8(name));
318                 frontend::Alert::error(_("Could not load class"), s);
319                 return false;
320         }
321         return true;
322 }
323
324
325 LayoutFileIndex defaultBaseclass()
326 {
327         if (LayoutFileList::get().haveClass("article"))
328                 return string("article");
329         if (LayoutFileList::get().empty())
330                 return string();
331         return LayoutFileList::get().classList().front();
332 }
333
334
335
336 // Reads the style files
337 bool LyXSetStyle()
338 {
339         LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
340
341         if (!LayoutFileList::get().read()) {
342                 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
343                         "during parsing.\n             Exiting.");
344                 return false;
345         }
346
347         LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
348         return true;
349 }
350
351
352 } // namespace lyx