]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.C
Re-add the RasterImage template.
[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 using std::vector;
34
35
36 // We have to have dummy default commands for security reasons!
37
38 ExternalTemplate::ExternalTemplate()
39         : inputFormat("*")
40 {}
41
42
43 ExternalTemplate::FormatTemplate::FormatTemplate()
44 {}
45
46
47 ExternalTemplateManager::ExternalTemplateManager()
48 {
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
116                 vector<string>::const_iterator it  = ft.preambleNames.begin();
117                 vector<string>::const_iterator end = ft.preambleNames.end();
118                 for (; it != end; ++it) {
119                         ost << "\t\tPreamble " << *it << '\n';
120                 }
121
122                 ost << "\tFormatEnd\n";
123         }
124 private:
125         ostream & ost;
126 };
127
128
129 void ExternalTemplate::dumpFormats(ostream & os) const
130 {
131         for_each(formats.begin(), formats.end(), dumpFormat(os));
132 }
133
134
135 void ExternalTemplateManager::dumpPreambleDefs(ostream & os) const
136 {
137         for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
138 }
139
140
141 void ExternalTemplateManager::dumpTemplates(ostream & os) const
142 {
143         for_each(templates.begin(), templates.end(), dumpTemplate(os));
144 }
145
146
147 ExternalTemplateManager & ExternalTemplateManager::get()
148 {
149         static ExternalTemplateManager externalTemplateManager;
150         return externalTemplateManager;
151 }
152
153
154 ExternalTemplateManager::Templates &
155 ExternalTemplateManager::getTemplates()
156 {
157         return templates;
158 }
159
160
161 ExternalTemplateManager::Templates const &
162 ExternalTemplateManager::getTemplates() const
163 {
164         return templates;
165 }
166
167
168 ExternalTemplate const *
169 ExternalTemplateManager::getTemplateByName(string const & name) const
170 {
171         Templates::const_iterator it = templates.find(name);
172         return (it == templates.end()) ? 0 : &it->second;
173 }
174
175
176 string const
177 ExternalTemplateManager::getPreambleDefByName(string const & name) const
178 {
179         string const trimmed_name = support::trim(name);
180         if (trimmed_name.empty())
181                 return string();
182
183         PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
184         if (it == preambledefs.end())
185                 return string();
186
187         return it->second;
188 }
189
190
191 void ExternalTemplateManager::readTemplates(string const & path)
192 {
193         support::Path p(path);
194
195         enum TemplateTags {
196                 TM_PREAMBLEDEF = 1,
197                 TM_PREAMBLEDEF_END,
198                 TM_TEMPLATE,
199                 TM_TEMPLATE_END
200         };
201
202         keyword_item templatetags[] = {
203                 { "preambledef", TM_PREAMBLEDEF },
204                 { "preambledefend", TM_PREAMBLEDEF_END },
205                 { "template", TM_TEMPLATE },
206                 { "templateend", TM_TEMPLATE_END }
207         };
208
209         LyXLex lex(templatetags, TM_TEMPLATE_END);
210
211         string filename = support::LibFileSearch("", "external_templates");
212         if (filename.empty() || !lex.setFile(filename)) {
213                 lex.printError("ExternalTemplateManager::readTemplates: "
214                                "No template file");
215                 return;
216         }
217
218         char const * const preamble_end_tag =
219                 templatetags[TM_PREAMBLEDEF_END-1].tag;
220
221         while (lex.isOK()) {
222                 switch (lex.lex()) {
223                 case TM_PREAMBLEDEF: {
224                         lex.next();
225                         string const name = lex.getString();
226                         preambledefs[name] = lex.getLongString(preamble_end_tag);
227                 }
228                 break;
229
230                 case TM_TEMPLATE: {
231                         lex.next();
232                         string const name = lex.getString();
233                         ExternalTemplate & tmp = templates[name];
234                         tmp.lyxName = name;
235                         tmp.readTemplate(lex);
236                 }
237                 break;
238
239                 case TM_TEMPLATE_END:
240                         lex.printError("Warning: End outside Template.");
241                 break;
242
243                 case TM_PREAMBLEDEF_END:
244                         lex.printError("Warning: End outside PreambleDef.");
245                 break;
246                 }
247         }
248 }
249
250
251 void ExternalTemplate::readTemplate(LyXLex & lex)
252 {
253         enum TemplateOptionTags {
254                 TO_GUINAME = 1,
255                 TO_HELPTEXT,
256                 TO_INPUTFORMAT,
257                 TO_FILTER,
258                 TO_EDITCMD,
259                 TO_AUTOMATIC,
260                 TO_FORMAT,
261                 TO_END
262         };
263
264         keyword_item templateoptiontags[] = {
265                 { "automaticproduction", TO_AUTOMATIC },
266                 { "editcommand", TO_EDITCMD },
267                 { "filefilter", TO_FILTER },
268                 { "format", TO_FORMAT },
269                 { "guiname", TO_GUINAME },
270                 { "helptext", TO_HELPTEXT },
271                 { "inputformat", TO_INPUTFORMAT },
272                 { "templateend", TO_END }
273         };
274
275         pushpophelper pph(lex, templateoptiontags, TO_END);
276
277         while (lex.isOK()) {
278                 switch (lex.lex()) {
279                 case TO_GUINAME:
280                         lex.next(true);
281                         guiName = lex.getString();
282                         break;
283
284                 case TO_HELPTEXT:
285                         helpText = lex.getLongString("HelpTextEnd");
286                         break;
287
288                 case TO_INPUTFORMAT:
289                         lex.next(true);
290                         inputFormat = lex.getString();
291                         break;
292
293                 case TO_FILTER:
294                         lex.next(true);
295                         fileRegExp = lex.getString();
296                         break;
297
298                 case TO_EDITCMD:
299                         lex.next(true);
300                         editCommand = lex.getString();
301                         break;
302
303                 case TO_AUTOMATIC:
304                         lex.next();
305                         automaticProduction = lex.getBool();
306                         break;
307
308                 case TO_FORMAT:
309                         lex.next(true);
310                         formats[lex.getString()].readFormat(lex);
311                         break;
312
313                 case TO_END:
314                         return;
315
316                 default:
317                         lex.printError("ExternalTemplate::readTemplate: "
318                                        "Wrong tag: $$Token");
319                         BOOST_ASSERT(false);
320                         break;
321                 }
322         }
323 }
324
325
326 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
327 {
328         enum FormatTags {
329                 FO_PRODUCT = 1,
330                 FO_UPDATEFORMAT,
331                 FO_UPDATERESULT,
332                 FO_REQUIREMENT,
333                 FO_PREAMBLE,
334                 FO_END
335         };
336
337         keyword_item formattags[] = {
338                 { "formatend", FO_END },
339                 { "preamble", FO_PREAMBLE },
340                 { "product", FO_PRODUCT },
341                 { "requirement", FO_REQUIREMENT },
342                 { "updateformat", FO_UPDATEFORMAT },
343                 { "updateresult", FO_UPDATERESULT }
344         };
345
346         pushpophelper pph(lex, formattags, FO_END);
347
348         while (lex.isOK()) {
349                 switch (lex.lex()) {
350                 case FO_PRODUCT:
351                         lex.next(true);
352                         product = lex.getString();
353                         break;
354
355                 case FO_UPDATEFORMAT:
356                         lex.next(true);
357                         updateFormat = lex.getString();
358                         break;
359
360                 case FO_UPDATERESULT:
361                         lex.next(true);
362                         updateResult = lex.getString();
363                         break;
364
365                 case FO_REQUIREMENT:
366                         lex.next(true);
367                         requirement = lex.getString();
368                         break;
369
370                 case FO_PREAMBLE:
371                         lex.next(true);
372                         preambleNames.push_back(lex.getString());
373                         break;
374
375                 case FO_END:
376                         return;
377                 }
378         }
379 }