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