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