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