]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.C
f95a3a9b66a7fa553f6236896105ef5ad3ce31ea
[lyx.git] / src / insets / ExternalTemplate.C
1 /**
2  * \file ExternalTemplate.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ExternalTemplate.h"
14
15 #include "debug.h"
16 #include "lyxlex.h"
17
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/path.h"
21 #include "support/path_defines.h"
22
23 #include <boost/assert.hpp>
24
25 #include <algorithm>
26
27 namespace support = lyx::support;
28
29 using std::endl;
30 using std::for_each;
31
32 using std::string;
33 using std::ostream;
34 using std::vector;
35
36
37 namespace lyx {
38 namespace external {
39
40 // We have to have dummy default commands for security reasons!
41 Template::Template()
42         : inputFormat("*")
43 {}
44
45
46 Template::Format::Format()
47 {}
48
49
50 TemplateManager::TemplateManager()
51 {
52         readTemplates(support::user_lyxdir());
53         if (lyxerr.debugging(Debug::EXTERNAL)) {
54                 dumpPreambleDefs(lyxerr);
55                 lyxerr << '\n';
56                 dumpTemplates(lyxerr);
57         }
58 }
59
60
61 class dumpPreambleDef {
62 public:
63         typedef TemplateManager::PreambleDefs::value_type value_type;
64
65         dumpPreambleDef(ostream & o) : ost(o) {}
66
67         void operator()(value_type const & vt) {
68                 ost << "PreambleDef " << vt.first << '\n'
69                     << vt.second
70                     << "PreambleDefEnd" << endl;
71         }
72
73 private:
74         ostream & ost;
75 };
76
77
78 class dumpTemplate {
79 public:
80         typedef TemplateManager::Templates::value_type value_type;
81
82         dumpTemplate(ostream & o) : ost(o) {}
83
84         void operator()(value_type const & vt) {
85                 Template const & et = vt.second;
86
87                 ost << "Template " << et.lyxName << '\n'
88                     << "\tGuiName " << et.guiName << '\n'
89                     << "\tHelpText\n"
90                     << et.helpText
91                     << "\tHelpTextEnd\n"
92                     << "\tInputFormat " << et.inputFormat << '\n'
93                     << "\tFileFilter " << et.fileRegExp << '\n'
94                     << "\tEditCommand " << et.editCommand << '\n'
95                     << "\tAutomaticProduction " << et.automaticProduction << '\n';
96                 et.dumpFormats(ost);
97                 ost << "TemplateEnd" << endl;
98
99         }
100
101 private:
102         ostream & ost;
103 };
104
105 class dumpFormat {
106 public:
107         typedef Template::Formats::value_type value_type;
108
109         dumpFormat(ostream & o) : ost(o) {}
110
111         void operator()(value_type const & vt) const{
112                 Template::Format const & ft = vt.second;
113                 ost << "\tFormat " << vt.first << '\n'
114                     << "\t\tProduct " << ft.product << '\n'
115                     << "\t\tUpdateFormat " << ft.updateFormat << '\n'
116                     << "\t\tUpdateResult " << ft.updateResult << '\n'
117                     << "\t\tRequirement " << ft.requirement << '\n';
118
119                 vector<string>::const_iterator it  = ft.preambleNames.begin();
120                 vector<string>::const_iterator end = ft.preambleNames.end();
121                 for (; it != end; ++it) {
122                         ost << "\t\tPreamble " << *it << '\n';
123                 }
124
125                 ost << "\tFormatEnd\n";
126         }
127 private:
128         ostream & ost;
129 };
130
131
132 void Template::dumpFormats(ostream & os) const
133 {
134         for_each(formats.begin(), formats.end(), dumpFormat(os));
135 }
136
137
138 void TemplateManager::dumpPreambleDefs(ostream & os) const
139 {
140         for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
141 }
142
143
144 void TemplateManager::dumpTemplates(ostream & os) const
145 {
146         for_each(templates.begin(), templates.end(), dumpTemplate(os));
147 }
148
149
150 TemplateManager & TemplateManager::get()
151 {
152         static TemplateManager externalTemplateManager;
153         return externalTemplateManager;
154 }
155
156
157 TemplateManager::Templates &
158 TemplateManager::getTemplates()
159 {
160         return templates;
161 }
162
163
164 TemplateManager::Templates const &
165 TemplateManager::getTemplates() const
166 {
167         return templates;
168 }
169
170
171 Template const *
172 TemplateManager::getTemplateByName(string const & name) const
173 {
174         Templates::const_iterator it = templates.find(name);
175         return (it == templates.end()) ? 0 : &it->second;
176 }
177
178
179 string const
180 TemplateManager::getPreambleDefByName(string const & name) const
181 {
182         string const trimmed_name = support::trim(name);
183         if (trimmed_name.empty())
184                 return string();
185
186         PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
187         if (it == preambledefs.end())
188                 return string();
189
190         return it->second;
191 }
192
193
194 void TemplateManager::readTemplates(string const & path)
195 {
196         support::Path p(path);
197
198         enum TemplateTags {
199                 TM_PREAMBLEDEF = 1,
200                 TM_PREAMBLEDEF_END,
201                 TM_TEMPLATE,
202                 TM_TEMPLATE_END
203         };
204
205         keyword_item templatetags[] = {
206                 { "preambledef", TM_PREAMBLEDEF },
207                 { "preambledefend", TM_PREAMBLEDEF_END },
208                 { "template", TM_TEMPLATE },
209                 { "templateend", TM_TEMPLATE_END }
210         };
211
212         LyXLex lex(templatetags, TM_TEMPLATE_END);
213
214         string filename = support::LibFileSearch("", "external_templates");
215         if (filename.empty() || !lex.setFile(filename)) {
216                 lex.printError("external::TemplateManager::readTemplates: "
217                                "No template file");
218                 return;
219         }
220
221         char const * const preamble_end_tag =
222                 templatetags[TM_PREAMBLEDEF_END-1].tag;
223
224         while (lex.isOK()) {
225                 switch (lex.lex()) {
226                 case TM_PREAMBLEDEF: {
227                         lex.next();
228                         string const name = lex.getString();
229                         preambledefs[name] = lex.getLongString(preamble_end_tag);
230                 }
231                 break;
232
233                 case TM_TEMPLATE: {
234                         lex.next();
235                         string const name = lex.getString();
236                         Template & tmp = templates[name];
237                         tmp.lyxName = name;
238                         tmp.readTemplate(lex);
239                 }
240                 break;
241
242                 case TM_TEMPLATE_END:
243                         lex.printError("Warning: End outside Template.");
244                 break;
245
246                 case TM_PREAMBLEDEF_END:
247                         lex.printError("Warning: End outside PreambleDef.");
248                 break;
249                 }
250         }
251 }
252
253
254 void Template::readTemplate(LyXLex & lex)
255 {
256         enum TemplateOptionTags {
257                 TO_GUINAME = 1,
258                 TO_HELPTEXT,
259                 TO_INPUTFORMAT,
260                 TO_FILTER,
261                 TO_EDITCMD,
262                 TO_AUTOMATIC,
263                 TO_FORMAT,
264                 TO_END
265         };
266
267         keyword_item templateoptiontags[] = {
268                 { "automaticproduction", TO_AUTOMATIC },
269                 { "editcommand", TO_EDITCMD },
270                 { "filefilter", TO_FILTER },
271                 { "format", TO_FORMAT },
272                 { "guiname", TO_GUINAME },
273                 { "helptext", TO_HELPTEXT },
274                 { "inputformat", TO_INPUTFORMAT },
275                 { "templateend", TO_END }
276         };
277
278         pushpophelper pph(lex, templateoptiontags, TO_END);
279
280         while (lex.isOK()) {
281                 switch (lex.lex()) {
282                 case TO_GUINAME:
283                         lex.next(true);
284                         guiName = lex.getString();
285                         break;
286
287                 case TO_HELPTEXT:
288                         helpText = lex.getLongString("HelpTextEnd");
289                         break;
290
291                 case TO_INPUTFORMAT:
292                         lex.next(true);
293                         inputFormat = lex.getString();
294                         break;
295
296                 case TO_FILTER:
297                         lex.next(true);
298                         fileRegExp = lex.getString();
299                         break;
300
301                 case TO_EDITCMD:
302                         lex.next(true);
303                         editCommand = lex.getString();
304                         break;
305
306                 case TO_AUTOMATIC:
307                         lex.next();
308                         automaticProduction = lex.getBool();
309                         break;
310
311                 case TO_FORMAT:
312                         lex.next(true);
313                         formats[lex.getString()].readFormat(lex);
314                         break;
315
316                 case TO_END:
317                         return;
318
319                 default:
320                         lex.printError("external::Template::readTemplate: "
321                                        "Wrong tag: $$Token");
322                         BOOST_ASSERT(false);
323                         break;
324                 }
325         }
326 }
327
328
329 void Template::Format::readFormat(LyXLex & lex)
330 {
331         enum FormatTags {
332                 FO_PRODUCT = 1,
333                 FO_UPDATEFORMAT,
334                 FO_UPDATERESULT,
335                 FO_REQUIREMENT,
336                 FO_PREAMBLE,
337                 FO_END
338         };
339
340         keyword_item formattags[] = {
341                 { "formatend", FO_END },
342                 { "preamble", FO_PREAMBLE },
343                 { "product", FO_PRODUCT },
344                 { "requirement", FO_REQUIREMENT },
345                 { "updateformat", FO_UPDATEFORMAT },
346                 { "updateresult", FO_UPDATERESULT }
347         };
348
349         pushpophelper pph(lex, formattags, FO_END);
350
351         while (lex.isOK()) {
352                 switch (lex.lex()) {
353                 case FO_PRODUCT:
354                         lex.next(true);
355                         product = lex.getString();
356                         break;
357
358                 case FO_UPDATEFORMAT:
359                         lex.next(true);
360                         updateFormat = lex.getString();
361                         break;
362
363                 case FO_UPDATERESULT:
364                         lex.next(true);
365                         updateResult = lex.getString();
366                         break;
367
368                 case FO_REQUIREMENT:
369                         lex.next(true);
370                         requirement = lex.getString();
371                         break;
372
373                 case FO_PREAMBLE:
374                         lex.next(true);
375                         preambleNames.push_back(lex.getString());
376                         break;
377
378                 case FO_END:
379                         return;
380                 }
381         }
382 }
383
384 } // namespace external
385 } // namespace lyx