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