]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.cpp
c172ccf13609b3dc6b3d428b5add636ed5133333
[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 void Template::readTemplate(Lexer & lex)
296 {
297         enum {
298                 TO_GUINAME = 1,
299                 TO_HELPTEXT,
300                 TO_INPUTFORMAT,
301                 TO_FILTER,
302                 TO_AUTOMATIC,
303                 TO_TRANSFORM,
304                 TO_FORMAT,
305                 TO_END
306         };
307
308         LexerKeyword templateoptiontags[] = {
309                 { "automaticproduction", TO_AUTOMATIC },
310                 { "filefilter", TO_FILTER },
311                 { "format", TO_FORMAT },
312                 { "guiname", TO_GUINAME },
313                 { "helptext", TO_HELPTEXT },
314                 { "inputformat", TO_INPUTFORMAT },
315                 { "templateend", TO_END },
316                 { "transform", TO_TRANSFORM }
317         };
318
319         PushPopHelper pph(lex, templateoptiontags);
320         lex.setContext("Template::readTemplate");
321
322         string token;
323         while (lex.isOK()) {
324                 switch (lex.lex()) {
325                 case TO_GUINAME:
326                         lex.next(true);
327                         guiName = lex.getString();
328                         break;
329
330                 case TO_HELPTEXT:
331                         helpText = lex.getLongString("HelpTextEnd");
332                         break;
333
334                 case TO_INPUTFORMAT:
335                         lex.next(true);
336                         inputFormat = lex.getString();
337                         break;
338
339                 case TO_FILTER:
340                         lex.next(true);
341                         fileRegExp = lex.getString();
342                         break;
343
344                 case TO_AUTOMATIC:
345                         lex.next();
346                         automaticProduction = lex.getBool();
347                         break;
348
349                 case TO_TRANSFORM:
350                         lex >> token;
351                         TransformID id = transformIDTranslator().find(token);
352                         if (int(id) == -1)
353                                 LYXERR0("Transform " << token << " is not recognized");
354                         else
355                                 ids.push_back(id);
356                         break;
357
358                 case TO_FORMAT:
359                         lex.next(true);
360                         formats[lex.getString()].readFormat(lex);
361                         break;
362
363                 case TO_END:
364                         return;
365
366                 default:
367                         lex.printError("external::Template::readTemplate: "
368                                        "Wrong tag: $$Token");
369                         BOOST_ASSERT(false);
370                         break;
371                 }
372         }
373 }
374
375
376 namespace {
377
378 void transform_not_found(ostream & os, string const & transform)
379 {
380         os << "external::Format::readFormat. Transformation \""
381            << transform << "\" is unrecognized." << endl;
382 }
383
384
385 void transform_class_not_found(ostream & os, string const & tclass)
386 {
387         os << "external::Format::readFormat. Transformation class \""
388            << tclass << "\" is unrecognized." << endl;
389 }
390
391
392 void setCommandFactory(Template::Format & format, string const & transform,
393                        string const & transformer_class)
394 {
395         bool class_found = false;
396         if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
397                 class_found = true;
398                 ResizeCommandFactory factory = ResizeLatexCommand::factory;
399                 format.command_transformers[Resize] =
400                         TransformStore(Resize, factory);
401
402         } else if (transform == "Rotate" &&
403                    transformer_class == "RotationLatexCommand") {
404                 class_found = true;
405                 RotationCommandFactory factory = RotationLatexCommand::factory;
406                 format.command_transformers[Rotate] =
407                         TransformStore(Rotate, factory);
408
409         } else
410                 transform_not_found(lyxerr, transform);
411
412         if (!class_found)
413                 transform_class_not_found(lyxerr, transformer_class);
414 }
415
416
417 void setOptionFactory(Template::Format & format, string const & transform,
418                 string const & transformer_class)
419 {
420         bool class_found = false;
421         if (transform == "Clip" && transformer_class == "ClipLatexOption") {
422                 class_found = true;
423                 ClipOptionFactory factory = ClipLatexOption::factory;
424                 format.option_transformers[Clip] =
425                                 TransformStore(Clip, factory);
426
427         } else if (transform == "Extra" && transformer_class == "ExtraOption") {
428                 class_found = true;
429                 ExtraOptionFactory factory = ExtraOption::factory;
430                 format.option_transformers[Extra] =
431                         TransformStore(Extra, factory);
432
433         } else if (transform == "Resize" &&
434                    transformer_class == "ResizeLatexOption") {
435                 class_found = true;
436                 ResizeOptionFactory factory = ResizeLatexOption::factory;
437                 format.option_transformers[Resize] =
438                         TransformStore(Resize, factory);
439
440         } else if (transform == "Rotate" &&
441                    transformer_class == "RotationLatexOption") {
442                 class_found = true;
443                 RotationOptionFactory factory = RotationLatexOption::factory;
444                 format.option_transformers[Rotate] =
445                         TransformStore(Rotate, factory);
446
447         } else
448                 transform_not_found(lyxerr, transform);
449
450         if (!class_found)
451                 transform_class_not_found(lyxerr, transformer_class);
452 }
453
454 } // namespace anon
455
456
457 void Template::Format::readFormat(Lexer & lex)
458 {
459         enum {
460                 FO_PRODUCT = 1,
461                 FO_UPDATEFORMAT,
462                 FO_UPDATERESULT,
463                 FO_REQUIREMENT,
464                 FO_OPTION,
465                 FO_PREAMBLE,
466                 FO_TRANSFORMCOMMAND,
467                 FO_TRANSFORMOPTION,
468                 FO_REFERENCEDFILE,
469                 FO_END
470         };
471
472         LexerKeyword formattags[] = {
473                 { "formatend", FO_END },
474                 { "option", FO_OPTION },
475                 { "preamble", FO_PREAMBLE },
476                 { "product", FO_PRODUCT },
477                 { "referencedfile", FO_REFERENCEDFILE },
478                 { "requirement", FO_REQUIREMENT },
479                 { "transformcommand", FO_TRANSFORMCOMMAND },
480                 { "transformoption", FO_TRANSFORMOPTION },
481                 { "updateformat", FO_UPDATEFORMAT },
482                 { "updateresult", FO_UPDATERESULT }
483         };
484
485         PushPopHelper pph(lex, formattags);
486
487         while (lex.isOK()) {
488                 switch (lex.lex()) {
489                 case FO_PRODUCT:
490                         lex.next(true);
491                         product = lex.getString();
492                         break;
493
494                 case FO_UPDATEFORMAT:
495                         lex.next(true);
496                         updateFormat = lex.getString();
497                         break;
498
499                 case FO_UPDATERESULT:
500                         lex.next(true);
501                         updateResult = lex.getString();
502                         break;
503
504                 case FO_REQUIREMENT:
505                         lex.next(true);
506                         requirements.push_back(lex.getString());
507                         break;
508
509                 case FO_PREAMBLE:
510                         lex.next(true);
511                         preambleNames.push_back(lex.getString());
512                         break;
513
514                 case FO_TRANSFORMCOMMAND: {
515                         lex.next(true);
516                         string const name = lex.getString();
517                         lex.next(true);
518                         setCommandFactory(*this, name, lex.getString());
519                         break;
520                 }
521
522                 case FO_TRANSFORMOPTION: {
523                         lex.next(true);
524                         string const name = lex.getString();
525                         lex.next(true);
526                         setOptionFactory(*this, name, lex.getString());
527                         break;
528                 }
529
530                 case FO_OPTION: {
531                         lex.next(true);
532                         string const name = lex.getString();
533                         lex.next(true);
534                         string const opt = lex.getString();
535                         options.push_back(Option(name, opt));
536                         break;
537                 }
538
539                 case FO_REFERENCEDFILE: {
540                         lex.next(true);
541                         string const format = lex.getString();
542                         lex.next(true);
543                         string const file = lex.getString();
544                         referencedFiles[format].push_back(file);
545                         break;
546                 }
547
548                 case FO_END:
549                         return;
550                 }
551         }
552 }
553
554 } // namespace external
555 } // namespace lyx