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