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