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