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