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