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