]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
Substack support for XHTML.
[lyx.git] / src / ModuleList.cpp
1 // -*- C++ -*-
2 /**
3  * \file ModuleList.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Richard Heck
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ModuleList.h"
15
16 #include "LaTeXFeatures.h"
17 #include "Lexer.h"
18
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23
24 #include <algorithm>
25         
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31
32 //global variable: module list
33 ModuleList theModuleList;
34
35
36 LyXModule::LyXModule(string const & n, string const & i, 
37                            string const & d, vector<string> const & p,
38                            vector<string> const & r, vector<string> const & e,
39                            string const & c):
40         name_(n), id_(i), description_(d), package_list_(p), 
41         required_modules_(r), excluded_modules_(e), category_(c),
42         checked_(false)
43 {
44         filename_ = id_ + ".module";
45 }
46
47
48 vector<string> LyXModule::prerequisites() const {
49 #ifdef TEX2LYX
50         return vector<string>();
51 #else
52         if (!checked_)
53                 isAvailable();
54         return prerequisites_;
55 #endif
56 }
57
58
59 bool LyXModule::isAvailable() const {
60 #ifdef TEX2LYX
61         return true;
62 #else
63         if (package_list_.empty())
64                 return true;
65         if (checked_)
66                 return available_;
67         checked_ = true;
68         available_ = true;
69         //check whether all of the required packages are available
70         vector<string>::const_iterator it  = package_list_.begin();
71         vector<string>::const_iterator end = package_list_.end(); 
72         for (; it != end; ++it) {
73                 if (!LaTeXFeatures::isAvailable(*it)) {
74                         available_ = false;
75                         prerequisites_.push_back(*it);
76                 }
77         }
78         return available_;
79 #endif
80 }
81
82
83 bool LyXModule::isCompatible(string const & modname) const
84 {
85         // do we exclude it?
86         if (find(excluded_modules_.begin(), excluded_modules_.end(), modname) !=
87                         excluded_modules_.end())
88                 return false;
89
90         LyXModule const * const lm = theModuleList[modname];
91         if (!lm)
92                 return true;
93
94         // does it exclude us?
95         vector<string> const excmods = lm->getExcludedModules();
96         if (find(excmods.begin(), excmods.end(), id_) != excmods.end())
97                 return false;
98
99         return true;
100 }
101
102
103 bool LyXModule::areCompatible(string const & mod1, string const & mod2)
104 {
105         LyXModule const * const lm1 = theModuleList[mod1];
106         if (lm1)
107                 return lm1->isCompatible(mod2);
108         LyXModule const * const lm2 = theModuleList[mod2];
109         if (lm2)
110                 return lm2->isCompatible(mod1);
111         // Can't check it either way.
112         return true;
113 }
114
115 // used when sorting the module list.
116 class ModuleSorter
117 {
118 public:
119         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
120         {
121                 return lm1.getName() < lm2.getName();
122         }
123 };
124
125
126 //Much of this is borrowed from LayoutFileList::read()
127 bool ModuleList::read()
128 {
129         FileName const real_file = libFileSearch("", "lyxmodules.lst");
130         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
131
132         if (real_file.empty()) {
133                 LYXERR0("unable to find modules file `lyxmodules.lst'.\n"
134                         << "No modules will be available.");
135                 return false;
136         }
137
138         Lexer lex;
139         if (!lex.setFile(real_file)) {
140                 LYXERR0("lyxlex was not able to set file: "
141                         << real_file << ".\nNo modules will be available.");
142                 return false;
143         }
144
145         if (!lex.isOK()) {
146                 LYXERR0("unable to open modules file  `"
147                         << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
148                         << "'\nNo modules will be available.");
149                 return false;
150         }
151
152         bool finished = false;
153         // Parse modules files
154         LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
155         while (lex.isOK() && !finished) {
156                 LYXERR(Debug::TCLASS, "\tline by line");
157                 switch (lex.lex()) {
158                 case Lexer::LEX_FEOF:
159                         finished = true;
160                         break;
161                 default:
162                         string const modname = lex.getString();
163                         LYXERR(Debug::TCLASS, "Module name: " << modname);
164                         if (!lex.next())
165                                 break;
166                         string const fname = lex.getString();
167                         LYXERR(Debug::TCLASS, "Filename: " << fname);
168                         if (!lex.next(true))
169                                 break;
170                         string const desc = lex.getString();
171                         LYXERR(Debug::TCLASS, "Description: " << desc);
172                         //FIXME Add packages
173                         if (!lex.next())
174                                 break;
175                         string str = lex.getString();
176                         LYXERR(Debug::TCLASS, "Packages: " << str);
177                         vector<string> pkgs;
178                         while (!str.empty()) {
179                                 string p;
180                                 str = split(str, p, ',');
181                                 pkgs.push_back(p);
182                         }
183                         if (!lex.next())
184                                 break;
185                         str = lex.getString();
186                         LYXERR(Debug::TCLASS, "Required: " << str);
187                         vector<string> req;
188                         while (!str.empty()) {
189                                 string p;
190                                 str = split(str, p, '|');
191                                 req.push_back(p);
192                         }
193                         if (!lex.next())
194                                 break;
195                         str = lex.getString();
196                         LYXERR(Debug::TCLASS, "Excluded: " << str);
197                         vector<string> exc;
198                         while (!str.empty()) {
199                                 string p;
200                                 str = split(str, p, '|');
201                                 exc.push_back(p);
202                         }
203                         if (!lex.next())
204                                 break;
205                         string const catgy = lex.getString();
206                         LYXERR(Debug::TCLASS, "Category: " << catgy);
207                         // This code is run when we have
208                         // modName, fname, desc, pkgs, req, exc, and catgy
209                         addLayoutModule(modname, fname, desc, pkgs, req, exc, catgy);
210                 } // end switch
211         } //end while
212         
213         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
214
215         if (!theModuleList.empty())
216                 sort(theModuleList.begin(), theModuleList.end(), ModuleSorter());
217         return true;
218 }
219
220
221 void ModuleList::addLayoutModule(string const & modname, 
222         string const & filename, string const & description,
223         vector<string> const & pkgs, vector<string> const & req,
224         vector<string> const & exc, string const & catgy)
225 {
226         LyXModule lm(modname, filename, description, pkgs, req, exc, catgy);
227         modlist_.push_back(lm);
228 }
229
230
231 LyXModuleList::const_iterator ModuleList::begin() const
232 {
233         return modlist_.begin();
234 }
235
236
237 LyXModuleList::iterator ModuleList::begin()
238 {
239         return modlist_.begin();
240 }
241
242
243 LyXModuleList::const_iterator ModuleList::end() const
244 {
245         return modlist_.end();
246 }
247
248
249 LyXModuleList::iterator ModuleList::end()
250 {
251         return modlist_.end();
252 }
253
254
255 LyXModule const * ModuleList::operator[](string const & str) const
256 {
257         LyXModuleList::const_iterator it = modlist_.begin();
258         for (; it != modlist_.end(); ++it)
259                 if (it->getID() == str) {
260                         LyXModule const & mod = *it;
261                         return &mod;
262                 }
263         return 0;
264 }
265
266 LyXModule * ModuleList::operator[](string const & str)
267 {
268         LyXModuleList::iterator it = modlist_.begin();
269         for (; it != modlist_.end(); ++it)
270                 if (it->getID() == str) {
271                 LyXModule & mod = *it;
272                 return &mod;
273                 }
274                 return 0;
275 }
276
277 } // namespace lyx