]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.C
0bffe07c0fdc1f8612fee3fe58586ee0a63d870d
[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  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ExternalTemplate.h"
15
16 #include "debug.h"
17 #include "lyxlex.h"
18
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/path.h"
22 #include "support/path_defines.h"
23 #include "support/translator.h"
24
25 #include <boost/assert.hpp>
26
27 #include <algorithm>
28
29 namespace support = lyx::support;
30
31 using std::endl;
32 using std::for_each;
33
34 using std::string;
35 using std::ostream;
36 using std::vector;
37
38
39 namespace lyx {
40 namespace external {
41
42 namespace {
43
44 typedef Translator<TransformID, string> TransformIDTranslator;
45 TransformIDTranslator const & transformIDTranslator();
46
47 } // namespace anon
48
49
50 // We have to have dummy default commands for security reasons!
51 Template::Template()
52         : inputFormat("*")
53 {}
54
55
56 Template::Format::Format()
57 {}
58
59
60 TemplateManager::TemplateManager()
61 {
62         readTemplates(support::user_lyxdir());
63         if (lyxerr.debugging(Debug::EXTERNAL)) {
64                 dumpPreambleDefs(lyxerr);
65                 lyxerr << '\n';
66                 dumpTemplates(lyxerr);
67         }
68 }
69
70
71 class dumpPreambleDef {
72 public:
73         typedef TemplateManager::PreambleDefs::value_type value_type;
74
75         dumpPreambleDef(ostream & o) : ost(o) {}
76
77         void operator()(value_type const & vt) {
78                 ost << "PreambleDef " << vt.first << '\n'
79                     << vt.second
80                     << "PreambleDefEnd" << endl;
81         }
82
83 private:
84         ostream & ost;
85 };
86
87
88 class dumpTemplate {
89 public:
90         typedef TemplateManager::Templates::value_type value_type;
91
92         dumpTemplate(ostream & o) : ost(o) {}
93
94         void operator()(value_type const & vt) {
95                 Template const & et = vt.second;
96
97                 ost << "Template " << et.lyxName << '\n'
98                     << "\tGuiName " << et.guiName << '\n'
99                     << "\tHelpText\n"
100                     << et.helpText
101                     << "\tHelpTextEnd\n"
102                     << "\tInputFormat " << et.inputFormat << '\n'
103                     << "\tFileFilter " << et.fileRegExp << '\n'
104                     << "\tAutomaticProduction " << et.automaticProduction << '\n';
105
106                 typedef vector<TransformID> IDs;
107                 IDs::const_iterator it  = et.transformIds.begin();
108                 IDs::const_iterator end = et.transformIds.end();
109                 for (; it != end; ++it) {
110                         ost << "\tTransform "
111                             << transformIDTranslator().find(*it) << '\n';
112                 }
113
114                 et.dumpFormats(ost);
115                 ost << "TemplateEnd" << endl;
116
117         }
118
119 private:
120         ostream & ost;
121 };
122
123 class dumpFormat {
124 public:
125         typedef Template::Formats::value_type value_type;
126
127         dumpFormat(ostream & o) : ost(o) {}
128
129         void operator()(value_type const & vt) const {
130                 Template::Format const & ft = vt.second;
131                 ost << "\tFormat " << vt.first << '\n'
132                     << "\t\tProduct " << ft.product << '\n'
133                     << "\t\tUpdateFormat " << ft.updateFormat << '\n'
134                     << "\t\tUpdateResult " << ft.updateResult << '\n'
135                     << "\t\tRequirement " << ft.requirement << '\n';
136
137                 typedef vector<Template::Option> Options;
138                 Options::const_iterator oit  = ft.options.begin();
139                 Options::const_iterator oend = ft.options.end();
140                 for (; oit != oend; ++oit) {
141                         ost << "\t\tOption "
142                             << oit->name
143                             << ": "
144                             << oit->option
145                             << '\n';
146                 }
147
148                 vector<string>::const_iterator pit  = ft.preambleNames.begin();
149                 vector<string>::const_iterator pend = ft.preambleNames.end();
150                 for (; pit != pend; ++pit) {
151                         ost << "\t\tPreamble " << *pit << '\n';
152                 }
153
154                 ost << "\tFormatEnd\n";
155         }
156 private:
157         ostream & ost;
158 };
159
160
161 void Template::dumpFormats(ostream & os) const
162 {
163         for_each(formats.begin(), formats.end(), dumpFormat(os));
164 }
165
166
167 void TemplateManager::dumpPreambleDefs(ostream & os) const
168 {
169         for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
170 }
171
172
173 void TemplateManager::dumpTemplates(ostream & os) const
174 {
175         for_each(templates.begin(), templates.end(), dumpTemplate(os));
176 }
177
178
179 TemplateManager & TemplateManager::get()
180 {
181         static TemplateManager externalTemplateManager;
182         return externalTemplateManager;
183 }
184
185
186 TemplateManager::Templates const &
187 TemplateManager::getTemplates() const
188 {
189         return templates;
190 }
191
192
193 Template const *
194 TemplateManager::getTemplateByName(string const & name) const
195 {
196         Templates::const_iterator it = templates.find(name);
197         return (it == templates.end()) ? 0 : &it->second;
198 }
199
200
201 string const
202 TemplateManager::getPreambleDefByName(string const & name) const
203 {
204         string const trimmed_name = support::trim(name);
205         if (trimmed_name.empty())
206                 return string();
207
208         PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
209         if (it == preambledefs.end())
210                 return string();
211
212         return it->second;
213 }
214
215
216 void TemplateManager::readTemplates(string const & path)
217 {
218         support::Path p(path);
219
220         enum TemplateTags {
221                 TM_PREAMBLEDEF = 1,
222                 TM_PREAMBLEDEF_END,
223                 TM_TEMPLATE,
224                 TM_TEMPLATE_END
225         };
226
227         keyword_item templatetags[] = {
228                 { "preambledef", TM_PREAMBLEDEF },
229                 { "preambledefend", TM_PREAMBLEDEF_END },
230                 { "template", TM_TEMPLATE },
231                 { "templateend", TM_TEMPLATE_END }
232         };
233
234         LyXLex lex(templatetags, TM_TEMPLATE_END);
235
236         string filename = support::LibFileSearch("", "external_templates");
237         if (filename.empty() || !lex.setFile(filename)) {
238                 lex.printError("external::TemplateManager::readTemplates: "
239                                "No template file");
240                 return;
241         }
242
243         char const * const preamble_end_tag =
244                 templatetags[TM_PREAMBLEDEF_END-1].tag;
245
246         while (lex.isOK()) {
247                 switch (lex.lex()) {
248                 case TM_PREAMBLEDEF: {
249                         lex.next();
250                         string const name = lex.getString();
251                         preambledefs[name] = lex.getLongString(preamble_end_tag);
252                 }
253                 break;
254
255                 case TM_TEMPLATE: {
256                         lex.next();
257                         string const name = lex.getString();
258                         Template & tmp = templates[name];
259                         tmp.lyxName = name;
260                         tmp.readTemplate(lex);
261                 }
262                 break;
263
264                 case TM_TEMPLATE_END:
265                         lex.printError("Warning: End outside Template.");
266                 break;
267
268                 case TM_PREAMBLEDEF_END:
269                         lex.printError("Warning: End outside PreambleDef.");
270                 break;
271                 }
272         }
273 }
274
275
276 namespace {
277
278 void add(vector<TransformID> & ids, string const & name)
279 {
280         TransformID id = transformIDTranslator().find(name);
281         if (int(id) == -1) {
282                 lyxerr << "external::Template::readTemplate\n"
283                        << "Transform " << name << " is not recognized"
284                        << std::endl;
285         } else {
286                 ids.push_back(id);
287         }
288 }
289
290 } // namespace anon
291
292
293 void Template::readTemplate(LyXLex & lex)
294 {
295         enum TemplateOptionTags {
296                 TO_GUINAME = 1,
297                 TO_HELPTEXT,
298                 TO_INPUTFORMAT,
299                 TO_FILTER,
300                 TO_AUTOMATIC,
301                 TO_TRANSFORM,
302                 TO_FORMAT,
303                 TO_END
304         };
305
306         keyword_item templateoptiontags[] = {
307                 { "automaticproduction", TO_AUTOMATIC },
308                 { "filefilter", TO_FILTER },
309                 { "format", TO_FORMAT },
310                 { "guiname", TO_GUINAME },
311                 { "helptext", TO_HELPTEXT },
312                 { "inputformat", TO_INPUTFORMAT },
313                 { "templateend", TO_END },
314                 { "transform", TO_TRANSFORM }
315         };
316
317         pushpophelper pph(lex, templateoptiontags, TO_END);
318
319         while (lex.isOK()) {
320                 switch (lex.lex()) {
321                 case TO_GUINAME:
322                         lex.next(true);
323                         guiName = lex.getString();
324                         break;
325
326                 case TO_HELPTEXT:
327                         helpText = lex.getLongString("HelpTextEnd");
328                         break;
329
330                 case TO_INPUTFORMAT:
331                         lex.next(true);
332                         inputFormat = lex.getString();
333                         break;
334
335                 case TO_FILTER:
336                         lex.next(true);
337                         fileRegExp = lex.getString();
338                         break;
339
340                 case TO_AUTOMATIC:
341                         lex.next();
342                         automaticProduction = lex.getBool();
343                         break;
344
345                 case TO_TRANSFORM:
346                         lex.next(true);
347                         add(transformIds, lex.getString());
348                         break;
349
350                 case TO_FORMAT:
351                         lex.next(true);
352                         formats[lex.getString()].readFormat(lex);
353                         break;
354
355                 case TO_END:
356                         return;
357
358                 default:
359                         lex.printError("external::Template::readTemplate: "
360                                        "Wrong tag: $$Token");
361                         BOOST_ASSERT(false);
362                         break;
363                 }
364         }
365 }
366
367
368 namespace {
369
370 void transform_not_found(std::ostream & os, string const & transform)
371 {
372         os << "external::Format::readFormat. Transformation \""
373            << transform << "\" is unrecognized." << std::endl;
374 }
375
376
377 void transform_class_not_found(std::ostream & os, string const & tclass)
378 {
379         os << "external::Format::readFormat. Transformation class \""
380            << tclass << "\" is unrecognized." << std::endl;
381 }
382
383
384 void setCommandFactory(Template::Format & format, string const & transform,
385                        string const & transformer_class)
386 {
387         bool class_found = false;
388         if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
389                 class_found = true;
390                 ResizeCommandFactory factory = ResizeLatexCommand::factory;
391                 format.command_transformers[Resize] =
392                         TransformStore(Resize, factory);
393
394         } else if (transform == "Rotate" &&
395                    transformer_class == "RotationLatexCommand") {
396                 class_found = true;
397                 RotationCommandFactory factory = RotationLatexCommand::factory;
398                 format.command_transformers[Rotate] =
399                         TransformStore(Rotate, factory);
400
401         } else
402                 transform_not_found(lyxerr, transform);
403
404         if (!class_found)
405                 transform_class_not_found(lyxerr, transformer_class);
406 }
407
408
409 void setOptionFactory(Template::Format & format, string const & transform,
410                 string const & transformer_class)
411 {
412         bool class_found = false;
413         if (transform == "Clip" && transformer_class == "ClipLatexOption") {
414                 class_found = true;
415                 ClipOptionFactory factory = ClipLatexOption::factory;
416                 format.option_transformers[Clip] =
417                                 TransformStore(Clip, factory);
418
419         } else if (transform == "Extra" && transformer_class == "ExtraOption") {
420                 class_found = true;
421                 ExtraOptionFactory factory = ExtraOption::factory;
422                 format.option_transformers[Extra] =
423                         TransformStore(Extra, factory);
424
425         } else if (transform == "Resize" &&
426                    transformer_class == "ResizeLatexOption") {
427                 class_found = true;
428                 ResizeOptionFactory factory = ResizeLatexOption::factory;
429                 format.option_transformers[Resize] =
430                         TransformStore(Resize, factory);
431
432         } else if (transform == "Rotate" &&
433                    transformer_class == "RotationLatexOption") {
434                 class_found = true;
435                 RotationOptionFactory factory = RotationLatexOption::factory;
436                 format.option_transformers[Rotate] =
437                         TransformStore(Rotate, factory);
438
439         } else
440                 transform_not_found(lyxerr, transform);
441
442         if (!class_found)
443                 transform_class_not_found(lyxerr, transformer_class);
444 }
445
446 } // namespace anon
447
448
449 void Template::Format::readFormat(LyXLex & lex)
450 {
451         enum FormatTags {
452                 FO_PRODUCT = 1,
453                 FO_UPDATEFORMAT,
454                 FO_UPDATERESULT,
455                 FO_REQUIREMENT,
456                 FO_OPTION,
457                 FO_PREAMBLE,
458                 FO_TRANSFORMCOMMAND,
459                 FO_TRANSFORMOPTION,
460                 FO_END
461         };
462
463         keyword_item formattags[] = {
464                 { "formatend", FO_END },
465                 { "option", FO_OPTION },
466                 { "preamble", FO_PREAMBLE },
467                 { "product", FO_PRODUCT },
468                 { "requirement", FO_REQUIREMENT },
469                 { "transformcommand", FO_TRANSFORMCOMMAND },
470                 { "transformoption", FO_TRANSFORMOPTION },
471                 { "updateformat", FO_UPDATEFORMAT },
472                 { "updateresult", FO_UPDATERESULT }
473         };
474
475         pushpophelper pph(lex, formattags, FO_END);
476
477         while (lex.isOK()) {
478                 switch (lex.lex()) {
479                 case FO_PRODUCT:
480                         lex.next(true);
481                         product = lex.getString();
482                         break;
483
484                 case FO_UPDATEFORMAT:
485                         lex.next(true);
486                         updateFormat = lex.getString();
487                         break;
488
489                 case FO_UPDATERESULT:
490                         lex.next(true);
491                         updateResult = lex.getString();
492                         break;
493
494                 case FO_REQUIREMENT:
495                         lex.next(true);
496                         requirement = lex.getString();
497                         break;
498
499                 case FO_PREAMBLE:
500                         lex.next(true);
501                         preambleNames.push_back(lex.getString());
502                         break;
503
504                 case FO_TRANSFORMCOMMAND: {
505                         lex.next(true);
506                         string const name = lex.getString();
507                         lex.next(true);
508                         setCommandFactory(*this, name, lex.getString());
509                         break;
510                 }
511
512                 case FO_TRANSFORMOPTION: {
513                         lex.next(true);
514                         string const name = lex.getString();
515                         lex.next(true);
516                         setOptionFactory(*this, name, lex.getString());
517                         break;
518                 }
519
520                 case FO_OPTION: {
521                         lex.next(true);
522                         string const name = lex.getString();
523                         lex.next(true);
524                         string const opt = lex.getString();
525                         options.push_back(Option(name, opt));
526                         break;
527                 }
528
529                 case FO_END:
530                         return;
531                 }
532         }
533 }
534
535 namespace {
536
537 TransformIDTranslator const initIDTranslator()
538 {
539         TransformIDTranslator translator(TransformID(-1), "");
540         translator.addPair(Rotate, "Rotate");
541         translator.addPair(Resize, "Resize");
542         translator.addPair(Clip,   "Clip");
543         translator.addPair(Extra,  "Extra");
544         return translator;
545 }
546
547
548 TransformIDTranslator const & transformIDTranslator()
549 {
550         static TransformIDTranslator const translator = initIDTranslator();
551         return translator;
552 }
553
554 } // namespace anon
555
556 } // namespace external
557 } // namespace lyx